Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LayoutDirectivesTest.php
Go to the documentation of this file.
1 <?php
9 namespace Magento\Framework\View;
10 
12 
13 class LayoutDirectivesTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $layoutFactory;
19 
23  protected $builderFactory;
24 
28  protected $objectManager;
29 
33  protected $state;
34 
35  protected function setUp()
36  {
38  $this->layoutFactory = $this->objectManager->get(\Magento\Framework\View\LayoutFactory::class);
39  $this->state = $this->objectManager->get(\Magento\Framework\App\State::class);
40  }
41 
48  protected function _getLayoutModel($fixtureFile)
49  {
50  $this->objectManager->get(\Magento\Framework\App\Cache\Type\Layout::class)->clean();
51  $layout = $this->layoutFactory->create();
53  $xml = simplexml_load_file(
54  __DIR__ . "/_files/layout_directives_test/{$fixtureFile}",
55  \Magento\Framework\View\Layout\Element::class
56  );
57  $layout->loadString($xml->asXml());
58  $layout->generateElements();
59  return $layout;
60  }
61 
76  public function testRenderElement()
77  {
78  $layout = $this->_getLayoutModel('render.xml');
79  $this->assertEquals('124', $layout->renderElement('container_one'));
80  $this->assertEquals('12', $layout->renderElement('block_one'));
81  }
82 
88  {
89  $layout = $this->_getLayoutModel('render.xml');
90  $this->assertEmpty($layout->renderElement('nonexisting_element'));
91 
92  $this->expectExceptionMessage(
93  'The element with the "nonexisting_element" ID wasn\'t found. Verify the ID and try again.'
94  );
95  }
96 
103  public function testGetBlockUnscheduled()
104  {
105  $layout = $this->_getLayoutModel('get_block.xml');
106  $this->assertInstanceOf(\Magento\Framework\View\Element\Text::class, $layout->getBlock('block_first'));
107  $this->assertInstanceOf(\Magento\Framework\View\Element\Text::class, $layout->getBlock('block_second'));
108  }
109 
111  {
112  $layout = $this->_getLayoutModel('arguments.xml');
113  $this->assertEquals('1', $layout->getBlock('block_with_args')->getOne());
114  $this->assertEquals('two', $layout->getBlock('block_with_args')->getTwo());
115  $this->assertEquals('3', $layout->getBlock('block_with_args')->getThree());
116  }
117 
119  {
120  $layout = $this->_getLayoutModel('arguments_complex_values.xml');
121  $this->assertEquals(
122  ['parameters' => ['first' => '1', 'second' => '2']],
123  $layout->getBlock('block_with_args_complex_values')->getOne()
124  );
125  $this->assertEquals('two', $layout->getBlock('block_with_args_complex_values')->getTwo());
126  $this->assertEquals(
127  ['extra' => ['key1' => 'value1', 'key2' => 'value2']],
128  $layout->getBlock('block_with_args_complex_values')->getThree()
129  );
130  }
131 
133  {
134  $layout = $this->_getLayoutModel('arguments_object_type.xml');
135  $this->assertInstanceOf(
136  \Magento\Framework\Data\Collection::class,
137  $layout->getBlock('block_with_object_args')->getOne()
138  );
139  $this->assertInstanceOf(
140  \Magento\Framework\Data\Collection::class,
141  $layout->getBlock('block_with_object_args')->getTwo()
142  );
143  $this->assertEquals(3, $layout->getBlock('block_with_object_args')->getThree());
144  }
145 
147  {
148  $layout = $this->_getLayoutModel('arguments_url_type.xml');
149  $this->assertContains('customer/account/login', $layout->getBlock('block_with_url_args')->getOne());
150  $this->assertContains('customer/account/logout', $layout->getBlock('block_with_url_args')->getTwo());
151  $this->assertContains('customer_id/3', $layout->getBlock('block_with_url_args')->getTwo());
152  }
153 
155  {
156  $this->markTestSkipped('Will be fixed after MAGETWO-33840 will be done');
157  $layout = $this->_getLayoutModel('arguments_object_type_updaters.xml');
158 
159  $expectedObjectData = [0 => 'updater call', 1 => 'updater call'];
160 
161  $expectedSimpleData = 1;
162 
163  $dataSource = $layout->getBlock('block_with_object_updater_args')->getOne();
164  $this->assertInstanceOf(\Magento\Framework\Data\Collection::class, $dataSource);
165  $this->assertEquals($expectedObjectData, $dataSource->getUpdaterCall());
166  $this->assertEquals($expectedSimpleData, $layout->getBlock('block_with_object_updater_args')->getTwo());
167  }
168 
172  public function testMoveSameAlias()
173  {
174  $layout = $this->_getLayoutModel('move_the_same_alias.xml');
175  $this->assertEquals('container1', $layout->getParentName('no_name3'));
176  }
177 
181  public function testMoveNewAlias()
182  {
183  $layout = $this->_getLayoutModel('move_new_alias.xml');
184  $this->assertEquals('new_alias', $layout->getElementAlias('no_name3'));
185  }
186 
188  {
189  $layout = $this->_getLayoutModel('action_for_anonymous_parent_block.xml');
190  $this->assertEquals('schedule_block0', $layout->getParentName('test.block.insert'));
191  $this->assertEquals('schedule_block1', $layout->getParentName('test.block.append'));
192  }
193 
197  public function testRemove()
198  {
199  $layout = $this->_getLayoutModel('remove.xml');
200  $this->assertFalse($layout->getBlock('no_name2'));
201  $this->assertFalse($layout->getBlock('child_block1'));
202  $this->assertTrue($layout->isBlock('child_block2'));
203  }
204 
208  public function testRemoveCancellation()
209  {
210  $layout = $this->_getLayoutModel('remove_cancellation.xml');
211  $this->assertTrue($layout->isContainer('container1'));
212  $this->assertTrue($layout->isBlock('child_block1'));
213  $this->assertTrue($layout->isBlock('no_name2'));
214  $this->assertFalse($layout->getBlock('not_exist'));
215  }
216 
220  public function testMove()
221  {
222  $layout = $this->_getLayoutModel('move.xml');
223  $this->assertEquals('container2', $layout->getParentName('container1'));
224  $this->assertEquals('container1', $layout->getParentName('no.name2'));
225  $this->assertEquals('block_container', $layout->getParentName('no_name3'));
226 
227  // verify `after` attribute
228  $this->assertEquals('block_container', $layout->getParentName('no_name'));
229  $childrenOrderArray = array_keys($layout->getChildBlocks($layout->getParentName('no_name')));
230  $positionAfter = array_search('child_block1', $childrenOrderArray);
231  $positionToVerify = array_search('no_name', $childrenOrderArray);
232  $this->assertEquals($positionAfter, --$positionToVerify);
233 
234  // verify `before` attribute
235  $this->assertEquals('block_container', $layout->getParentName('no_name4'));
236  $childrenOrderArray = array_keys($layout->getChildBlocks($layout->getParentName('no_name4')));
237  $positionBefore = array_search('child_block2', $childrenOrderArray);
238  $positionToVerify = array_search('no_name4', $childrenOrderArray);
239  $this->assertEquals($positionBefore, ++$positionToVerify);
240  }
241 
245  public function testMoveBroken()
246  {
247  $this->_getLayoutModel('move_broken.xml');
248  }
249 
253  public function testMoveAliasBroken()
254  {
255  $this->_getLayoutModel('move_alias_broken.xml');
256  }
257 
258  public function testRemoveBroken()
259  {
260  if ($this->state->getMode() === State::MODE_DEVELOPER) {
261  $this->expectException('OutOfBoundsException');
262  }
263  $this->_getLayoutModel('remove_broken.xml');
264  }
265 
272  public function testSortSpecialCases($case, $expectedResult)
273  {
274  $layout = $this->_getLayoutModel($case);
275  $this->assertEquals($expectedResult, $layout->renderElement('root'));
276  }
277 
282  {
283  return [
284  'Before element which is after' => ['sort_before_after.xml', '312'],
285  'Before element which is previous' => ['sort_before_before.xml', '213'],
286  'After element which is after' => ['sort_after_after.xml', '312'],
287  'After element which is previous' => ['sort_after_previous.xml', '321']
288  ];
289  }
290 
295  public function testIfConfigForBlock()
296  {
297  $layout = $this->_getLayoutModel('ifconfig.xml');
298  $this->assertFalse($layout->getBlock('block1'));
299  $this->assertFalse($layout->getBlock('block2'));
300  $this->assertInstanceOf(\Magento\Framework\View\Element\BlockInterface::class, $layout->getBlock('block3'));
301  $this->assertFalse($layout->getBlock('block4'));
302  }
303 
308  public function testBlockGroups()
309  {
310  $layout = $this->_getLayoutModel('group.xml');
311  $childNames = $layout->getBlock('block1')->getGroupChildNames('group1');
312  $this->assertEquals(['block2', 'block3'], $childNames);
313  }
314 }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$case