Commit cd0cfafe authored by Andrey Tymchuk's avatar Andrey Tymchuk Committed by atymchuk
Browse files

Another functional test.

parent 5c893234
Loading
Loading
Loading
Loading
+60 −5
Original line number Diff line number Diff line
@@ -54,29 +54,29 @@ class SimpleSitemapViewsTest extends SimpleSitemapViewsTestBase {
  public function testAddArgumentsToIndex() {
    // Arguments with the wrong value should not be indexed.
    $this->sitemapViews->addArgumentsToIndex($this->testView, ['page2']);
    $this->assertEquals(0, $this->sitemapViews->getArgumentsFromIndexCount());
    $this->assertIndexSize(0);

    // Non-indexable arguments should not be indexed.
    $args = ['page', $this->node->getTitle(), $this->node->id()];
    $this->sitemapViews->addArgumentsToIndex($this->testView, $args);
    $this->assertEquals(0, $this->sitemapViews->getArgumentsFromIndexCount());
    $this->assertIndexSize(0);

    // The argument set should not be indexed more than once.
    for ($i = 0; $i < 2; $i++) {
      $this->sitemapViews->addArgumentsToIndex($this->testView, ['page']);
      $this->assertEquals(1, $this->sitemapViews->getArgumentsFromIndexCount());
      $this->assertIndexSize(1);
    }

    // A new set of arguments must be indexed.
    $args = ['page', $this->node->getTitle()];
    $this->sitemapViews->addArgumentsToIndex($this->testView, $args);
    $this->assertEquals(2, $this->sitemapViews->getArgumentsFromIndexCount());
    $this->assertIndexSize(2);

    // The number of argument sets in the index for one view display should not
    // exceed the maximum number of link variations.
    $args = ['page', $this->node2->getTitle()];
    $this->sitemapViews->addArgumentsToIndex($this->testView, $args);
    $this->assertEquals(2, $this->sitemapViews->getArgumentsFromIndexCount());
    $this->assertIndexSize(2);
  }

  /**
@@ -99,4 +99,59 @@ class SimpleSitemapViewsTest extends SimpleSitemapViewsTestBase {
    $this->assertSession()->responseContains("$test_view_url/page/$title");
  }

  /**
   * Tests the garbage collection process.
   */
  public function testGarbageCollector() {
    // Disable cron generation, since data can be removed
    // from the index during generation.
    $this->generator->saveSetting('cron_generate', FALSE);

    // Record with the wrong set of indexed arguments must be removed.
    $this->addRecordToIndex(
      $this->testView->id(),
      $this->testView->current_display,
      ['type', 'title', 'nid'],
      ['page', $this->node->getTitle(), $this->node->id()]
    );
    $this->cron->run();
    $this->assertIndexSize(0);

    // Record of a non-existent view must be removed.
    $this->addRecordToIndex(
      'simple_sitemap_fake_view',
      $this->testView->current_display,
      ['type', 'title'],
      ['page', $this->node->getTitle()]
    );
    $this->cron->run();
    $this->assertIndexSize(0);

    // Record of a non-existent display must be removed.
    $this->addRecordToIndex(
      $this->testView->id(),
      'simple_sitemap_fake_display',
      ['type', 'title'],
      ['page', $this->node->getTitle()]
    );
    $this->cron->run();
    $this->assertIndexSize(0);

    // The number of records should not exceed the specified limit.
    for ($i = 0; $i < 3; $i++) {
      $this->addRecordToIndex(
        $this->testView->id(),
        $this->testView->current_display,
        ['type', 'title'],
        ['page2', "Node$i"]
      );
    }
    $this->cron->run();
    $this->assertIndexSize(2);

    // Records about pages with empty result must be removed during generation.
    $this->generator->generateSitemap('backend');
    $this->assertIndexSize(0);
  }

}
+47 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\Tests\simple_sitemap_views\Functional;

use Drupal\Tests\simple_sitemap\Functional\SimplesitemapTestBase;
use Drupal\simple_sitemap_views\SimpleSitemapViews;
use Drupal\views\Views;

/**
@@ -25,6 +26,13 @@ abstract class SimpleSitemapViewsTestBase extends SimplesitemapTestBase {
   */
  protected $sitemapViews;

  /**
   * The cron service.
   *
   * @var \Drupal\Core\CronInterface
   */
  protected $cron;

  /**
   * Test view.
   *
@@ -39,8 +47,47 @@ abstract class SimpleSitemapViewsTestBase extends SimplesitemapTestBase {
    parent::setUp();

    $this->sitemapViews = $this->container->get('simple_sitemap.views');
    $this->cron = $this->container->get('cron');

    $this->testView = Views::getView('simple_sitemap_views_test_view');
    $this->testView->setDisplay('page_1');
  }

  /**
   * Asserts the size of the arguments index.
   *
   * @param int $size
   *   The expected size.
   */
  protected function assertIndexSize($size) {
    $this->assertEquals($size, $this->sitemapViews->getArgumentsFromIndexCount());
  }

  /**
   * Adds a record to the arguments index.
   *
   * @param string $view_id
   *   The view ID.
   * @param string $display_id
   *   The view display ID.
   * @param array $args_ids
   *   A set of argument IDs.
   * @param array $args_values
   *   A set of argument values.
   */
  protected function addRecordToIndex($view_id, $display_id, array $args_ids, array $args_values) {
    $args_ids = implode(SimpleSitemapViews::ARGUMENT_SEPARATOR, $args_ids);
    $args_values = implode(SimpleSitemapViews::ARGUMENT_SEPARATOR, $args_values);

    // Insert a record into the index table.
    $query = $this->database->insert('simple_sitemap_views');
    $query->fields([
      'view_id' => $view_id,
      'display_id' => $display_id,
      'arguments_ids' => $args_ids,
      'arguments_values' => $args_values,
    ]);
    $query->execute();
  }

}