4245cb567ea734fd5dea2a228b0c17e1783d8698
[yaffs-website] / web / modules / contrib / redirect / modules / redirect_404 / src / Tests / Redirect404TestBase.php
1 <?php
2
3 namespace Drupal\redirect_404\Tests;
4
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\simpletest\WebTestBase;
7
8 /**
9  * This class provides methods specifically for testing redirect 404 paths.
10  */
11 abstract class Redirect404TestBase extends WebTestBase {
12
13   /**
14    * Modules to enable.
15    *
16    * @var array
17    */
18   public static $modules = [
19     'redirect_404',
20     'node',
21     'path',
22   ];
23
24   /**
25    * Permissions for the admin user.
26    *
27    * @var array
28    */
29   protected $adminPermissions = [
30     'administer redirects',
31     'administer redirect settings',
32     'access site reports',
33     'access content',
34     'bypass node access',
35     'create url aliases',
36     'administer url aliases',
37   ];
38
39   /**
40    * A user with administrative permissions.
41    *
42    * @var \Drupal\user\UserInterface
43    */
44   protected $adminUser;
45
46   /**
47    * {@inheritdoc}
48    */
49   public function setUp() {
50     parent::setUp();
51
52     // Create an admin user.
53     $this->adminUser = $this->drupalCreateUser($this->adminPermissions);
54     $this->drupalLogin($this->adminUser);
55
56     $this->drupalCreateContentType(['type' => 'page', 'name' => 'Page']);
57   }
58
59   /**
60    * Passes if the language of the 404 path IS found on the loaded page.
61    *
62    * Because assertText() checks also in the Language select options, this
63    * specific assertion in the redirect 404 table body is needed.
64    *
65    * @param string $language
66    *   The language to assert in the redirect 404 table body.
67    * @param string $body
68    *   (optional) The table body xpath where to assert the language. Defaults
69    *   to '//table/tbody'.
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
74    *   will be displayed.
75    *
76    * @return bool
77    *   TRUE on pass, FALSE on fail.
78    */
79   protected function assertLanguageInTableBody($language, $body = '//table/tbody', $message = '') {
80     return $this->assertLanguageInTableBodyHelper($language, $body, $message, FALSE);
81   }
82
83   /**
84    * Passes if the language of the 404 path is NOT found on the loaded page.
85    *
86    * Because assertText() checks also in the Language select options, this
87    * specific assertion in the redirect 404 table body is needed.
88    *
89    * @param string $language
90    *   The language to assert in the redirect 404 table body.
91    * @param string $body
92    *   (optional) The table body xpath where to assert the language. Defaults
93    *   to '//table/tbody'.
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
98    *   will be displayed.
99    *
100    * @return bool
101    *   TRUE on pass, FALSE on fail.
102    */
103   protected function assertNoLanguageInTableBody($language, $body = '//table/tbody', $message = '') {
104     return $this->assertLanguageInTableBodyHelper($language, $body, $message, TRUE);
105   }
106
107   /**
108    * Helper for assertLanguageInTableBody and assertNoLanguageInTableBody.
109    *
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
119    *   will be displayed.
120    * @param bool $not_exists
121    *   (optional) TRUE if this language should not exist, FALSE if it should.
122    *   Defaults to TRUE.
123    *
124    * @return bool
125    *   TRUE on pass, FALSE on fail.
126    */
127   protected function assertLanguageInTableBodyHelper($language, $body = '//table/tbody', $message = '', $not_exists = TRUE) {
128     if (!$message) {
129       if (!$not_exists) {
130         $message = new FormattableMarkup('Language "@language" found in 404 table.', ['@language' => $language]);
131       }
132       else {
133         $message = new FormattableMarkup('Language "@language" not found in 404 table.', ['@language' => $language]);
134       }
135     }
136
137     if ($not_exists) {
138       return $this->assertFalse(strpos($this->xpath($body)[0]->asXML(), $language), $message);
139     }
140     else {
141       return $this->assertTrue(strpos($this->xpath($body)[0]->asXML(), $language), $message);
142     }
143   }
144
145 }