Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / redirect / modules / redirect_404 / tests / src / Kernel / Fix404RedirectCronJobTest.php
1 <?php
2
3 namespace Drupal\Tests\redirect_404\Kernel;
4
5 use Drupal\KernelTests\KernelTestBase;
6
7 /**
8  * Tests the clean up cron job for redirect_404.
9  *
10  * @group redirect_404
11  */
12 class Fix404RedirectCronJobTest extends KernelTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['redirect_404'];
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function setUp() {
25     parent::setUp();
26     $this->installSchema('redirect_404', 'redirect_404');
27
28     // Insert some records in the test table with a given count and timestamp.
29     $this->insert404Row('/test1', 12, strtotime('now'));
30     $this->insert404Row('/test2', 5, strtotime('-1 hour'));
31     $this->insert404Row('/test3', 315, strtotime('-1 week'));
32     $this->insert404Row('/test4', 300, strtotime('-1 month'));
33     $this->insert404Row('/test5', 1557, strtotime('-1 week'));
34     $this->insert404Row('/test6', 1, strtotime('-1 day'));
35   }
36
37   /**
38    * Tests adding and deleting rows from redirect_404 table.
39    */
40   function testRedirect404CronJob() {
41     // Set the limit to 3 just for the test.
42     \Drupal::configFactory()
43       ->getEditable('redirect_404.settings')
44       ->set('row_limit', 3)
45       ->save();
46
47     // Check that there are 6 rows in the redirect_404 table.
48     $result = db_query("SELECT COUNT(*) FROM {redirect_404}")->fetchField();
49     $this->assertEquals(6, $result);
50
51     // Run cron to drop 3 rows from the redirect_404 test table.
52     redirect_404_cron();
53
54     $result = db_query("SELECT COUNT(*) FROM {redirect_404}")->fetchField();
55     $this->assertEquals(3, $result);
56
57     // Check there are only 3 rows with more count in the redirect_404 table.
58     if (\Drupal::database()->driver() == 'mysql' || \Drupal::database()->driver() == 'pgsql') {
59       $this->assertNo404Row('/test1');
60       $this->assertNo404Row('/test2');
61       $this->assert404Row('/test3');
62       $this->assert404Row('/test4');
63       $this->assert404Row('/test5');
64       $this->assertNo404Row('/test6');
65     }
66     else {
67       // In SQLite is the opposite: the 3 rows kept are the newest ones.
68       $this->assert404Row('/test1');
69       $this->assert404Row('/test2');
70       $this->assertNo404Row('/test3');
71       $this->assertNo404Row('/test4');
72       $this->assertNo404Row('/test5');
73       $this->assert404Row('/test6');
74     }
75   }
76
77   /**
78    * Tests adding rows and deleting one row from redirect_404 table.
79    */
80   function testRedirect404CronJobKeepAllButOne() {
81     // Set the limit to 5 just for the test.
82     \Drupal::configFactory()
83       ->getEditable('redirect_404.settings')
84       ->set('row_limit', 5)
85       ->save();
86
87     // Check that there are 6 rows in the redirect_404 table.
88     $result = db_query("SELECT COUNT(*) FROM {redirect_404}")->fetchField();
89     $this->assertEquals(6, $result);
90
91     // Run cron to drop just 1 row from the redirect_404 test table.
92     redirect_404_cron();
93
94     $result = db_query("SELECT COUNT(*) FROM {redirect_404}")->fetchField();
95     $this->assertEquals(5, $result);
96
97     // Check only the row with least count has been removed from the table.
98     if (\Drupal::database()->driver() == 'mysql' || \Drupal::database()->driver() == 'pgsql') {
99       $this->assert404Row('/test1');
100       $this->assert404Row('/test2');
101       $this->assert404Row('/test3');
102       $this->assert404Row('/test4');
103       $this->assert404Row('/test5');
104       $this->assertNo404Row('/test6');
105     }
106     else {
107       // In SQlite, only the oldest row is deleted.
108       $this->assert404Row('/test1');
109       $this->assert404Row('/test2');
110       $this->assert404Row('/test3');
111       $this->assertNo404Row('/test4');
112       $this->assert404Row('/test5');
113       $this->assert404Row('/test6');
114     }
115   }
116
117   /**
118    * Inserts a 404 request log in the redirect_404 test table.
119    *
120    * @param string $path
121    *   The path of the request.
122    * @param int $count
123    *   (optional) The visits count of the request.
124    * @param int $timestamp
125    *   (optional) The timestamp of the last visited request.
126    * @param string $langcode
127    *   (optional) The langcode of the request.
128    */
129   protected function insert404Row($path, $count = 1, $timestamp = 0, $langcode = 'en') {
130     db_insert('redirect_404')
131     ->fields([
132       'path' => $path,
133       'langcode' => $langcode,
134       'count' => $count,
135       'timestamp' => $timestamp,
136       'resolved' => 0,
137     ])
138     ->execute();
139   }
140
141   /**
142    * Passes if the row with the given parameters is in the redirect_404 table.
143    *
144    * @param string $path
145    *   The path of the request.
146    * @param string $langcode
147    *   (optional) The langcode of the request.
148    */
149   protected function assert404Row($path, $langcode = 'en') {
150     $this->assert404RowHelper($path, $langcode, FALSE);
151   }
152
153   /**
154    * Passes if the row with the given parameters is NOT in the redirect_404 table.
155    *
156    * @param string $path
157    *   The path of the request.
158    * @param string $langcode
159    *   (optional) The langcode of the request.
160    */
161   protected function assertNo404Row($path, $langcode = 'en') {
162     $this->assert404RowHelper($path, $langcode, TRUE);
163   }
164
165   /**
166    * Passes if the row with the given parameters is in the redirect_404 table.
167    *
168    * @param string $path
169    *   The path of the request.
170    * @param string $langcode
171    *   (optional) The langcode of the request.
172    * @param bool $not_exists
173    *   (optional) TRUE if this 404 row should not exist in the redirect_404
174    *   table, FALSE if it should. Defaults to TRUE.
175    */
176   protected function assert404RowHelper($path, $langcode = 'en', $not_exists = TRUE) {
177     $result = db_select('redirect_404', 'r404')
178       ->fields('r404', ['path'])
179       ->condition('path', $path)
180       ->condition('langcode', $langcode)
181       ->condition('resolved', 0)
182       ->execute()
183       ->fetchField();
184
185     if ($not_exists) {
186       $this->assertNotEquals($path, $result);
187     }
188     else {
189       $this->assertEquals($path, $result);
190     }
191   }
192 }