Chris@0: adminUser = $this->drupalCreateUser(['administer site configuration', 'link to any page']); Chris@0: $this->adminUser->roles[] = 'administrator'; Chris@0: $this->adminUser->save(); Chris@0: Chris@0: user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access user profiles']); Chris@0: user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['access user profiles']); Chris@0: } Chris@0: Chris@0: public function testPageNotFound() { Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $this->drupalGet($this->randomMachineName(10)); Chris@0: $this->assertText(t('Page not found'), 'Found the default 404 page'); Chris@0: Chris@0: // Set a custom 404 page without a starting slash. Chris@0: $edit = [ Chris@0: 'site_404' => 'user/' . $this->adminUser->id(), Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/system/site-information', $edit, t('Save configuration')); Chris@0: $this->assertRaw(SafeMarkup::format("The path '%path' has to start with a slash.", ['%path' => $edit['site_404']])); Chris@0: Chris@0: // Use a custom 404 page. Chris@0: $edit = [ Chris@0: 'site_404' => '/user/' . $this->adminUser->id(), Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/system/site-information', $edit, t('Save configuration')); Chris@0: Chris@0: $this->drupalGet($this->randomMachineName(10)); Chris@0: $this->assertText($this->adminUser->getUsername(), 'Found the custom 404 page'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that an inaccessible custom 404 page falls back to the default. Chris@0: */ Chris@0: public function testPageNotFoundCustomPageWithAccessDenied() { Chris@0: // Sets up a 404 page not accessible by the anonymous user. Chris@0: $this->config('system.site')->set('page.404', '/system-test/custom-4xx')->save(); Chris@0: Chris@0: $this->drupalGet('/this-path-does-not-exist'); Chris@0: $this->assertNoText('Admin-only 4xx response'); Chris@0: $this->assertText('The requested page could not be found.'); Chris@0: $this->assertResponse(404); Chris@0: // Verify the access cacheability metadata for custom 404 is bubbled. Chris@0: $this->assertCacheContext('user.roles'); Chris@0: Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $this->drupalGet('/this-path-does-not-exist'); Chris@0: $this->assertText('Admin-only 4xx response'); Chris@0: $this->assertNoText('The requested page could not be found.'); Chris@0: $this->assertResponse(404); Chris@0: // Verify the access cacheability metadata for custom 404 is bubbled. Chris@0: $this->assertCacheContext('user.roles'); Chris@0: } Chris@0: Chris@0: }