PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/core/tests/Drupal/Tests/Core/Menu/LocalTaskDefaultTest.php

http://github.com/drupal/drupal
PHP | 348 lines | 196 code | 57 blank | 95 comment | 0 complexity | e127e553341b88f403e435ac10cbb83c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Tests\Core\Menu\LocalTaskDefaultTest.
  5. */
  6. namespace Drupal\Tests\Core\Menu;
  7. use Drupal\Core\Menu\LocalTaskDefault;
  8. use Drupal\Core\Routing\RouteMatch;
  9. use Drupal\Core\Routing\RouteProviderInterface;
  10. use Drupal\Core\StringTranslation\TranslatableMarkup;
  11. use Drupal\Tests\UnitTestCase;
  12. use Symfony\Component\Routing\Route;
  13. /**
  14. * @coversDefaultClass \Drupal\Core\Menu\LocalTaskDefault
  15. * @group Menu
  16. */
  17. class LocalTaskDefaultTest extends UnitTestCase {
  18. /**
  19. * The tested local task default plugin.
  20. *
  21. * @var \Drupal\Core\Menu\LocalTaskDefault
  22. */
  23. protected $localTaskBase;
  24. /**
  25. * The used plugin configuration.
  26. *
  27. * @var array
  28. */
  29. protected $config = [];
  30. /**
  31. * The used plugin ID.
  32. *
  33. * @var string
  34. */
  35. protected $pluginId = 'local_task_default';
  36. /**
  37. * The used plugin definition.
  38. *
  39. * @var array
  40. */
  41. protected $pluginDefinition = [
  42. 'id' => 'local_task_default',
  43. ];
  44. /**
  45. * The mocked translator.
  46. *
  47. * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit\Framework\MockObject\MockObject
  48. */
  49. protected $stringTranslation;
  50. /**
  51. * The mocked route provider.
  52. *
  53. * @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit\Framework\MockObject\MockObject
  54. */
  55. protected $routeProvider;
  56. protected function setUp() {
  57. parent::setUp();
  58. $this->stringTranslation = $this->createMock('Drupal\Core\StringTranslation\TranslationInterface');
  59. $this->routeProvider = $this->createMock('Drupal\Core\Routing\RouteProviderInterface');
  60. }
  61. /**
  62. * Setups the local task default.
  63. */
  64. protected function setupLocalTaskDefault() {
  65. $this->localTaskBase = new TestLocalTaskDefault($this->config, $this->pluginId, $this->pluginDefinition);
  66. $this->localTaskBase
  67. ->setRouteProvider($this->routeProvider);
  68. }
  69. /**
  70. * @covers ::getRouteParameters
  71. */
  72. public function testGetRouteParametersForStaticRoute() {
  73. $this->pluginDefinition = [
  74. 'route_name' => 'test_route',
  75. ];
  76. $this->routeProvider->expects($this->once())
  77. ->method('getRouteByName')
  78. ->with('test_route')
  79. ->will($this->returnValue(new Route('/test-route')));
  80. $this->setupLocalTaskDefault();
  81. $route_match = new RouteMatch('', new Route('/'));
  82. $this->assertEquals([], $this->localTaskBase->getRouteParameters($route_match));
  83. }
  84. /**
  85. * @covers ::getRouteParameters
  86. */
  87. public function testGetRouteParametersInPluginDefinitions() {
  88. $this->pluginDefinition = [
  89. 'route_name' => 'test_route',
  90. 'route_parameters' => ['parameter' => 'example'],
  91. ];
  92. $this->routeProvider->expects($this->once())
  93. ->method('getRouteByName')
  94. ->with('test_route')
  95. ->will($this->returnValue(new Route('/test-route/{parameter}')));
  96. $this->setupLocalTaskDefault();
  97. $route_match = new RouteMatch('', new Route('/'));
  98. $this->assertEquals(['parameter' => 'example'], $this->localTaskBase->getRouteParameters($route_match));
  99. }
  100. /**
  101. * @covers ::getRouteParameters
  102. */
  103. public function testGetRouteParametersForDynamicRouteWithNonUpcastedParameters() {
  104. $this->pluginDefinition = [
  105. 'route_name' => 'test_route',
  106. ];
  107. $route = new Route('/test-route/{parameter}');
  108. $this->routeProvider->expects($this->once())
  109. ->method('getRouteByName')
  110. ->with('test_route')
  111. ->will($this->returnValue($route));
  112. $this->setupLocalTaskDefault();
  113. $route_match = new RouteMatch('', $route, [], ['parameter' => 'example']);
  114. $this->assertEquals(['parameter' => 'example'], $this->localTaskBase->getRouteParameters($route_match));
  115. }
  116. /**
  117. * Tests the getRouteParameters method for a route with upcasted parameters.
  118. *
  119. * @covers ::getRouteParameters
  120. */
  121. public function testGetRouteParametersForDynamicRouteWithUpcastedParameters() {
  122. $this->pluginDefinition = [
  123. 'route_name' => 'test_route',
  124. ];
  125. $route = new Route('/test-route/{parameter}');
  126. $this->routeProvider->expects($this->once())
  127. ->method('getRouteByName')
  128. ->with('test_route')
  129. ->will($this->returnValue($route));
  130. $this->setupLocalTaskDefault();
  131. $route_match = new RouteMatch('', $route, ['parameter' => (object) 'example2'], ['parameter' => 'example']);
  132. $this->assertEquals(['parameter' => 'example'], $this->localTaskBase->getRouteParameters($route_match));
  133. }
  134. /**
  135. * Tests the getRouteParameters method for a route with upcasted parameters.
  136. *
  137. * @covers ::getRouteParameters
  138. */
  139. public function testGetRouteParametersForDynamicRouteWithUpcastedParametersEmptyRawParameters() {
  140. $this->pluginDefinition = [
  141. 'route_name' => 'test_route',
  142. ];
  143. $route = new Route('/test-route/{parameter}');
  144. $this->routeProvider->expects($this->once())
  145. ->method('getRouteByName')
  146. ->with('test_route')
  147. ->will($this->returnValue($route));
  148. $this->setupLocalTaskDefault();
  149. $route_match = new RouteMatch('', $route, ['parameter' => (object) 'example2']);
  150. $this->assertEquals(['parameter' => (object) 'example2'], $this->localTaskBase->getRouteParameters($route_match));
  151. }
  152. /**
  153. * Defines a data provider for testGetWeight().
  154. *
  155. * @return array
  156. * A list or test plugin definition and expected weight.
  157. */
  158. public function providerTestGetWeight() {
  159. return [
  160. // Manually specify a weight, so this is used.
  161. [['weight' => 314], 'test_id', 314],
  162. // Ensure that a default tab gets a lower weight.
  163. [
  164. [
  165. 'base_route' => 'local_task_default',
  166. 'route_name' => 'local_task_default',
  167. 'id' => 'local_task_default',
  168. ],
  169. 'local_task_default',
  170. -10,
  171. ],
  172. // If the base route is different from the route of the tab, ignore it.
  173. [
  174. [
  175. 'base_route' => 'local_task_example',
  176. 'route_name' => 'local_task_other',
  177. 'id' => 'local_task_default',
  178. ],
  179. 'local_task_default',
  180. 0,
  181. ],
  182. // Ensure that a default tab of a derivative gets the default value.
  183. [
  184. [
  185. 'base_route' => 'local_task_example',
  186. 'id' => 'local_task_derivative_default:example_id',
  187. 'route_name' => 'local_task_example',
  188. ],
  189. 'local_task_derivative_default:example_id',
  190. -10,
  191. ],
  192. ];
  193. }
  194. /**
  195. * @dataProvider providerTestGetWeight
  196. * @covers ::getWeight
  197. */
  198. public function testGetWeight($plugin_definition, $plugin_id, $expected_weight) {
  199. $this->pluginDefinition = $plugin_definition;
  200. $this->pluginId = $plugin_id;
  201. $this->setupLocalTaskDefault();
  202. $this->assertEquals($expected_weight, $this->localTaskBase->getWeight());
  203. }
  204. /**
  205. * @covers ::getActive
  206. * @covers ::setActive
  207. */
  208. public function testActive() {
  209. $this->setupLocalTaskDefault();
  210. $this->assertFalse($this->localTaskBase->getActive());
  211. $this->localTaskBase->setActive();
  212. $this->assertTrue($this->localTaskBase->getActive());
  213. }
  214. /**
  215. * @covers ::getTitle
  216. */
  217. public function testGetTitle() {
  218. $this->pluginDefinition['title'] = (new TranslatableMarkup('Example', [], [], $this->stringTranslation));
  219. $this->stringTranslation->expects($this->once())
  220. ->method('translateString')
  221. ->with($this->pluginDefinition['title'])
  222. ->will($this->returnValue('Example translated'));
  223. $this->setupLocalTaskDefault();
  224. $this->assertEquals('Example translated', $this->localTaskBase->getTitle());
  225. }
  226. /**
  227. * @covers ::getTitle
  228. */
  229. public function testGetTitleWithContext() {
  230. $title = 'Example';
  231. $this->pluginDefinition['title'] = (new TranslatableMarkup($title, [], ['context' => 'context'], $this->stringTranslation));
  232. $this->stringTranslation->expects($this->once())
  233. ->method('translateString')
  234. ->with($this->pluginDefinition['title'])
  235. ->will($this->returnValue('Example translated with context'));
  236. $this->setupLocalTaskDefault();
  237. $this->assertEquals('Example translated with context', $this->localTaskBase->getTitle());
  238. }
  239. /**
  240. * @covers ::getTitle
  241. */
  242. public function testGetTitleWithTitleArguments() {
  243. $this->pluginDefinition['title'] = (new TranslatableMarkup('Example @test', ['@test' => 'value'], [], $this->stringTranslation));
  244. $this->stringTranslation->expects($this->once())
  245. ->method('translateString')
  246. ->with($this->pluginDefinition['title'])
  247. ->will($this->returnValue('Example value'));
  248. $this->setupLocalTaskDefault();
  249. $this->assertEquals('Example value', $this->localTaskBase->getTitle());
  250. }
  251. /**
  252. * @covers ::getOptions
  253. */
  254. public function testGetOptions() {
  255. $this->pluginDefinition['options'] = [
  256. 'attributes' => ['class' => ['example']],
  257. ];
  258. $this->setupLocalTaskDefault();
  259. $route_match = new RouteMatch('', new Route('/'));
  260. $this->assertEquals($this->pluginDefinition['options'], $this->localTaskBase->getOptions($route_match));
  261. $this->localTaskBase->setActive(TRUE);
  262. $this->assertEquals([
  263. 'attributes' => [
  264. 'class' => [
  265. 'example',
  266. 'is-active',
  267. ],
  268. ],
  269. ], $this->localTaskBase->getOptions($route_match));
  270. }
  271. /**
  272. * @covers ::getCacheContexts
  273. * @covers ::getCacheTags
  274. * @covers ::getCacheMaxAge
  275. */
  276. public function testCacheabilityMetadata() {
  277. $this->pluginDefinition['cache_contexts'] = ['route'];
  278. $this->pluginDefinition['cache_tags'] = ['kitten'];
  279. $this->pluginDefinition['cache_max_age'] = 3600;
  280. $this->setupLocalTaskDefault();
  281. $this->assertEquals(['route'], $this->localTaskBase->getCacheContexts());
  282. $this->assertEquals(['kitten'], $this->localTaskBase->getCacheTags());
  283. $this->assertEquals(3600, $this->localTaskBase->getCacheMaxAge());
  284. }
  285. }
  286. class TestLocalTaskDefault extends LocalTaskDefault {
  287. public function setRouteProvider(RouteProviderInterface $route_provider) {
  288. $this->routeProvider = $route_provider;
  289. return $this;
  290. }
  291. }