1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Sulu. |
5
|
|
|
* |
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sulu\Bundle\ValidationBundle\Tests\Functional\Validation; |
13
|
|
|
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Client; |
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This test class is testing sulu validation. |
19
|
|
|
*/ |
20
|
|
|
class ValidationTest extends WebTestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Client |
24
|
|
|
*/ |
25
|
|
|
protected $client; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function setUp() |
31
|
|
|
{ |
32
|
|
|
$this->client = static::createClient(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Test calling a route without validation. |
37
|
|
|
*/ |
38
|
|
|
public function testNoValidation() |
39
|
|
|
{ |
40
|
|
|
$this->client->request('GET', '/no-validation'); |
41
|
|
|
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Test validation success on GET request. |
46
|
|
|
*/ |
47
|
|
|
public function testGetValidationSuccess() |
48
|
|
|
{ |
49
|
|
|
$this->client->request('GET', '/get-validation', ['locale' => 'en']); |
50
|
|
|
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Tests validation error on GET request. |
55
|
|
|
*/ |
56
|
|
|
public function testGetValidationError() |
57
|
|
|
{ |
58
|
|
|
$this->client->request('GET', '/get-validation'); |
59
|
|
|
$this->assertEquals(400, $this->client->getResponse()->getStatusCode()); |
60
|
|
|
$responseContent = json_decode($this->client->getResponse()->getContent(), true); |
61
|
|
|
$this->assertCount(1, $responseContent); |
62
|
|
|
$this->assertResponseContainsProperties($responseContent, ['locale']); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Test validation success on POST request. |
67
|
|
|
*/ |
68
|
|
|
public function testPostValidationSuccess() |
69
|
|
|
{ |
70
|
|
|
$this->client->request( |
71
|
|
|
'POST', |
72
|
|
|
'/post-validation', |
73
|
|
|
[ |
74
|
|
|
'locale' => 'en', |
75
|
|
|
'name' => 'test', |
76
|
|
|
'attributes' => [ |
77
|
|
|
['id' => 2], |
78
|
|
|
], |
79
|
|
|
] |
80
|
|
|
); |
81
|
|
|
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Tests validation error on POST request. |
86
|
|
|
*/ |
87
|
|
|
public function testPostValidationError() |
88
|
|
|
{ |
89
|
|
|
$this->client->request('POST', '/post-validation'); |
90
|
|
|
$this->assertEquals(400, $this->client->getResponse()->getStatusCode()); |
91
|
|
|
$responseContent = json_decode($this->client->getResponse()->getContent(), true); |
92
|
|
|
$this->assertCount(2, $responseContent); |
93
|
|
|
$this->assertResponseContainsProperties($responseContent, ['name', 'attributes']); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Tests what happens if schema file contains invalid json. |
98
|
|
|
*/ |
99
|
|
|
public function testValidateInvalidSchema() |
100
|
|
|
{ |
101
|
|
|
$this->client->request('POST', '/invalid-schema'); |
102
|
|
|
$this->assertEquals(500, $this->client->getResponse()->getStatusCode()); |
103
|
|
|
$responseContent = json_decode($this->client->getResponse()->getContent(), true); |
104
|
|
|
$this->assertContains('No valid json found in file', $responseContent['message']); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testValidationOfSchemaWithInlineRefs() |
108
|
|
|
{ |
109
|
|
|
$data = [ |
110
|
|
|
'billingAddress' => [ |
111
|
|
|
'street' => 'Teststreet', |
112
|
|
|
'city' => 'Testcity', |
113
|
|
|
'zip' => 'ABC1234', |
114
|
|
|
'country' => 'Testcountry', |
115
|
|
|
], |
116
|
|
|
'shippingAddress' => [ |
117
|
|
|
'street' => 'Teststreet', |
118
|
|
|
'city' => 'Testcity', |
119
|
|
|
'zip' => 'ABC1234', |
120
|
|
|
'country' => 'Testcountry', |
121
|
|
|
], |
122
|
|
|
]; |
123
|
|
|
|
124
|
|
|
$this->client->request('POST', '/schema-with-inline-refs', $data); |
125
|
|
|
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testValidationOfSchemaWithRefs() |
129
|
|
|
{ |
130
|
|
|
$data = [ |
131
|
|
|
'billingAddress' => [ |
132
|
|
|
'street' => 'Teststreet', |
133
|
|
|
'city' => 'Testcity', |
134
|
|
|
'zip' => 'ABC1234', |
135
|
|
|
'country' => 'Testcountry', |
136
|
|
|
], |
137
|
|
|
'shippingAddress' => [ |
138
|
|
|
'street' => 'Teststreet', |
139
|
|
|
'city' => 'Testcity', |
140
|
|
|
'zip' => 'ABC1234', |
141
|
|
|
'country' => 'Testcountry', |
142
|
|
|
], |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
|
$this->client->request('POST', '/schema-with-refs', $data); |
146
|
|
|
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Tests if missing shipping address and missing zip of billing address are detected when using inline refs. |
151
|
|
|
*/ |
152
|
|
|
public function testValidationOfSchemaWithInlineRefsAndErrors() |
153
|
|
|
{ |
154
|
|
|
$data = [ |
155
|
|
|
'billingAddress' => [ |
156
|
|
|
'street' => 'Teststreet', |
157
|
|
|
'city' => 'Testcity', |
158
|
|
|
'country' => 'Testcountry', |
159
|
|
|
], |
160
|
|
|
]; |
161
|
|
|
|
162
|
|
|
$this->client->request('POST', '/schema-with-inline-refs', $data); |
163
|
|
|
$this->assertEquals(400, $this->client->getResponse()->getStatusCode()); |
164
|
|
|
$responseContent = json_decode($this->client->getResponse()->getContent(), true); |
165
|
|
|
$this->assertResponseContainsProperties($responseContent, ['shippingAddress', 'billingAddress.zip']); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Tests if missing shipping address and missing zip of billing address are detected when using refs. |
170
|
|
|
*/ |
171
|
|
|
public function testValidationOfSchemaWithRefsAndErrors() |
172
|
|
|
{ |
173
|
|
|
$data = [ |
174
|
|
|
'billingAddress' => [ |
175
|
|
|
'street' => 'Teststreet', |
176
|
|
|
'city' => 'Testcity', |
177
|
|
|
'country' => 'Testcountry', |
178
|
|
|
], |
179
|
|
|
]; |
180
|
|
|
|
181
|
|
|
$this->client->request('POST', '/schema-with-refs', $data); |
182
|
|
|
$this->assertEquals(400, $this->client->getResponse()->getStatusCode()); |
183
|
|
|
$responseContent = json_decode($this->client->getResponse()->getContent(), true); |
184
|
|
|
$this->assertResponseContainsProperties($responseContent, ['shippingAddress', 'billingAddress.zip']); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Tests if response contains expected properties. |
189
|
|
|
* |
190
|
|
|
* @param array $responseContent |
191
|
|
|
* @param array $properties |
192
|
|
|
*/ |
193
|
|
|
public function assertResponseContainsProperties(array $responseContent, array $properties) |
194
|
|
|
{ |
195
|
|
|
foreach ($responseContent as $index => $content) { |
196
|
|
|
$this->assertContains($content['property'], $properties); |
197
|
|
|
unset($responseContent[$index]); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|