592364636d954f36159d458f12ae45ace40de25c
[yaffs-website] / web / core / modules / system / tests / src / Functional / System / ResponseGeneratorTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\System;
4
5 use Drupal\rest\Entity\RestResourceConfig;
6 use Drupal\Tests\BrowserTestBase;
7
8 /**
9  * Tests to see if generator header is added.
10  *
11  * @group system
12  */
13 class ResponseGeneratorTest extends BrowserTestBase {
14
15   /**
16    * Modules to install.
17    *
18    * @var array
19    */
20   public static $modules = ['hal', 'rest', 'node', 'basic_auth'];
21
22   /**
23    * {@inheritdoc}
24    */
25   protected function setUp() {
26     parent::setUp();
27     $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
28
29     $account = $this->drupalCreateUser(['access content']);
30     $this->drupalLogin($account);
31   }
32
33   /**
34    * Test to see if generator header is added.
35    */
36   public function testGeneratorHeaderAdded() {
37
38     $node = $this->drupalCreateNode();
39
40     list($version) = explode('.', \Drupal::VERSION, 2);
41     $expectedGeneratorHeader = 'Drupal ' . $version . ' (https://www.drupal.org)';
42
43     // Check to see if the header is added when viewing a normal content page
44     $this->drupalGet($node->urlInfo());
45     $this->assertResponse(200);
46     $this->assertEqual('text/html; charset=UTF-8', $this->drupalGetHeader('Content-Type'));
47     $this->assertEqual($expectedGeneratorHeader, $this->drupalGetHeader('X-Generator'));
48
49     // Check to see if the header is also added for a non-successful response
50     $this->drupalGet('llama');
51     $this->assertResponse(404);
52     $this->assertEqual('text/html; charset=UTF-8', $this->drupalGetHeader('Content-Type'));
53     $this->assertEqual($expectedGeneratorHeader, $this->drupalGetHeader('X-Generator'));
54
55     // Enable cookie-based authentication for the entity:node REST resource.
56     /** @var \Drupal\rest\RestResourceConfigInterface $resource_config */
57     $resource_config = RestResourceConfig::load('entity.node');
58     $configuration = $resource_config->get('configuration');
59     $configuration['authentication'][] = 'cookie';
60     $resource_config->set('configuration', $configuration)->save();
61     $this->rebuildAll();
62
63     // Tests to see if this also works for a non-html request
64     $this->drupalGet($node->toUrl()->setOption('query', ['_format' => 'hal_json']));
65     $this->assertResponse(200);
66     $this->assertEqual('application/hal+json', $this->drupalGetHeader('Content-Type'));
67     $this->assertEqual($expectedGeneratorHeader, $this->drupalGetHeader('X-Generator'));
68
69   }
70
71 }