123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Config\Test\Unit\Model\Config;
- use Magento\Config\Model\Config\ScopeDefiner;
- use Magento\Config\Model\Config\Structure;
- use Magento\Config\Model\Config\Structure\Data;
- use Magento\Config\Model\Config\Structure\Element\FlyweightFactory;
- use Magento\Config\Model\Config\Structure\Element\Iterator\Tab as TabIterator;
- use PHPUnit_Framework_MockObject_MockObject as Mock;
- /**
- * Test for Structure.
- *
- * @see Structure
- */
- class StructureTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var Structure|Mock
- */
- protected $_model;
- /**
- * @var FlyweightFactory|Mock
- */
- protected $_flyweightFactory;
- /**
- * @var TabIterator|Mock
- */
- protected $_tabIteratorMock;
- /**
- * @var Data|Mock
- */
- protected $_structureDataMock;
- /**
- * @var ScopeDefiner|Mock
- */
- protected $_scopeDefinerMock;
- /**
- * @var array
- */
- protected $_structureData;
- protected function setUp()
- {
- $this->_flyweightFactory = $this->getMockBuilder(FlyweightFactory::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->_tabIteratorMock = $this->getMockBuilder(TabIterator::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->_structureDataMock = $this->getMockBuilder(Data::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->_scopeDefinerMock = $this->getMockBuilder(ScopeDefiner::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->_structureData = require dirname(__DIR__) . '/_files/converted_config.php';
- $this->_scopeDefinerMock->expects($this->any())
- ->method('getScope')
- ->willReturn('scope');
- $this->_structureDataMock->expects($this->once())
- ->method('get')
- ->willReturn($this->_structureData['config']['system']);
- $this->_model = new Structure(
- $this->_structureDataMock,
- $this->_tabIteratorMock,
- $this->_flyweightFactory,
- $this->_scopeDefinerMock
- );
- }
- public function testGetTabsBuildsSectionTree()
- {
- $expected = ['tab1' => ['children' => ['section1' => ['tab' => 'tab1']]]];
- $this->_structureDataMock = $this->getMockBuilder(Data::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->_structureDataMock->expects($this->any())
- ->method('get')
- ->willReturn(
- ['sections' => ['section1' => ['tab' => 'tab1']], 'tabs' => ['tab1' => []]]
- );
- $this->_tabIteratorMock->expects($this->once())
- ->method('setElements')
- ->with($expected);
- $model = new \Magento\Config\Model\Config\Structure(
- $this->_structureDataMock,
- $this->_tabIteratorMock,
- $this->_flyweightFactory,
- $this->_scopeDefinerMock
- );
- $this->assertEquals($this->_tabIteratorMock, $model->getTabs());
- }
- public function testGetSectionList()
- {
- $expected = [
- 'section1_child_id_1' => true,
- 'section1_child_id_2' => true,
- 'section1_child_id_3' => true,
- 'section2_child_id_1' => true
- ];
- $this->_structureDataMock = $this->getMockBuilder(Data::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->_structureDataMock->expects($this->any())
- ->method('get')
- ->willReturn(
- [
- 'sections' => [
- 'section1' => [
- 'children' => [
- 'child_id_1' => 'child_data',
- 'child_id_2' => 'child_data',
- 'child_id_3' => 'child_data'
- ]
- ],
- 'section2' => [
- 'children' => [
- 'child_id_1' => 'child_data'
- ]
- ],
- ]
- ]
- );
- $model = new \Magento\Config\Model\Config\Structure(
- $this->_structureDataMock,
- $this->_tabIteratorMock,
- $this->_flyweightFactory,
- $this->_scopeDefinerMock
- );
- $this->assertEquals($expected, $model->getSectionList());
- }
- /**
- * @param string $path
- * @param string $expectedType
- * @param string $expectedId
- * @param string $expectedPath
- * @dataProvider emptyElementDataProvider
- */
- public function testGetElementReturnsEmptyElementIfNotExistingElementIsRequested(
- $path,
- $expectedType,
- $expectedId,
- $expectedPath
- ) {
- $elementMock = $this->getElementReturnsEmptyElementIfNotExistingElementIsRequested(
- $expectedType,
- $expectedId,
- $expectedPath
- );
- $this->assertEquals($elementMock, $this->_model->getElement($path));
- }
- /**
- * @param string $path
- * @param string $expectedType
- * @param string $expectedId
- * @param string $expectedPath
- * @dataProvider emptyElementDataProvider
- */
- public function testGetElementReturnsEmptyByConfigPathElementIfNotExistingElementIsRequested(
- $path,
- $expectedType,
- $expectedId,
- $expectedPath
- ) {
- $elementMock = $this->getElementReturnsEmptyElementIfNotExistingElementIsRequested(
- $expectedType,
- $expectedId,
- $expectedPath
- );
- $this->assertEquals($elementMock, $this->_model->getElementByConfigPath($path));
- }
- /**
- * @param string $expectedType
- * @param string $expectedId
- * @param string $expectedPath
- * @return Mock
- */
- private function getElementReturnsEmptyElementIfNotExistingElementIsRequested(
- $expectedType,
- $expectedId,
- $expectedPath
- ) {
- $expectedConfig = ['id' => $expectedId, 'path' => $expectedPath, '_elementType' => $expectedType];
- $elementMock = $this->getMockBuilder(Structure\ElementInterface::class)
- ->getMockForAbstractClass();
- $elementMock->expects($this->once())
- ->method('setData')
- ->with($expectedConfig);
- $this->_flyweightFactory->expects($this->once())
- ->method('create')
- ->with($expectedType)
- ->willReturn($elementMock);
- return $elementMock;
- }
- /**
- * @return array
- */
- public function emptyElementDataProvider()
- {
- return [
- ['someSection/group_1/nonexisting_field', 'field', 'nonexisting_field', 'someSection/group_1'],
- ['section_1/group_1/nonexisting_field', 'field', 'nonexisting_field', 'section_1/group_1'],
- ['section_1/nonexisting_group', 'group', 'nonexisting_group', 'section_1'],
- ['nonexisting_section', 'section', 'nonexisting_section', '']
- ];
- }
- public function testGetElementReturnsProperElementByPath()
- {
- $elementMock = $this->getElementPathReturnsProperElementByPath();
- $this->assertEquals($elementMock, $this->_model->getElement('section_1/group_level_1/field_3'));
- }
- public function testGetElementByConfigPathReturnsProperElementByPath()
- {
- $elementMock = $this->getElementPathReturnsProperElementByPath();
- $this->assertEquals($elementMock, $this->_model->getElementByConfigPath('section_1/group_level_1/field_3'));
- }
- /**
- * @return Mock
- */
- private function getElementPathReturnsProperElementByPath()
- {
- $section = $this->_structureData['config']['system']['sections']['section_1'];
- $fieldData = $section['children']['group_level_1']['children']['field_3'];
- $elementMock = $this->getMockBuilder(Structure\Element\Field::class)
- ->disableOriginalConstructor()
- ->getMock();
- $elementMock->expects($this->once())
- ->method('setData')
- ->with($fieldData, 'scope');
- $this->_flyweightFactory->expects($this->once())
- ->method('create')
- ->with('field')
- ->willReturn($elementMock);
- return $elementMock;
- }
- public function testGetElementByPathPartsIfSectionDataIsEmpty()
- {
- $fieldData = [
- 'id' => 'field_3',
- 'path' => 'section_1/group_level_1',
- '_elementType' => 'field',
- ];
- $pathParts = explode('/', 'section_1/group_level_1/field_3');
- $elementMock = $this->getMockBuilder(Structure\Element\Field::class)
- ->disableOriginalConstructor()
- ->getMock();
- $structureDataMock = $this->getMockBuilder(Data::class)
- ->disableOriginalConstructor()
- ->getMock();
- $elementMock->expects($this->once())
- ->method('setData')
- ->with($fieldData, 'scope');
- $this->_flyweightFactory->expects($this->once())
- ->method('create')
- ->with('field')
- ->willReturn($elementMock);
- $structureDataMock->expects($this->once())
- ->method('get')
- ->willReturn([]);
- $structureMock = new Structure(
- $structureDataMock,
- $this->_tabIteratorMock,
- $this->_flyweightFactory,
- $this->_scopeDefinerMock
- );
- $this->assertEquals($elementMock, $structureMock->getElementByPathParts($pathParts));
- }
- public function testGetFirstSectionReturnsFirstAllowedSection()
- {
- $tabMock = $this->getMockBuilder(Structure\Element\Tab::class)
- ->disableOriginalConstructor()
- ->setMethods(['current', 'getChildren', 'rewind'])
- ->getMock();
- $tabMock->expects($this->any())
- ->method('getChildren')
- ->willReturnSelf();
- $tabMock->expects($this->once())
- ->method('rewind');
- $section = $this->getMockBuilder(Structure\Element\Section::class)
- ->disableOriginalConstructor()
- ->setMethods(['isVisible', 'getData'])
- ->getMock();
- $section->expects($this->any())
- ->method('isVisible')
- ->willReturn(true);
- $section->expects($this->any())
- ->method('getData')
- ->willReturn('currentSection');
- $tabMock->expects($this->any())
- ->method('current')
- ->willReturn($section);
- $this->_tabIteratorMock->expects($this->once())
- ->method('rewind');
- $this->_tabIteratorMock->expects($this->once())
- ->method('current')
- ->willReturn($tabMock);
- $this->assertEquals('currentSection', $this->_model->getFirstSection()->getData());
- }
- public function testGetElementReturnsProperElementByPathCachesObject()
- {
- $elementMock = $this->getElementReturnsProperElementByPathCachesObject();
- $this->assertEquals($elementMock, $this->_model->getElement('section_1/group_level_1/field_3'));
- $this->assertEquals($elementMock, $this->_model->getElement('section_1/group_level_1/field_3'));
- }
- public function testGetElementByConfigPathReturnsProperElementByPathCachesObject()
- {
- $elementMock = $this->getElementReturnsProperElementByPathCachesObject();
- $this->assertEquals($elementMock, $this->_model->getElementByConfigPath('section_1/group_level_1/field_3'));
- $this->assertEquals($elementMock, $this->_model->getElementByConfigPath('section_1/group_level_1/field_3'));
- }
- /**
- * @return Mock
- */
- private function getElementReturnsProperElementByPathCachesObject()
- {
- $section = $this->_structureData['config']['system']['sections']['section_1'];
- $fieldData = $section['children']['group_level_1']['children']['field_3'];
- $elementMock = $this->getMockBuilder(Structure\Element\Field::class)
- ->disableOriginalConstructor()
- ->getMock();
- $elementMock->expects($this->once())
- ->method('setData')
- ->with($fieldData, 'scope');
- $this->_flyweightFactory->expects($this->once())
- ->method('create')
- ->with('field')
- ->willReturn($elementMock);
- return $elementMock;
- }
- /**
- * @param $attributeName
- * @param $attributeValue
- * @param $paths
- * @dataProvider getFieldPathsByAttributeDataProvider
- */
- public function testGetFieldPathsByAttribute($attributeName, $attributeValue, $paths)
- {
- $this->assertEquals($paths, $this->_model->getFieldPathsByAttribute($attributeName, $attributeValue));
- }
- /**
- * @return array
- */
- public function getFieldPathsByAttributeDataProvider()
- {
- return [
- [
- 'backend_model',
- \Magento\Config\Model\Config\Backend\Encrypted::class,
- [
- 'section_1/group_1/field_2',
- 'section_1/group_level_1/group_level_2/group_level_3/field_3_1_1',
- 'section_2/group_3/field_4',
- ]
- ],
- ['attribute_2', 'test_value_2', ['section_2/group_3/field_4']]
- ];
- }
- public function testGetFieldPaths()
- {
- $expected = [
- 'section/group/field2' => [
- 'field_2'
- ],
- 'field_3' => [
- 'field_3',
- 'field_3'
- ],
- 'field_3_1' => [
- 'field_3_1'
- ],
- 'field_3_1_1' => [
- 'field_3_1_1'
- ],
- 'section/group/field4' => [
- 'field_4',
- ],
- 'field_5' => [
- 'field_5',
- ],
- ];
- $this->assertSame(
- $expected,
- $this->_model->getFieldPaths()
- );
- }
- }
|