3 namespace Drupal\redirect_404\Tests;
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\simpletest\WebTestBase;
9 * This class provides methods specifically for testing redirect 404 paths.
11 abstract class Redirect404TestBase extends WebTestBase {
18 public static $modules = [
25 * Permissions for the admin user.
29 protected $adminPermissions = [
30 'administer redirects',
31 'administer redirect settings',
35 'administer url aliases',
39 * A user with administrative permissions.
41 * @var \Drupal\user\UserInterface
48 public function setUp() {
51 // Create an admin user.
52 $this->adminUser = $this->drupalCreateUser($this->adminPermissions);
53 $this->drupalLogin($this->adminUser);
55 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Page']);
59 * Passes if the language of the 404 path IS found on the loaded page.
61 * Because assertText() checks also in the Language select options, this
62 * specific assertion in the redirect 404 table body is needed.
64 * @param string $language
65 * The language to assert in the redirect 404 table body.
67 * (optional) The table body xpath where to assert the language. Defaults
69 * @param string $message
70 * (optional) A message to display with the assertion. Do not translate
71 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
72 * variables in the message text, not t(). If left blank, a default message
76 * TRUE on pass, FALSE on fail.
78 protected function assertLanguageInTableBody($language, $body = '//table/tbody', $message = '') {
79 return $this->assertLanguageInTableBodyHelper($language, $body, $message, FALSE);
83 * Passes if the language of the 404 path is NOT found on the loaded page.
85 * Because assertText() checks also in the Language select options, this
86 * specific assertion in the redirect 404 table body is needed.
88 * @param string $language
89 * The language to assert in the redirect 404 table body.
91 * (optional) The table body xpath where to assert the language. Defaults
93 * @param string $message
94 * (optional) A message to display with the assertion. Do not translate
95 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
96 * variables in the message text, not t(). If left blank, a default message
100 * TRUE on pass, FALSE on fail.
102 protected function assertNoLanguageInTableBody($language, $body = '//table/tbody', $message = '') {
103 return $this->assertLanguageInTableBodyHelper($language, $body, $message, TRUE);
107 * Helper for assertLanguageInTableBody and assertNoLanguageInTableBody.
109 * @param array $language
110 * The language to assert in the redirect 404 table body.
111 * @param string $body
112 * (optional) The table body xpath where to assert the language. Defaults
113 * to '//table/tbody'.
114 * @param string $message
115 * (optional) A message to display with the assertion. Do not translate
116 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
117 * variables in the message text, not t(). If left blank, a default message
119 * @param bool $not_exists
120 * (optional) TRUE if this language should not exist, FALSE if it should.
124 * TRUE on pass, FALSE on fail.
126 protected function assertLanguageInTableBodyHelper($language, $body = '//table/tbody', $message = '', $not_exists = TRUE) {
129 $message = new FormattableMarkup('Language "@language" found in 404 table.', ['@language' => $language]);
132 $message = new FormattableMarkup('Language "@language" not found in 404 table.', ['@language' => $language]);
137 return $this->assertFalse(strpos($this->xpath($body)[0]->asXML(), $language), $message);
140 return $this->assertTrue(strpos($this->xpath($body)[0]->asXML(), $language), $message);