Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / rest / src / Tests / ResourceTest.php
1 <?php
2
3 namespace Drupal\rest\Tests;
4
5 use Drupal\Core\Session\AccountInterface;
6 use Drupal\rest\RestResourceConfigInterface;
7 use Drupal\user\Entity\Role;
8 use Drupal\user\RoleInterface;
9
10 /**
11  * Tests the structure of a REST resource.
12  *
13  * @group rest
14  */
15 class ResourceTest extends RESTTestBase {
16
17   /**
18    * Modules to install.
19    *
20    * @var array
21    */
22   public static $modules = ['hal', 'rest', 'entity_test', 'rest_test'];
23
24   /**
25    * The entity.
26    *
27    * @var \Drupal\Core\Entity\EntityInterface
28    */
29   protected $entity;
30
31   /**
32    * {@inheritdoc}
33    */
34   protected function setUp() {
35     parent::setUp();
36     // Create an entity programmatic.
37     $this->entity = $this->entityCreate('entity_test');
38     $this->entity->save();
39
40     Role::load(AccountInterface::ANONYMOUS_ROLE)
41       ->grantPermission('view test entity')
42       ->save();
43   }
44
45   /**
46    * Tests that a resource without formats cannot be enabled.
47    */
48   public function testFormats() {
49     $this->resourceConfigStorage->create([
50       'id' => 'entity.entity_test',
51       'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
52       'configuration' => [
53         'GET' => [
54           'supported_auth' => [
55             'basic_auth',
56           ],
57         ],
58       ],
59     ])->save();
60
61     // Verify that accessing the resource returns 406.
62     $response = $this->httpRequest($this->entity->urlInfo()->setRouteParameter('_format', $this->defaultFormat), 'GET');
63     // \Drupal\Core\Routing\RequestFormatRouteFilter considers the canonical,
64     // non-REST route a match, but a lower quality one: no format restrictions
65     // means there's always a match and hence when there is no matching REST
66     // route, the non-REST route is used, but can't render into
67     // application/hal+json, so it returns a 406.
68     $this->assertResponse('406', 'HTTP response code is 406 when the resource does not define formats, because it falls back to the canonical, non-REST route.');
69     $this->curlClose();
70   }
71
72   /**
73    * Tests that a resource without authentication cannot be enabled.
74    */
75   public function testAuthentication() {
76     $this->resourceConfigStorage->create([
77       'id' => 'entity.entity_test',
78       'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
79       'configuration' => [
80         'GET' => [
81           'supported_formats' => [
82             'hal_json',
83           ],
84         ],
85       ],
86     ])->save();
87
88     // Verify that accessing the resource returns 401.
89     $response = $this->httpRequest($this->entity->urlInfo()->setRouteParameter('_format', $this->defaultFormat), 'GET');
90     // \Drupal\Core\Routing\RequestFormatRouteFilter considers the canonical,
91     // non-REST route a match, but a lower quality one: no format restrictions
92     // means there's always a match and hence when there is no matching REST
93     // route, the non-REST route is used, but can't render into
94     // application/hal+json, so it returns a 406.
95     $this->assertResponse('406', 'HTTP response code is 406 when the resource does not define formats, because it falls back to the canonical, non-REST route.');
96     $this->curlClose();
97   }
98
99   /**
100    * Tests that serialization_class is optional.
101    */
102   public function testSerializationClassIsOptional() {
103     $this->enableService('serialization_test', 'POST', 'json');
104
105     Role::load(RoleInterface::ANONYMOUS_ID)
106       ->grantPermission('restful post serialization_test')
107       ->save();
108
109     $serialized = $this->container->get('serializer')->serialize(['foo', 'bar'], 'json');
110     $this->httpRequest('serialization_test', 'POST', $serialized, 'application/json');
111     $this->assertResponse(200);
112     $this->assertResponseBody('["foo","bar"]');
113   }
114
115   /**
116    * Tests that resource URI paths are formatted properly.
117    */
118   public function testUriPaths() {
119     $this->enableService('entity:entity_test');
120     /** @var \Drupal\rest\Plugin\Type\ResourcePluginManager $manager */
121     $manager = \Drupal::service('plugin.manager.rest');
122
123     foreach ($manager->getDefinitions() as $resource => $definition) {
124       foreach ($definition['uri_paths'] as $key => $uri_path) {
125         $this->assertFalse(strpos($uri_path, '//'), 'The resource URI path does not have duplicate slashes.');
126       }
127     }
128   }
129
130 }