You are here

public function TaxonomyMenuConfigurationTest::testTaxonomyMenuCountNodes in Taxonomy menu 8

Tests Taxonommy Menu "Node count" option.

@TODO Add a test for recursive count.

File

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

Class

TaxonomyMenuConfigurationTest
Tests Taxonomy menu configuration options.

Namespace

Drupal\taxonomy_menu\Tests

Code

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

  /*
    Create several nodes and attach them to different terms of our hierarchy
    in order to match the following scheme. We don't use "hide empty terms".
    option.

    terms[1]          | depth: 0 | 0 node attached
    -- terms[2]       | depth: 1 | 0 node attached
    -- terms[3]       | depth: 1 | 2 nodes attached
    ---- terms[4]     | depth: 2 | 2 nodes attached
    -- terms[5]       | depth: 1 | 1 node attached
    terms[6]          | depth: 0 | 1 node attached
    -- terms[7]       | depth: 1 | 0 node attached

    We expect the following result for number of items:
    - terms[1]: count is 0
    - terms[3]: count is 2
    - terms[4]: count is 2
    - terms[5]: count is 1
    - terms[6]: count is 1
    - Others  : count is 0
  */
  $this
    ->setUpTermReferenceAndNodes('article', array(
    3,
    3,
    4,
    4,
    5,
    6,
  ));

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

  // Assert that the count is correct in the menu links according to the scheme.
  $index = 1;
  $positive_count_terms_index = array(
    3,
    4,
    5,
    6,
  );
  $visible_item = array(
    'hidden' => FALSE,
  );
  foreach ($this->terms_hierarchy as $term) {
    $mlid = _taxonomy_menu_get_mlid($term
      ->id(), $vid);
    $menu_link = menu_link_load($mlid);
    if ($mlid) {
      switch ($index) {
        case '3':
        case '4':
          $count = 2;
          break;
        case '5':
        case '6':
          $count = 1;
          break;
        default:
          $count = 0;
          break;
      }
      if (in_array($index, $positive_count_terms_index)) {
        $this
          ->assertMenuLink($mlid, array(
          'link_title' => $term
            ->getName() . ' (' . $count . ')',
        ));
      }
      else {
        $this
          ->assertMenuLink($mlid, array(
          'link_title' => $term
            ->getName(),
        ));
      }
    }
    else {
      $this
        ->fail('No mlid could be found for the term ' . $term
        ->id());
    }
    $index++;
  }
}