1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Tests\Request\Validator\Rule; |
13
|
|
|
|
14
|
|
|
use Overblog\GraphQLBundle\Request\Validator\Rule\QueryDepth; |
15
|
|
|
|
16
|
|
|
class QueryDepthTest extends AbstractQuerySecurityTest |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param $max |
20
|
|
|
* @param $count |
21
|
|
|
* |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
protected function getErrorMessage($max, $count) |
25
|
|
|
{ |
26
|
|
|
return QueryDepth::maxQueryDepthErrorMessage($max, $count); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param $maxDepth |
31
|
|
|
* |
32
|
|
|
* @return QueryDepth |
33
|
|
|
*/ |
34
|
|
|
protected function createRule($maxDepth) |
35
|
|
|
{ |
36
|
|
|
return new QueryDepth($maxDepth); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param $queryDepth |
41
|
|
|
* @param int $maxQueryDepth |
42
|
|
|
* @param array $expectedErrors |
43
|
|
|
* @dataProvider queryDataProvider |
44
|
|
|
*/ |
45
|
|
|
public function testSimpleQueries($queryDepth, $maxQueryDepth = 7, $expectedErrors = []) |
46
|
|
|
{ |
47
|
|
|
$this->assertDocumentValidator($this->buildRecursiveQuery($queryDepth), $maxQueryDepth, $expectedErrors); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param $queryDepth |
52
|
|
|
* @param int $maxQueryDepth |
53
|
|
|
* @param array $expectedErrors |
54
|
|
|
* @dataProvider queryDataProvider |
55
|
|
|
*/ |
56
|
|
|
public function testFragmentQueries($queryDepth, $maxQueryDepth = 7, $expectedErrors = []) |
57
|
|
|
{ |
58
|
|
|
$this->assertDocumentValidator($this->buildRecursiveUsingFragmentQuery($queryDepth), $maxQueryDepth, $expectedErrors); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $queryDepth |
63
|
|
|
* @param int $maxQueryDepth |
64
|
|
|
* @param array $expectedErrors |
65
|
|
|
* @dataProvider queryDataProvider |
66
|
|
|
*/ |
67
|
|
|
public function testInlineFragmentQueries($queryDepth, $maxQueryDepth = 7, $expectedErrors = []) |
68
|
|
|
{ |
69
|
|
|
$this->assertDocumentValidator($this->buildRecursiveUsingInlineFragmentQuery($queryDepth), $maxQueryDepth, $expectedErrors); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testComplexityIntrospectionQuery() |
73
|
|
|
{ |
74
|
|
|
$this->assertIntrospectionQuery(7); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testIntrospectionTypeMetaFieldQuery() |
78
|
|
|
{ |
79
|
|
|
$this->assertIntrospectionTypeMetaFieldQuery(1); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testTypeNameMetaFieldQuery() |
83
|
|
|
{ |
84
|
|
|
$this->assertTypeNameMetaFieldQuery(1); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function queryDataProvider() |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
|
|
[1], // Valid because depth under default limit (7) |
91
|
|
|
[2], |
92
|
|
|
[3], |
93
|
|
|
[4], |
94
|
|
|
[5], |
95
|
|
|
[6], |
96
|
|
|
[7], |
97
|
|
|
[8, 9], // Valid because depth under new limit (9) |
98
|
|
|
[10, 0], // Valid because 0 depth disable limit |
99
|
|
|
[ |
100
|
|
|
10, |
101
|
|
|
8, |
102
|
|
|
[$this->createFormattedError(8, 10)], |
103
|
|
|
], // failed because depth over limit (8) |
104
|
|
|
[ |
105
|
|
|
20, |
106
|
|
|
15, |
107
|
|
|
[$this->createFormattedError(15, 20)], |
108
|
|
|
], // failed because depth over limit (15) |
109
|
|
|
]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function buildRecursiveQuery($depth) |
113
|
|
|
{ |
114
|
|
|
$query = sprintf('query MyQuery { human%s }', $this->buildRecursiveQueryPart($depth)); |
115
|
|
|
|
116
|
|
|
return $query; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function buildRecursiveUsingFragmentQuery($depth) |
120
|
|
|
{ |
121
|
|
|
$query = sprintf( |
122
|
|
|
'query MyQuery { human { ...F1 } } fragment F1 on Human %s', |
123
|
|
|
$this->buildRecursiveQueryPart($depth) |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
return $query; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
private function buildRecursiveUsingInlineFragmentQuery($depth) |
130
|
|
|
{ |
131
|
|
|
$query = sprintf( |
132
|
|
|
'query MyQuery { human { ...on Human %s } }', |
133
|
|
|
$this->buildRecursiveQueryPart($depth) |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
return $query; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
private function buildRecursiveQueryPart($depth) |
140
|
|
|
{ |
141
|
|
|
$templates = [ |
142
|
|
|
'human' => ' { firstName%s } ', |
143
|
|
|
'dog' => ' dogs { name%s } ', |
144
|
|
|
]; |
145
|
|
|
|
146
|
|
|
$part = $templates['human']; |
147
|
|
|
|
148
|
|
|
for ($i = 1; $i <= $depth; ++$i) { |
149
|
|
|
$key = ($i % 2 == 1) ? 'human' : 'dog'; |
150
|
|
|
$template = $templates[$key]; |
151
|
|
|
|
152
|
|
|
$part = sprintf($part, ('human' == $key ? ' owner ' : '').$template); |
153
|
|
|
} |
154
|
|
|
$part = str_replace('%s', '', $part); |
155
|
|
|
|
156
|
|
|
return $part; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|