Skip to content

Commit 7dd2aac

Browse files
ENGCOM-8729: Fix #30471 - Page layouts are hard-coded in Magento\Widget\Block\Adminhtml\Widget\Instance\Edit\Chooser\Container #31886
- Merge Pull Request #31886 from Bartlomiejsz/magento2:bugfix/30471_page_layouts_hardcoded - Merged commits: 1. 1b9f190 2. 2ea06fb 3. db81b6b 4. 23db47e
2 parents 2e2af0a + 23db47e commit 7dd2aac

File tree

3 files changed

+64
-30
lines changed

3 files changed

+64
-30
lines changed

app/code/Magento/Widget/Block/Adminhtml/Widget/Instance/Edit/Chooser/Container.php

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Widget\Block\Adminhtml\Widget\Instance\Edit\Chooser;
79

10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\View\Element\Context;
12+
use Magento\Framework\View\Element\Html\Select;
13+
use Magento\Framework\View\Layout\ProcessorFactory;
14+
use Magento\Framework\View\Model\PageLayout\Config\BuilderInterface as PageLayoutConfigBuilder;
15+
use Magento\Theme\Model\ResourceModel\Theme\CollectionFactory;
16+
817
/**
918
* A chooser for container for widget instances
1019
*
@@ -13,40 +22,53 @@
1322
* @method \Magento\Widget\Block\Adminhtml\Widget\Instance\Edit\Chooser\Container setTheme($theme)
1423
* @method \Magento\Widget\Block\Adminhtml\Widget\Instance\Edit\Chooser\Container setArea($area)
1524
*/
16-
class Container extends \Magento\Framework\View\Element\Html\Select
25+
class Container extends Select
1726
{
1827
/**#@+
1928
* Frontend page layouts
29+
* @deprecated hardcoded list was replaced with checking actual existing layouts
30+
* @see \Magento\Framework\View\Model\PageLayout\Config\BuilderInterface::getPageLayoutsConfig
2031
*/
2132
const PAGE_LAYOUT_1COLUMN = '1column-center';
2233
const PAGE_LAYOUT_2COLUMNS_LEFT = '2columns-left';
2334
const PAGE_LAYOUT_2COLUMNS_RIGHT = '2columns-right';
2435
const PAGE_LAYOUT_3COLUMNS = '3columns';
2536
/**#@-*/
2637

27-
/**#@-*/
38+
/**
39+
* @var ProcessorFactory
40+
*/
2841
protected $_layoutProcessorFactory;
2942

3043
/**
31-
* @var \Magento\Theme\Model\ResourceModel\Theme\CollectionFactory
44+
* @var CollectionFactory
3245
*/
3346
protected $_themesFactory;
3447

3548
/**
36-
* @param \Magento\Framework\View\Element\Context $context
37-
* @param \Magento\Framework\View\Layout\ProcessorFactory $layoutProcessorFactory
38-
* @param \Magento\Theme\Model\ResourceModel\Theme\CollectionFactory $themesFactory
49+
* @var PageLayoutConfigBuilder
50+
*/
51+
private $pageLayoutConfigBuilder;
52+
53+
/**
54+
* @param Context $context
55+
* @param ProcessorFactory $layoutProcessorFactory
56+
* @param CollectionFactory $themesFactory
3957
* @param array $data
58+
* @param PageLayoutConfigBuilder|null $pageLayoutConfigBuilder
4059
*/
4160
public function __construct(
42-
\Magento\Framework\View\Element\Context $context,
43-
\Magento\Framework\View\Layout\ProcessorFactory $layoutProcessorFactory,
44-
\Magento\Theme\Model\ResourceModel\Theme\CollectionFactory $themesFactory,
45-
array $data = []
61+
Context $context,
62+
ProcessorFactory $layoutProcessorFactory,
63+
CollectionFactory $themesFactory,
64+
array $data = [],
65+
PageLayoutConfigBuilder $pageLayoutConfigBuilder = null
4666
) {
67+
parent::__construct($context, $data);
4768
$this->_layoutProcessorFactory = $layoutProcessorFactory;
4869
$this->_themesFactory = $themesFactory;
49-
parent::__construct($context, $data);
70+
$this->pageLayoutConfigBuilder = $pageLayoutConfigBuilder
71+
?? ObjectManager::getInstance()->get(PageLayoutConfigBuilder::class);
5072
}
5173

5274
/**
@@ -101,19 +123,22 @@ protected function _beforeToHtml()
101123
$this->addOption($containerName, $containerLabel);
102124
}
103125
}
126+
104127
return parent::_beforeToHtml();
105128
}
106129

107130
/**
108131
* Retrieve theme instance by its identifier
109132
*
110133
* @param int $themeId
134+
*
111135
* @return \Magento\Theme\Model\Theme|null
112136
*/
113137
protected function _getThemeInstance($themeId)
114138
{
115139
/** @var \Magento\Theme\Model\ResourceModel\Theme\Collection $themeCollection */
116140
$themeCollection = $this->_themesFactory->create();
141+
117142
return $themeCollection->getItemById($themeId);
118143
}
119144

@@ -124,11 +149,8 @@ protected function _getThemeInstance($themeId)
124149
*/
125150
protected function getPageLayouts()
126151
{
127-
return [
128-
self::PAGE_LAYOUT_1COLUMN,
129-
self::PAGE_LAYOUT_2COLUMNS_LEFT,
130-
self::PAGE_LAYOUT_2COLUMNS_RIGHT,
131-
self::PAGE_LAYOUT_3COLUMNS,
132-
];
152+
$pageLayoutsConfig = $this->pageLayoutConfigBuilder->getPageLayoutsConfig();
153+
154+
return array_keys($pageLayoutsConfig->getPageLayouts());
133155
}
134156
}

