You are here

public function TaxonomyMenuUnitTest::testTaxonomyMenuTermsHierarchy in Taxonomy menu 8

Tests the hierarchy of menu links in a menu.

File

lib/Drupal/taxonomy_menu/Tests/TaxonomyMenuUnitTest.php, line 99
Contains \Drupal\taxonomy_menu\Tests\TaxonomyMenuUnitTest.

Class

TaxonomyMenuUnitTest
Tests for taxonomy vocabulary functions.

Namespace

Drupal\taxonomy_menu\Tests

Code

public function testTaxonomyMenuTermsHierarchy() {
  $vid = $this->vocabulary
    ->id();
  $edit = array();

  // Settings
  $edit['taxonomy_menu[vocab_parent]'] = 'main:0';
  $this
    ->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
  $this
    ->assertResponse(200);

  // Given a taxonomy term, which id is tid:
  //   - ptid      -->  - plid
  //     - tid     -->    - mlid
  // Do the following verification for each term in the hierarchy; the ending
  // plid determined by the methods below should be equal.
  //   - method1: tid --> mlid --> plid
  //   - method2: tid --> ptid --> plid
  foreach ($this->terms_hierarchy as $term) {

    // 1. Get plid by getting the associated mlid for the term tid.
    $mlid = _taxonomy_menu_get_mlid($term
      ->id(), $vid);
    if ($mlid) {
      $menu_link = menu_link_load($mlid);
      $plid_from_mlid = $menu_link['plid'];

      // 2. Get plid by getting the associated mlid for the parent term ptid.
      // We don't handle multiple parents, break after first one.
      $parents = taxonomy_term_load_parents($term
        ->id());
      if (!empty($parents)) {
        foreach ($parents as $ptid => $parent) {
          $plid_from_ptid = _taxonomy_menu_get_mlid($ptid, $vid);

          // Assert that both plid found by the two different methods are equal.
          $message = 'Parent mlids from taxonomy term ' . $term
            ->id() . ' are a match.';
          $this
            ->assertEqual($plid_from_mlid, $plid_from_ptid, $message);
          break;
        }
      }
      else {

        // Current term has no parent term. This means that the name of the
        // vocabulary should be associated to the 'navigation' root.
        // Menu link of the current term as defined by taxonomy menu table.
        $this
          ->assertEqual($menu_link['plid'], 0);
      }
    }
    else {
      $this
        ->fail("mlid for taxonomy term " . $term
        ->id() . " could not be found.");
    }
  }
}