2842aeb14c1572f0c5a5b1df418852d92024adcb
[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 content',
33     'bypass node access',
34     'create url aliases',
35     'administer url aliases',
36   ];
37
38   /**
39    * A user with administrative permissions.
40    *
41    * @var \Drupal\user\UserInterface
42    */
43   protected $adminUser;
44
45   /**
46    * {@inheritdoc}
47    */
48   public function setUp() {
49     parent::setUp();
50
51     // Create an admin user.
52     $this->adminUser = $this->drupalCreateUser($this->adminPermissions);
53     $this->drupalLogin($this->adminUser);
54
55     $this->drupalCreateContentType(['type' => 'page', 'name' => 'Page']);
56   }
57
58   /**
59    * Passes if the language of the 404 path IS found on the loaded page.
60    *
61    * Because assertText() checks also in the Language select options, this
62    * specific assertion in the redirect 404 table body is needed.
63    *
64    * @param string $language
65    *   The language to assert in the redirect 404 table body.
66    * @param string $body
67    *   (optional) The table body xpath where to assert the language. Defaults
68    *   to '//table/tbody'.
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
73    *   will be displayed.
74    *
75    * @return bool
76    *   TRUE on pass, FALSE on fail.
77    */
78   protected function assertLanguageInTableBody($language, $body = '//table/tbody', $message = '') {
79     return $this->assertLanguageInTableBodyHelper($language, $body, $message, FALSE);
80   }
81
82   /**
83    * Passes if the language of the 404 path is NOT found on the loaded page.
84    *
85    * Because assertText() checks also in the Language select options, this
86    * specific assertion in the redirect 404 table body is needed.
87    *
88    * @param string $language
89    *   The language to assert in the redirect 404 table body.
90    * @param string $body
91    *   (optional) The table body xpath where to assert the language. Defaults
92    *   to '//table/tbody'.
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
97    *   will be displayed.
98    *
99    * @return bool
100    *   TRUE on pass, FALSE on fail.
101    */
102   protected function assertNoLanguageInTableBody($language, $body = '//table/tbody', $message = '') {
103     return $this->assertLanguageInTableBodyHelper($language, $body, $message, TRUE);
104   }
105
106   /**
107    * Helper for assertLanguageInTableBody and assertNoLanguageInTableBody.
108    *
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
118    *   will be displayed.
119    * @param bool $not_exists
120    *   (optional) TRUE if this language should not exist, FALSE if it should.
121    *   Defaults to TRUE.
122    *
123    * @return bool
124    *   TRUE on pass, FALSE on fail.
125    */
126   protected function assertLanguageInTableBodyHelper($language, $body = '//table/tbody', $message = '', $not_exists = TRUE) {
127     if (!$message) {
128       if (!$not_exists) {
129         $message = new FormattableMarkup('Language "@language" found in 404 table.', ['@language' => $language]);
130       }
131       else {
132         $message = new FormattableMarkup('Language "@language" not found in 404 table.', ['@language' => $language]);
133       }
134     }
135
136     if ($not_exists) {
137       return $this->assertFalse(strpos($this->xpath($body)[0]->asXML(), $language), $message);
138     }
139     else {
140       return $this->assertTrue(strpos($this->xpath($body)[0]->asXML(), $language), $message);
141     }
142   }
143
144 }