1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the Functional\TestCase class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @version //autogentag// |
10
|
|
|
*/ |
11
|
|
|
namespace eZ\Bundle\EzPublishRestBundle\Tests\Functional; |
12
|
|
|
|
13
|
|
|
use Buzz\Message\Request as HttpRequest; |
14
|
|
|
use Buzz\Message\Response as HttpResponse; |
15
|
|
|
use PHPUnit_Framework_TestCase; |
16
|
|
|
|
17
|
|
|
class TestCase extends PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \Buzz\Client\ClientInterface |
21
|
|
|
*/ |
22
|
|
|
private $httpClient; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $httpHost; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
* Basic auth login:password |
32
|
|
|
*/ |
33
|
|
|
private $httpAuth; |
34
|
|
|
|
35
|
|
|
protected static $testSuffix; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $headers = []; |
41
|
|
|
|
42
|
|
|
protected function setUp() |
43
|
|
|
{ |
44
|
|
|
parent::setUp(); |
45
|
|
|
|
46
|
|
|
$this->httpHost = getenv('EZP_TEST_REST_HOST') ?: 'localhost'; |
47
|
|
|
$this->httpAuth = getenv('EZP_TEST_REST_AUTH') ?: 'admin:publish'; |
48
|
|
|
|
49
|
|
|
$this->httpClient = new \Buzz\Client\Curl(); |
50
|
|
|
$this->httpClient->setVerifyPeer(false); |
51
|
|
|
$this->httpClient->setTimeout(90); |
52
|
|
|
$this->httpClient->setOption(CURLOPT_FOLLOWLOCATION, false); |
53
|
|
|
$this->httpClient->setOption(CURLOPT_COOKIEFILE, null); |
54
|
|
|
|
55
|
|
|
$request = $this->createHttpRequest('POST', '/api/ezp/v2/user/sessions', 'SessionInput+json', 'Session+json'); |
56
|
|
|
$request->setContent('{"SessionInput": {"login": "admin", "password": "publish"}}'); |
57
|
|
|
$response = $this->sendHttpRequest($request); |
58
|
|
|
self::assertHttpResponseCodeEquals($response, 201); |
59
|
|
|
$session = json_decode($response->getContent())->Session; |
60
|
|
|
$this->headers[] = sprintf('Cookie: %s=%s', $session->name, $session->identifier); |
61
|
|
|
$this->headers[] = sprintf('X-CSRF-Token: %s', $session->csrfToken); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return HttpResponse |
66
|
|
|
*/ |
67
|
|
|
public function sendHttpRequest(HttpRequest $request) |
68
|
|
|
{ |
69
|
|
|
$response = new HttpResponse(); |
70
|
|
|
$this->httpClient->send($request, $response); |
71
|
|
|
|
72
|
|
|
return $response; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return HttpRequest |
77
|
|
|
*/ |
78
|
|
|
public function createHttpRequest($method, $uri, $contentType = '', $acceptType = '') |
79
|
|
|
{ |
80
|
|
|
$headers = array_merge( |
81
|
|
|
$method === 'POST' && $uri === '/api/ezp/v2/user/sessions' ? [] : $this->headers, |
82
|
|
|
[ |
83
|
|
|
'Content-Type: ' . $this->generateMediaTypeString($contentType), |
84
|
|
|
'Accept: ' . $this->generateMediaTypeString($acceptType), |
85
|
|
|
] |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
switch ($method) { |
89
|
|
|
case 'PUBLISH': $method = 'POST'; $headers[] = 'X-HTTP-Method-Override: PUBLISH'; break; |
90
|
|
|
case 'MOVE': $method = 'POST'; $headers[] = 'X-HTTP-Method-Override: MOVE'; break; |
91
|
|
|
case 'PATCH': $method = 'PATCH'; $headers[] = 'X-HTTP-Method-Override: PATCH'; break; |
92
|
|
|
case 'COPY': $method = 'POST'; $headers[] = 'X-HTTP-Method-Override: COPY'; break; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$request = new HttpRequest($method, $uri, $this->httpHost); |
96
|
|
|
$request->addHeaders($headers); |
97
|
|
|
|
98
|
|
|
return $request; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function assertHttpResponseCodeEquals(HttpResponse $response, $expected) |
102
|
|
|
{ |
103
|
|
|
$responseCode = $response->getStatusCode(); |
104
|
|
|
if ($responseCode != $expected) { |
105
|
|
|
$errorMessageString = ''; |
106
|
|
|
if (strpos($response->getHeader('Content-Type'), 'application/vnd.ez.api.ErrorMessage+xml') !== false) { |
107
|
|
|
$body = \simplexml_load_string($response->getContent()); |
108
|
|
|
$errorMessageString = $this->getHttpResponseCodeErrorMessage($body); |
109
|
|
|
} elseif (strpos($response->getHeader('Content-Type'), 'application/vnd.ez.api.ErrorMessage+json') !== false) { |
110
|
|
|
$body = json_decode($response->getContent()); |
111
|
|
|
$errorMessageString = $this->getHttpResponseCodeErrorMessage($body->ErrorMessage); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
self::assertEquals($expected, $responseCode, $errorMessageString); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function getHttpResponseCodeErrorMessage($errorMessage) |
119
|
|
|
{ |
120
|
|
|
$errorMessageString = <<< EOF |
121
|
|
|
Server error message ({$errorMessage->errorCode}): {$errorMessage->errorMessage} |
122
|
|
|
|
123
|
|
|
{$errorMessage->errorDescription} |
124
|
|
|
|
125
|
|
|
EOF; |
126
|
|
|
|
127
|
|
|
// If server is in debug mode it will return file, line and trace. |
128
|
|
|
if (!empty($errorMessage->file)) { |
129
|
|
|
$errorMessageString .= "\nIn {$errorMessage->file}:{$errorMessage->line}\n\n{$errorMessage->trace}"; |
130
|
|
|
} else { |
131
|
|
|
$errorMessageString .= "\nIn \<no trace, debug disabled\>"; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $errorMessageString; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
protected function assertHttpResponseHasHeader(HttpResponse $response, $header, $expectedValue = null) |
138
|
|
|
{ |
139
|
|
|
$headerValue = $response->getHeader($header); |
140
|
|
|
self::assertNotNull($headerValue, "Failed asserting that response has a $header header"); |
141
|
|
|
if ($expectedValue !== null) { |
142
|
|
|
self::assertEquals($expectedValue, $headerValue); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
protected function generateMediaTypeString($typeString) |
147
|
|
|
{ |
148
|
|
|
return "application/vnd.ez.api.$typeString"; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
protected function addCreatedElement($href) |
152
|
|
|
{ |
153
|
|
|
$testCase = $this; |
154
|
|
|
self::$createdContent[$href] = function () use ($href, $testCase) { |
155
|
|
|
$testCase->sendHttpRequest( |
156
|
|
|
$testCase->createHttpRequest('DELETE', $href) |
157
|
|
|
); |
158
|
|
|
}; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public static function tearDownAfterClass() |
162
|
|
|
{ |
163
|
|
|
self::clearCreatedElement(self::$createdContent); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
private static function clearCreatedElement(array $contentArray) |
167
|
|
|
{ |
168
|
|
|
foreach (array_reverse($contentArray) as $href => $callback) { |
169
|
|
|
$callback(); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string $parentLocationId The REST id of the parent location |
175
|
|
|
* |
176
|
|
|
* @return array created Content, as an array |
177
|
|
|
*/ |
178
|
|
|
protected function createFolder($string, $parentLocationId) |
179
|
|
|
{ |
180
|
|
|
$string = $this->addTestSuffix($string); |
181
|
|
|
$xml = <<< XML |
182
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
183
|
|
|
<ContentCreate> |
184
|
|
|
<ContentType href="/api/ezp/v2/content/types/1" /> |
185
|
|
|
<mainLanguageCode>eng-GB</mainLanguageCode> |
186
|
|
|
<LocationCreate> |
187
|
|
|
<ParentLocation href="{$parentLocationId}" /> |
188
|
|
|
<priority>0</priority> |
189
|
|
|
<hidden>false</hidden> |
190
|
|
|
<sortField>PATH</sortField> |
191
|
|
|
<sortOrder>ASC</sortOrder> |
192
|
|
|
</LocationCreate> |
193
|
|
|
<Section href="/api/ezp/v2/content/sections/1" /> |
194
|
|
|
<alwaysAvailable>true</alwaysAvailable> |
195
|
|
|
<remoteId>{$string}</remoteId> |
196
|
|
|
<User href="/api/ezp/v2/user/users/14" /> |
197
|
|
|
<modificationDate>2012-09-30T12:30:00</modificationDate> |
198
|
|
|
<fields> |
199
|
|
|
<field> |
200
|
|
|
<fieldDefinitionIdentifier>name</fieldDefinitionIdentifier> |
201
|
|
|
<languageCode>eng-GB</languageCode> |
202
|
|
|
<fieldValue>{$string}</fieldValue> |
203
|
|
|
</field> |
204
|
|
|
</fields> |
205
|
|
|
</ContentCreate> |
206
|
|
|
XML; |
207
|
|
|
|
208
|
|
|
return $this->createContent($xml); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param $xml |
213
|
|
|
* |
214
|
|
|
* @return array Content key of the Content struct array |
215
|
|
|
*/ |
216
|
|
|
protected function createContent($xml) |
217
|
|
|
{ |
218
|
|
|
$request = $this->createHttpRequest('POST', '/api/ezp/v2/content/objects', 'ContentCreate+xml', 'Content+json'); |
219
|
|
|
$request->setContent($xml); |
220
|
|
|
|
221
|
|
|
$response = $this->sendHttpRequest($request); |
222
|
|
|
|
223
|
|
|
self::assertHttpResponseCodeEquals($response, 201); |
224
|
|
|
|
225
|
|
|
$content = json_decode($response->getContent(), true); |
226
|
|
|
|
227
|
|
|
if (!isset($content['Content']['CurrentVersion']['Version'])) { |
228
|
|
|
self::fail("Incomplete response (no version):\n" . $response->getContent() . "\n"); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
$response = $this->sendHttpRequest( |
232
|
|
|
$request = $this->createHttpRequest('PUBLISH', $content['Content']['CurrentVersion']['Version']['_href']) |
233
|
|
|
); |
234
|
|
|
|
235
|
|
|
self::assertHttpResponseCodeEquals($response, 204); |
236
|
|
|
|
237
|
|
|
$this->addCreatedElement($content['Content']['_href'], true); |
238
|
|
|
|
239
|
|
|
return $content['Content']; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param string $contentHref |
244
|
|
|
* |
245
|
|
|
* @return array |
246
|
|
|
*/ |
247
|
|
View Code Duplication |
protected function getContentLocations($contentHref) |
248
|
|
|
{ |
249
|
|
|
$response = $this->sendHttpRequest( |
250
|
|
|
$this->createHttpRequest('GET', "$contentHref/locations", '', 'LocationList+json') |
251
|
|
|
); |
252
|
|
|
self::assertHttpResponseCodeEquals($response, 200); |
253
|
|
|
$folderLocations = json_decode($response->getContent(), true); |
254
|
|
|
|
255
|
|
|
return $folderLocations; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
protected function addTestSuffix($string) |
259
|
|
|
{ |
260
|
|
|
if (!isset(self::$testSuffix)) { |
261
|
|
|
self::$testSuffix = uniqid(); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return $string . '_' . self::$testSuffix; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* List of REST contentId (/content/objects/12345) created by tests. |
269
|
|
|
* |
270
|
|
|
* @var array |
271
|
|
|
*/ |
272
|
|
|
private static $createdContent = array(); |
273
|
|
|
} |
274
|
|
|
|