Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
LayoutTest Class Reference
Inheritance diagram for LayoutTest:

Public Member Functions

 testDestructor ()
 
 testGetUpdate ()
 
 testGenerateGetAllBlocks ()
 
 testGetElementProperty ()
 
 testIsBlock ()
 
 testSetUnsetBlock ()
 
 testCreateBlock ($blockType, $blockName, array $blockData, $expectedName)
 
 createBlockDataProvider ()
 
 testCreateBlockNotExists ($name)
 
 blockNotExistsDataProvider ()
 
 testAddBlock ()
 
 testAddContainer ($htmlTag)
 
 addContainerDataProvider ()
 
 testAddContainerInvalidHtmlTag ()
 
 testGetChildBlock ()
 
 testSetChild ()
 
 testReorderChild (\Magento\Framework\View\LayoutInterface $layout)
 
 testGetChildBlocks ()
 
 testAddBlockInvalidType ()
 
 testIsContainer ()
 
 testIsManipulationAllowed ()
 
 testRenameElement ()
 
 testGetBlock ()
 
 testGetParentName ()
 
 testGetElementAlias ()
 
 testGetOutput ()
 
 testGetMessagesBlock ()
 
 testGetBlockSingleton ()
 
 testUpdateContainerAttributes ()
 
 testIsCacheable ()
 
 testIsNonCacheable ()
 

Protected Member Functions

 setUp ()
 

Protected Attributes

 $_layout
 
 $layoutFactory
 

Detailed Description

@SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 19 of file LayoutTest.php.

Member Function Documentation

◆ addContainerDataProvider()

addContainerDataProvider ( )

Definition at line 253 of file LayoutTest.php.

254  {
255  return [
256  ['dd'],
257  ['div'],
258  ['dl'],
259  ['fieldset'],
260  ['header'],
261  ['ol'],
262  ['p'],
263  ['section'],
264  ['table'],
265  ['tfoot'],
266  ['ul']
267  ];
268  }

◆ blockNotExistsDataProvider()

blockNotExistsDataProvider ( )

Definition at line 215 of file LayoutTest.php.

216  {
217  return [[''], ['block_not_exists']];
218  }

◆ createBlockDataProvider()

createBlockDataProvider ( )

Definition at line 190 of file LayoutTest.php.

191  {
192  return [
193  'named block' => [\Magento\Framework\View\Element\Template::class,
194  'some_block_name_full_class',
195  ['type' => \Magento\Framework\View\Element\Template::class, 'is_anonymous' => false],
196  '/^some_block_name_full_class$/',
197  ],
198  'no name block' => [\Magento\Framework\View\Element\Text\ListText::class,
199  '',
200  ['type' => \Magento\Framework\View\Element\Text\ListText::class, 'key1' => 'value1'],
201  '/text\\\\list/',
202  ]
203  ];
204  }

◆ setUp()

setUp ( )
protected

Definition at line 31 of file LayoutTest.php.

32  {
34  $this->layoutFactory = $objectManager->get(\Magento\Framework\View\LayoutFactory::class);
35  $this->_layout = $this->layoutFactory->create();
36  $objectManager->get(\Magento\Framework\App\Cache\Type\Layout::class)->clean();
37  }
$objectManager
Definition: bootstrap.php:17

◆ testAddBlock()

testAddBlock ( )

Definition at line 220 of file LayoutTest.php.

221  {
222  $this->assertInstanceOf(
223  \Magento\Framework\View\Element\Text::class,
224  $this->_layout->addBlock(\Magento\Framework\View\Element\Text::class, 'block1')
225  );
227  \Magento\Framework\View\Element\Text::class
228  );
229  $block2->setNameInLayout('block2');
230  $this->_layout->addBlock($block2, '', 'block1');
231 
232  $this->assertTrue($this->_layout->hasElement('block1'));
233  $this->assertTrue($this->_layout->hasElement('block2'));
234  $this->assertEquals('block1', $this->_layout->getParentName('block2'));
235  }

◆ testAddBlockInvalidType()

testAddBlockInvalidType ( )

@expectedException \Magento\Framework\Exception\LocalizedException

Definition at line 366 of file LayoutTest.php.

367  {
368  $this->_layout->addBlock('invalid_name', 'child');
369  }

◆ testAddContainer()

testAddContainer (   $htmlTag)

@magentoAppIsolation enabled @dataProvider addContainerDataProvider()

Definition at line 241 of file LayoutTest.php.

