You are here

function Fix404RedirectCronJobTest::testRedirect404CronJob in Redirect 8

Tests adding and deleting rows from redirect_404 table.

File

modules/redirect_404/tests/src/Kernel/Fix404RedirectCronJobTest.php, line 41

Class

Fix404RedirectCronJobTest
Tests the clean up cron job for redirect_404.

Namespace

Drupal\Tests\redirect_404\Kernel

Code

function testRedirect404CronJob() {

  // Set the limit to 3 just for the test.
  \Drupal::configFactory()
    ->getEditable('redirect_404.settings')
    ->set('row_limit', 3)
    ->save();

  // Check that there are 6 rows in the redirect_404 table.
  $result = Database::getConnection()
    ->query("SELECT COUNT(*) FROM {redirect_404}")
    ->fetchField();
  $this
    ->assertEquals(6, $result);

  // Run cron to drop 3 rows from the redirect_404 test table.
  redirect_404_cron();
  $result = Database::getConnection()
    ->query("SELECT COUNT(*) FROM {redirect_404}")
    ->fetchField();
  $this
    ->assertEquals(3, $result);

  // Check there are only 3 rows with more count in the redirect_404 table.
  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 {

    // In SQLite is the opposite: the 3 rows kept are the newest ones.
    $this
      ->assert404Row('/test1');
    $this
      ->assert404Row('/test2');
    $this
      ->assertNo404Row('/test3');
    $this
      ->assertNo404Row('/test4');
    $this
      ->assertNo404Row('/test5');
    $this
      ->assert404Row('/test6');
  }
}