You are here

public function SitemapTaxonomyTermsTest::testVocabularyDepth in Sitemap 2.0.x

Same name and namespace in other branches
  1. 8.2 src/Tests/SitemapTaxonomyTermsTest.php \Drupal\sitemap\Tests\SitemapTaxonomyTermsTest::testVocabularyDepth()
  2. 8 src/Tests/SitemapTaxonomyTermsTest.php \Drupal\sitemap\Tests\SitemapTaxonomyTermsTest::testVocabularyDepth()

Tests vocabulary depth settings.

File

src/Tests/SitemapTaxonomyTermsTest.php, line 103

Class

SitemapTaxonomyTermsTest
Tests the display of taxonomies based on sitemap settings.

Namespace

Drupal\sitemap\Tests

Code

public function testVocabularyDepth() {

  // Create terms.
  $this->terms = $this
    ->createNestedTerms($this->vocabulary);

  // Set to show all taxonomy terms, even if they are not assigned to content.
  $vid = $this->vocabulary
    ->id();
  $this
    ->saveSitemapForm([
    "plugins[vocabulary:{$vid}][settings][term_count_threshold]" => Vocabulary::THRESHOLD_DISABLED,
  ]);

  // Change vocabulary depth to its maximum (9).
  $this
    ->saveSitemapForm([
    "plugins[vocabulary:{$vid}][settings][term_depth]" => Vocabulary::DEPTH_MAX,
  ]);

  // Assert that all tags are listed in the sitemap.
  $this
    ->drupalGet('/sitemap');
  foreach ($this->terms as $term) {
    $this
      ->assertText($term
      ->label());
  }

  // Change vocabulary depth to 0.
  $this
    ->saveSitemapForm([
    "plugins[vocabulary:{$vid}][settings][term_depth]" => Vocabulary::DEPTH_DISABLED,
  ]);

  // Assert that no tags are listed in the sitemap.
  $this
    ->drupalGet('/sitemap');
  foreach ($this->terms as $term) {
    $this
      ->assertNoText($term
      ->label());
  }

  // Change vocabulary depth to 1.
  $this
    ->saveSitemapForm([
    "plugins[vocabulary:{$vid}][settings][term_depth]" => 1,
  ]);

  // Assert that only tag 1 is listed in the sitemap.
  $this
    ->drupalGet('/sitemap');
  $this
    ->assertText($this->terms[0]
    ->label());
  $this
    ->assertNoText($this->terms[1]
    ->label());
  $this
    ->assertNoText($this->terms[2]
    ->label());

  // Change vocabulary depth to 2.
  $this
    ->saveSitemapForm([
    "plugins[vocabulary:{$vid}][settings][term_depth]" => 2,
  ]);

  // Assert that tag 1 and tag 2 are listed in the sitemap.
  $this
    ->drupalGet('/sitemap');
  $this
    ->assertText($this->terms[0]
    ->label());
  $this
    ->assertText($this->terms[1]
    ->label());
  $this
    ->assertNoText($this->terms[2]
    ->label());

  // Change vocabulary depth to 3.
  $this
    ->saveSitemapForm([
    "plugins[vocabulary:{$vid}][settings][term_depth]" => 3,
  ]);

  // Assert that all tags are listed in the sitemap.
  $this
    ->drupalGet('/sitemap');
  foreach ($this->terms as $term) {
    $this
      ->assertText($term
      ->label());
  }

  // Test display when parent term does not meet threshold.
  $this
    ->saveSitemapForm([
    "plugins[vocabulary:{$vid}][settings][term_count_threshold]" => 1,
  ]);
  $childTerms = $this->terms;
  array_shift($childTerms);
  $this
    ->createNodeWithTerms($childTerms);
  $this
    ->drupalGet('/sitemap');
  foreach ($this->terms as $term) {
    $this
      ->assertText($term
      ->label());
  }

  // TODO: Check for empty <li>s as well.
  // Test show_count when parent term does not meet threshold.
  $this
    ->saveSitemapForm([
    "plugins[vocabulary:{$vid}][settings][show_count]" => TRUE,
  ]);
  $this
    ->drupalGet('/sitemap');
  $elements = $this
    ->cssSelect(".sitemap-plugin--vocabulary .count:contains('(1)')");
  $this
    ->assertEqual(count($elements), 2, 'Node counts included.');
}