Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\rest\Tests;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\NestedArray;
|
Chris@0
|
6 use Drupal\Core\Config\Entity\ConfigEntityType;
|
Chris@0
|
7 use Drupal\node\NodeInterface;
|
Chris@0
|
8 use Drupal\rest\RestResourceConfigInterface;
|
Chris@0
|
9 use Drupal\simpletest\WebTestBase;
|
Chris@0
|
10 use GuzzleHttp\Cookie\FileCookieJar;
|
Chris@0
|
11 use GuzzleHttp\Cookie\SetCookie;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Test helper class that provides a REST client method to send HTTP requests.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @deprecated in Drupal 8.3.x-dev and will be removed before Drupal 9.0.0. Use \Drupal\Tests\rest\Functional\ResourceTestBase and \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase instead. Only retained for contributed module tests that may be using this base class.
|
Chris@0
|
17 */
|
Chris@0
|
18 abstract class RESTTestBase extends WebTestBase {
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * The REST resource config storage.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @var \Drupal\Core\Entity\EntityStorageInterface
|
Chris@0
|
24 */
|
Chris@0
|
25 protected $resourceConfigStorage;
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * The default serialization format to use for testing REST operations.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @var string
|
Chris@0
|
31 */
|
Chris@0
|
32 protected $defaultFormat;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * The default MIME type to use for testing REST operations.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var string
|
Chris@0
|
38 */
|
Chris@0
|
39 protected $defaultMimeType;
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * The entity type to use for testing.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @var string
|
Chris@0
|
45 */
|
Chris@0
|
46 protected $testEntityType = 'entity_test';
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * The default authentication provider to use for testing REST operations.
|
Chris@0
|
50 *
|
Chris@0
|
51 * @var array
|
Chris@0
|
52 */
|
Chris@0
|
53 protected $defaultAuth;
|
Chris@0
|
54
|
Chris@0
|
55
|
Chris@0
|
56 /**
|
Chris@0
|
57 * The raw response body from http request operations.
|
Chris@0
|
58 *
|
Chris@0
|
59 * @var array
|
Chris@0
|
60 */
|
Chris@0
|
61 protected $responseBody;
|
Chris@0
|
62
|
Chris@0
|
63 /**
|
Chris@0
|
64 * Modules to install.
|
Chris@0
|
65 *
|
Chris@0
|
66 * @var array
|
Chris@0
|
67 */
|
Chris@0
|
68 public static $modules = ['rest', 'entity_test'];
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * The last response.
|
Chris@0
|
72 *
|
Chris@0
|
73 * @var \Psr\Http\Message\ResponseInterface
|
Chris@0
|
74 */
|
Chris@0
|
75 protected $response;
|
Chris@0
|
76
|
Chris@0
|
77 protected function setUp() {
|
Chris@0
|
78 parent::setUp();
|
Chris@0
|
79 $this->defaultFormat = 'hal_json';
|
Chris@0
|
80 $this->defaultMimeType = 'application/hal+json';
|
Chris@0
|
81 $this->defaultAuth = ['cookie'];
|
Chris@0
|
82 $this->resourceConfigStorage = $this->container->get('entity_type.manager')->getStorage('rest_resource_config');
|
Chris@0
|
83 // Create a test content type for node testing.
|
Chris@0
|
84 if (in_array('node', static::$modules)) {
|
Chris@0
|
85 $this->drupalCreateContentType(['name' => 'resttest', 'type' => 'resttest']);
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 $this->cookieFile = $this->publicFilesDirectory . '/cookie.jar';
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * Calculates cookies used by guzzle later.
|
Chris@0
|
93 *
|
Chris@0
|
94 * @return \GuzzleHttp\Cookie\CookieJarInterface
|
Chris@0
|
95 * The used CURL options in guzzle.
|
Chris@0
|
96 */
|
Chris@0
|
97 protected function cookies() {
|
Chris@0
|
98 $cookies = [];
|
Chris@0
|
99
|
Chris@0
|
100 foreach ($this->cookies as $key => $cookie) {
|
Chris@0
|
101 $cookies[$key][] = $cookie['value'];
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 $request = \Drupal::request();
|
Chris@0
|
105 $cookies = NestedArray::mergeDeep($cookies, $this->extractCookiesFromRequest($request));
|
Chris@0
|
106
|
Chris@0
|
107 $cookie_jar = new FileCookieJar($this->cookieFile);
|
Chris@0
|
108 foreach ($cookies as $key => $cookie_values) {
|
Chris@0
|
109 foreach ($cookie_values as $cookie_value) {
|
Chris@0
|
110 // setcookie() sets the value of a cookie to be deleted, when its gonna
|
Chris@0
|
111 // be removed.
|
Chris@0
|
112 if ($cookie_value !== 'deleted') {
|
Chris@0
|
113 $cookie_jar->setCookie(new SetCookie(['Name' => $key, 'Value' => $cookie_value, 'Domain' => $request->getHost()]));
|
Chris@0
|
114 }
|
Chris@0
|
115 }
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 return $cookie_jar;
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 /**
|
Chris@0
|
122 * Helper function to issue a HTTP request with simpletest's cURL.
|
Chris@0
|
123 *
|
Chris@0
|
124 * @param string|\Drupal\Core\Url $url
|
Chris@0
|
125 * A Url object or system path.
|
Chris@0
|
126 * @param string $method
|
Chris@0
|
127 * HTTP method, one of GET, POST, PUT or DELETE.
|
Chris@0
|
128 * @param string $body
|
Chris@0
|
129 * The body for POST and PUT.
|
Chris@0
|
130 * @param string $mime_type
|
Chris@0
|
131 * The MIME type of the transmitted content.
|
Chris@0
|
132 * @param bool $csrf_token
|
Chris@0
|
133 * If NULL, a CSRF token will be retrieved and used. If FALSE, omit the
|
Chris@0
|
134 * X-CSRF-Token request header (to simulate developer error). Otherwise, the
|
Chris@0
|
135 * passed in value will be used as the value for the X-CSRF-Token request
|
Chris@0
|
136 * header (to simulate developer error, by sending an invalid CSRF token).
|
Chris@0
|
137 *
|
Chris@0
|
138 * @return string
|
Chris@0
|
139 * The content returned from the request.
|
Chris@0
|
140 */
|
Chris@0
|
141 protected function httpRequest($url, $method, $body = NULL, $mime_type = NULL, $csrf_token = NULL) {
|
Chris@0
|
142 if (!isset($mime_type)) {
|
Chris@0
|
143 $mime_type = $this->defaultMimeType;
|
Chris@0
|
144 }
|
Chris@0
|
145 if (!in_array($method, ['GET', 'HEAD', 'OPTIONS', 'TRACE'])) {
|
Chris@0
|
146 // GET the CSRF token first for writing requests.
|
Chris@0
|
147 $requested_token = $this->drupalGet('session/token');
|
Chris@0
|
148 }
|
Chris@0
|
149
|
Chris@0
|
150 $client = \Drupal::httpClient();
|
Chris@0
|
151 $url = $this->buildUrl($url);
|
Chris@0
|
152
|
Chris@0
|
153 $options = [
|
Chris@0
|
154 'http_errors' => FALSE,
|
Chris@0
|
155 'cookies' => $this->cookies(),
|
Chris@0
|
156 'curl' => [
|
Chris@0
|
157 CURLOPT_HEADERFUNCTION => [&$this, 'curlHeaderCallback'],
|
Chris@0
|
158 ],
|
Chris@0
|
159 ];
|
Chris@0
|
160 switch ($method) {
|
Chris@0
|
161 case 'GET':
|
Chris@0
|
162 $options += [
|
Chris@0
|
163 'headers' => [
|
Chris@0
|
164 'Accept' => $mime_type,
|
Chris@0
|
165 ],
|
Chris@0
|
166 ];
|
Chris@0
|
167 $response = $client->get($url, $options);
|
Chris@0
|
168 break;
|
Chris@0
|
169
|
Chris@0
|
170 case 'HEAD':
|
Chris@0
|
171 $response = $client->head($url, $options);
|
Chris@0
|
172 break;
|
Chris@0
|
173
|
Chris@0
|
174 case 'POST':
|
Chris@0
|
175 $options += [
|
Chris@0
|
176 'headers' => $csrf_token !== FALSE ? [
|
Chris@0
|
177 'Content-Type' => $mime_type,
|
Chris@0
|
178 'X-CSRF-Token' => ($csrf_token === NULL ? $requested_token : $csrf_token),
|
Chris@0
|
179 ] : [
|
Chris@0
|
180 'Content-Type' => $mime_type,
|
Chris@0
|
181 ],
|
Chris@0
|
182 'body' => $body,
|
Chris@0
|
183 ];
|
Chris@0
|
184 $response = $client->post($url, $options);
|
Chris@0
|
185 break;
|
Chris@0
|
186
|
Chris@0
|
187 case 'PUT':
|
Chris@0
|
188 $options += [
|
Chris@0
|
189 'headers' => $csrf_token !== FALSE ? [
|
Chris@0
|
190 'Content-Type' => $mime_type,
|
Chris@0
|
191 'X-CSRF-Token' => ($csrf_token === NULL ? $requested_token : $csrf_token),
|
Chris@0
|
192 ] : [
|
Chris@0
|
193 'Content-Type' => $mime_type,
|
Chris@0
|
194 ],
|
Chris@0
|
195 'body' => $body,
|
Chris@0
|
196 ];
|
Chris@0
|
197 $response = $client->put($url, $options);
|
Chris@0
|
198 break;
|
Chris@0
|
199
|
Chris@0
|
200 case 'PATCH':
|
Chris@0
|
201 $options += [
|
Chris@0
|
202 'headers' => $csrf_token !== FALSE ? [
|
Chris@0
|
203 'Content-Type' => $mime_type,
|
Chris@0
|
204 'X-CSRF-Token' => ($csrf_token === NULL ? $requested_token : $csrf_token),
|
Chris@0
|
205 ] : [
|
Chris@0
|
206 'Content-Type' => $mime_type,
|
Chris@0
|
207 ],
|
Chris@0
|
208 'body' => $body,
|
Chris@0
|
209 ];
|
Chris@0
|
210 $response = $client->patch($url, $options);
|
Chris@0
|
211 break;
|
Chris@0
|
212
|
Chris@0
|
213 case 'DELETE':
|
Chris@0
|
214 $options += [
|
Chris@0
|
215 'headers' => $csrf_token !== FALSE ? [
|
Chris@0
|
216 'Content-Type' => $mime_type,
|
Chris@0
|
217 'X-CSRF-Token' => ($csrf_token === NULL ? $requested_token : $csrf_token),
|
Chris@0
|
218 ] : [],
|
Chris@0
|
219 ];
|
Chris@0
|
220 $response = $client->delete($url, $options);
|
Chris@0
|
221 break;
|
Chris@0
|
222 }
|
Chris@0
|
223
|
Chris@0
|
224 $this->response = $response;
|
Chris@0
|
225 $this->responseBody = (string) $response->getBody();
|
Chris@0
|
226 $this->setRawContent($this->responseBody);
|
Chris@0
|
227
|
Chris@0
|
228 // Ensure that any changes to variables in the other thread are picked up.
|
Chris@0
|
229 $this->refreshVariables();
|
Chris@0
|
230
|
Chris@0
|
231 $this->verbose($method . ' request to: ' . $url .
|
Chris@0
|
232 '<hr />Code: ' . $this->response->getStatusCode() .
|
Chris@0
|
233 (isset($options['headers']) ? '<hr />Request headers: ' . nl2br(print_r($options['headers'], TRUE)) : '') .
|
Chris@0
|
234 (isset($options['body']) ? '<hr />Request body: ' . nl2br(print_r($options['body'], TRUE)) : '') .
|
Chris@0
|
235 '<hr />Response headers: ' . nl2br(print_r($response->getHeaders(), TRUE)) .
|
Chris@0
|
236 '<hr />Response body: ' . $this->responseBody);
|
Chris@0
|
237
|
Chris@0
|
238 return $this->responseBody;
|
Chris@0
|
239 }
|
Chris@0
|
240
|
Chris@0
|
241 /**
|
Chris@0
|
242 * {@inheritdoc}
|
Chris@0
|
243 */
|
Chris@0
|
244 protected function assertResponse($code, $message = '', $group = 'Browser') {
|
Chris@0
|
245 if (!isset($this->response)) {
|
Chris@0
|
246 return parent::assertResponse($code, $message, $group);
|
Chris@0
|
247 }
|
Chris@0
|
248 return $this->assertEqual($code, $this->response->getStatusCode(), $message ? $message : "HTTP response expected $code, actual {$this->response->getStatusCode()}", $group);
|
Chris@0
|
249 }
|
Chris@0
|
250
|
Chris@0
|
251 /**
|
Chris@0
|
252 * {@inheritdoc}
|
Chris@0
|
253 */
|
Chris@0
|
254 protected function drupalGetHeaders($all_requests = FALSE) {
|
Chris@0
|
255 if (!isset($this->response)) {
|
Chris@0
|
256 return parent::drupalGetHeaders($all_requests);
|
Chris@0
|
257 }
|
Chris@0
|
258 $lowercased_keys = array_map('strtolower', array_keys($this->response->getHeaders()));
|
Chris@0
|
259 return array_map(function (array $header) {
|
Chris@0
|
260 return implode(', ', $header);
|
Chris@0
|
261 }, array_combine($lowercased_keys, array_values($this->response->getHeaders())));
|
Chris@0
|
262 }
|
Chris@0
|
263
|
Chris@0
|
264 /**
|
Chris@0
|
265 * {@inheritdoc}
|
Chris@0
|
266 */
|
Chris@0
|
267 protected function drupalGetHeader($name, $all_requests = FALSE) {
|
Chris@0
|
268 if (!isset($this->response)) {
|
Chris@0
|
269 return parent::drupalGetHeader($name, $all_requests);
|
Chris@0
|
270 }
|
Chris@0
|
271 if ($header = $this->response->getHeader($name)) {
|
Chris@0
|
272 return implode(', ', $header);
|
Chris@0
|
273 }
|
Chris@0
|
274 }
|
Chris@0
|
275
|
Chris@0
|
276 /**
|
Chris@0
|
277 * Creates entity objects based on their types.
|
Chris@0
|
278 *
|
Chris@0
|
279 * @param string $entity_type
|
Chris@0
|
280 * The type of the entity that should be created.
|
Chris@0
|
281 *
|
Chris@0
|
282 * @return \Drupal\Core\Entity\EntityInterface
|
Chris@0
|
283 * The new entity object.
|
Chris@0
|
284 */
|
Chris@0
|
285 protected function entityCreate($entity_type) {
|
Chris@0
|
286 return $this->container->get('entity_type.manager')
|
Chris@0
|
287 ->getStorage($entity_type)
|
Chris@0
|
288 ->create($this->entityValues($entity_type));
|
Chris@0
|
289 }
|
Chris@0
|
290
|
Chris@0
|
291 /**
|
Chris@0
|
292 * Provides an array of suitable property values for an entity type.
|
Chris@0
|
293 *
|
Chris@0
|
294 * Required properties differ from entity type to entity type, so we keep a
|
Chris@0
|
295 * minimum mapping here.
|
Chris@0
|
296 *
|
Chris@0
|
297 * @param string $entity_type_id
|
Chris@0
|
298 * The ID of the type of entity that should be created.
|
Chris@0
|
299 *
|
Chris@0
|
300 * @return array
|
Chris@0
|
301 * An array of values keyed by property name.
|
Chris@0
|
302 */
|
Chris@0
|
303 protected function entityValues($entity_type_id) {
|
Chris@0
|
304 switch ($entity_type_id) {
|
Chris@0
|
305 case 'entity_test':
|
Chris@0
|
306 return [
|
Chris@0
|
307 'name' => $this->randomMachineName(),
|
Chris@0
|
308 'user_id' => 1,
|
Chris@0
|
309 'field_test_text' => [
|
Chris@0
|
310 0 => [
|
Chris@0
|
311 'value' => $this->randomString(),
|
Chris@0
|
312 'format' => 'plain_text',
|
Chris@0
|
313 ],
|
Chris@0
|
314 ],
|
Chris@0
|
315 ];
|
Chris@0
|
316 case 'config_test':
|
Chris@0
|
317 return [
|
Chris@0
|
318 'id' => $this->randomMachineName(),
|
Chris@0
|
319 'label' => 'Test label',
|
Chris@0
|
320 ];
|
Chris@0
|
321 case 'node':
|
Chris@0
|
322 return ['title' => $this->randomString(), 'type' => 'resttest'];
|
Chris@0
|
323 case 'node_type':
|
Chris@0
|
324 return [
|
Chris@0
|
325 'type' => 'article',
|
Chris@0
|
326 'name' => $this->randomMachineName(),
|
Chris@0
|
327 ];
|
Chris@0
|
328 case 'user':
|
Chris@0
|
329 return ['name' => $this->randomMachineName()];
|
Chris@0
|
330
|
Chris@0
|
331 case 'comment':
|
Chris@0
|
332 return [
|
Chris@0
|
333 'subject' => $this->randomMachineName(),
|
Chris@0
|
334 'entity_type' => 'node',
|
Chris@0
|
335 'comment_type' => 'comment',
|
Chris@0
|
336 'comment_body' => $this->randomString(),
|
Chris@0
|
337 'entity_id' => 'invalid',
|
Chris@0
|
338 'field_name' => 'comment',
|
Chris@0
|
339 ];
|
Chris@0
|
340 case 'taxonomy_vocabulary':
|
Chris@0
|
341 return [
|
Chris@0
|
342 'vid' => 'tags',
|
Chris@0
|
343 'name' => $this->randomMachineName(),
|
Chris@0
|
344 ];
|
Chris@0
|
345 case 'block':
|
Chris@0
|
346 // Block placements depend on themes, ensure Bartik is installed.
|
Chris@0
|
347 $this->container->get('theme_installer')->install(['bartik']);
|
Chris@0
|
348 return [
|
Chris@0
|
349 'id' => strtolower($this->randomMachineName(8)),
|
Chris@0
|
350 'plugin' => 'system_powered_by_block',
|
Chris@0
|
351 'theme' => 'bartik',
|
Chris@0
|
352 'region' => 'header',
|
Chris@0
|
353 ];
|
Chris@0
|
354 default:
|
Chris@0
|
355 if ($this->isConfigEntity($entity_type_id)) {
|
Chris@0
|
356 return $this->configEntityValues($entity_type_id);
|
Chris@0
|
357 }
|
Chris@0
|
358 return [];
|
Chris@0
|
359 }
|
Chris@0
|
360 }
|
Chris@0
|
361
|
Chris@0
|
362 /**
|
Chris@0
|
363 * Enables the REST service interface for a specific entity type.
|
Chris@0
|
364 *
|
Chris@0
|
365 * @param string|false $resource_type
|
Chris@0
|
366 * The resource type that should get REST API enabled or FALSE to disable all
|
Chris@0
|
367 * resource types.
|
Chris@0
|
368 * @param string $method
|
Chris@0
|
369 * The HTTP method to enable, e.g. GET, POST etc.
|
Chris@0
|
370 * @param string|array $format
|
Chris@0
|
371 * (Optional) The serialization format, e.g. hal_json, or a list of formats.
|
Chris@0
|
372 * @param array $auth
|
Chris@0
|
373 * (Optional) The list of valid authentication methods.
|
Chris@0
|
374 */
|
Chris@0
|
375 protected function enableService($resource_type, $method = 'GET', $format = NULL, array $auth = []) {
|
Chris@0
|
376 if ($resource_type) {
|
Chris@0
|
377 // Enable REST API for this entity type.
|
Chris@0
|
378 $resource_config_id = str_replace(':', '.', $resource_type);
|
Chris@0
|
379 // get entity by id
|
Chris@0
|
380 /** @var \Drupal\rest\RestResourceConfigInterface $resource_config */
|
Chris@0
|
381 $resource_config = $this->resourceConfigStorage->load($resource_config_id);
|
Chris@0
|
382 if (!$resource_config) {
|
Chris@0
|
383 $resource_config = $this->resourceConfigStorage->create([
|
Chris@0
|
384 'id' => $resource_config_id,
|
Chris@0
|
385 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
|
Chris@0
|
386 'configuration' => []
|
Chris@0
|
387 ]);
|
Chris@0
|
388 }
|
Chris@0
|
389 $configuration = $resource_config->get('configuration');
|
Chris@0
|
390
|
Chris@0
|
391 if (is_array($format)) {
|
Chris@0
|
392 for ($i = 0; $i < count($format); $i++) {
|
Chris@0
|
393 $configuration[$method]['supported_formats'][] = $format[$i];
|
Chris@0
|
394 }
|
Chris@0
|
395 }
|
Chris@0
|
396 else {
|
Chris@0
|
397 if ($format == NULL) {
|
Chris@0
|
398 $format = $this->defaultFormat;
|
Chris@0
|
399 }
|
Chris@0
|
400 $configuration[$method]['supported_formats'][] = $format;
|
Chris@0
|
401 }
|
Chris@0
|
402
|
Chris@0
|
403 if (!is_array($auth) || empty($auth)) {
|
Chris@0
|
404 $auth = $this->defaultAuth;
|
Chris@0
|
405 }
|
Chris@0
|
406 foreach ($auth as $auth_provider) {
|
Chris@0
|
407 $configuration[$method]['supported_auth'][] = $auth_provider;
|
Chris@0
|
408 }
|
Chris@0
|
409
|
Chris@0
|
410 $resource_config->set('configuration', $configuration);
|
Chris@0
|
411 $resource_config->save();
|
Chris@0
|
412 }
|
Chris@0
|
413 else {
|
Chris@0
|
414 foreach ($this->resourceConfigStorage->loadMultiple() as $resource_config) {
|
Chris@0
|
415 $resource_config->delete();
|
Chris@0
|
416 }
|
Chris@0
|
417 }
|
Chris@0
|
418 $this->rebuildCache();
|
Chris@0
|
419 }
|
Chris@0
|
420
|
Chris@0
|
421 /**
|
Chris@0
|
422 * Rebuilds routing caches.
|
Chris@0
|
423 */
|
Chris@0
|
424 protected function rebuildCache() {
|
Chris@0
|
425 $this->container->get('router.builder')->rebuildIfNeeded();
|
Chris@0
|
426 }
|
Chris@0
|
427
|
Chris@0
|
428 /**
|
Chris@0
|
429 * {@inheritdoc}
|
Chris@0
|
430 *
|
Chris@0
|
431 * This method is overridden to deal with a cURL quirk: the usage of
|
Chris@0
|
432 * CURLOPT_CUSTOMREQUEST cannot be unset on the cURL handle, so we need to
|
Chris@0
|
433 * override it every time it is omitted.
|
Chris@0
|
434 */
|
Chris@0
|
435 protected function curlExec($curl_options, $redirect = FALSE) {
|
Chris@0
|
436 unset($this->response);
|
Chris@0
|
437
|
Chris@0
|
438 if (!isset($curl_options[CURLOPT_CUSTOMREQUEST])) {
|
Chris@0
|
439 if (!empty($curl_options[CURLOPT_HTTPGET])) {
|
Chris@0
|
440 $curl_options[CURLOPT_CUSTOMREQUEST] = 'GET';
|
Chris@0
|
441 }
|
Chris@0
|
442 if (!empty($curl_options[CURLOPT_POST])) {
|
Chris@0
|
443 $curl_options[CURLOPT_CUSTOMREQUEST] = 'POST';
|
Chris@0
|
444 }
|
Chris@0
|
445 }
|
Chris@0
|
446 return parent::curlExec($curl_options, $redirect);
|
Chris@0
|
447 }
|
Chris@0
|
448
|
Chris@0
|
449 /**
|
Chris@0
|
450 * Provides the necessary user permissions for entity operations.
|
Chris@0
|
451 *
|
Chris@0
|
452 * @param string $entity_type_id
|
Chris@0
|
453 * The entity type.
|
Chris@0
|
454 * @param string $operation
|
Chris@0
|
455 * The operation, one of 'view', 'create', 'update' or 'delete'.
|
Chris@0
|
456 *
|
Chris@0
|
457 * @return array
|
Chris@0
|
458 * The set of user permission strings.
|
Chris@0
|
459 */
|
Chris@0
|
460 protected function entityPermissions($entity_type_id, $operation) {
|
Chris@0
|
461 switch ($entity_type_id) {
|
Chris@0
|
462 case 'entity_test':
|
Chris@0
|
463 switch ($operation) {
|
Chris@0
|
464 case 'view':
|
Chris@0
|
465 return ['view test entity'];
|
Chris@0
|
466 case 'create':
|
Chris@0
|
467 case 'update':
|
Chris@0
|
468 case 'delete':
|
Chris@0
|
469 return ['administer entity_test content'];
|
Chris@0
|
470 }
|
Chris@0
|
471 case 'node':
|
Chris@0
|
472 switch ($operation) {
|
Chris@0
|
473 case 'view':
|
Chris@0
|
474 return ['access content'];
|
Chris@0
|
475 case 'create':
|
Chris@0
|
476 return ['create resttest content'];
|
Chris@0
|
477 case 'update':
|
Chris@0
|
478 return ['edit any resttest content'];
|
Chris@0
|
479 case 'delete':
|
Chris@0
|
480 return ['delete any resttest content'];
|
Chris@0
|
481 }
|
Chris@0
|
482
|
Chris@0
|
483 case 'comment':
|
Chris@0
|
484 switch ($operation) {
|
Chris@0
|
485 case 'view':
|
Chris@0
|
486 return ['access comments'];
|
Chris@0
|
487
|
Chris@0
|
488 case 'create':
|
Chris@0
|
489 return ['post comments', 'skip comment approval'];
|
Chris@0
|
490
|
Chris@0
|
491 case 'update':
|
Chris@0
|
492 return ['edit own comments'];
|
Chris@0
|
493
|
Chris@0
|
494 case 'delete':
|
Chris@0
|
495 return ['administer comments'];
|
Chris@0
|
496 }
|
Chris@0
|
497 break;
|
Chris@0
|
498
|
Chris@0
|
499 case 'user':
|
Chris@0
|
500 switch ($operation) {
|
Chris@0
|
501 case 'view':
|
Chris@0
|
502 return ['access user profiles'];
|
Chris@0
|
503
|
Chris@0
|
504 default:
|
Chris@0
|
505 return ['administer users'];
|
Chris@0
|
506 }
|
Chris@0
|
507
|
Chris@0
|
508 default:
|
Chris@0
|
509 if ($this->isConfigEntity($entity_type_id)) {
|
Chris@0
|
510 $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
|
Chris@0
|
511 if ($admin_permission = $entity_type->getAdminPermission()) {
|
Chris@0
|
512 return [$admin_permission];
|
Chris@0
|
513 }
|
Chris@0
|
514 }
|
Chris@0
|
515 }
|
Chris@0
|
516 return [];
|
Chris@0
|
517 }
|
Chris@0
|
518
|
Chris@0
|
519 /**
|
Chris@0
|
520 * Loads an entity based on the location URL returned in the location header.
|
Chris@0
|
521 *
|
Chris@0
|
522 * @param string $location_url
|
Chris@0
|
523 * The URL returned in the Location header.
|
Chris@0
|
524 *
|
Chris@0
|
525 * @return \Drupal\Core\Entity\Entity|false
|
Chris@0
|
526 * The entity or FALSE if there is no matching entity.
|
Chris@0
|
527 */
|
Chris@0
|
528 protected function loadEntityFromLocationHeader($location_url) {
|
Chris@0
|
529 $url_parts = explode('/', $location_url);
|
Chris@0
|
530 $id = end($url_parts);
|
Chris@0
|
531 return $this->container->get('entity_type.manager')
|
Chris@0
|
532 ->getStorage($this->testEntityType)->load($id);
|
Chris@0
|
533 }
|
Chris@0
|
534
|
Chris@0
|
535 /**
|
Chris@0
|
536 * Remove node fields that can only be written by an admin user.
|
Chris@0
|
537 *
|
Chris@0
|
538 * @param \Drupal\node\NodeInterface $node
|
Chris@0
|
539 * The node to remove fields where non-administrative users cannot write.
|
Chris@0
|
540 *
|
Chris@0
|
541 * @return \Drupal\node\NodeInterface
|
Chris@0
|
542 * The node with removed fields.
|
Chris@0
|
543 */
|
Chris@0
|
544 protected function removeNodeFieldsForNonAdminUsers(NodeInterface $node) {
|
Chris@0
|
545 $node->set('status', NULL);
|
Chris@0
|
546 $node->set('created', NULL);
|
Chris@0
|
547 $node->set('changed', NULL);
|
Chris@0
|
548 $node->set('promote', NULL);
|
Chris@0
|
549 $node->set('sticky', NULL);
|
Chris@0
|
550 $node->set('revision_timestamp', NULL);
|
Chris@0
|
551 $node->set('revision_log', NULL);
|
Chris@0
|
552 $node->set('uid', NULL);
|
Chris@0
|
553
|
Chris@0
|
554 return $node;
|
Chris@0
|
555 }
|
Chris@0
|
556
|
Chris@0
|
557 /**
|
Chris@0
|
558 * Check to see if the HTTP request response body is identical to the expected
|
Chris@0
|
559 * value.
|
Chris@0
|
560 *
|
Chris@0
|
561 * @param $expected
|
Chris@0
|
562 * The first value to check.
|
Chris@0
|
563 * @param $message
|
Chris@0
|
564 * (optional) A message to display with the assertion. Do not translate
|
Chris@0
|
565 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
|
Chris@0
|
566 * variables in the message text, not t(). If left blank, a default message
|
Chris@0
|
567 * will be displayed.
|
Chris@0
|
568 * @param $group
|
Chris@0
|
569 * (optional) The group this message is in, which is displayed in a column
|
Chris@0
|
570 * in test output. Use 'Debug' to indicate this is debugging output. Do not
|
Chris@0
|
571 * translate this string. Defaults to 'Other'; most tests do not override
|
Chris@0
|
572 * this default.
|
Chris@0
|
573 *
|
Chris@0
|
574 * @return bool
|
Chris@0
|
575 * TRUE if the assertion succeeded, FALSE otherwise.
|
Chris@0
|
576 */
|
Chris@0
|
577 protected function assertResponseBody($expected, $message = '', $group = 'REST Response') {
|
Chris@0
|
578 return $this->assertIdentical($expected, $this->responseBody, $message ? $message : strtr('Response body @expected (expected) is equal to @response (actual).', ['@expected' => var_export($expected, TRUE), '@response' => var_export($this->responseBody, TRUE)]), $group);
|
Chris@0
|
579 }
|
Chris@0
|
580
|
Chris@0
|
581 /**
|
Chris@0
|
582 * Checks if an entity type id is for a Config Entity.
|
Chris@0
|
583 *
|
Chris@0
|
584 * @param string $entity_type_id
|
Chris@0
|
585 * The entity type ID to check.
|
Chris@0
|
586 *
|
Chris@0
|
587 * @return bool
|
Chris@0
|
588 * TRUE if the entity is a Config Entity, FALSE otherwise.
|
Chris@0
|
589 */
|
Chris@0
|
590 protected function isConfigEntity($entity_type_id) {
|
Chris@0
|
591 return \Drupal::entityTypeManager()->getDefinition($entity_type_id) instanceof ConfigEntityType;
|
Chris@0
|
592 }
|
Chris@0
|
593
|
Chris@0
|
594 /**
|
Chris@0
|
595 * Provides an array of suitable property values for a config entity type.
|
Chris@0
|
596 *
|
Chris@0
|
597 * Config entities have some common keys that need to be created. Required
|
Chris@0
|
598 * properties differ among config entity types, so we keep a minimum mapping
|
Chris@0
|
599 * here.
|
Chris@0
|
600 *
|
Chris@0
|
601 * @param string $entity_type_id
|
Chris@0
|
602 * The ID of the type of entity that should be created.
|
Chris@0
|
603 *
|
Chris@0
|
604 * @return array
|
Chris@0
|
605 * An array of values keyed by property name.
|
Chris@0
|
606 */
|
Chris@0
|
607 protected function configEntityValues($entity_type_id) {
|
Chris@0
|
608 $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
|
Chris@0
|
609 $keys = $entity_type->getKeys();
|
Chris@0
|
610 $values = [];
|
Chris@0
|
611 // Fill out known key values that are shared across entity types.
|
Chris@0
|
612 foreach ($keys as $key) {
|
Chris@0
|
613 if ($key === 'id' || $key === 'label') {
|
Chris@0
|
614 $values[$key] = $this->randomMachineName();
|
Chris@0
|
615 }
|
Chris@0
|
616 }
|
Chris@0
|
617 // Add extra values for particular entity types.
|
Chris@0
|
618 switch ($entity_type_id) {
|
Chris@0
|
619 case 'block':
|
Chris@0
|
620 $values['plugin'] = 'system_powered_by_block';
|
Chris@0
|
621 break;
|
Chris@0
|
622 }
|
Chris@0
|
623 return $values;
|
Chris@0
|
624 }
|
Chris@0
|
625
|
Chris@0
|
626 }
|