Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Routing / ContentNegotiationRoutingTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Routing;
4
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\KernelTests\KernelTestBase;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\Response;
9
10 /**
11  * Tests content negotiation routing variations.
12  *
13  * @group ContentNegotiation
14  */
15 class ContentNegotiationRoutingTest extends KernelTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['conneg_test'];
21
22   /**
23    * {@inheritdoc}
24    */
25   public function register(ContainerBuilder $container) {
26     parent::register($container);
27
28     // \Drupal\KernelTests\KernelTestBase::register() removes the alias path
29     // processor.
30     if ($container->hasDefinition('path_processor_alias')) {
31       $definition = $container->getDefinition('path_processor_alias');
32       $definition->addTag('path_processor_inbound', ['priority' => 100])->addTag('path_processor_outbound', ['priority' => 300]);
33     }
34   }
35
36   /**
37    * Tests the content negotiation aspect of routing.
38    */
39   public function testContentRouting() {
40     /** @var \Drupal\Core\Path\AliasStorageInterface $path_alias_storage */
41     $path_alias_storage = $this->container->get('path.alias_storage');
42     // Alias with extension pointing to no extension/constant content-type.
43     $path_alias_storage->save('/conneg/html', '/alias.html');
44
45     // Alias with extension pointing to dynamic extension/linked content-type.
46     $path_alias_storage->save('/conneg/html?_format=json', '/alias.json');
47
48     $tests = [
49       // ['path', 'accept', 'content-type'],
50
51       // Extension is part of the route path. Constant Content-type.
52       ['conneg/simple.json', '', 'application/json'],
53       ['conneg/simple.json', 'application/xml', 'application/json'],
54       ['conneg/simple.json', 'application/json', 'application/json'],
55       // No extension. Constant Content-type.
56       ['conneg/html', '', 'text/html'],
57       ['conneg/html', '*/*', 'text/html'],
58       ['conneg/html', 'application/xml', 'text/html'],
59       ['conneg/html', 'text/xml', 'text/html'],
60       ['conneg/html', 'text/html', 'text/html'],
61       // Dynamic extension. Linked Content-type.
62       ['conneg/html?_format=json', '', 'application/json'],
63       ['conneg/html?_format=json', '*/*', 'application/json'],
64       ['conneg/html?_format=json', 'application/xml', 'application/json'],
65       ['conneg/html?_format=json', 'application/json', 'application/json'],
66       ['conneg/html?_format=xml', '', 'application/xml'],
67       ['conneg/html?_format=xml', '*/*', 'application/xml'],
68       ['conneg/html?_format=xml', 'application/json', 'application/xml'],
69       ['conneg/html?_format=xml', 'application/xml', 'application/xml'],
70
71       // Path with a variable. Variable contains a period.
72       ['conneg/plugin/plugin.id', '', 'text/html'],
73       ['conneg/plugin/plugin.id', '*/*', 'text/html'],
74       ['conneg/plugin/plugin.id', 'text/xml', 'text/html'],
75       ['conneg/plugin/plugin.id', 'text/html', 'text/html'],
76
77       // Alias with extension pointing to no extension/constant content-type.
78       ['alias.html', '', 'text/html'],
79       ['alias.html', '*/*', 'text/html'],
80       ['alias.html', 'text/xml', 'text/html'],
81       ['alias.html', 'text/html', 'text/html'],
82     ];
83
84     foreach ($tests as $test) {
85       $path = $test[0];
86       $accept_header = $test[1];
87       $content_type = $test[2];
88       $message = "Testing path:$path Accept:$accept_header Content-type:$content_type";
89       $request = Request::create('/' . $path);
90       if ($accept_header) {
91         $request->headers->set('Accept', $accept_header);
92       }
93
94       /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
95       $kernel = \Drupal::getContainer()->get('http_kernel');
96       $response = $kernel->handle($request);
97       // Verbose message since simpletest doesn't let us provide a message and
98       // see the error.
99       $this->assertTrue(TRUE, $message);
100       $this->assertEqual($response->getStatusCode(), Response::HTTP_OK);
101       $this->assertTrue(strpos($response->headers->get('Content-type'), $content_type) !== FALSE);
102     }
103   }
104
105   /**
106    * Full negotiation by header only.
107    */
108   public function testFullNegotiation() {
109     $this->enableModules(['accept_header_routing_test']);
110     \Drupal::service('router.builder')->rebuild();
111     $tests = [
112       // ['path', 'accept', 'content-type'],
113
114       ['conneg/negotiate', '', 'text/html'], // 406?
115       ['conneg/negotiate', '', 'text/html'], // 406?
116       // ['conneg/negotiate', '*/*', '??'],
117       ['conneg/negotiate', 'application/json', 'application/json'],
118       ['conneg/negotiate', 'application/xml', 'application/xml'],
119       ['conneg/negotiate', 'application/json', 'application/json'],
120       ['conneg/negotiate', 'application/xml', 'application/xml'],
121     ];
122
123     foreach ($tests as $test) {
124       $path = $test[0];
125       $accept_header = $test[1];
126       $content_type = $test[2];
127       $message = "Testing path:$path Accept:$accept_header Content-type:$content_type";
128       $request = Request::create('/' . $path);
129       $request->headers->set('Accept', $accept_header);
130
131       /** @var \Symfony\Component\HttpKernel\HttpKernelInterface $kernel */
132       $kernel = \Drupal::getContainer()->get('http_kernel');
133       $response = $kernel->handle($request);
134       // Verbose message since simpletest doesn't let us provide a message and
135       // see the error.
136       $this->pass($message);
137       $this->assertEqual($response->getStatusCode(), Response::HTTP_OK);
138     }
139   }
140
141 }