3 namespace Drupal\Tests\Core\Menu;
5 use Drupal\Core\Menu\MenuTreeParameters;
6 use Drupal\Tests\UnitTestCase;
9 * Tests the menu link tree parameters value object.
13 * @coversDefaultClass \Drupal\Core\Menu\MenuTreeParameters
15 class MenuTreeParametersTest extends UnitTestCase {
18 * Provides test data for testSetMinDepth().
20 public function providerTestSetMinDepth() {
23 // Valid values at the extremes and in the middle.
28 // Invalid values are mapped to the closest valid value.
29 $data[] = [-10000, 1];
31 // … except for those invalid values that reach beyond the maximum depth,
32 // because MenuTreeParameters is a value object and hence cannot depend
33 // on anything; to know the actual maximum depth, it'd have to depend on the
34 // MenuTreeStorage service.
36 $data[] = [100000, 100000];
42 * Tests setMinDepth().
44 * @covers ::setMinDepth
45 * @dataProvider providerTestSetMinDepth
47 public function testSetMinDepth($min_depth, $expected) {
48 $parameters = new MenuTreeParameters();
49 $parameters->setMinDepth($min_depth);
50 $this->assertEquals($expected, $parameters->minDepth);
54 * Tests addExpandedParents().
56 * @covers ::addExpandedParents
58 public function testAddExpanded() {
59 $parameters = new MenuTreeParameters();
61 // Verify default value.
62 $this->assertEquals([], $parameters->expandedParents);
64 // Add actual menu link plugin IDs to be expanded.
65 $parameters->addExpandedParents(['foo', 'bar', 'baz']);
66 $this->assertEquals(['foo', 'bar', 'baz'], $parameters->expandedParents);
68 // Add additional menu link plugin IDs; they should be merged, not replacing
70 $parameters->addExpandedParents(['qux', 'quux']);
71 $this->assertEquals(['foo', 'bar', 'baz', 'qux', 'quux'], $parameters->expandedParents);
73 // Add pre-existing menu link plugin IDs; they should not be added again;
75 $parameters->addExpandedParents(['bar', 'quux']);
76 $this->assertEquals(['foo', 'bar', 'baz', 'qux', 'quux'], $parameters->expandedParents);
80 * Tests addCondition().
82 * @covers ::addCondition
84 public function testAddCondition() {
85 $parameters = new MenuTreeParameters();
87 // Verify default value.
88 $this->assertEquals([], $parameters->conditions);
91 $parameters->addCondition('expanded', 1);
92 $this->assertEquals(['expanded' => 1], $parameters->conditions);
94 // Add another condition.
95 $parameters->addCondition('has_children', 0);
96 $this->assertEquals(['expanded' => 1, 'has_children' => 0], $parameters->conditions);
98 // Add a condition with an operator.
99 $parameters->addCondition('provider', ['module1', 'module2'], 'IN');
100 $this->assertEquals(['expanded' => 1, 'has_children' => 0, 'provider' => [['module1', 'module2'], 'IN']], $parameters->conditions);
102 // Add another condition with an operator.
103 $parameters->addCondition('id', 1337, '<');
104 $this->assertEquals(['expanded' => 1, 'has_children' => 0, 'provider' => [['module1', 'module2'], 'IN'], 'id' => [1337, '<']], $parameters->conditions);
106 // It's impossible to add two conditions on the same field; in that case,
107 // the old condition will be overwritten.
108 $parameters->addCondition('provider', 'other_module');
109 $this->assertEquals(['expanded' => 1, 'has_children' => 0, 'provider' => 'other_module', 'id' => [1337, '<']], $parameters->conditions);
113 * Tests onlyEnabledLinks().
115 * @covers ::onlyEnabledLinks
117 public function testOnlyEnabledLinks() {
118 $parameters = new MenuTreeParameters();
119 $parameters->onlyEnabledLinks();
120 $this->assertEquals(1, $parameters->conditions['enabled']);
124 * Tests setTopLevelOnly().
126 * @covers ::setTopLevelOnly
128 public function testSetTopLevelOnly() {
129 $parameters = new MenuTreeParameters();
130 $parameters->setTopLevelOnly();
131 $this->assertEquals(1, $parameters->maxDepth);
135 * Tests excludeRoot().
137 * @covers ::excludeRoot
139 public function testExcludeRoot() {
140 $parameters = new MenuTreeParameters();
141 $parameters->excludeRoot();
142 $this->assertEquals(1, $parameters->minDepth);