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