You are here

public function XmlSitemapNodeFunctionalTest::testCron in XML sitemap 2.x

Same name and namespace in other branches
  1. 8 tests/src/Functional/XmlSitemapNodeFunctionalTest.php \Drupal\Tests\xmlsitemap\Functional\XmlSitemapNodeFunctionalTest::testCron()

Test the import of old nodes via cron.

File

tests/src/Functional/XmlSitemapNodeFunctionalTest.php, line 279

Class

XmlSitemapNodeFunctionalTest
Tests the generation of node links.

Namespace

Drupal\Tests\xmlsitemap\Functional

Code

public function testCron() {
  $limit = 5;
  $this->config
    ->set('batch_limit', $limit)
    ->save();
  $nodes = [];
  for ($i = 1; $i <= $limit + 1; $i++) {
    $node = $this
      ->drupalCreateNode();
    array_push($nodes, $node);

    // Need to delay by one second so the nodes don't all have the same
    // timestamp.
    sleep(1);
  }

  // Clear all the node link data so we can emulate 'old' nodes.
  \Drupal::database()
    ->delete('xmlsitemap')
    ->condition('type', 'node')
    ->execute();

  // Run cron to import old nodes.
  xmlsitemap_cron();
  for ($i = 1; $i <= $limit + 1; $i++) {
    $node = array_pop($nodes);
    if ($i != 1) {

      // The first $limit nodes should be inserted.
      $this
        ->assertSitemapLinkValues('node', $node
        ->id(), [
        'access' => 1,
        'status' => 1,
      ]);
    }
    else {

      // Any beyond $limit should not be in the sitemap.
      $this
        ->assertNoSitemapLink([
        'type' => 'node',
        'id' => $node
          ->id(),
      ]);
    }
  }
}