summaryrefslogtreecommitdiffstats
path: root/core/modules/system/lib/Drupal/system/Tests/Menu/TreeOutputTest.php
blob: 7c5948d35c52420b7828b5d9623dfce2fe661d08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php

/**
 * @file
 * Definition of Drupal\system\Tests\Menu\TreeOutputTest.
 */

namespace Drupal\system\Tests\Menu;

use Drupal\simpletest\DrupalUnitTestBase;

/**
 * Menu tree output related tests.
 */
class TreeOutputTest extends DrupalUnitTestBase {

  public static $modules = array('system', 'menu_link', 'field');

  /**
   * Dummy link structure acceptable for menu_tree_output().
   */
  protected $tree_data = array();

  public static function getInfo() {
    return array(
      'name' => 'Menu tree output',
      'description' => 'Tests menu tree output functions.',
      'group' => 'Menu',
    );
  }

  function setUp() {
    parent::setUp();

    $this->installSchema('system', array('router', 'menu_router'));
  }

  /**
   * Validate the generation of a proper menu tree output.
   */
  function testMenuTreeData() {
    $storage_controller = $this->container->get('entity.manager')->getStorageController('menu_link');
    // @todo Prettify this tree buildup code, it's very hard to read.
    $this->tree_data = array(
      '1'=> array(
        'link' => $storage_controller->create(array('menu_name' => 'main-menu', 'mlid' => 1, 'hidden' => 0, 'has_children' => 1, 'title' => 'Item 1', 'in_active_trail' => 1, 'access' => 1, 'href' => 'a', 'localized_options' => array('attributes' => array('title' =>'')))),
        'below' => array(
          '2' => array('link' => $storage_controller->create(array('menu_name' => 'main-menu', 'mlid' => 2, 'hidden' => 0, 'has_children' => 1, 'title' => 'Item 2', 'in_active_trail' => 1, 'access' => 1, 'href' => 'a/b', 'localized_options' => array('attributes' => array('title' =>'')))),
            'below' => array(
              '3' => array('link' => $storage_controller->create(array('menu_name' => 'main-menu', 'mlid' => 3, 'hidden' => 0, 'has_children' => 0, 'title' => 'Item 3', 'in_active_trail' => 0, 'access' => 1, 'href' => 'a/b/c', 'localized_options' => array('attributes' => array('title' =>'')))),
                'below' => array() ),
              '4' => array('link' => $storage_controller->create(array('menu_name' => 'main-menu', 'mlid' => 4, 'hidden' => 0, 'has_children' => 0, 'title' => 'Item 4', 'in_active_trail' => 0, 'access' => 1, 'href' => 'a/b/d', 'localized_options' => array('attributes' => array('title' =>'')))),
                'below' => array() )
              )
            )
          )
        ),
      '5' => array('link' => $storage_controller->create(array('menu_name' => 'main-menu', 'mlid' => 5, 'hidden' => 1, 'has_children' => 0, 'title' => 'Item 5', 'in_active_trail' => 0, 'access' => 1, 'href' => 'e', 'localized_options' => array('attributes' => array('title' =>'')))), 'below' => array()),
      '6' => array('link' => $storage_controller->create(array('menu_name' => 'main-menu', 'mlid' => 6, 'hidden' => 0, 'has_children' => 0, 'title' => 'Item 6', 'in_active_trail' => 0, 'access' => 0, 'href' => 'f', 'localized_options' => array('attributes' => array('title' =>'')))), 'below' => array()),
      '7' => array('link' => $storage_controller->create(array('menu_name' => 'main-menu', 'mlid' => 7, 'hidden' => 0, 'has_children' => 0, 'title' => 'Item 7', 'in_active_trail' => 0, 'access' => 1, 'href' => 'g', 'localized_options' => array('attributes' => array('title' =>'')))), 'below' => array())
    );

    $output = menu_tree_output($this->tree_data);

    // Validate that the - in main-menu is changed into an underscore
    $this->assertEqual( $output['1']['#theme'], 'menu_link__main_menu', 'Hyphen is changed to a dash on menu_link');
    $this->assertEqual( $output['#theme_wrappers'][0], 'menu_tree__main_menu', 'Hyphen is changed to a dash on menu_tree wrapper');
    // Looking for child items in the data
    $this->assertEqual( $output['1']['#below']['2']['#href'], 'a/b', 'Checking the href on a child item');
    $this->assertTrue( in_array('active-trail',$output['1']['#below']['2']['#attributes']['class']) , 'Checking the active trail class');
    // Validate that the hidden and no access items are missing
    $this->assertFalse( isset($output['5']), 'Hidden item should be missing');
    $this->assertFalse( isset($output['6']), 'False access should be missing');
    // Item 7 is after a couple hidden items. Just to make sure that 5 and 6 are skipped and 7 still included
    $this->assertTrue( isset($output['7']), 'Item after hidden items is present');
  }
}