Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / system / tests / src / Functional / System / TrustedHostsTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\System;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests output on the status overview page.
9  *
10  * @group system
11  */
12 class TrustedHostsTest extends BrowserTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   protected function setUp() {
18     parent::setUp();
19
20     $admin_user = $this->drupalCreateUser([
21       'administer site configuration',
22     ]);
23     $this->drupalLogin($admin_user);
24   }
25
26   /**
27    * Tests that the status page shows an error when the trusted host setting
28    * is missing from settings.php
29    */
30   public function testStatusPageWithoutConfiguration() {
31     $this->drupalGet('admin/reports/status');
32     $this->assertResponse(200, 'The status page is reachable.');
33
34     $this->assertRaw(t('Trusted Host Settings'));
35     $this->assertRaw(t('The trusted_host_patterns setting is not configured in settings.php.'));
36   }
37
38   /**
39    * Tests that the status page shows the trusted patterns from settings.php.
40    */
41   public function testStatusPageWithConfiguration() {
42     $settings['settings']['trusted_host_patterns'] = (object) [
43       'value' => ['^' . preg_quote(\Drupal::request()->getHost()) . '$'],
44       'required' => TRUE,
45     ];
46
47     $this->writeSettings($settings);
48
49     $this->drupalGet('admin/reports/status');
50     $this->assertResponse(200, 'The status page is reachable.');
51
52     $this->assertRaw(t('Trusted Host Settings'));
53     $this->assertRaw(t('The trusted_host_patterns setting is set to allow'));
54   }
55
56   /**
57    * Tests that fake requests have the proper host configured.
58    *
59    * @see \Drupal\Core\Http\TrustedHostsRequestFactory
60    */
61   public function testFakeRequests() {
62     $this->container->get('module_installer')->install(['trusted_hosts_test']);
63     $this->container->get('router.builder')->rebuild();
64
65     $host = $this->container->get('request_stack')->getCurrentRequest()->getHost();
66     $settings['settings']['trusted_host_patterns'] = (object) [
67       'value' => ['^' . preg_quote($host) . '$'],
68       'required' => TRUE,
69     ];
70
71     $this->writeSettings($settings);
72
73     $this->drupalGet('trusted-hosts-test/fake-request');
74     $this->assertText('Host: ' . $host);
75   }
76
77   /**
78    * Tests that shortcut module works together with host verification.
79    */
80   public function testShortcut() {
81     $this->container->get('module_installer')->install(['block', 'shortcut']);
82     $this->rebuildContainer();
83     $this->container->get('router.builder')->rebuild();
84
85     /** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */
86     $entity_manager = $this->container->get('entity.manager');
87     $shortcut_storage = $entity_manager->getStorage('shortcut');
88
89     $shortcut = $shortcut_storage->create([
90       'title' => $this->randomString(),
91       'link' => 'internal:/admin/reports/status',
92       'shortcut_set' => 'default',
93     ]);
94     $shortcut_storage->save($shortcut);
95
96     // Grant the current user access to see the shortcuts.
97     $role_storage = $entity_manager->getStorage('user_role');
98     $roles = $this->loggedInUser->getRoles(TRUE);
99     /** @var \Drupal\user\RoleInterface $role */
100     $role = $role_storage->load(reset($roles));
101     $role->grantPermission('access shortcuts')->save();
102
103     $this->drupalPlaceBlock('shortcuts');
104
105     $this->drupalGet('');
106     $this->assertLink($shortcut->label());
107   }
108
109 }