3 namespace Drupal\Tests\redirect_404\Kernel;
5 use Drupal\Core\Database\Database;
6 use Drupal\KernelTests\KernelTestBase;
9 * Tests the clean up cron job for redirect_404.
13 class Fix404RedirectCronJobTest extends KernelTestBase {
20 public static $modules = ['redirect_404'];
25 protected function setUp() {
27 $this->installSchema('redirect_404', 'redirect_404');
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'));
39 * Tests adding and deleting rows from redirect_404 table.
41 function testRedirect404CronJob() {
42 // Set the limit to 3 just for the test.
43 \Drupal::configFactory()
44 ->getEditable('redirect_404.settings')
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);
52 // Run cron to drop 3 rows from the redirect_404 test table.
55 $result = db_query("SELECT COUNT(*) as rows FROM {redirect_404}")->fetchField();
56 $this->assertEquals(3, $result);
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');
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');
79 * Tests adding rows and deleting one row from redirect_404 table.
81 function testRedirect404CronJobKeepAllButOne() {
82 // Set the limit to 5 just for the test.
83 \Drupal::configFactory()
84 ->getEditable('redirect_404.settings')
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);
92 // Run cron to drop just 1 row from the redirect_404 test table.
95 $result = db_query("SELECT COUNT(*) as rows FROM {redirect_404}")->fetchField();
96 $this->assertEquals(5, $result);
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');
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');
119 * Inserts a 404 request log in the redirect_404 test table.
121 * @param string $path
122 * The path of the request.
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.
130 protected function insert404Row($path, $count = 1, $timestamp = 0, $langcode = 'en') {
131 db_insert('redirect_404')
134 'langcode' => $langcode,
136 'timestamp' => $timestamp,
143 * Passes if the row with the given parameters is in the redirect_404 table.
145 * @param string $path
146 * The path of the request.
147 * @param string $langcode
148 * (optional) The langcode of the request.
150 protected function assert404Row($path, $langcode = 'en') {
151 $this->assert404RowHelper($path, $langcode, FALSE);
155 * Passes if the row with the given parameters is NOT in the redirect_404 table.
157 * @param string $path
158 * The path of the request.
159 * @param string $langcode
160 * (optional) The langcode of the request.
162 protected function assertNo404Row($path, $langcode = 'en') {
163 $this->assert404RowHelper($path, $langcode, TRUE);
167 * Passes if the row with the given parameters is in the redirect_404 table.
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.
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)
187 $this->assertNotEquals($path, $result);
190 $this->assertEquals($path, $result);