You are here

public function XmlSitemapUnitTest::testGetChunkCount in XML sitemap 8

Same name and namespace in other branches
  1. 2.x tests/src/Functional/XmlSitemapUnitTest.php \Drupal\Tests\xmlsitemap\Functional\XmlSitemapUnitTest::testGetChunkCount()

Tests for xmlsitemap_get_chunk_count().

File

tests/src/Functional/XmlSitemapUnitTest.php, line 76

Class

XmlSitemapUnitTest
Unit tests for the XML sitemap module.

Namespace

Drupal\Tests\xmlsitemap\Functional

Code

public function testGetChunkCount() {

  // Set a low chunk size for testing.
  $this->config
    ->set('chunk_size', 4)
    ->save();
  $database = \Drupal::database();

  // Make the total number of links just equal to the chunk size.
  $count = $database
    ->query("SELECT COUNT(id) FROM {xmlsitemap}")
    ->fetchField();
  for ($i = $count; $i < 4; $i++) {
    $this
      ->addSitemapLink();
    $this
      ->assertEquals(1, xmlsitemap_get_chunk_count(TRUE));
  }
  $this
    ->assertEquals(4, $database
    ->query("SELECT COUNT(id) FROM {xmlsitemap}")
    ->fetchField());

  // Add a disabled link, should not change the chunk count.
  $this
    ->addSitemapLink([
    'status' => FALSE,
  ]);
  $this
    ->assertEquals(1, xmlsitemap_get_chunk_count(TRUE));

  // Add a visible link, should finally bump up the chunk count.
  $this
    ->addSitemapLink();
  $this
    ->assertEquals(2, xmlsitemap_get_chunk_count(TRUE));

  // Change all links to disabled. The chunk count should be 1 not 0.
  $database
    ->query("UPDATE {xmlsitemap} SET status = 0");
  $this
    ->assertEquals(1, xmlsitemap_get_chunk_count(TRUE));
  $this
    ->assertEquals(0, xmlsitemap_get_link_count());

  // Delete all links. The chunk count should be 1 not 0.
  $database
    ->query("DELETE FROM {xmlsitemap}");
  $this
    ->assertEquals(0, $database
    ->query("SELECT COUNT(id) FROM {xmlsitemap}")
    ->fetchField());
  $this
    ->assertEquals(1, xmlsitemap_get_chunk_count(TRUE));
}