LayoutDirectivesTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * Set of tests of layout directives handling behavior
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\View;
  9. use Magento\Framework\App\State;
  10. class LayoutDirectivesTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\View\LayoutFactory
  14. */
  15. protected $layoutFactory;
  16. /**
  17. * @var \Magento\Framework\View\Layout\BuilderFactory
  18. */
  19. protected $builderFactory;
  20. /**
  21. * @var \Magento\Framework\ObjectManagerInterface
  22. */
  23. protected $objectManager;
  24. /**
  25. * @var \Magento\Framework\App\State
  26. */
  27. protected $state;
  28. protected function setUp()
  29. {
  30. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  31. $this->layoutFactory = $this->objectManager->get(\Magento\Framework\View\LayoutFactory::class);
  32. $this->state = $this->objectManager->get(\Magento\Framework\App\State::class);
  33. }
  34. /**
  35. * Prepare a layout model with pre-loaded fixture of an update XML
  36. *
  37. * @param string $fixtureFile
  38. * @return \Magento\Framework\View\LayoutInterface
  39. */
  40. protected function _getLayoutModel($fixtureFile)
  41. {
  42. $this->objectManager->get(\Magento\Framework\App\Cache\Type\Layout::class)->clean();
  43. $layout = $this->layoutFactory->create();
  44. /** @var $xml \Magento\Framework\View\Layout\Element */
  45. $xml = simplexml_load_file(
  46. __DIR__ . "/_files/layout_directives_test/{$fixtureFile}",
  47. \Magento\Framework\View\Layout\Element::class
  48. );
  49. $layout->loadString($xml->asXml());
  50. $layout->generateElements();
  51. return $layout;
  52. }
  53. /**
  54. * Test scheduled operations in the rendering of elements
  55. *
  56. * Expected behavior:
  57. * 1) block1 was not declared at the moment when "1" invocation declared. The operation is scheduled
  58. * 2) block1 creation directive schedules adding "2" as well
  59. * 3) block2 is generated with "3"
  60. * 4) yet another action schedules replacing value of block2 into "4"
  61. * 5) when entire layout is read, all scheduled operations are executed in the same order as declared
  62. * (blocks are instantiated first, of course)
  63. * The end result can be observed in container1
  64. *
  65. * @magentoAppIsolation enabled
  66. */
  67. public function testRenderElement()
  68. {
  69. $layout = $this->_getLayoutModel('render.xml');
  70. $this->assertEquals('124', $layout->renderElement('container_one'));
  71. $this->assertEquals('12', $layout->renderElement('block_one'));
  72. }
  73. /**
  74. * @expectedException \OutOfBoundsException
  75. * @magentoAppIsolation enabled
  76. */
  77. public function testRenderNonExistentElementShouldThrowException()
  78. {
  79. $layout = $this->_getLayoutModel('render.xml');
  80. $this->assertEmpty($layout->renderElement('nonexisting_element'));
  81. $this->expectExceptionMessage(
  82. 'The element with the "nonexisting_element" ID wasn\'t found. Verify the ID and try again.'
  83. );
  84. }
  85. /**
  86. * Invoke getBlock() while layout is being generated
  87. *
  88. * Assertions in this test are pure formalism. The point is to emulate situation where block refers to other block
  89. * while the latter hasn't been generated yet, and assure that there is no crash
  90. */
  91. public function testGetBlockUnscheduled()
  92. {
  93. $layout = $this->_getLayoutModel('get_block.xml');
  94. $this->assertInstanceOf(\Magento\Framework\View\Element\Text::class, $layout->getBlock('block_first'));
  95. $this->assertInstanceOf(\Magento\Framework\View\Element\Text::class, $layout->getBlock('block_second'));
  96. }
  97. public function testLayoutArgumentsDirective()
  98. {
  99. $layout = $this->_getLayoutModel('arguments.xml');
  100. $this->assertEquals('1', $layout->getBlock('block_with_args')->getOne());
  101. $this->assertEquals('two', $layout->getBlock('block_with_args')->getTwo());
  102. $this->assertEquals('3', $layout->getBlock('block_with_args')->getThree());
  103. }
  104. public function testLayoutArgumentsDirectiveIfComplexValues()
  105. {
  106. $layout = $this->_getLayoutModel('arguments_complex_values.xml');
  107. $this->assertEquals(
  108. ['parameters' => ['first' => '1', 'second' => '2']],
  109. $layout->getBlock('block_with_args_complex_values')->getOne()
  110. );
  111. $this->assertEquals('two', $layout->getBlock('block_with_args_complex_values')->getTwo());
  112. $this->assertEquals(
  113. ['extra' => ['key1' => 'value1', 'key2' => 'value2']],
  114. $layout->getBlock('block_with_args_complex_values')->getThree()
  115. );
  116. }
  117. public function testLayoutObjectArgumentsDirective()
  118. {
  119. $layout = $this->_getLayoutModel('arguments_object_type.xml');
  120. $this->assertInstanceOf(
  121. \Magento\Framework\Data\Collection::class,
  122. $layout->getBlock('block_with_object_args')->getOne()
  123. );
  124. $this->assertInstanceOf(
  125. \Magento\Framework\Data\Collection::class,
  126. $layout->getBlock('block_with_object_args')->getTwo()
  127. );
  128. $this->assertEquals(3, $layout->getBlock('block_with_object_args')->getThree());
  129. }
  130. public function testLayoutUrlArgumentsDirective()
  131. {
  132. $layout = $this->_getLayoutModel('arguments_url_type.xml');
  133. $this->assertContains('customer/account/login', $layout->getBlock('block_with_url_args')->getOne());
  134. $this->assertContains('customer/account/logout', $layout->getBlock('block_with_url_args')->getTwo());
  135. $this->assertContains('customer_id/3', $layout->getBlock('block_with_url_args')->getTwo());
  136. }
  137. public function testLayoutObjectArgumentUpdatersDirective()
  138. {
  139. $this->markTestSkipped('Will be fixed after MAGETWO-33840 will be done');
  140. $layout = $this->_getLayoutModel('arguments_object_type_updaters.xml');
  141. $expectedObjectData = [0 => 'updater call', 1 => 'updater call'];
  142. $expectedSimpleData = 1;
  143. $dataSource = $layout->getBlock('block_with_object_updater_args')->getOne();
  144. $this->assertInstanceOf(\Magento\Framework\Data\Collection::class, $dataSource);
  145. $this->assertEquals($expectedObjectData, $dataSource->getUpdaterCall());
  146. $this->assertEquals($expectedSimpleData, $layout->getBlock('block_with_object_updater_args')->getTwo());
  147. }
  148. /**
  149. * @magentoAppIsolation enabled
  150. */
  151. public function testMoveSameAlias()
  152. {
  153. $layout = $this->_getLayoutModel('move_the_same_alias.xml');
  154. $this->assertEquals('container1', $layout->getParentName('no_name3'));
  155. }
  156. /**
  157. * @magentoAppIsolation enabled
  158. */
  159. public function testMoveNewAlias()
  160. {
  161. $layout = $this->_getLayoutModel('move_new_alias.xml');
  162. $this->assertEquals('new_alias', $layout->getElementAlias('no_name3'));
  163. }
  164. public function testActionAnonymousParentBlock()
  165. {
  166. $layout = $this->_getLayoutModel('action_for_anonymous_parent_block.xml');
  167. $this->assertEquals('schedule_block0', $layout->getParentName('test.block.insert'));
  168. $this->assertEquals('schedule_block1', $layout->getParentName('test.block.append'));
  169. }
  170. /**
  171. * @magentoAppIsolation enabled
  172. */
  173. public function testRemove()
  174. {
  175. $layout = $this->_getLayoutModel('remove.xml');
  176. $this->assertFalse($layout->getBlock('no_name2'));
  177. $this->assertFalse($layout->getBlock('child_block1'));
  178. $this->assertTrue($layout->isBlock('child_block2'));
  179. }
  180. /**
  181. * @magentoAppIsolation enabled
  182. */
  183. public function testRemoveCancellation()
  184. {
  185. $layout = $this->_getLayoutModel('remove_cancellation.xml');
  186. $this->assertTrue($layout->isContainer('container1'));
  187. $this->assertTrue($layout->isBlock('child_block1'));
  188. $this->assertTrue($layout->isBlock('no_name2'));
  189. $this->assertFalse($layout->getBlock('not_exist'));
  190. }
  191. /**
  192. * @magentoAppIsolation enabled
  193. */
  194. public function testMove()
  195. {
  196. $layout = $this->_getLayoutModel('move.xml');
  197. $this->assertEquals('container2', $layout->getParentName('container1'));
  198. $this->assertEquals('container1', $layout->getParentName('no.name2'));
  199. $this->assertEquals('block_container', $layout->getParentName('no_name3'));
  200. // verify `after` attribute
  201. $this->assertEquals('block_container', $layout->getParentName('no_name'));
  202. $childrenOrderArray = array_keys($layout->getChildBlocks($layout->getParentName('no_name')));
  203. $positionAfter = array_search('child_block1', $childrenOrderArray);
  204. $positionToVerify = array_search('no_name', $childrenOrderArray);
  205. $this->assertEquals($positionAfter, --$positionToVerify);
  206. // verify `before` attribute
  207. $this->assertEquals('block_container', $layout->getParentName('no_name4'));
  208. $childrenOrderArray = array_keys($layout->getChildBlocks($layout->getParentName('no_name4')));
  209. $positionBefore = array_search('child_block2', $childrenOrderArray);
  210. $positionToVerify = array_search('no_name4', $childrenOrderArray);
  211. $this->assertEquals($positionBefore, ++$positionToVerify);
  212. }
  213. /**
  214. * @expectedException \Magento\Framework\Exception\LocalizedException
  215. */
  216. public function testMoveBroken()
  217. {
  218. $this->_getLayoutModel('move_broken.xml');
  219. }
  220. /**
  221. * @expectedException \Magento\Framework\Exception\LocalizedException
  222. */
  223. public function testMoveAliasBroken()
  224. {
  225. $this->_getLayoutModel('move_alias_broken.xml');
  226. }
  227. public function testRemoveBroken()
  228. {
  229. if ($this->state->getMode() === State::MODE_DEVELOPER) {
  230. $this->expectException('OutOfBoundsException');
  231. }
  232. $this->_getLayoutModel('remove_broken.xml');
  233. }
  234. /**
  235. * @param string $case
  236. * @param string $expectedResult
  237. * @dataProvider sortSpecialCasesDataProvider
  238. * @magentoAppIsolation enabled
  239. */
  240. public function testSortSpecialCases($case, $expectedResult)
  241. {
  242. $layout = $this->_getLayoutModel($case);
  243. $this->assertEquals($expectedResult, $layout->renderElement('root'));
  244. }
  245. /**
  246. * @return array
  247. */
  248. public function sortSpecialCasesDataProvider()
  249. {
  250. return [
  251. 'Before element which is after' => ['sort_before_after.xml', '312'],
  252. 'Before element which is previous' => ['sort_before_before.xml', '213'],
  253. 'After element which is after' => ['sort_after_after.xml', '312'],
  254. 'After element which is previous' => ['sort_after_previous.xml', '321']
  255. ];
  256. }
  257. /**
  258. * @magentoConfigFixture current_store true_options 1
  259. * @magentoAppIsolation enabled
  260. */
  261. public function testIfConfigForBlock()
  262. {
  263. $layout = $this->_getLayoutModel('ifconfig.xml');
  264. $this->assertFalse($layout->getBlock('block1'));
  265. $this->assertFalse($layout->getBlock('block2'));
  266. $this->assertInstanceOf(\Magento\Framework\View\Element\BlockInterface::class, $layout->getBlock('block3'));
  267. $this->assertFalse($layout->getBlock('block4'));
  268. }
  269. /**
  270. * @magentoConfigFixture current_store true_options 1
  271. * @magentoAppIsolation enabled
  272. */
  273. public function testBlockGroups()
  274. {
  275. $layout = $this->_getLayoutModel('group.xml');
  276. $childNames = $layout->getBlock('block1')->getGroupChildNames('group1');
  277. $this->assertEquals(['block2', 'block3'], $childNames);
  278. }
  279. }