View source
<?php
namespace Drupal\Tests\redirect_404\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Core\Database\Database;
class Fix404RedirectCronJobTest extends KernelTestBase {
public static $modules = [
'redirect_404',
];
protected function setUp() {
parent::setUp();
$this
->installSchema('redirect_404', 'redirect_404');
$this
->insert404Row('/test1', 12, 5, strtotime('now'));
$this
->insert404Row('/test2', 5, 3, strtotime('-1 hour'));
$this
->insert404Row('/test3', 315, 0, strtotime('-1 week'));
$this
->insert404Row('/test4', 300, 0, strtotime('-1 month'));
$this
->insert404Row('/test5', 1557, 0, strtotime('-1 week'));
$this
->insert404Row('/test6', 1, 0, strtotime('-1 day'));
}
function testRedirect404CronJob() {
\Drupal::configFactory()
->getEditable('redirect_404.settings')
->set('row_limit', 3)
->save();
$result = Database::getConnection()
->query("SELECT COUNT(*) FROM {redirect_404}")
->fetchField();
$this
->assertEquals(6, $result);
redirect_404_cron();
$result = Database::getConnection()
->query("SELECT COUNT(*) FROM {redirect_404}")
->fetchField();
$this
->assertEquals(3, $result);
if (\Drupal::database()
->driver() == 'mysql' || \Drupal::database()
->driver() == 'pgsql') {
$this
->assertNo404Row('/test1');
$this
->assertNo404Row('/test2');
$this
->assert404Row('/test3');
$this
->assert404Row('/test4');
$this
->assert404Row('/test5');
$this
->assertNo404Row('/test6');
}
else {
$this
->assert404Row('/test1');
$this
->assert404Row('/test2');
$this
->assertNo404Row('/test3');
$this
->assertNo404Row('/test4');
$this
->assertNo404Row('/test5');
$this
->assert404Row('/test6');
}
}
function testRedirect404CronJobKeepAllButOne() {
\Drupal::configFactory()
->getEditable('redirect_404.settings')
->set('row_limit', 5)
->save();
$result = Database::getConnection()
->query("SELECT COUNT(*) FROM {redirect_404}")
->fetchField();
$this
->assertEquals(6, $result);
redirect_404_cron();
$result = Database::getConnection()
->query("SELECT COUNT(*) FROM {redirect_404}")
->fetchField();
$this
->assertEquals(5, $result);
if (\Drupal::database()
->driver() == 'mysql' || \Drupal::database()
->driver() == 'pgsql') {
$this
->assert404Row('/test1');
$this
->assert404Row('/test2');
$this
->assert404Row('/test3');
$this
->assert404Row('/test4');
$this
->assert404Row('/test5');
$this
->assertNo404Row('/test6');
}
else {
$this
->assert404Row('/test1');
$this
->assert404Row('/test2');
$this
->assert404Row('/test3');
$this
->assertNo404Row('/test4');
$this
->assert404Row('/test5');
$this
->assert404Row('/test6');
}
}
function testRedirect404CronJobDailyCountReset() {
$result = \Drupal::database()
->query("SELECT COUNT(*) FROM {redirect_404} WHERE daily_count > 0")
->fetchField();
$this
->assertEquals(2, $result);
redirect_404_cron();
$result = \Drupal::database()
->query("SELECT COUNT(*) FROM {redirect_404} WHERE daily_count > 0")
->fetchField();
$this
->assertEquals(0, $result);
$this
->insert404Row('/test7', 2, 2, time());
redirect_404_cron();
$this
->assert404Row('/test7');
$result = \Drupal::database()
->query("SELECT COUNT(*) FROM {redirect_404} WHERE daily_count > 0")
->fetchField();
$this
->assertEquals(1, $result);
}
protected function insert404Row($path, $count = 1, $daily_count = 0, $timestamp = 0, $langcode = 'en') {
\Drupal::database()
->insert('redirect_404')
->fields([
'path' => $path,
'langcode' => $langcode,
'count' => $count,
'daily_count' => $daily_count,
'timestamp' => $timestamp,
'resolved' => 0,
])
->execute();
}
protected function assert404Row($path, $langcode = 'en') {
$this
->assert404RowHelper($path, $langcode, FALSE);
}
protected function assertNo404Row($path, $langcode = 'en') {
$this
->assert404RowHelper($path, $langcode, TRUE);
}
protected function assert404RowHelper($path, $langcode = 'en', $not_exists = TRUE) {
$result = Database::getConnection()
->select('redirect_404', 'r404')
->fields('r404', [
'path',
])
->condition('path', $path)
->condition('langcode', $langcode)
->condition('resolved', 0)
->execute()
->fetchField();
if ($not_exists) {
$this
->assertNotEquals($path, $result);
}
else {
$this
->assertEquals($path, $result);
}
}
}