MenuTreeParametersTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace Drupal\Tests\Core\Menu;
  3. use Drupal\Core\Menu\MenuTreeParameters;
  4. use Drupal\Tests\UnitTestCase;
  5. /**
  6. * Tests the menu link tree parameters value object.
  7. *
  8. * @group Menu
  9. *
  10. * @coversDefaultClass \Drupal\Core\Menu\MenuTreeParameters
  11. */
  12. class MenuTreeParametersTest extends UnitTestCase {
  13. /**
  14. * Provides test data for testSetMinDepth().
  15. */
  16. public function providerTestSetMinDepth() {
  17. $data = [];
  18. // Valid values at the extremes and in the middle.
  19. $data[] = [1, 1];
  20. $data[] = [2, 2];
  21. $data[] = [9, 9];
  22. // Invalid values are mapped to the closest valid value.
  23. $data[] = [-10000, 1];
  24. $data[] = [0, 1];
  25. // … except for those invalid values that reach beyond the maximum depth,
  26. // because MenuTreeParameters is a value object and hence cannot depend
  27. // on anything; to know the actual maximum depth, it'd have to depend on the
  28. // MenuTreeStorage service.
  29. $data[] = [10, 10];
  30. $data[] = [100000, 100000];
  31. return $data;
  32. }
  33. /**
  34. * Tests setMinDepth().
  35. *
  36. * @covers ::setMinDepth
  37. * @dataProvider providerTestSetMinDepth
  38. */
  39. public function testSetMinDepth($min_depth, $expected) {
  40. $parameters = new MenuTreeParameters();
  41. $parameters->setMinDepth($min_depth);
  42. $this->assertEquals($expected, $parameters->minDepth);
  43. }
  44. /**
  45. * Tests addExpandedParents().
  46. *
  47. * @covers ::addExpandedParents
  48. */
  49. public function testAddExpanded() {
  50. $parameters = new MenuTreeParameters();
  51. // Verify default value.
  52. $this->assertEquals([], $parameters->expandedParents);
  53. // Add actual menu link plugin IDs to be expanded.
  54. $parameters->addExpandedParents(['foo', 'bar', 'baz']);
  55. $this->assertEquals(['foo', 'bar', 'baz'], $parameters->expandedParents);
  56. // Add additional menu link plugin IDs; they should be merged, not replacing
  57. // the old ones.
  58. $parameters->addExpandedParents(['qux', 'quux']);
  59. $this->assertEquals(['foo', 'bar', 'baz', 'qux', 'quux'], $parameters->expandedParents);
  60. // Add pre-existing menu link plugin IDs; they should not be added again;
  61. // this is a set.
  62. $parameters->addExpandedParents(['bar', 'quux']);
  63. $this->assertEquals(['foo', 'bar', 'baz', 'qux', 'quux'], $parameters->expandedParents);
  64. }
  65. /**
  66. * Tests addCondition().
  67. *
  68. * @covers ::addCondition
  69. */
  70. public function testAddCondition() {
  71. $parameters = new MenuTreeParameters();
  72. // Verify default value.
  73. $this->assertEquals([], $parameters->conditions);
  74. // Add a condition.
  75. $parameters->addCondition('expanded', 1);
  76. $this->assertEquals(['expanded' => 1], $parameters->conditions);
  77. // Add another condition.
  78. $parameters->addCondition('has_children', 0);
  79. $this->assertEquals(['expanded' => 1, 'has_children' => 0], $parameters->conditions);
  80. // Add a condition with an operator.
  81. $parameters->addCondition('provider', ['module1', 'module2'], 'IN');
  82. $this->assertEquals(['expanded' => 1, 'has_children' => 0, 'provider' => [['module1', 'module2'], 'IN']], $parameters->conditions);
  83. // Add another condition with an operator.
  84. $parameters->addCondition('id', 1337, '<');
  85. $this->assertEquals(['expanded' => 1, 'has_children' => 0, 'provider' => [['module1', 'module2'], 'IN'], 'id' => [1337, '<']], $parameters->conditions);
  86. // It's impossible to add two conditions on the same field; in that case,
  87. // the old condition will be overwritten.
  88. $parameters->addCondition('provider', 'other_module');
  89. $this->assertEquals(['expanded' => 1, 'has_children' => 0, 'provider' => 'other_module', 'id' => [1337, '<']], $parameters->conditions);
  90. }
  91. /**
  92. * Tests onlyEnabledLinks().
  93. *
  94. * @covers ::onlyEnabledLinks
  95. */
  96. public function testOnlyEnabledLinks() {
  97. $parameters = new MenuTreeParameters();
  98. $parameters->onlyEnabledLinks();
  99. $this->assertEquals(1, $parameters->conditions['enabled']);
  100. }
  101. /**
  102. * Tests setTopLevelOnly().
  103. *
  104. * @covers ::setTopLevelOnly
  105. */
  106. public function testSetTopLevelOnly() {
  107. $parameters = new MenuTreeParameters();
  108. $parameters->setTopLevelOnly();
  109. $this->assertEquals(1, $parameters->maxDepth);
  110. }
  111. /**
  112. * Tests excludeRoot().
  113. *
  114. * @covers ::excludeRoot
  115. */
  116. public function testExcludeRoot() {
  117. $parameters = new MenuTreeParameters();
  118. $parameters->excludeRoot();
  119. $this->assertEquals(1, $parameters->minDepth);
  120. }
  121. /**
  122. * @covers ::serialize
  123. * @covers ::unserialize
  124. */
  125. public function testSerialize() {
  126. $parameters = new MenuTreeParameters();
  127. $parameters->setRoot(1);
  128. $parameters->setMinDepth('2');
  129. $parameters->setMaxDepth('9');
  130. $parameters->addExpandedParents(['', 'foo']);
  131. $parameters->setActiveTrail(['', 'bar']);
  132. $after_serialize = unserialize(serialize($parameters));
  133. $this->assertSame('1', $after_serialize->root);
  134. $this->assertSame(2, $after_serialize->minDepth);
  135. $this->assertSame(9, $after_serialize->maxDepth);
  136. $this->assertSame(['', 'foo'], $after_serialize->expandedParents);
  137. $this->assertSame(['bar'], $after_serialize->activeTrail);
  138. }
  139. }