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',
32 'access site reports',
36 'administer url aliases',
40 * A user with administrative permissions.
42 * @var \Drupal\user\UserInterface
49 public function setUp() {
52 // Create an admin user.
53 $this->adminUser = $this->drupalCreateUser($this->adminPermissions);
54 $this->drupalLogin($this->adminUser);
56 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Page']);
60 * Passes if the language of the 404 path IS found on the loaded page.
62 * Because assertText() checks also in the Language select options, this
63 * specific assertion in the redirect 404 table body is needed.
65 * @param string $language
66 * The language to assert in the redirect 404 table body.
68 * (optional) The table body xpath where to assert the language. Defaults
70 * @param string $message
71 * (optional) A message to display with the assertion. Do not translate
72 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
73 * variables in the message text, not t(). If left blank, a default message
77 * TRUE on pass, FALSE on fail.
79 protected function assertLanguageInTableBody($language, $body = '//table/tbody', $message = '') {
80 return $this->assertLanguageInTableBodyHelper($language, $body, $message, FALSE);
84 * Passes if the language of the 404 path is NOT found on the loaded page.
86 * Because assertText() checks also in the Language select options, this
87 * specific assertion in the redirect 404 table body is needed.
89 * @param string $language
90 * The language to assert in the redirect 404 table body.
92 * (optional) The table body xpath where to assert the language. Defaults
94 * @param string $message
95 * (optional) A message to display with the assertion. Do not translate
96 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
97 * variables in the message text, not t(). If left blank, a default message
101 * TRUE on pass, FALSE on fail.
103 protected function assertNoLanguageInTableBody($language, $body = '//table/tbody', $message = '') {
104 return $this->assertLanguageInTableBodyHelper($language, $body, $message, TRUE);
108 * Helper for assertLanguageInTableBody and assertNoLanguageInTableBody.
110 * @param array $language
111 * The language to assert in the redirect 404 table body.
112 * @param string $body
113 * (optional) The table body xpath where to assert the language. Defaults
114 * to '//table/tbody'.
115 * @param string $message
116 * (optional) A message to display with the assertion. Do not translate
117 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
118 * variables in the message text, not t(). If left blank, a default message
120 * @param bool $not_exists
121 * (optional) TRUE if this language should not exist, FALSE if it should.
125 * TRUE on pass, FALSE on fail.
127 protected function assertLanguageInTableBodyHelper($language, $body = '//table/tbody', $message = '', $not_exists = TRUE) {
130 $message = new FormattableMarkup('Language "@language" found in 404 table.', ['@language' => $language]);
133 $message = new FormattableMarkup('Language "@language" not found in 404 table.', ['@language' => $language]);
138 return $this->assertFalse(strpos($this->xpath($body)[0]->asXML(), $language), $message);
141 return $this->assertTrue(strpos($this->xpath($body)[0]->asXML(), $language), $message);