ccaf3b9c72e086e892d45a5155e2f6a15cf9be9f
[yaffs-website] / MenuTreeParametersTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Menu;
4
5 use Drupal\Core\Menu\MenuTreeParameters;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * Tests the menu link tree parameters value object.
10  *
11  * @group Menu
12  *
13  * @coversDefaultClass \Drupal\Core\Menu\MenuTreeParameters
14  */
15 class MenuTreeParametersTest extends UnitTestCase {
16
17   /**
18    * Provides test data for testSetMinDepth().
19    */
20   public function providerTestSetMinDepth() {
21     $data = [];
22
23     // Valid values at the extremes and in the middle.
24     $data[] = [1, 1];
25     $data[] = [2, 2];
26     $data[] = [9, 9];
27
28     // Invalid values are mapped to the closest valid value.
29     $data[] = [-10000, 1];
30     $data[] = [0, 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.
35     $data[] = [10, 10];
36     $data[] = [100000, 100000];
37
38     return $data;
39   }
40
41   /**
42    * Tests setMinDepth().
43    *
44    * @covers ::setMinDepth
45    * @dataProvider providerTestSetMinDepth
46    */
47   public function testSetMinDepth($min_depth, $expected) {
48     $parameters = new MenuTreeParameters();
49     $parameters->setMinDepth($min_depth);
50     $this->assertEquals($expected, $parameters->minDepth);
51   }
52
53   /**
54    * Tests addExpandedParents().
55    *
56    * @covers ::addExpandedParents
57    */
58   public function testAddExpanded() {
59     $parameters = new MenuTreeParameters();
60
61     // Verify default value.
62     $this->assertEquals([], $parameters->expandedParents);
63
64     // Add actual menu link plugin IDs to be expanded.
65     $parameters->addExpandedParents(['foo', 'bar', 'baz']);
66     $this->assertEquals(['foo', 'bar', 'baz'], $parameters->expandedParents);
67
68     // Add additional menu link plugin IDs; they should be merged, not replacing
69     // the old ones.
70     $parameters->addExpandedParents(['qux', 'quux']);
71     $this->assertEquals(['foo', 'bar', 'baz', 'qux', 'quux'], $parameters->expandedParents);
72
73     // Add pre-existing menu link plugin IDs; they should not be added again;
74     // this is a set.
75     $parameters->addExpandedParents(['bar', 'quux']);
76     $this->assertEquals(['foo', 'bar', 'baz', 'qux', 'quux'], $parameters->expandedParents);
77   }
78
79   /**
80    * Tests addCondition().
81    *
82    * @covers ::addCondition
83    */
84   public function testAddCondition() {
85     $parameters = new MenuTreeParameters();
86
87     // Verify default value.
88     $this->assertEquals([], $parameters->conditions);
89
90     // Add a condition.
91     $parameters->addCondition('expanded', 1);
92     $this->assertEquals(['expanded' => 1], $parameters->conditions);
93
94     // Add another condition.
95     $parameters->addCondition('has_children', 0);
96     $this->assertEquals(['expanded' => 1, 'has_children' => 0], $parameters->conditions);
97
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);
101
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);
105
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);
110   }
111
112   /**
113    * Tests onlyEnabledLinks().
114    *
115    * @covers ::onlyEnabledLinks
116    */
117   public function testOnlyEnabledLinks() {
118     $parameters = new MenuTreeParameters();
119     $parameters->onlyEnabledLinks();
120     $this->assertEquals(1, $parameters->conditions['enabled']);
121   }
122
123   /**
124    * Tests setTopLevelOnly().
125    *
126    * @covers ::setTopLevelOnly
127    */
128   public function testSetTopLevelOnly() {
129     $parameters = new MenuTreeParameters();
130     $parameters->setTopLevelOnly();
131     $this->assertEquals(1, $parameters->maxDepth);
132   }
133
134   /**
135    * Tests excludeRoot().
136    *
137    * @covers ::excludeRoot
138    */
139   public function testExcludeRoot() {
140     $parameters = new MenuTreeParameters();
141     $parameters->excludeRoot();
142     $this->assertEquals(1, $parameters->minDepth);
143   }
144
145 }