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
|
|
|
protected function setUp() |
38
|
|
|
{ |
39
|
|
|
parent::setUp(); |
40
|
|
|
|
41
|
|
|
$this->httpHost = getenv('EZP_TEST_REST_HOST') ?: 'localhost'; |
42
|
|
|
$this->httpAuth = getenv('EZP_TEST_REST_AUTH') ?: 'admin:publish'; |
43
|
|
|
|
44
|
|
|
$this->httpClient = new \Buzz\Client\Curl(); |
45
|
|
|
$this->httpClient->setVerifyPeer(false); |
46
|
|
|
$this->httpClient->setTimeout(90); |
47
|
|
|
$this->httpClient->setOption(CURLOPT_FOLLOWLOCATION, false); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return HttpResponse |
52
|
|
|
*/ |
53
|
|
|
public function sendHttpRequest(HttpRequest $request) |
54
|
|
|
{ |
55
|
|
|
$response = new HttpResponse(); |
56
|
|
|
$this->httpClient->send($request, $response); |
57
|
|
|
|
58
|
|
|
return $response; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return HttpRequest |
63
|
|
|
*/ |
64
|
|
|
public function createHttpRequest($method, $uri, $contentType = '', $acceptType = '') |
65
|
|
|
{ |
66
|
|
|
$request = new HttpRequest($method, $uri, $this->httpHost); |
67
|
|
|
$request->addHeader('Authorization: Basic ' . base64_encode($this->httpAuth)); |
68
|
|
|
$request->addHeader('Content-Type: ' . $this->generateMediaTypeString($contentType)); |
69
|
|
|
$request->addHeader('Accept: ' . $this->generateMediaTypeString($acceptType)); |
70
|
|
|
|
71
|
|
|
return $request; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function assertHttpResponseCodeEquals(HttpResponse $response, $expected) |
75
|
|
|
{ |
76
|
|
|
$responseCode = $response->getStatusCode(); |
77
|
|
|
if ($responseCode != $expected) { |
78
|
|
|
$errorMessageString = ''; |
79
|
|
|
if (strpos($response->getHeader('Content-Type'), 'application/vnd.ez.api.ErrorMessage+xml') !== false) { |
80
|
|
|
$body = \simplexml_load_string($response->getContent()); |
81
|
|
|
$errorMessageString = $this->getHttpResponseCodeErrrorMessage($body); |
82
|
|
|
} elseif (strpos($response->getHeader('Content-Type'), 'application/vnd.ez.api.ErrorMessage+json') !== false) { |
83
|
|
|
$body = json_decode($response->getContent()); |
84
|
|
|
$errorMessageString = $this->getHttpResponseCodeErrrorMessage($body->ErrorMessage); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
self::assertEquals($expected, $responseCode, $errorMessageString); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function getHttpResponseCodeErrrorMessage($body) |
92
|
|
|
{ |
93
|
|
|
$errorMessageString = "Error message ({$body->errorCode}): {$body->errorDescription}"; |
94
|
|
|
|
95
|
|
|
// If server is in debug mode it will return file, line and trace. |
96
|
|
|
if (isset($body->file)) { |
97
|
|
|
$errorMessageString .= "\n In {$body->file}:{$body->line}\n\n{$body->trace}"; |
98
|
|
|
} else { |
99
|
|
|
$errorMessageString .= "\n In \<no trace, debug disabled\>"; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $errorMessageString; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected function assertHttpResponseHasHeader(HttpResponse $response, $header, $expectedValue = null) |
106
|
|
|
{ |
107
|
|
|
$headerValue = $response->getHeader($header); |
108
|
|
|
self::assertNotNull($headerValue, "Failed asserting that response has a $header header"); |
109
|
|
|
if ($expectedValue !== null) { |
110
|
|
|
self::assertEquals($expectedValue, $headerValue); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function generateMediaTypeString($typeString) |
115
|
|
|
{ |
116
|
|
|
return "application/vnd.ez.api.$typeString"; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function addCreatedElement($href) |
120
|
|
|
{ |
121
|
|
|
$testCase = $this; |
122
|
|
|
self::$createdContent[$href] = function () use ($href, $testCase) { |
123
|
|
|
$testCase->sendHttpRequest( |
124
|
|
|
$testCase->createHttpRequest('DELETE', $href) |
125
|
|
|
); |
126
|
|
|
}; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public static function tearDownAfterClass() |
130
|
|
|
{ |
131
|
|
|
self::clearCreatedElement(self::$createdContent); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private static function clearCreatedElement(array $contentArray) |
135
|
|
|
{ |
136
|
|
|
foreach (array_reverse($contentArray) as $href => $callback) { |
137
|
|
|
$callback(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $parentLocationId The REST id of the parent location |
143
|
|
|
* |
144
|
|
|
* @return array created Content, as an array |
145
|
|
|
*/ |
146
|
|
|
protected function createFolder($string, $parentLocationId) |
147
|
|
|
{ |
148
|
|
|
$string = $this->addTestSuffix($string); |
149
|
|
|
$xml = <<< XML |
150
|
|
|
<?xml version="1.0" encoding="UTF-8"?> |
151
|
|
|
<ContentCreate> |
152
|
|
|
<ContentType href="/api/ezp/v2/content/types/1" /> |
153
|
|
|
<mainLanguageCode>eng-GB</mainLanguageCode> |
154
|
|
|
<LocationCreate> |
155
|
|
|
<ParentLocation href="{$parentLocationId}" /> |
156
|
|
|
<priority>0</priority> |
157
|
|
|
<hidden>false</hidden> |
158
|
|
|
<sortField>PATH</sortField> |
159
|
|
|
<sortOrder>ASC</sortOrder> |
160
|
|
|
</LocationCreate> |
161
|
|
|
<Section href="/api/ezp/v2/content/sections/1" /> |
162
|
|
|
<alwaysAvailable>true</alwaysAvailable> |
163
|
|
|
<remoteId>{$string}</remoteId> |
164
|
|
|
<User href="/api/ezp/v2/user/users/14" /> |
165
|
|
|
<modificationDate>2012-09-30T12:30:00</modificationDate> |
166
|
|
|
<fields> |
167
|
|
|
<field> |
168
|
|
|
<fieldDefinitionIdentifier>name</fieldDefinitionIdentifier> |
169
|
|
|
<languageCode>eng-GB</languageCode> |
170
|
|
|
<fieldValue>{$string}</fieldValue> |
171
|
|
|
</field> |
172
|
|
|
</fields> |
173
|
|
|
</ContentCreate> |
174
|
|
|
XML; |
175
|
|
|
|
176
|
|
|
return $this->createContent($xml); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param $xml |
181
|
|
|
* |
182
|
|
|
* @return array Content key of the Content struct array |
183
|
|
|
*/ |
184
|
|
|
protected function createContent($xml) |
185
|
|
|
{ |
186
|
|
|
$request = $this->createHttpRequest('POST', '/api/ezp/v2/content/objects', 'ContentCreate+xml', 'Content+json'); |
187
|
|
|
$request->setContent($xml); |
188
|
|
|
|
189
|
|
|
$response = $this->sendHttpRequest($request); |
190
|
|
|
|
191
|
|
|
self::assertHttpResponseCodeEquals($response, 201); |
192
|
|
|
|
193
|
|
|
$content = json_decode($response->getContent(), true); |
194
|
|
|
|
195
|
|
|
if (!isset($content['Content']['CurrentVersion']['Version'])) { |
196
|
|
|
self::fail("Incomplete response (no version):\n" . $response->getContent() . "\n"); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$response = $this->sendHttpRequest( |
200
|
|
|
$request = $this->createHttpRequest('PUBLISH', $content['Content']['CurrentVersion']['Version']['_href']) |
201
|
|
|
); |
202
|
|
|
|
203
|
|
|
self::assertHttpResponseCodeEquals($response, 204); |
204
|
|
|
|
205
|
|
|
$this->addCreatedElement($content['Content']['_href'], true); |
206
|
|
|
|
207
|
|
|
return $content['Content']; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string $contentHref |
212
|
|
|
* |
213
|
|
|
* @return array |
214
|
|
|
*/ |
215
|
|
View Code Duplication |
protected function getContentLocations($contentHref) |
216
|
|
|
{ |
217
|
|
|
$response = $this->sendHttpRequest( |
218
|
|
|
$this->createHttpRequest('GET', "$contentHref/locations", '', 'LocationList+json') |
219
|
|
|
); |
220
|
|
|
self::assertHttpResponseCodeEquals($response, 200); |
221
|
|
|
$folderLocations = json_decode($response->getContent(), true); |
222
|
|
|
|
223
|
|
|
return $folderLocations; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
protected function addTestSuffix($string) |
227
|
|
|
{ |
228
|
|
|
if (!isset(self::$testSuffix)) { |
229
|
|
|
self::$testSuffix = uniqid(); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return $string . '_' . self::$testSuffix; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* List of REST contentId (/content/objects/12345) created by tests. |
237
|
|
|
* |
238
|
|
|
* @var array |
239
|
|
|
*/ |
240
|
|
|
private static $createdContent = array(); |
241
|
|
|
} |
242
|
|
|
|