3 namespace Drupal\Tests\redirect_404\Kernel;
5 use Drupal\KernelTests\KernelTestBase;
8 * Tests the clean up cron job for redirect_404.
12 class Fix404RedirectCronJobTest extends KernelTestBase {
19 public static $modules = ['redirect_404'];
24 protected function setUp() {
26 $this->installSchema('redirect_404', 'redirect_404');
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'));
38 * Tests adding and deleting rows from redirect_404 table.
40 function testRedirect404CronJob() {
41 // Set the limit to 3 just for the test.
42 \Drupal::configFactory()
43 ->getEditable('redirect_404.settings')
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);
51 // Run cron to drop 3 rows from the redirect_404 test table.
54 $result = db_query("SELECT COUNT(*) FROM {redirect_404}")->fetchField();
55 $this->assertEquals(3, $result);
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');
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');
78 * Tests adding rows and deleting one row from redirect_404 table.
80 function testRedirect404CronJobKeepAllButOne() {
81 // Set the limit to 5 just for the test.
82 \Drupal::configFactory()
83 ->getEditable('redirect_404.settings')
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);
91 // Run cron to drop just 1 row from the redirect_404 test table.
94 $result = db_query("SELECT COUNT(*) FROM {redirect_404}")->fetchField();
95 $this->assertEquals(5, $result);
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');
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');
118 * Inserts a 404 request log in the redirect_404 test table.
120 * @param string $path
121 * The path of the request.
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.
129 protected function insert404Row($path, $count = 1, $timestamp = 0, $langcode = 'en') {
130 db_insert('redirect_404')
133 'langcode' => $langcode,
135 'timestamp' => $timestamp,
142 * Passes if the row with the given parameters is in the redirect_404 table.
144 * @param string $path
145 * The path of the request.
146 * @param string $langcode
147 * (optional) The langcode of the request.
149 protected function assert404Row($path, $langcode = 'en') {
150 $this->assert404RowHelper($path, $langcode, FALSE);
154 * Passes if the row with the given parameters is NOT in the redirect_404 table.
156 * @param string $path
157 * The path of the request.
158 * @param string $langcode
159 * (optional) The langcode of the request.
161 protected function assertNo404Row($path, $langcode = 'en') {
162 $this->assert404RowHelper($path, $langcode, TRUE);
166 * Passes if the row with the given parameters is in the redirect_404 table.
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.
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)
186 $this->assertNotEquals($path, $result);
189 $this->assertEquals($path, $result);