Skip to content

Commit 04dec7e

Browse files
authored
Merge pull request #6 from integer-net/tests
#4 add Tests for Product and Page. Fix issue in Page and Product plug…
2 parents 1c4fa34 + e7ed0f7 commit 04dec7e

14 files changed

+685
-101
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5+
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6+
7+
## 1.0.0 - 2020-04-27
8+
### Added
9+
- Plugins for category, product and page layouts that allow custom layout files to be loaded with a global identifier
10+
(`0`). E.g. `catalog_category_view_selectable_0_.xml` for Categories.
11+
12+
## 1.1.0 - 2020-05-02
13+
### Added
14+
- Adds frontend test coverage for global custom layout updates
15+
- Fixes [#7](https://github.com/integer-net/magento2-global-custom-layout/issues/7) where layout handles were not merged in Product and Page Plugins' `afterFetchAvailableFiles()` method.

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ More info on default behaviour of selectable layouts:
4949
5050
Zero configuration needed.
5151
52+
## Change log
53+
54+
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
55+
56+
## Testing
57+
58+
### Magento Integration Tests
59+
60+
0. Configure test database in `dev/tests/integration/etc/install-config-mysql.php`. [Read more in the Magento docs.](https://devdocs.magento.com/guides/v2.3/test/integration/integration_test_execution.html)
61+
62+
1. Copy `Test/Integration/phpunit.xml.dist` from the package to `dev/tests/integration/phpunit.xml` in your Magento installation.
63+
64+
2. In that directory, run
65+
``` bash
66+
../../../vendor/bin/phpunit
67+
```
68+
5269
## Contributing
5370
5471
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "integer-net/magento2-global-custom-layout",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Module for Magento 2 that allows you to add global layout update files to be selected from admin, by using '0' instead of an entity_id",
55
"authors": [
66
{

src/Plugin/CategoryLayoutPlugin.php

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ class CategoryLayoutPlugin
4444
public function __construct(
4545
FlyweightFactory $themeFactory,
4646
DesignInterface $design,
47-
LayoutProcessorFactory $layoutProcessorFactory
48-
)
47+
LayoutProcessorFactory $layoutProcessorFactory)
4948
{
5049
$this->themeFactory = $themeFactory;
5150
$this->design = $design;
@@ -66,7 +65,7 @@ private function getLayoutProcessor(): LayoutProcessor
6665
[
6766
'theme' => $this->themeFactory->create(
6867
$this->design->getConfigurationDesignTheme(Area::AREA_FRONTEND)
69-
)
68+
),
7069
]
7170
);
7271
$this->themeFactory = null;
@@ -88,32 +87,30 @@ private function getLayoutProcessor(): LayoutProcessor
8887
public function afterFetchAvailableFiles(
8988
LayoutUpdateManager $subject,
9089
array $result,
91-
CategoryInterface $category
92-
): array
90+
CategoryInterface $category): array
9391
{
94-
if (!$category->getId()) {
95-
return $result;
96-
}
97-
9892
$handles = $this->getLayoutProcessor()->getAvailableHandles();
9993

100-
return array_merge($result, array_filter(
101-
array_map(
102-
function(string $handle) use ($category) : ?string {
103-
preg_match(
104-
'/^catalog\_category\_view\_selectable\_0\_([a-z0-9]+)/i',
105-
$handle,
106-
$selectable
107-
);
108-
if (!empty($selectable[1])) {
109-
return $selectable[1];
110-
}
111-
112-
return null;
113-
},
114-
$handles
94+
return array_merge(
95+
$result,
96+
array_filter(
97+
array_map(
98+
function (string $handle): ?string {
99+
preg_match(
100+
'/^catalog\_category\_view\_selectable\_0\_([a-z0-9]+)/i',
101+
$handle,
102+
$selectable
103+
);
104+
if (!empty($selectable[1])) {
105+
return $selectable[1];
106+
}
107+
108+
return null;
109+
},
110+
$handles
111+
)
115112
)
116-
));
113+
);
117114
}
118115

119116
/**
@@ -132,8 +129,7 @@ public function afterExtractCustomSettings(
132129
LayoutUpdateManager $subject,
133130
$result,
134131
CategoryInterface $category,
135-
DataObject $intoSettings
136-
): void
132+
DataObject $intoSettings): void
137133
{
138134
if ($category->getId() && $value = $this->extractAttributeValue($category)) {
139135
$handles = $intoSettings->getPageLayoutHandles() ?? [];

src/Plugin/PageLayoutPlugin.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Magento\Cms\Api\Data\PageInterface;
77
use Magento\Cms\Api\PageRepositoryInterface;
8+
use Magento\Cms\Model\Page\CustomLayoutManagerInterface;
89
use Magento\Cms\Model\Page\CustomLayout\CustomLayoutManager;
910
use Magento\Cms\Model\Page\CustomLayout\Data\CustomLayoutSelectedInterface;
1011
use Magento\Cms\Model\Page\IdentityMap;
@@ -95,20 +96,20 @@ private function getLayoutProcessor(): LayoutProcessor
9596
/**
9697
* Fetch list of available global files/handles for the page.
9798
*
98-
* @param CustomLayoutManager $subject
99-
* @param array $handles
99+
* @param CustomLayoutManagerInterface $subject
100+
* @param array $result
100101
* @param PageInterface $page
101102
* @return array
102103
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
103104
*/
104105
public function afterFetchAvailableFiles(
105-
CustomLayoutManager $subject,
106-
array $handles,
106+
CustomLayoutManagerInterface $subject,
107+
array $result,
107108
PageInterface $page
108109
): array {
109110
$handles = $this->getLayoutProcessor()->getAvailableHandles();
110111

111-
return array_filter(
112+
return array_merge($result, array_filter(
112113
array_map(
113114
function(string $handle) : ?string {
114115
preg_match(
@@ -124,18 +125,18 @@ function(string $handle) : ?string {
124125
},
125126
$handles
126127
)
127-
);
128+
));
128129
}
129130

130131
/**
131-
* @param CustomLayoutManager $subject
132+
* @param CustomLayoutManagerInterface $subject
132133
* @param $result
133134
* @param PageLayout $layout
134135
* @param CustomLayoutSelectedInterface $layoutSelected
135136
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
136137
*/
137138
public function afterApplyUpdate(
138-
CustomLayoutManager $subject,
139+
CustomLayoutManagerInterface $subject,
139140
$result,
140141
PageLayout $layout,
141142
CustomLayoutSelectedInterface $layoutSelected

src/Plugin/ProductLayoutPlugin.php

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
use Magento\Framework\View\Model\Layout\Merge as LayoutProcessor;
1515
use Magento\Framework\View\Model\Layout\MergeFactory as LayoutProcessorFactory;
1616

17-
class ProductLayoutPlugin {
17+
class ProductLayoutPlugin
18+
{
1819

1920
/**
2021
* @var FlyweightFactory
@@ -44,8 +45,8 @@ class ProductLayoutPlugin {
4445
public function __construct(
4546
FlyweightFactory $themeFactory,
4647
DesignInterface $design,
47-
LayoutProcessorFactory $layoutProcessorFactory
48-
) {
48+
LayoutProcessorFactory $layoutProcessorFactory)
49+
{
4950
$this->themeFactory = $themeFactory;
5051
$this->design = $design;
5152
$this->layoutProcessorFactory = $layoutProcessorFactory;
@@ -65,7 +66,7 @@ private function getLayoutProcessor(): LayoutProcessor
6566
[
6667
'theme' => $this->themeFactory->create(
6768
$this->design->getConfigurationDesignTheme(Area::AREA_FRONTEND)
68-
)
69+
),
6970
]
7071
);
7172
$this->themeFactory = null;
@@ -79,37 +80,36 @@ private function getLayoutProcessor(): LayoutProcessor
7980
* Fetch list of available global files/handles for the product.
8081
*
8182
* @param LayoutUpdateManager $subject
82-
* @param array $handles
83-
* @param CategoryInterface $category
83+
* @param array $result
84+
* @param ProductInterface $product
8485
* @return array
8586
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
8687
*/
8788
public function afterFetchAvailableFiles(
8889
LayoutUpdateManager $subject,
89-
array $handles,
90-
ProductInterface $product
91-
): array {
92-
if (!$product->getSku()) {
93-
return [];
94-
}
95-
90+
array $result,
91+
ProductInterface $product): array
92+
{
9693
$handles = $this->getLayoutProcessor()->getAvailableHandles();
9794

98-
return array_filter(
99-
array_map(
100-
function(string $handle) : ?string {
101-
preg_match(
102-
'/^catalog\_product\_view\_selectable\_0\_([a-z0-9]+)/i',
103-
$handle,
104-
$selectable
105-
);
106-
if (!empty($selectable[1])) {
107-
return $selectable[1];
108-
}
109-
110-
return null;
111-
},
112-
$handles
95+
return array_merge(
96+
$result,
97+
array_filter(
98+
array_map(
99+
function (string $handle): ?string {
100+
preg_match(
101+
'/^catalog\_product\_view\_selectable\_0\_([a-z0-9]+)/i',
102+
$handle,
103+
$selectable
104+
);
105+
if (!empty($selectable[1])) {
106+
return $selectable[1];
107+
}
108+
109+
return null;
110+
},
111+
$handles
112+
)
113113
)
114114
);
115115
}
@@ -130,8 +130,8 @@ public function afterExtractCustomSettings(
130130
LayoutUpdateManager $subject,
131131
$result,
132132
ProductInterface $product,
133-
DataObject $intoSettings
134-
): void {
133+
DataObject $intoSettings): void
134+
{
135135
if ($product->getSku() && $value = $this->extractAttributeValue($product)) {
136136
$handles = $intoSettings->getPageLayoutHandles() ?? [];
137137
$handles = array_merge_recursive(

src/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<type name="Magento\Catalog\Model\Product\Attribute\LayoutUpdateManager">
77
<plugin name="integernet_global_custom_layout" type="IntegerNet\GlobalCustomLayout\Plugin\ProductLayoutPlugin" />
88
</type>
9-
<type name="Magento\Cms\Model\Page\CustomLayout\CustomLayoutManager">
9+
<type name="Magento\Cms\Model\Page\CustomLayoutManagerInterface">
1010
<plugin name="integernet_global_custom_layout" type="IntegerNet\GlobalCustomLayout\Plugin\PageLayoutPlugin"/>
1111
</type>
1212
</config>

tests/Integration/AbstractFrontendControllerTest.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
namespace IntegerNet\GlobalCustomLayout\Test\Integration;
55

6-
use IntegerNet\GlobalCustomLayout\Test\Util\CategoryLayoutUpdateManager;
7-
use Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager;
6+
use IntegerNet\GlobalCustomLayout\Test\src\CategoryLayoutUpdateManager;
7+
use IntegerNet\GlobalCustomLayout\Test\src\PageLayoutUpdateManager;
8+
use IntegerNet\GlobalCustomLayout\Test\src\ProductLayoutUpdateManager;
89
use Magento\Framework\ObjectManagerInterface;
910
use Magento\Framework\View\LayoutInterface;
1011
use Magento\TestFramework\Helper\Bootstrap;
@@ -16,6 +17,14 @@
1617
*/
1718
abstract class AbstractFrontendControllerTest extends AbstractController
1819
{
20+
/** @var int */
21+
const STORE_ID = 0;
22+
23+
const TEST_FILE = 'test-file';
24+
25+
/** @var int */
26+
const GLOBAL_IDENTIFIER = 0;
27+
1928
/**
2029
* @var ObjectManagerInterface
2130
*/
@@ -24,7 +33,7 @@ abstract class AbstractFrontendControllerTest extends AbstractController
2433
/**
2534
* @var LayoutInterface
2635
*/
27-
protected $layoutInterface;
36+
protected $layoutInterface;
2837

2938
/**
3039
* @inheritdoc
@@ -44,10 +53,11 @@ private function setUpPreferences(): void
4453
$this->objectManager->configure(
4554
[
4655
'preferences' => [
47-
LayoutUpdateManager::class => CategoryLayoutUpdateManager::class,
48-
]
56+
\Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager::class => CategoryLayoutUpdateManager::class,
57+
\Magento\Catalog\Model\Product\Attribute\LayoutUpdateManager::class => ProductLayoutUpdateManager::class,
58+
\Magento\Cms\Model\Page\CustomLayoutManagerInterface::class => PageLayoutUpdateManager::class,
59+
],
4960
]
5061
);
51-
$this->objectManager->removeSharedInstance(LayoutUpdateManager::class);
5262
}
5363
}

0 commit comments

Comments
 (0)