app/code/Magento/Widget/Test/Unit/Block/Adminhtml/Widget/Instance/Edit/Chooser/AbstractContainerTest.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@
1212
use Magento\Framework\App\Config\ScopeConfigInterface;
1313
use Magento\Framework\Escaper;
1414
use Magento\Framework\Event\Manager;
15-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1615
use Magento\Framework\View\Layout\ProcessorFactory;
1716
use Magento\Framework\View\Model\Layout\Merge;
17+
use Magento\Framework\View\Model\PageLayout\Config\BuilderInterface as PageLayoutConfigBuilder;
18+
use Magento\Framework\View\PageLayout\Config as PageLayoutConfig;
1819
use Magento\Theme\Model\ResourceModel\Theme\Collection;
1920
use Magento\Theme\Model\ResourceModel\Theme\CollectionFactory;
2021
use Magento\Theme\Model\Theme;
2122
use PHPUnit\Framework\MockObject\MockObject;
2223
use PHPUnit\Framework\TestCase;
2324

25+
/**
26+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27+
*/
2428
abstract class AbstractContainerTest extends TestCase
2529
{
2630
/**
@@ -69,17 +73,15 @@ abstract class AbstractContainerTest extends TestCase
6973
protected $escaperMock;
7074

7175
/**
72-
* @var ObjectManagerHelper
76+
* @var PageLayoutConfigBuilder|MockObject
7377
*/
74-
protected $objectManagerHelper;
78+
protected $pageLayoutConfigBuilderMock;
7579

7680
/**
7781
* @return void
7882
*/
7983
protected function setUp(): void
8084
{
81-
$this->objectManagerHelper = new ObjectManagerHelper($this);
82-
8385
$this->eventManagerMock = $this->getMockBuilder(Manager::class)
8486
->setMethods(['dispatch'])
8587
->disableOriginalConstructor()
@@ -116,7 +118,7 @@ protected function setUp(): void
116118
Escaper::class,
117119
['escapeHtml', 'escapeHtmlAttr']
118120
);
119-
$this->escaperMock->expects($this->any())->method('escapeHtmlAttr')->willReturnArgument(0);
121+
$this->escaperMock->method('escapeHtmlAttr')->willReturnArgument(0);
120122

121123
$this->contextMock = $this->getMockBuilder(Context::class)
122124
->setMethods(['getEventManager', 'getScopeConfig', 'getEscaper'])
@@ -125,5 +127,16 @@ protected function setUp(): void
125127
$this->contextMock->expects($this->once())->method('getEventManager')->willReturn($this->eventManagerMock);
126128
$this->contextMock->expects($this->once())->method('getScopeConfig')->willReturn($this->scopeConfigMock);
127129
$this->contextMock->expects($this->once())->method('getEscaper')->willReturn($this->escaperMock);
130+
131+
$this->pageLayoutConfigBuilderMock = $this->getMockBuilder(PageLayoutConfigBuilder::class)
132+
->getMockForAbstractClass();
133+
$pageLayoutConfigMock = $this->getMockBuilder(PageLayoutConfig::class)
134+
->onlyMethods(['getPageLayouts'])
135+
->disableOriginalConstructor()
136+
->getMock();
137+
$pageLayoutConfigMock->method('getPageLayouts')
138+
->willReturn(['empty' => 'Empty']);
139+
$this->pageLayoutConfigBuilderMock->method('getPageLayoutsConfig')
140+
->willReturn($pageLayoutConfigMock);
128141
}
129142
}

app/code/Magento/Widget/Test/Unit/Block/Adminhtml/Widget/Instance/Edit/Chooser/ContainerTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ protected function setUp(): void
2323
{
2424
parent::setUp();
2525

26-
$this->containerBlock = $this->objectManagerHelper->getObject(
27-
Container::class,
28-
[
29-
'context' => $this->contextMock,
30-
'themesFactory' => $this->themeCollectionFactoryMock,
31-
'layoutProcessorFactory' => $this->layoutProcessorFactoryMock
32-
]
26+
$this->containerBlock = new Container(
27+
$this->contextMock,
28+
$this->layoutProcessorFactoryMock,
29+
$this->themeCollectionFactoryMock,
30+
[],
31+
$this->pageLayoutConfigBuilderMock
3332
);
3433
}
3534

0 commit comments

Comments
 (0)