Pull merge.
[yaffs-website] / web / core / modules / system / tests / src / Functional / System / PageNotFoundTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\System;
4
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
8 use Drupal\user\RoleInterface;
9
10 /**
11  * Tests page not found functionality, including custom 404 pages.
12  *
13  * @group system
14  */
15 class PageNotFoundTest extends BrowserTestBase {
16
17   use AssertPageCacheContextsAndTagsTrait;
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['system_test'];
25
26   protected $adminUser;
27
28   protected function setUp() {
29     parent::setUp();
30
31     // Create an administrative user.
32     $this->adminUser = $this->drupalCreateUser(['administer site configuration', 'link to any page']);
33     $this->adminUser->roles[] = 'administrator';
34     $this->adminUser->save();
35
36     user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access user profiles']);
37     user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['access user profiles']);
38   }
39
40   public function testPageNotFound() {
41     $this->drupalLogin($this->adminUser);
42     $this->drupalGet($this->randomMachineName(10));
43     $this->assertText(t('Page not found'), 'Found the default 404 page');
44
45     // Set a custom 404 page without a starting slash.
46     $edit = [
47       'site_404' => 'user/' . $this->adminUser->id(),
48     ];
49     $this->drupalPostForm('admin/config/system/site-information', $edit, t('Save configuration'));
50     $this->assertRaw(new FormattableMarkup("The path '%path' has to start with a slash.", ['%path' => $edit['site_404']]));
51
52     // Use a custom 404 page.
53     $edit = [
54       'site_404' => '/user/' . $this->adminUser->id(),
55     ];
56     $this->drupalPostForm('admin/config/system/site-information', $edit, t('Save configuration'));
57
58     $this->drupalGet($this->randomMachineName(10));
59     $this->assertText($this->adminUser->getUsername(), 'Found the custom 404 page');
60   }
61
62   /**
63    * Tests that an inaccessible custom 404 page falls back to the default.
64    */
65   public function testPageNotFoundCustomPageWithAccessDenied() {
66     // Sets up a 404 page not accessible by the anonymous user.
67     $this->config('system.site')->set('page.404', '/system-test/custom-4xx')->save();
68
69     $this->drupalGet('/this-path-does-not-exist');
70     $this->assertNoText('Admin-only 4xx response');
71     $this->assertText('The requested page could not be found.');
72     $this->assertResponse(404);
73     // Verify the access cacheability metadata for custom 404 is bubbled.
74     $this->assertCacheContext('user.roles');
75
76     $this->drupalLogin($this->adminUser);
77     $this->drupalGet('/this-path-does-not-exist');
78     $this->assertText('Admin-only 4xx response');
79     $this->assertNoText('The requested page could not be found.');
80     $this->assertResponse(404);
81     // Verify the access cacheability metadata for custom 404 is bubbled.
82     $this->assertCacheContext('user.roles');
83   }
84
85 }