Skip to content

Commit 6d6d1ac

Browse files
committed
Adds tests for DisableIntrospection Validation rule
1 parent e7cde5e commit 6d6d1ac

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
namespace GraphQL\Tests\Validator;
3+
4+
use GraphQL\Error\FormattedError;
5+
use GraphQL\Language\SourceLocation;
6+
use GraphQL\Validator\Rules\DisableIntrospection;
7+
8+
class DisableIntrospectionTest extends TestCase
9+
{
10+
// Validate: Disable Introspection
11+
12+
/**
13+
* @it fails if the query contains __schema
14+
*/
15+
public function testQueryContainsSchema()
16+
{
17+
$this->expectFailsRule(new DisableIntrospection(DisableIntrospection::ENABLED), '
18+
query {
19+
__schema {
20+
queryType {
21+
name
22+
}
23+
}
24+
}
25+
',
26+
[$this->error(3, 9)]
27+
);
28+
}
29+
30+
/**
31+
* @it fails if the query contains __type
32+
*/
33+
public function testQueryContainsType()
34+
{
35+
$this->expectFailsRule(new DisableIntrospection(DisableIntrospection::ENABLED), '
36+
query {
37+
__type(
38+
name: "Query"
39+
){
40+
name
41+
}
42+
}
43+
',
44+
[$this->error(3, 9)]
45+
);
46+
}
47+
48+
/**
49+
* @it does not fail on a query that does not contain __type
50+
*/
51+
public function testValidQuery()
52+
{
53+
$this->expectPassesRule(new DisableIntrospection(DisableIntrospection::ENABLED), '
54+
query {
55+
user {
56+
name
57+
email
58+
friends {
59+
name
60+
}
61+
}
62+
}
63+
');
64+
}
65+
66+
/**
67+
* @it does not fail when not enabled
68+
*/
69+
public function testQueryWhenDisabled()
70+
{
71+
$this->expectPassesRule(new DisableIntrospection(DisableIntrospection::DISABLED), '
72+
query {
73+
__type(
74+
name: "Query"
75+
){
76+
name
77+
}
78+
}
79+
');
80+
}
81+
82+
/**
83+
* @it has a public interface for enabeling the rule
84+
*/
85+
public function testPublicEnableInterface()
86+
{
87+
$disableIntrospection = new DisableIntrospection(DisableIntrospection::DISABLED);
88+
$disableIntrospection->setEnabled(DisableIntrospection::ENABLED);
89+
$this->expectFailsRule($disableIntrospection, '
90+
query {
91+
__type(
92+
name: "Query"
93+
){
94+
name
95+
}
96+
}
97+
',
98+
[$this->error(3, 9)]
99+
);
100+
}
101+
102+
/**
103+
* @it has a public interface for disableing the rule
104+
*/
105+
public function testPublicDisableInterface()
106+
{
107+
$disableIntrospection = new DisableIntrospection(DisableIntrospection::ENABLED);
108+
$disableIntrospection->setEnabled(DisableIntrospection::DISABLED);
109+
$this->expectPassesRule($disableIntrospection, '
110+
query {
111+
__type(
112+
name: "Query"
113+
){
114+
name
115+
}
116+
}
117+
');
118+
}
119+
120+
121+
private function error($line, $column)
122+
{
123+
return FormattedError::create(
124+
DisableIntrospection::introspectionDisabledMessage(),
125+
[ new SourceLocation($line, $column) ]
126+
);
127+
}
128+
}

0 commit comments

Comments
 (0)