242  {
243  $this->assertFalse($this->_layout->hasElement('container'));
244  $this->_layout->addContainer('container', 'Container', ['htmlTag' => $htmlTag]);
245  $this->assertTrue($this->_layout->hasElement('container'));
246  $this->assertTrue($this->_layout->isContainer('container'));
247  $this->assertEquals($htmlTag, $this->_layout->getElementProperty('container', 'htmlTag'));
248 
249  $this->_layout->addContainer('container1', 'Container 1', [], 'container', 'c1');
250  $this->assertEquals('container1', $this->_layout->getChildName('container', 'c1'));
251  }

◆ testAddContainerInvalidHtmlTag()

testAddContainerInvalidHtmlTag ( )

@magentoAppIsolation enabled

Definition at line 273 of file LayoutTest.php.

274  {
275  $msg = 'Html tag "span" is forbidden for usage in containers. ' .
276  'Consider to use one of the allowed: aside, dd, div, dl, fieldset, main, nav, ' .
277  'header, footer, ol, p, section, table, tfoot, ul.';
278  $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
279  $this->expectExceptionMessage($msg);
280  $this->_layout->addContainer('container', 'Container', ['htmlTag' => 'span']);
281  }

◆ testCreateBlock()

testCreateBlock (   $blockType,
  $blockName,
array  $blockData,
  $expectedName 
)

@dataProvider createBlockDataProvider

Definition at line 180 of file LayoutTest.php.

181  {
182  $expectedData = $blockData + ['type' => $blockType];
183 
184  $block = $this->_layout->createBlock($blockType, $blockName, ['data' => $blockData]);
185 
186  $this->assertRegExp($expectedName, $block->getNameInLayout());
187  $this->assertEquals($expectedData, $block->getData());
188  }
$block
Definition: block.php:8

◆ testCreateBlockNotExists()

testCreateBlockNotExists (   $name)

@dataProvider blockNotExistsDataProvider @expectedException \Magento\Framework\Exception\LocalizedException

Definition at line 210 of file LayoutTest.php.

211  {
212  $this->_layout->createBlock($name);
213  }
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ testDestructor()

testDestructor ( )

@magentoAppIsolation enabled @magentoAppArea frontend

Definition at line 53 of file LayoutTest.php.

54  {
55  $this->_layout->addBlock(\Magento\Framework\View\Element\Text::class, 'test');
56  $this->assertNotEmpty($this->_layout->getAllBlocks());
57  $this->_layout->__destruct();
58  $this->assertEmpty($this->_layout->getAllBlocks());
59  }

◆ testGenerateGetAllBlocks()

testGenerateGetAllBlocks ( )

A smoke test for generating elements

See sophisticated tests at \Magento\Framework\View\LayoutDirectivesTest

See also
\Magento\Framework\View\LayoutDirectivesTest @magentoAppIsolation enabled

Definition at line 101 of file LayoutTest.php.

102  {
103  $this->_layout->setXml(
104  simplexml_load_string(
105  '<layout>
106  <block class="Magento\Framework\View\Element\Text" name="block1">
107  <block class="Magento\Framework\View\Element\Text"/>
108  </block>
109  <block class="Magento\Framework\View\Element\Text" template="test" ttl="360"/>
110  <block class="Magento\Framework\View\Element\Text"/>
111  </layout>',
112  \Magento\Framework\View\Layout\Element::class
113  )
114  );
115  $this->assertEquals([], $this->_layout->getAllBlocks());
116  $this->_layout->generateElements();
117  $expected = ['block1', 'block1_schedule_block0', 'schedule_block1', 'schedule_block2'];
118  $this->assertSame($expected, array_keys($this->_layout->getAllBlocks()));
119  $child = $this->_layout->getBlock('block1_schedule_block0');
120  $this->assertSame($this->_layout->getBlock('block1'), $child->getParentBlock());
121  $this->assertEquals('test', $this->_layout->getBlock('schedule_block1')->getData('template'));
122  $this->assertEquals('360', $this->_layout->getBlock('schedule_block1')->getData('ttl'));
123  $this->assertFalse($this->_layout->getBlock('nonexisting'));
124  }

◆ testGetBlock()

testGetBlock ( )

Definition at line 418 of file LayoutTest.php.

419  {
420  $this->assertFalse($this->_layout->getBlock('test'));
422  \Magento\Framework\View\Layout::class
423  )->createBlock(
424  \Magento\Framework\View\Element\Text::class
425  );
426  $this->_layout->setBlock('test', $block);
427  $this->assertSame($block, $this->_layout->getBlock('test'));
428  }
$block
Definition: block.php:8

