3 namespace Drupal\Tests\system\Functional\System;
5 use Drupal\Tests\BrowserTestBase;
8 * Tests output on the status overview page.
12 class TrustedHostsTest extends BrowserTestBase {
17 protected function setUp() {
20 $admin_user = $this->drupalCreateUser([
21 'administer site configuration',
23 $this->drupalLogin($admin_user);
27 * Tests that the status page shows an error when the trusted host setting
28 * is missing from settings.php
30 public function testStatusPageWithoutConfiguration() {
31 $this->drupalGet('admin/reports/status');
32 $this->assertResponse(200, 'The status page is reachable.');
34 $this->assertRaw(t('Trusted Host Settings'));
35 $this->assertRaw(t('The trusted_host_patterns setting is not configured in settings.php.'));
39 * Tests that the status page shows the trusted patterns from settings.php.
41 public function testStatusPageWithConfiguration() {
42 $settings['settings']['trusted_host_patterns'] = (object) [
43 'value' => ['^' . preg_quote(\Drupal::request()->getHost()) . '$'],
47 $this->writeSettings($settings);
49 $this->drupalGet('admin/reports/status');
50 $this->assertResponse(200, 'The status page is reachable.');
52 $this->assertRaw(t('Trusted Host Settings'));
53 $this->assertRaw(t('The trusted_host_patterns setting is set to allow'));
57 * Tests that fake requests have the proper host configured.
59 * @see \Drupal\Core\Http\TrustedHostsRequestFactory
61 public function testFakeRequests() {
62 $this->container->get('module_installer')->install(['trusted_hosts_test']);
63 $this->container->get('router.builder')->rebuild();
65 $host = $this->container->get('request_stack')->getCurrentRequest()->getHost();
66 $settings['settings']['trusted_host_patterns'] = (object) [
67 'value' => ['^' . preg_quote($host) . '$'],
71 $this->writeSettings($settings);
73 $this->drupalGet('trusted-hosts-test/fake-request');
74 $this->assertText('Host: ' . $host);
78 * Tests that shortcut module works together with host verification.
80 public function testShortcut() {
81 $this->container->get('module_installer')->install(['block', 'shortcut']);
82 $this->rebuildContainer();
83 $this->container->get('router.builder')->rebuild();
85 /** @var \Drupal\Core\Entity\EntityManagerInterface $entity_manager */
86 $entity_manager = $this->container->get('entity.manager');
87 $shortcut_storage = $entity_manager->getStorage('shortcut');
89 $shortcut = $shortcut_storage->create([
90 'title' => $this->randomString(),
91 'link' => 'internal:/admin/reports/status',
92 'shortcut_set' => 'default',
94 $shortcut_storage->save($shortcut);
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();
103 $this->drupalPlaceBlock('shortcuts');
105 $this->drupalGet('');
106 $this->assertLink($shortcut->label());