You are here

public function TaxonomyMenuConfigurationTest::testTaxonomyMenuHideEmptyTerms in Taxonomy menu 8

Tests Taxonommy Menu "Hide Empty terms" option.

File

lib/Drupal/taxonomy_menu/Tests/TaxonomyMenuConfigurationTest.php, line 221
Contains \Drupal\taxonomy_menu\Tests\TaxonomyMenuConfigurationTest.

Class

TaxonomyMenuConfigurationTest
Tests Taxonomy menu configuration options.

Namespace

Drupal\taxonomy_menu\Tests

Code

public function testTaxonomyMenuHideEmptyTerms() {
  $vid = $this->vocabulary
    ->id();

  // Create several nodes and attach them to different terms of our hierarchy
  // in order to match the following scheme.

  /** terms[1]         | depth: 0 | 0 node  -> hidden
   * -- terms[2]       | depth: 1 | 0 node  -> hidden
   * -- terms[3]       | depth: 1 | 2 nodes -> displayed
   * ---- terms[4]     | depth: 2 | 0 node  -> hidden
   * -- terms[5]       | depth: 1 | 1 node  -> displayed
   * terms[6]          | depth: 0 | 0 node  -> hidden
   * -- terms[7]       | depth: 1 | 0 node  -> hidden   */
  $this
    ->setUpTermReferenceAndNodes('article', array(
    3,
    3,
    5,
  ));

  // Set settings (don't hide empty terms) and save.
  $edit = array(
    'taxonomy_menu[vocab_parent]' => 'main:0',
    'taxonomy_menu[options_structure][hide_empty_terms]' => FALSE,
  );
  $this
    ->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));

  // Assert that all links are displayed.
  foreach ($this->terms_hierarchy as $term) {
    $mlid = _taxonomy_menu_get_mlid($term
      ->id(), $vid);
    if ($mlid) {
      $this
        ->assertMenuLink($mlid, array(
        'hidden' => FALSE,
      ));
    }
    else {
      $this
        ->fail('No mlid could be found for the term ' . $term
        ->id());
    }
  }

  // Set settings (hide empty terms) and save.
  $edit['taxonomy_menu[options_structure][hide_empty_terms]'] = TRUE;
  $this
    ->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));

  // Assert that the hidden property of the taxonomy menu's menu links are
  // set according to the scheme.
  $visible_terms_index = array(
    3,
    5,
  );
  $index = 1;
  foreach ($this->terms_hierarchy as $term) {
    $mlid = _taxonomy_menu_get_mlid($term
      ->id(), $vid);
    if ($mlid) {
      if (in_array($index, $visible_terms_index)) {
        $this
          ->assertMenuLink($mlid, array(
          'hidden' => FALSE,
        ));
      }
      else {
        $this
          ->assertMenuLink($mlid, array(
          'hidden' => TRUE,
        ));
      }
    }
    else {
      $this
        ->fail('No mlid could be found for the term ' . $term->tid);
    }
    $index++;
  }
}