◆ testGetBlockSingleton()

testGetBlockSingleton ( )

Definition at line 476 of file LayoutTest.php.

477  {
478  $block = $this->_layout->getBlockSingleton(\Magento\Framework\View\Element\Text::class);
479  $this->assertInstanceOf(\Magento\Framework\View\Element\Text::class, $block);
480  $this->assertSame($block, $this->_layout->getBlockSingleton(\Magento\Framework\View\Element\Text::class));
481  }
$block
Definition: block.php:8

◆ testGetChildBlock()

testGetChildBlock ( )

@magentoAppIsolation enabled

Definition at line 286 of file LayoutTest.php.

287  {
288  $this->_layout->addContainer('parent', 'Parent');
289  $block = $this->_layout->addBlock(
290  \Magento\Framework\View\Element\Text::class,
291  'block',
292  'parent',
293  'block_alias'
294  );
295  $this->_layout->addContainer('container', 'Container', [], 'parent', 'container_alias');
296  $this->assertSame($block, $this->_layout->getChildBlock('parent', 'block_alias'));
297  $this->assertFalse($this->_layout->getChildBlock('parent', 'container_alias'));
298  }
$block
Definition: block.php:8

◆ testGetChildBlocks()

testGetChildBlocks ( )

@magentoAppIsolation enabled

Definition at line 354 of file LayoutTest.php.

355  {
356  $this->_layout->addContainer('parent', 'Parent');
357  $block1 = $this->_layout->addBlock(\Magento\Framework\View\Element\Text::class, 'block1', 'parent');
358  $this->_layout->addContainer('container', 'Container', [], 'parent');
359  $block2 = $this->_layout->addBlock(\Magento\Framework\View\Element\Template::class, 'block2', 'parent');
360  $this->assertSame(['block1' => $block1, 'block2' => $block2], $this->_layout->getChildBlocks('parent'));
361  }

◆ testGetElementAlias()

testGetElementAlias ( )

Definition at line 441 of file LayoutTest.php.

442  {
443  $this->_layout->addContainer('one', 'One');
444  $this->_layout->addContainer('two', 'One', [], 'one', '1');
445  $this->assertFalse($this->_layout->getElementAlias('one'));
446  $this->assertEquals('1', $this->_layout->getElementAlias('two'));
447  }

◆ testGetElementProperty()

testGetElementProperty ( )

Definition at line 126 of file LayoutTest.php.

127  {
128  $name = 'test';
129  $this->_layout->addContainer($name, 'Test', ['option1' => 1, 'option2' => 2]);
130  $this->assertEquals(
131  'Test',
132  $this->_layout->getElementProperty($name, \Magento\Framework\View\Layout\Element::CONTAINER_OPT_LABEL)
133  );
134  $this->assertEquals(
135  \Magento\Framework\View\Layout\Element::TYPE_CONTAINER,
136  $this->_layout->getElementProperty($name, 'type')
137  );
138  $this->assertSame(2, $this->_layout->getElementProperty($name, 'option2'));
139 
140  $this->_layout->addBlock(\Magento\Framework\View\Element\Text::class, 'text', $name);
141  $this->assertEquals(
142  \Magento\Framework\View\Layout\Element::TYPE_BLOCK,
143  $this->_layout->getElementProperty('text', 'type')
144  );
145  $this->assertSame(
146  ['text' => 'text'],
147  $this->_layout->getElementProperty($name, \Magento\Framework\Data\Structure::CHILDREN)
148  );
149  }
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ testGetMessagesBlock()

testGetMessagesBlock ( )

Definition at line 471 of file LayoutTest.php.

472  {
473  $this->assertInstanceOf(\Magento\Framework\View\Element\Messages::class, $this->_layout->getMessagesBlock());
474  }

◆ testGetOutput()

testGetOutput ( )

@covers \Magento\Framework\View\Layout::addOutputElement @covers \Magento\Framework\View\Layout::getOutput @covers \Magento\Framework\View\Layout::removeOutputElement

Definition at line 454 of file LayoutTest.php.

