/** * Set container-specific data to structure element * * @param \Magento\Framework\View\Layout\Data\Structure $structure * @param string $elementName * @param array $options * @return void */ public function generateContainer(Layout\Data\Structure $structure, $elementName, $options) { unset($options['type']); $this->validateOptions($options); foreach ($options as $key => $value) { $structure->setAttribute($elementName, $key, $value); } }
/** * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $loggerExpects * @param string $stateMode * @return void * @dataProvider reorderChildElementLogDataProvider */ public function testReorderChildElementLog($loggerExpects, $stateMode) { $parentName = 'parent'; $childName = 'child'; $offsetOrSibling = '-'; $this->stateMock->expects($this->once())->method('getMode')->willReturn($stateMode); $this->loggerMock->expects($loggerExpects)->method('critical')->with("Broken reference: the '{$childName}' tries to reorder itself towards '', but " . "their parents are different: '{$parentName}' and '' respectively."); $this->dataStructure->reorderChildElement($parentName, $childName, $offsetOrSibling); }
/** * Remove scheduled element * * @param ScheduledStructure $scheduledStructure * @param Structure $structure * @param string $elementName * @param bool $isChild * @return $this */ protected function removeElement(ScheduledStructure $scheduledStructure, Structure $structure, $elementName, $isChild = false) { $elementsToRemove = array_keys($structure->getChildren($elementName)); $scheduledStructure->unsetElement($elementName); foreach ($elementsToRemove as $element) { $this->removeElement($scheduledStructure, $structure, $element, true); } if (!$isChild) { $structure->unsetElement($elementName); } return $this; }
/** * @param array $schedule * @param array $expectedSchedule * @return void * @dataProvider processDataProvider */ public function testProcess($schedule, $expectedSchedule) { foreach ($schedule['structure'] as $structureElement) { $this->scheduledStructure->setStructureElement($structureElement, []); } $reorderMap = []; foreach ($schedule['sort'] as $elementName => $sort) { list($parentName, $sibling, $isAfter) = $sort; $this->scheduledStructure->setElementToSortList($parentName, $elementName, $sibling, $isAfter); $reorderMap[] = [$parentName, $elementName, $sibling, $isAfter]; } foreach ($schedule['move'] as $elementName => $move) { $this->scheduledStructure->setElementToMove($elementName, $move); list($destination, $sibling, $isAfter) = $move; $reorderMap[] = [$destination, $elementName, $sibling, $isAfter]; } $invocation = $this->structureMock->expects($this->any())->method('reorderChildElement'); call_user_func_array([$invocation, 'withConsecutive'], $reorderMap); foreach ($schedule['remove'] as $remove) { $this->scheduledStructure->setElementToRemoveList($remove); } $this->helperMock->expects($this->atLeastOnce())->method('scheduleElement')->with($this->scheduledStructure, $this->structureMock, $this->anything())->willReturnCallback(function ($scheduledStructure, $structure, $elementName) use($schedule) { /** * @var $scheduledStructure ScheduledStructure * @var $structure DataStructure */ $this->assertContains($elementName, $schedule['structure']); $scheduledStructure->unsetStructureElement($elementName); $scheduledStructure->setElement($elementName, []); $structure->createStructuralElement($elementName, 'block', 'someClass'); }); $this->model->process($this->readerContextMock, $this->generatorContextMock); $this->assertEquals($expectedSchedule, $this->scheduledStructure->getElements()); }
/** * Create component object * * @param \Magento\Framework\View\Layout\Data\Structure $structure * @param string $elementName * @param string $data * @return \Magento\Framework\View\Element\UiComponentInterface */ protected function generateComponent(Layout\Data\Structure $structure, $elementName, $data) { $attributes = $data['attributes']; if (!empty($attributes['group'])) { $structure->addToParentGroup($elementName, $attributes['group']); } $arguments = empty($data['arguments']) ? [] : $this->evaluateArguments($data['arguments']); $componentName = isset($attributes['component']) ? $attributes['component'] : ''; $uiComponent = $this->uiComponentFactory->createUiComponent($componentName, $elementName, $arguments); return $uiComponent; }
/** * @param bool $hasParent * @param int $setAsChild * @param int $toRemoveList * @param string $siblingName * @param bool $isAfter * @param int $toSortList * @return void * * @dataProvider scheduleElementDataProvider */ public function testScheduleElement($hasParent, $setAsChild, $toRemoveList, $siblingName, $isAfter, $toSortList) { $key = 'key'; $parentName = 'parent'; $alias = 'alias'; $block = 'block'; $data = ['data']; $this->scheduledStructureMock->expects($this->any())->method('getStructureElement')->willReturnMap([[$key, null, [Layout\ScheduledStructure\Helper::SCHEDULED_STRUCTURE_INDEX_TYPE => $block, Layout\ScheduledStructure\Helper::SCHEDULED_STRUCTURE_INDEX_ALIAS => $alias, Layout\ScheduledStructure\Helper::SCHEDULED_STRUCTURE_INDEX_PARENT_NAME => $parentName, Layout\ScheduledStructure\Helper::SCHEDULED_STRUCTURE_INDEX_SIBLING_NAME => $siblingName, Layout\ScheduledStructure\Helper::SCHEDULED_STRUCTURE_INDEX_IS_AFTER => $isAfter]], [$parentName, null, []]]); $this->scheduledStructureMock->expects($this->any())->method('getStructureElementData')->willReturnMap([[$key, null, $data], [$parentName, null, $data]]); $this->scheduledStructureMock->expects($this->any())->method('hasStructureElement')->willReturn(true); $this->scheduledStructureMock->expects($this->once())->method('setElement')->with($key, [$block, $data]); $this->dataStructureMock->expects($this->once())->method('createElement')->with($key, ['type' => $block]); $this->dataStructureMock->expects($this->once())->method('hasElement')->with($parentName)->willReturn($hasParent); $this->dataStructureMock->expects($this->exactly($setAsChild))->method('setAsChild')->with($key, $parentName, $alias)->willReturn(true); $this->scheduledStructureMock->expects($this->exactly($toRemoveList))->method('setElementToBrokenParentList')->with($key); $this->scheduledStructureMock->expects($this->exactly($toSortList))->method('setElementToSortList')->with($parentName, $key, $siblingName, $isAfter); $this->helper->scheduleElement($this->scheduledStructureMock, $this->dataStructureMock, $key); }
/** * Get element alias by name * * @param string $name * @return bool|string */ public function getElementAlias($name) { $this->build(); return $this->structure->getChildAlias($this->structure->getParentId($name), $name); }
/** * Create block and set related data * * @param \Magento\Framework\View\Layout\ScheduledStructure $scheduledStructure * @param \Magento\Framework\View\Layout\Data\Structure $structure * @param string $elementName * @return \Magento\Framework\View\Element\AbstractBlock */ protected function generateBlock(Layout\ScheduledStructure $scheduledStructure, Layout\Data\Structure $structure, $elementName) { list(, $data) = $scheduledStructure->getElement($elementName); $attributes = $data['attributes']; if (!empty($attributes['group'])) { $structure->addToParentGroup($elementName, $attributes['group']); } if (!empty($attributes['display'])) { $structure->setAttribute($elementName, 'display', $attributes['display']); } // create block $className = $attributes['class']; $block = $this->createBlock($className, $elementName, ['data' => $this->evaluateArguments($data['arguments'])]); if (!empty($attributes['template'])) { $block->setTemplate($attributes['template']); } if (!empty($attributes['ttl'])) { $ttl = (int) $attributes['ttl']; $block->setTtl($ttl); } return $block; }
/** * Register an element in structure * * Will assign an "anonymous" name to the element, if provided with an empty name * * @param Layout\Data\Structure $structure * @param string $name * @param string $type * @param string $class * @return string */ protected function _createStructuralElement(Layout\Data\Structure $structure, $name, $type, $class) { if (empty($name)) { $name = $this->_generateAnonymousName($class); } $structure->createElement($name, ['type' => $type]); return $name; }
/** * @param array $structureElements * * @dataProvider processWithExceptionDataProvider * @expectedException \Magento\Framework\Exception\LocalizedException */ public function testProcessWithException($structureElements) { $this->scheduledStructureMock->expects($this->once())->method('getElements')->willReturn($structureElements); $this->structureMock->expects($this->once())->method('setAttribute')->willReturnSelf(); $this->container->process($this->readerContextMock, $this->generatorContextMock); }
/** * Create component object * * @param Structure $structure * @param string $elementName * @param string $data * @param LayoutInterface $layout * @return ContainerInterface */ protected function generateComponent(Structure $structure, $elementName, $data, LayoutInterface $layout) { $attributes = $data['attributes']; if (!empty($attributes['group'])) { $structure->addToParentGroup($elementName, $attributes['group']); } $context = $this->contextFactory->create(['namespace' => $elementName, 'pageLayout' => $layout]); $component = $this->uiComponentFactory->create($elementName, null, ['context' => $context]); $this->prepareComponent($component); /** @var ContainerInterface $blockContainer */ $blockContainer = $this->blockFactory->createBlock(static::CONTAINER, ['component' => $component]); return $blockContainer; }