|
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 GraphQL\FormattedError; |
|
15
|
|
|
use GraphQL\Language\Parser; |
|
16
|
|
|
use GraphQL\Type\Introspection; |
|
17
|
|
|
use GraphQL\Validator\DocumentValidator; |
|
18
|
|
|
use Overblog\GraphQLBundle\Request\Validator\Rule\AbstractQuerySecurity; |
|
19
|
|
|
|
|
20
|
|
|
abstract class AbstractQuerySecurityTest extends \PHPUnit_Framework_TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @param $max |
|
24
|
|
|
* |
|
25
|
|
|
* @return AbstractQuerySecurity |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract protected function createRule($max); |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param $max |
|
31
|
|
|
* @param $count |
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
|
abstract protected function getErrorMessage($max, $count); |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @expectedException \InvalidArgumentException |
|
39
|
|
|
* @expectedExceptionMessage argument must be greater or equal to 0. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function testMaxQueryDepthMustBeGreaterOrEqualTo0() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->createRule(-1); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
protected function createFormattedError($max, $count, $locations = []) |
|
47
|
|
|
{ |
|
48
|
|
|
return FormattedError::create($this->getErrorMessage($max, $count), $locations); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
protected function assertDocumentValidator($queryString, $max, array $expectedErrors = []) |
|
52
|
|
|
{ |
|
53
|
|
|
$errors = DocumentValidator::validate( |
|
54
|
|
|
Schema::buildSchema(), |
|
55
|
|
|
Parser::parse($queryString), |
|
56
|
|
|
[$this->createRule($max)] |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertEquals($expectedErrors, array_map(['GraphQL\Error', 'formatError'], $errors), $queryString); |
|
60
|
|
|
|
|
61
|
|
|
return $errors; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function assertIntrospectionQuery($maxExpected) |
|
65
|
|
|
{ |
|
66
|
|
|
$query = Introspection::getIntrospectionQuery(true); |
|
67
|
|
|
|
|
68
|
|
|
$this->assertMaxValue($query, $maxExpected); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected function assertIntrospectionTypeMetaFieldQuery($maxExpected) |
|
72
|
|
|
{ |
|
73
|
|
|
$query = ' |
|
74
|
|
|
{ |
|
75
|
|
|
__type(name: "Human") { |
|
76
|
|
|
name |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
'; |
|
80
|
|
|
|
|
81
|
|
|
$this->assertMaxValue($query, $maxExpected); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function assertTypeNameMetaFieldQuery($maxExpected) |
|
85
|
|
|
{ |
|
86
|
|
|
$query = ' |
|
87
|
|
|
{ |
|
88
|
|
|
human { |
|
89
|
|
|
__typename |
|
90
|
|
|
firstName |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
'; |
|
94
|
|
|
$this->assertMaxValue($query, $maxExpected); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function assertMaxValue($query, $maxExpected) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->assertDocumentValidator($query, $maxExpected); |
|
100
|
|
|
$newMax = $maxExpected - 1; |
|
101
|
|
|
if ($newMax !== AbstractQuerySecurity::DISABLED) { |
|
102
|
|
|
$this->assertDocumentValidator($query, $newMax, [$this->createFormattedError($newMax, $maxExpected)]); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|