455  {
456  $blockName = 'block_' . __METHOD__;
457  $expectedText = "some_text_for_{$blockName}";
458 
459  $block = $this->_layout->addBlock(\Magento\Framework\View\Element\Text::class, $blockName);
460  $block->setText($expectedText);
461 
462  $this->_layout->addOutputElement($blockName);
463  // add the same element twice should not produce output duplicate
464  $this->_layout->addOutputElement($blockName);
465  $this->assertEquals($expectedText, $this->_layout->getOutput());
466 
467  $this->_layout->removeOutputElement($blockName);
468  $this->assertEmpty($this->_layout->getOutput());
469  }
$block
Definition: block.php:8

◆ testGetParentName()

testGetParentName ( )

@magentoAppIsolation enabled

Definition at line 433 of file LayoutTest.php.

434  {
435  $this->_layout->addContainer('one', 'One');
436  $this->_layout->addContainer('two', 'Two', [], 'one');
437  $this->assertFalse($this->_layout->getParentName('one'));
438  $this->assertEquals('one', $this->_layout->getParentName('two'));
439  }

◆ testGetUpdate()

testGetUpdate ( )

Definition at line 61 of file LayoutTest.php.

62  {
63  $this->assertInstanceOf(\Magento\Framework\View\Layout\ProcessorInterface::class, $this->_layout->getUpdate());
64  }

◆ testIsBlock()

testIsBlock ( )

@magentoAppIsolation enabled

Definition at line 154 of file LayoutTest.php.

155  {
156  $this->assertFalse($this->_layout->isBlock('container'));
157  $this->assertFalse($this->_layout->isBlock('block'));
158  $this->_layout->addContainer('container', 'Container');
159  $this->_layout->addBlock(\Magento\Framework\View\Element\Text::class, 'block');
160  $this->assertFalse($this->_layout->isBlock('container'));
161  $this->assertTrue($this->_layout->isBlock('block'));
162  }

◆ testIsCacheable()

testIsCacheable ( )

Definition at line 498 of file LayoutTest.php.

499  {
500  $this->_layout->setXml(
501  simplexml_load_file(
502  __DIR__ . '/_files/layout/cacheable.xml',
503  \Magento\Framework\View\Layout\Element::class
504  )
505  );
506  $this->_layout->generateElements();
507  $this->assertTrue($this->_layout->isCacheable());
508  }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60

◆ testIsContainer()

testIsContainer ( )

@magentoAppIsolation enabled

Definition at line 374 of file LayoutTest.php.

375  {
376  $block = 'block';
377  $container = 'container';
378  $this->_layout->addBlock(\Magento\Framework\View\Element\Text::class, $block);
379  $this->_layout->addContainer($container, 'Container');
380  $this->assertFalse($this->_layout->isContainer($block));
381  $this->assertTrue($this->_layout->isContainer($container));
382  $this->assertFalse($this->_layout->isContainer('invalid_name'));
383  }
$block
Definition: block.php:8

◆ testIsManipulationAllowed()

testIsManipulationAllowed ( )

Definition at line 385 of file LayoutTest.php.

386  {
387  $this->_layout->addBlock(\Magento\Framework\View\Element\Text::class, 'block1');
388  $this->_layout->addBlock(\Magento\Framework\View\Element\Text::class, 'block2', 'block1');
389  $this->assertFalse($this->_layout->isManipulationAllowed('block1'));
390  $this->assertFalse($this->_layout->isManipulationAllowed('block2'));
391 
392  $this->_layout->addContainer('container1', 'Container 1');
393  $this->_layout->addBlock(\Magento\Framework\View\Element\Text::class, 'block3', 'container1');
394  $this->_layout->addContainer('container2', 'Container 2', [], 'container1');
395  $this->assertFalse($this->_layout->isManipulationAllowed('container1'));
396  $this->assertTrue($this->_layout->isManipulationAllowed('block3'));
397  $this->assertTrue($this->_layout->isManipulationAllowed('container2'));
398  }

◆ testIsNonCacheable()

testIsNonCacheable ( )

Definition at line 510 of file LayoutTest.php.

511  {
512  $this->_layout->setXml(
513  simplexml_load_file(
514  __DIR__ . '/_files/layout/non_cacheable.xml',
515  \Magento\Framework\View\Layout\Element::class
516  )
517  );
518  $this->_layout->generateElements();
519  $this->assertFalse($this->_layout->isCacheable());
520  }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60

◆ testRenameElement()

testRenameElement ( )

Definition at line 400 of file LayoutTest.php.

