Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LayoutTest.php
Go to the documentation of this file.
1 <?php
7 
9 
14 class LayoutTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $model;
20 
24  protected $structureMock;
25 
30 
34  protected $themeResolverMock;
35 
39  protected $processorMock;
40 
44  protected $eventManagerMock;
45 
50 
55 
59  protected $cacheMock;
60 
64  protected $readerPoolMock;
65 
69  protected $generatorPoolMock;
70 
75 
80 
84  protected $readerContextMock;
85 
89  private $pageConfigStructure;
90 
94  private $layoutScheduledSructure;
95 
100 
104  protected $appStateMock;
105 
109  protected $loggerMock;
110 
114  private $serializer;
115 
116  protected function setUp()
117  {
118  $this->structureMock = $this->getMockBuilder(\Magento\Framework\View\Layout\Data\Structure::class)
119  ->disableOriginalConstructor()
120  ->getMock();
121  $this->processorFactoryMock = $this->createPartialMock(
122  \Magento\Framework\View\Layout\ProcessorFactory::class,
123  ['create']
124  );
125  $this->themeResolverMock = $this->getMockForAbstractClass(
126  \Magento\Framework\View\Design\Theme\ResolverInterface::class
127  );
128  $this->processorMock = $this->createMock(\Magento\Framework\View\Model\Layout\Merge::class);
129  $this->eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
130  $this->generatorBlockMock = $this->getMockBuilder(\Magento\Framework\View\Layout\Generator\Block::class)
131  ->disableOriginalConstructor()->getMock();
132  $this->generatorContainerMock = $this->getMockBuilder(\Magento\Framework\View\Layout\Generator\Container::class)
133  ->disableOriginalConstructor()->getMock();
134  $this->cacheMock = $this->getMockBuilder(\Magento\Framework\Cache\FrontendInterface::class)
135  ->disableOriginalConstructor()
136  ->getMock();
137  $this->readerPoolMock = $this->getMockBuilder(\Magento\Framework\View\Layout\ReaderPool::class)
138  ->disableOriginalConstructor()
139  ->getMock();
140  $this->messageManagerMock = $this->getMockBuilder(\Magento\Framework\Message\ManagerInterface::class)
141  ->disableOriginalConstructor()
142  ->getMock();
143 
144  $this->generatorPoolMock = $this->getMockBuilder(\Magento\Framework\View\Layout\GeneratorPool::class)
145  ->disableOriginalConstructor()->getMock();
146  $this->generatorPoolMock->expects($this->any())
147  ->method('getGenerator')
148  ->will(
149  $this->returnValueMap([
150  [\Magento\Framework\View\Layout\Generator\Block::TYPE, $this->generatorBlockMock],
151  [\Magento\Framework\View\Layout\Generator\Container::TYPE, $this->generatorContainerMock],
152  ])
153  );
154 
155  $this->readerContextFactoryMock = $this->getMockBuilder(
156  \Magento\Framework\View\Layout\Reader\ContextFactory::class
157  )->disableOriginalConstructor()->getMock();
158 
159  $this->pageConfigStructure = $this->getMockBuilder(\Magento\Framework\View\Page\Config\Structure::class)
160  ->setMethods(['__toArray', 'populateWithArray'])
161  ->getMock();
162  $this->layoutScheduledSructure = $this->getMockBuilder(\Magento\Framework\View\Layout\ScheduledStructure::class)
163  ->setMethods(['__toArray', 'populateWithArray'])
164  ->getMock();
165  $this->readerContextMock = $this->getMockBuilder(\Magento\Framework\View\Layout\Reader\Context::class)
166  ->setMethods(['getPageConfigStructure', 'getScheduledStructure'])
167  ->disableOriginalConstructor()
168  ->getMock();
169  $this->readerContextMock->expects($this->any())->method('getPageConfigStructure')
170  ->willReturn($this->pageConfigStructure);
171  $this->readerContextMock->expects($this->any())->method('getScheduledStructure')
172  ->willReturn($this->layoutScheduledSructure);
173 
174  $this->generatorContextFactoryMock = $this->getMockBuilder(
175  \Magento\Framework\View\Layout\Generator\ContextFactory::class
176  )
177  ->disableOriginalConstructor()
178  ->getMock();
179  $this->appStateMock = $this->getMockBuilder(\Magento\Framework\App\State::class)
180  ->disableOriginalConstructor()
181  ->getMock();
182  $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
183  ->getMock();
184  $this->serializer = $this->createMock(\Magento\Framework\Serialize\SerializerInterface::class);
185  $this->serializer->expects($this->any())->method('serialize')
186  ->willReturnCallback(function ($value) {
187  return json_encode($value);
188  });
189  $this->serializer->expects($this->any())->method('unserialize')
190  ->willReturnCallback(function ($value) {
191  return json_decode($value, true);
192  });
193 
194  $this->model = new \Magento\Framework\View\Layout(
195  $this->processorFactoryMock,
196  $this->eventManagerMock,
197  $this->structureMock,
198  $this->messageManagerMock,
199  $this->themeResolverMock,
200  $this->readerPoolMock,
201  $this->generatorPoolMock,
202  $this->cacheMock,
203  $this->readerContextFactoryMock,
204  $this->generatorContextFactoryMock,
205  $this->appStateMock,
206  $this->loggerMock,
207  true,
208  $this->serializer
209  );
210  }
211 
212  public function testCreateBlockSuccess()
213  {
214  $blockMock = $this->getMockBuilder(\Magento\Framework\View\Element\AbstractBlock::class)
215  ->disableOriginalConstructor()
216  ->getMockForAbstractClass();
217  $this->structureMock->expects($this->once())
218  ->method('createStructuralElement')
219  ->with(
220  'blockname',
221  \Magento\Framework\View\Layout\Element::TYPE_BLOCK,
222  \Magento\Framework\View\Element\AbstractBlock::class
223  )->willReturn('blockname');
224  $this->generatorBlockMock->expects($this->once())->method('createBlock')->will($this->returnValue($blockMock));
225 
226  $this->model->createBlock(\Magento\Framework\View\Element\AbstractBlock::class, 'blockname', []);
227  $this->assertInstanceOf(
228  \Magento\Framework\View\Element\AbstractBlock::class,
229  $this->model->getBlock('blockname')
230  );
231  $this->assertFalse($this->model->getBlock('not_exist'));
232  }
233 
234  public function testGetUpdate()
235  {
236  $themeMock = $this->getMockForAbstractClass(\Magento\Framework\View\Design\ThemeInterface::class);
237 
238  $this->themeResolverMock->expects($this->once())
239  ->method('get')
240  ->will($this->returnValue($themeMock));
241 
242  $this->processorFactoryMock->expects($this->once())
243  ->method('create')
244  ->with(['theme' => $themeMock])
245  ->will($this->returnValue($this->processorMock));
246 
247  $this->assertEquals($this->processorMock, $this->model->getUpdate());
248  $this->assertEquals($this->processorMock, $this->model->getUpdate());
249  }
250 
251  public function testGenerateXml()
252  {
253  $themeMock = $this->getMockForAbstractClass(\Magento\Framework\View\Design\ThemeInterface::class);
254 
255  $this->themeResolverMock->expects($this->once())
256  ->method('get')
257  ->will($this->returnValue($themeMock));
258 
259  $this->processorFactoryMock->expects($this->once())
260  ->method('create')
261  ->with(['theme' => $themeMock])
262  ->will($this->returnValue($this->processorMock));
263 
264  $xmlString = '<?xml version="1.0"?><layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
265  . '<some_update>123</some_update></layout>';
266  $xml = simplexml_load_string($xmlString, \Magento\Framework\View\Layout\Element::class);
267  $this->processorMock->expects($this->once())
268  ->method('asSimplexml')
269  ->will($this->returnValue($xml));
270 
271  $this->structureMock->expects($this->once())
272  ->method('importElements')
273  ->with($this->equalTo([]))
274  ->will($this->returnSelf());
275  $this->assertSame($this->model, $this->model->generateXml());
276  $this->assertSame('<some_update>123</some_update>', $this->model->getNode('some_update')->asXML());
277  }
278 
279  public function testGetChildBlock()
280  {
281  $customBlockName = 'custom_block';
282  $customBlockParentName = 'custom_block_parent';
283  $customBlockAlias = 'custom_block_alias';
284 
285  $blockMock = $this->getMockBuilder(\Magento\Framework\View\Element\Template::class)
286  ->disableOriginalConstructor()
287  ->getMock();
288 
289  $this->structureMock->expects($this->once())
290  ->method('getChildId')
291  ->with($customBlockParentName, $customBlockAlias)
292  ->willReturn($customBlockName);
293 
294  $this->structureMock->expects($this->once())
295  ->method('hasElement')
296  ->with($customBlockName)
297  ->willReturn(true);
298 
299  $this->structureMock->expects($this->once())
300  ->method('getAttribute')
301  ->with($customBlockName, 'type')
302  ->willReturn(\Magento\Framework\View\Layout\Element::TYPE_BLOCK);
303 
304  $this->model->setBlock($customBlockName, $blockMock);
305  $this->assertInstanceOf(
306  \Magento\Framework\View\Element\AbstractBlock::class,
307  $this->model->getChildBlock($customBlockParentName, $customBlockAlias)
308  );
309  }
310 
311  public function testGetChildNonExistBlock()
312  {
313  $this->structureMock->expects($this->once())
314  ->method('getChildId')
315  ->with('non_exist_parent', 'non_exist_alias')
316  ->willReturn(false);
317  $this->assertFalse($this->model->getChildBlock('non_exist_parent', 'non_exist_alias'));
318  }
319 
320  public function testSetChild()
321  {
322  $elementName = 'child';
323  $parentName = 'parent';
324  $alias = 'some_alias';
325  $this->structureMock->expects($this->once())
326  ->method('setAsChild')
327  ->with($this->equalTo($elementName), $this->equalTo($parentName), $this->equalTo($alias))
328  ->will($this->returnSelf());
329  $this->assertSame($this->model, $this->model->setChild($parentName, $elementName, $alias));
330  }
331 
332  public function testUnsetChild()
333  {
334  $parentName = 'parent';
335  $alias = 'some_alias';
336  $this->structureMock->expects($this->once())
337  ->method('unsetChild')
338  ->with($this->equalTo($parentName), $this->equalTo($alias))
339  ->will($this->returnSelf());
340  $this->assertSame($this->model, $this->model->unsetChild($parentName, $alias));
341  }
342 
343  public function testGetChildNames()
344  {
345  $parentName = 'parent';
346  $childrenArray = ['key1' => 'value1', 'key2' => 'value2'];
347  $this->structureMock->expects($this->once())
348  ->method('getChildren')
349  ->with($this->equalTo($parentName))
350  ->will($this->returnValue($childrenArray));
351  $this->assertSame(['key1', 'key2'], $this->model->getChildNames($parentName));
352  }
353 
354  public function testGetChildBlocks()
355  {
356  $parentName = 'parent';
357  $childrenArray = ['block_name' => 'value1'];
358  $this->structureMock->expects($this->once())
359  ->method('getChildren')
360  ->with($this->equalTo($parentName))
361  ->will($this->returnValue($childrenArray));
362 
363  $blockMock = $this->getMockBuilder(\Magento\Framework\View\Element\AbstractBlock::class)
364  ->disableOriginalConstructor()
365  ->getMockForAbstractClass();
366  $this->structureMock->expects($this->once())
367  ->method('createStructuralElement')
368  ->with(
369  'block_name',
370  \Magento\Framework\View\Layout\Element::TYPE_BLOCK,
371  \Magento\Framework\View\Element\AbstractBlock::class
372  )->willReturn('block_name');
373  $this->generatorBlockMock->expects($this->once())->method('createBlock')->will($this->returnValue($blockMock));
374 
375  $this->assertSame(
376  $blockMock,
377  $this->model->createBlock(\Magento\Framework\View\Element\AbstractBlock::class, 'block_name', [])
378  );
379  $this->assertSame(['value1' => $blockMock], $this->model->getChildBlocks($parentName));
380  }
381 
382  public function testGetChildName()
383  {
384  $parentName = 'parent';
385  $alias = 'some_alias';
386  $this->structureMock->expects($this->once())
387  ->method('getChildId')
388  ->with($this->equalTo($parentName), $this->equalTo($alias))
389  ->will($this->returnValue('1'));
390  $this->assertSame('1', $this->model->getChildName($parentName, $alias));
391  }
392 
393  public function testAddToParentGroup()
394  {
395  $blockName = 'block_name';
396  $parentGroup = 'parent_group';
397  $this->structureMock->expects($this->once())
398  ->method('addToParentGroup')
399  ->with($this->equalTo($blockName), $this->equalTo($parentGroup))
400  ->will($this->returnSelf());
401  $this->assertSame($this->structureMock, $this->model->addToParentGroup($blockName, $parentGroup));
402  }
403 
404  public function testGetGroupChildNames()
405  {
406  $blockName = 'block_name';
407  $groupName = 'group_name';
408  $this->structureMock->expects($this->once())
409  ->method('getGroupChildNames')
410  ->with($this->equalTo($blockName), $this->equalTo($groupName))
411  ->will($this->returnSelf());
412  $this->assertSame($this->structureMock, $this->model->getGroupChildNames($blockName, $groupName));
413  }
414 
415  public function testHasElement()
416  {
417  $elementName = 'name';
418  $this->structureMock->expects($this->once())
419  ->method('hasElement')
420  ->with($this->equalTo($elementName))
421  ->will($this->returnValue(true));
422  $this->assertTrue($this->model->hasElement($elementName));
423  }
424 
425  public function testGetElementProperty()
426  {
427  $elementName = 'name';
428  $elementAttr = 'attribute';
429  $result = 'result';
430  $this->structureMock->expects($this->once())
431  ->method('getAttribute')
432  ->with($this->equalTo($elementName), $this->equalTo($elementAttr))
433  ->will($this->returnValue($result));
434  $this->assertSame($result, $this->model->getElementProperty($elementName, $elementAttr));
435  }
436 
443  public function testIsContainer($hasElement, $attribute, $result)
444  {
445  $elementName = 'element_name';
446  $this->structureMock->expects($this->once())
447  ->method('hasElement')
448  ->with($this->equalTo($elementName))
449  ->will($this->returnValue($hasElement));
450  if ($hasElement) {
451  $this->structureMock->expects($this->once())
452  ->method('getAttribute')
453  ->with($this->equalTo($elementName), $this->equalTo('type'))
454  ->will($this->returnValue($attribute));
455  }
456  $this->assertSame($result, $this->model->isContainer($elementName));
457  }
458 
462  public function isContainerDataProvider()
463  {
464  return [
465  [false, '', false],
466  [true, 'container', true],
467  [true, 'block', false],
468  [true, 'something', false],
469  ];
470  }
471 
478  public function testIsManipulationAllowed($parentName, $containerConfig, $result)
479  {
480  $elementName = 'element_name';
481  $this->structureMock->expects($this->once())
482  ->method('getParentId')
483  ->with($this->equalTo($elementName))
484  ->will($this->returnValue($parentName));
485  if ($parentName) {
486  $this->structureMock->expects($this->once())
487  ->method('hasElement')
488  ->with($this->equalTo($parentName))
489  ->will($this->returnValue($containerConfig['has_element']));
490  if ($containerConfig['has_element']) {
491  $this->structureMock->expects($this->once())
492  ->method('getAttribute')
493  ->with($this->equalTo($parentName), $this->equalTo('type'))
494  ->will($this->returnValue($containerConfig['attribute']));
495  }
496  }
497 
498  $this->assertSame($result, $this->model->isManipulationAllowed($elementName));
499  }
500 
505  {
506  return [
507  ['parent', ['has_element' => true, 'attribute' => 'container'], true],
508  ['parent', ['has_element' => true, 'attribute' => 'block'], false],
509  [false, [], false],
510  ];
511  }
512 
518  public function testSetGetBlock()
519  {
520  $blockName = 'some_name';
521  $blockMock = $this->getMockBuilder(\Magento\Framework\View\Element\AbstractBlock::class)
522  ->disableOriginalConstructor()
523  ->getMockForAbstractClass();
524  $this->assertSame($this->model, $this->model->setBlock($blockName, $blockMock));
525  $this->assertSame([$blockName => $blockMock], $this->model->getAllBlocks());
526  $this->structureMock->expects($this->once())
527  ->method('unsetElement')
528  ->with($this->equalTo($blockName))
529  ->will($this->returnSelf());
530  $this->assertSame($this->model, $this->model->unsetElement($blockName));
531  $this->assertSame([], $this->model->getAllBlocks());
532  }
533 
534  public function testRenameElement()
535  {
536  $oldName = 'old_name';
537  $newName = 'new_name';
538  $blockMock = $this->getMockBuilder(\Magento\Framework\View\Element\AbstractBlock::class)
539  ->disableOriginalConstructor()
540  ->getMockForAbstractClass();
541 
542  $this->structureMock->expects($this->once())
543  ->method('renameElement')
544  ->with($this->equalTo($oldName), $this->equalTo($newName))
545  ->will($this->returnSelf());
546  $this->assertSame($this->model, $this->model->setBlock($oldName, $blockMock));
547  $this->assertSame($this->model, $this->model->renameElement($oldName, $newName));
548  $this->assertSame([$newName => $blockMock], $this->model->getAllBlocks());
549  }
550 
551  public function testGetParentName()
552  {
553  $childName = 'child_name';
554  $parentId = 'parent_id';
555  $this->structureMock->expects($this->once())
556  ->method('getParentId')
557  ->with($this->equalTo($childName))
558  ->will($this->returnValue($parentId));
559  $this->assertSame($parentId, $this->model->getParentName($childName));
560  }
561 
562  public function testGetElementAlias()
563  {
564  $name = 'child_name';
565  $parentId = 'parent_id';
566  $alias = 'alias';
567  $this->structureMock->expects($this->once())
568  ->method('getParentId')
569  ->with($this->equalTo($name))
570  ->will($this->returnValue($parentId));
571  $this->structureMock->expects($this->once())
572  ->method('getChildAlias')
573  ->with($this->equalTo($parentId), $this->equalTo($name))
574  ->will($this->returnValue($alias));
575  $this->assertSame($alias, $this->model->getElementAlias($name));
576  }
577 
578  public function testAddRemoveOutputElement()
579  {
580  $this->assertSame($this->model, $this->model->addOutputElement('name'));
581  $this->assertSame($this->model, $this->model->removeOutputElement('name'));
582  }
583 
584  public function testIsPrivate()
585  {
586  $this->assertFalse($this->model->isPrivate());
587  $this->assertSame($this->model, $this->model->setIsPrivate(true));
588  $this->assertTrue($this->model->isPrivate());
589  }
590 
596  public function testGetBlockSingleton($type, $blockInstance, $isAbstract)
597  {
598  $blockMock = $this->createMock($blockInstance);
599  $this->generatorBlockMock->expects($this->once())->method('createBlock')->will($this->returnValue($blockMock));
600 
601  if ($isAbstract) {
602  $blockMock->expects($this->any())
603  ->method('setLayout')
604  ->with($this->equalTo($this->model))
605  ->will($this->returnSelf());
606  }
607  $this->assertInstanceOf($blockInstance, $this->model->getBlockSingleton($type));
608  // singleton test
609  $this->assertInstanceOf($blockInstance, $this->model->getBlockSingleton($type));
610  }
611 
616  {
617  return [
618  [
619  'some_type', \Magento\Framework\View\Element\Template::class,
620  true,
621  ],
622  ];
623  }
624 
631  public function testAddGetRendererOptions($rendererData, $getData, $result)
632  {
633  $this->assertSame(
634  $this->model,
635  $this->model->addAdjustableRenderer(
636  $rendererData['namespace'],
637  $rendererData['static_type'],
638  $rendererData['dynamic_type'],
639  $rendererData['type'],
640  $rendererData['template'],
641  $rendererData['data']
642  )
643  );
644  $this->assertSame(
645  $result,
646  $this->model->getRendererOptions($getData['namespace'], $getData['static_type'], $getData['dynamic_type'])
647  );
648  }
649 
654  {
655  $rendererData = [
656  'namespace' => 'namespace_value',
657  'static_type' => 'static_type_value',
658  'dynamic_type' => 'dynamic_type_value',
659  'type' => 'type_value',
660  'template' => 'template.phtml',
661  'data' => ['some' => 'data'],
662  ];
663  return [
664  'wrong namespace' => [
665  $rendererData,
666  [
667  'namespace' => 'wrong namespace',
668  'static_type' => 'static_type_value',
669  'dynamic_type' => 'dynamic_type_value',
670  ],
671  null,
672  ],
673  'wrong static type' => [
674  $rendererData,
675  [
676  'namespace' => 'namespace_value',
677  'static_type' => 'wrong static type',
678  'dynamic_type' => 'dynamic_type_value',
679  ],
680  null,
681  ],
682  'wrong dynamic type' => [
683  $rendererData,
684  [
685  'namespace' => 'namespace_value',
686  'static_type' => 'static_type_value',
687  'dynamic_type' => 'wrong dynamic type',
688  ],
689  null,
690  ],
691  'set and get test' => [
692  $rendererData,
693  [
694  'namespace' => 'namespace_value',
695  'static_type' => 'static_type_value',
696  'dynamic_type' => 'dynamic_type_value',
697  ],
698  [
699  'type' => 'type_value',
700  'template' => 'template.phtml',
701  'data' => ['some' => 'data'],
702  ],
703  ],
704  ];
705  }
706 
712  public function testIsCacheable($xmlString, $result)
713  {
714  $xml = simplexml_load_string($xmlString, \Magento\Framework\View\Layout\Element::class);
715  $this->assertSame($this->model, $this->model->setXml($xml));
716  $this->assertSame($result, $this->model->isCacheable());
717  }
718 
722  public function isCacheableDataProvider()
723  {
724  return [
725  [
726  '<?xml version="1.0"?><layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
727  . '<block></block></layout>',
728  true,
729  ],
730  [
731  '<?xml version="1.0"?><layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
732  . '<block cacheable="false"></block></layout>',
733  false
734  ],
735  ];
736  }
737 
738  public function testGenerateElementsWithoutCache()
739  {
740  $this->readerContextFactoryMock->expects($this->once())
741  ->method('create')
742  ->willReturn($this->readerContextMock);
743  $layoutCacheId = 'layout_cache_id';
744  $handles = ['default', 'another'];
746  $xml = simplexml_load_string('<layout/>', \Magento\Framework\View\Layout\Element::class);
747  $this->model->setXml($xml);
748 
749  $themeMock = $this->getMockForAbstractClass(\Magento\Framework\View\Design\ThemeInterface::class);
750  $this->themeResolverMock->expects($this->once())
751  ->method('get')
752  ->willReturn($themeMock);
753  $this->processorFactoryMock->expects($this->once())
754  ->method('create')
755  ->with(['theme' => $themeMock])
756  ->willReturn($this->processorMock);
757 
758  $this->processorMock->expects($this->once())
759  ->method('getCacheId')
760  ->willReturn($layoutCacheId);
761  $this->processorMock->expects($this->once())
762  ->method('getHandles')
763  ->willReturn($handles);
764 
765  $this->cacheMock->expects($this->once())
766  ->method('load')
767  ->with('structure_' . $layoutCacheId)
768  ->willReturn(false);
769 
770  $this->readerPoolMock->expects($this->once())
771  ->method('interpret')
772  ->with($this->readerContextMock, $xml)
773  ->willReturnSelf();
774 
775  $pageConfigStructureData = [
776  'field_1' => 123,
777  'field_2' => 'text',
778  'field_3' => [
779  'field_3_1' => '1244',
780  'field_3_2' => null,
781  'field_3_3' => false,
782  ]
783  ];
784  $this->pageConfigStructure->expects($this->any())->method('__toArray')
785  ->willReturn($pageConfigStructureData);
786 
787  $layoutScheduledStructureData = [
788  'field_1' => 1283,
789  'field_2' => 'text_qwertyuiop[]asdfghjkl;'
790  ];
791  $this->layoutScheduledSructure->expects($this->any())->method('__toArray')
792  ->willReturn($layoutScheduledStructureData);
793  $data = [
794  'pageConfigStructure' => $pageConfigStructureData,
795  'scheduledStructure' => $layoutScheduledStructureData
796  ];
797 
798  $this->cacheMock->expects($this->once())
799  ->method('save')
800  ->with(json_encode($data), 'structure_' . $layoutCacheId, $handles)
801  ->willReturn(true);
802 
803  $generatorContextMock = $this->getMockBuilder(\Magento\Framework\View\Layout\Generator\Context::class)
804  ->disableOriginalConstructor()
805  ->getMock();
806  $this->generatorContextFactoryMock->expects($this->once())
807  ->method('create')
808  ->with(['structure' => $this->structureMock, 'layout' => $this->model])
809  ->willReturn($generatorContextMock);
810 
811  $this->generatorPoolMock->expects($this->once())
812  ->method('process')
813  ->with($this->readerContextMock, $generatorContextMock)
814  ->willReturn(true);
815 
816  $elements = [
817  'name_1' => ['type' => '', 'parent' => null],
818  'name_2' => ['type' => \Magento\Framework\View\Layout\Element::TYPE_CONTAINER, 'parent' => null],
819  'name_3' => ['type' => '', 'parent' => 'parent'],
820  'name_4' => ['type' => \Magento\Framework\View\Layout\Element::TYPE_CONTAINER, 'parent' => 'parent'],
821  ];
822 
823  $this->structureMock->expects($this->once())
824  ->method('exportElements')
825  ->willReturn($elements);
826 
827  $this->model->generateElements();
828  }
829 
830  public function testGenerateElementsWithCache()
831  {
832  $layoutCacheId = 'layout_cache_id';
834  $xml = simplexml_load_string('<layout/>', \Magento\Framework\View\Layout\Element::class);
835  $this->model->setXml($xml);
836 
837  $this->readerContextFactoryMock->expects($this->once())
838  ->method('create')
839  ->willReturn($this->readerContextMock);
840  $themeMock = $this->getMockForAbstractClass(\Magento\Framework\View\Design\ThemeInterface::class);
841  $this->themeResolverMock->expects($this->once())
842  ->method('get')
843  ->willReturn($themeMock);
844  $this->processorFactoryMock->expects($this->once())
845  ->method('create')
846  ->with(['theme' => $themeMock])
847  ->willReturn($this->processorMock);
848 
849  $this->processorMock->expects($this->once())
850  ->method('getCacheId')
851  ->willReturn($layoutCacheId);
852 
853  $pageConfigStructureData = [
854  'field_1' => 123,
855  'field_2' => 'text',
856  'field_3' => [
857  'field_3_1' => '1244',
858  'field_3_2' => null,
859  'field_3_3' => false,
860  ]
861  ];
862  $this->pageConfigStructure->expects($this->once())->method('populateWithArray')
863  ->with($pageConfigStructureData);
864 
865  $layoutScheduledStructureData = [
866  'field_1' => 1283,
867  'field_2' => 'text_qwertyuiop[]asdfghjkl;'
868  ];
869  $this->layoutScheduledSructure->expects($this->once())->method('populateWithArray')
870  ->with($layoutScheduledStructureData);
871  $data = [
872  'pageConfigStructure' => $pageConfigStructureData,
873  'scheduledStructure' => $layoutScheduledStructureData
874  ];
875 
876  $this->cacheMock->expects($this->once())
877  ->method('load')
878  ->with('structure_' . $layoutCacheId)
879  ->willReturn(json_encode($data));
880 
881  $this->readerPoolMock->expects($this->never())
882  ->method('interpret');
883  $this->cacheMock->expects($this->never())
884  ->method('save');
885 
886  $generatorContextMock = $this->getMockBuilder(\Magento\Framework\View\Layout\Generator\Context::class)
887  ->disableOriginalConstructor()
888  ->getMock();
889  $this->generatorContextFactoryMock->expects($this->once())
890  ->method('create')
891  ->with(['structure' => $this->structureMock, 'layout' => $this->model])
892  ->willReturn($generatorContextMock);
893 
894  $this->generatorPoolMock->expects($this->once())
895  ->method('process')
896  ->with($this->readerContextMock, $generatorContextMock)
897  ->willReturn(true);
898 
899  $elements = [
900  'name_1' => ['type' => '', 'parent' => null],
901  'name_2' => ['type' => \Magento\Framework\View\Layout\Element::TYPE_CONTAINER, 'parent' => null],
902  'name_3' => ['type' => '', 'parent' => 'parent'],
903  'name_4' => ['type' => \Magento\Framework\View\Layout\Element::TYPE_CONTAINER, 'parent' => 'parent'],
904  ];
905 
906  $this->structureMock->expects($this->once())
907  ->method('exportElements')
908  ->willReturn($elements);
909 
910  $this->model->generateElements();
911  }
912 
913  public function testGetXml()
914  {
915  $xml = '<layout/>';
916  $this->assertSame($xml, \Magento\Framework\View\Layout::LAYOUT_NODE);
917  }
918 
923  public function testRenderElementDisplay($displayValue)
924  {
925  $name = 'test_container';
926  $child = 'child_block';
927  $children = [$child => true];
928  $blockHtml = '<html/>';
929 
930  $this->structureMock->expects($this->atLeastOnce())
931  ->method('getAttribute')
932  ->willReturnMap(
933  [
934  [$name, 'display', $displayValue],
935  [$child, 'display', $displayValue],
936  [$child, 'type', \Magento\Framework\View\Layout\Element::TYPE_BLOCK]
937  ]
938  );
939 
940  $this->structureMock->expects($this->atLeastOnce())->method('hasElement')
941  ->willReturnMap(
942  [
943  [$child, true]
944  ]
945  );
946 
947  $this->structureMock->expects($this->once())
948  ->method('getChildren')
949  ->with($name)
950  ->willReturn($children);
951 
952  $block = $this->createMock(\Magento\Framework\View\Element\AbstractBlock::class);
953  $block->expects($this->once())->method('toHtml')->willReturn($blockHtml);
954 
955  $renderingOutput = new \Magento\Framework\DataObject();
956  $renderingOutput->setData('output', $blockHtml);
957 
958  $this->eventManagerMock->expects($this->at(0))
959  ->method('dispatch')
960  ->with(
961  'core_layout_render_element',
962  ['element_name' => $child, 'layout' => $this->model, 'transport' => $renderingOutput]
963  );
964  $this->eventManagerMock->expects($this->at(1))
965  ->method('dispatch')
966  ->with(
967  'core_layout_render_element',
968  ['element_name' => $name, 'layout' => $this->model, 'transport' => $renderingOutput]
969  );
970 
971  $this->model->setBlock($child, $block);
972  $this->assertEquals($blockHtml, $this->model->renderElement($name, false));
973  }
974 
979  public function testRenderElementDoNotDisplay($displayValue)
980  {
981  $displayValue = 'false';
982  $name = 'test_container';
983  $blockHtml = '';
984 
985  $this->structureMock->expects($this->atLeastOnce())
986  ->method('getAttribute')
987  ->willReturnMap([[$name, 'display', $displayValue]]);
988 
989  $this->assertEquals($blockHtml, $this->model->renderElement($name, false));
990  }
991 
996  {
997  return [
998  ['false'],
999  ['0'],
1000  [0]
1001  ];
1002  }
1003 
1008  {
1009  return [
1010  [true],
1011  ['1'],
1012  [1],
1013  ['true'],
1014  [false],
1015  [null]
1016  ];
1017  }
1018 }
return false
Definition: gallery.phtml:36
testIsManipulationAllowed($parentName, $containerConfig, $result)
Definition: LayoutTest.php:478
$block
Definition: block.php:8
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
testAddGetRendererOptions($rendererData, $getData, $result)
Definition: LayoutTest.php:631
testIsContainer($hasElement, $attribute, $result)
Definition: LayoutTest.php:443
$children
Definition: actions.phtml:11
if(!trim($html)) $alias
Definition: details.phtml:20
testGetBlockSingleton($type, $blockInstance, $isAbstract)
Definition: LayoutTest.php:596
$elementName
Definition: gallery.phtml:10
if(!isset($_GET['name'])) $name
Definition: log.php:14