401  {
402  $blockName = 'block';
403  $expBlockName = 'block_renamed';
404  $containerName = 'container';
405  $expContainerName = 'container_renamed';
406  $block = $this->_layout->createBlock(\Magento\Framework\View\Element\Text::class, $blockName);
407  $this->_layout->addContainer($containerName, 'Container');
408 
409  $this->assertEquals($block, $this->_layout->getBlock($blockName));
410  $this->_layout->renameElement($blockName, $expBlockName);
411  $this->assertEquals($block, $this->_layout->getBlock($expBlockName));
412 
413  $this->_layout->hasElement($containerName);
414  $this->_layout->renameElement($containerName, $expContainerName);
415  $this->_layout->hasElement($expContainerName);
416  }
$block
Definition: block.php:8

◆ testReorderChild()

testReorderChild ( \Magento\Framework\View\LayoutInterface  $layout)
Parameters
\Magento\Framework\View\LayoutInterface$layout@depends testSetChild

Definition at line 318 of file LayoutTest.php.

319  {
320  $layout->addContainer('four', 'Four', [], 'one');
321 
322  // offset +1
323  $layout->reorderChild('one', 'four', 1);
324  $this->assertSame(['two', 'four', 'three'], $layout->getChildNames('one'));
325 
326  // offset -2
327  $layout->reorderChild('one', 'three', 2, false);
328  $this->assertSame(['two', 'three', 'four'], $layout->getChildNames('one'));
329 
330  // after sibling
331  $layout->reorderChild('one', 'two', 'three');
332  $this->assertSame(['three', 'two', 'four'], $layout->getChildNames('one'));
333 
334  // after everyone
335  $layout->reorderChild('one', 'three', '-');
336  $this->assertSame(['two', 'four', 'three'], $layout->getChildNames('one'));
337 
338  // before sibling
339  $layout->reorderChild('one', 'four', 'two', false);
340  $this->assertSame(['four', 'two', 'three'], $layout->getChildNames('one'));
341 
342  // before everyone
343  $layout->reorderChild('one', 'two', '-', false);
344  $this->assertSame(['two', 'four', 'three'], $layout->getChildNames('one'));
345 
346  //reorder by sibling alias
347  $layout->reorderChild('one', 'two', 'three_alias', true);
348  $this->assertSame(['four', 'three', 'two'], $layout->getChildNames('one'));
349  }

◆ testSetChild()

testSetChild ( )
Returns
\Magento\Framework\View\Layout

Definition at line 303 of file LayoutTest.php.

304  {
305  $this->_layout->addContainer('one', 'One');
306  $this->_layout->addContainer('two', 'Two');
307  $this->_layout->addContainer('three', 'Three');
308  $this->assertSame($this->_layout, $this->_layout->setChild('one', 'two', ''));
309  $this->_layout->setChild('one', 'three', 'three_alias');
310  $this->assertSame(['two', 'three'], $this->_layout->getChildNames('one'));
311  return $this->_layout;
312  }

◆ testSetUnsetBlock()

testSetUnsetBlock ( )

Definition at line 164 of file LayoutTest.php.

165  {
166  $expectedBlockName = 'block_' . __METHOD__;
167  $expectedBlock = $this->_layout->createBlock(\Magento\Framework\View\Element\Text::class);
168 
169  $this->_layout->setBlock($expectedBlockName, $expectedBlock);
170  $this->assertSame($expectedBlock, $this->_layout->getBlock($expectedBlockName));
171 
172  $this->_layout->unsetElement($expectedBlockName);
173  $this->assertFalse($this->_layout->getBlock($expectedBlockName));
174  $this->assertFalse($this->_layout->hasElement($expectedBlockName));
175  }

◆ testUpdateContainerAttributes()

testUpdateContainerAttributes ( )

Definition at line 483 of file LayoutTest.php.

484  {
485  $this->_layout->setXml(
486  simplexml_load_file(
487  __DIR__ . '/_files/layout/container_attributes.xml',
488  \Magento\Framework\View\Layout\Element::class
489  )
490  );
491  $this->_layout->generateElements();
492  $result = $this->_layout->renderElement('container1', false);
493  $this->assertEquals('<div id="container1-2" class="class12">Test11Test12</div>', $result);
494  $result = $this->_layout->renderElement('container2', false);
495  $this->assertEquals('<div id="container2-2" class="class22">Test21Test22</div>', $result);
496  }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60

Field Documentation

◆ $_layout

$_layout
protected

Definition at line 24 of file LayoutTest.php.

◆ $layoutFactory

$layoutFactory
protected

Definition at line 29 of file LayoutTest.php.


The documentation for this class was generated from the following file: