Skip to content

#4 add Tests for Product and Page. Fix issue in Page and Product plug… #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 1.0.0 - 2020-04-27
### Added
- Plugins for category, product and page layouts that allow custom layout files to be loaded with a global identifier
(`0`). E.g. `catalog_category_view_selectable_0_.xml` for Categories.

## 1.1.0 - 2020-05-02
### Added
- Adds frontend test coverage for global custom layout updates
- 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.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ More info on default behaviour of selectable layouts:
Zero configuration needed.
## Change log
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Testing
### Magento Integration Tests
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)
1. Copy `Test/Integration/phpunit.xml.dist` from the package to `dev/tests/integration/phpunit.xml` in your Magento installation.
2. In that directory, run
``` bash
../../../vendor/bin/phpunit
```
## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "integer-net/magento2-global-custom-layout",
"version": "1.0.0",
"version": "1.1.0",
"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",
"authors": [
{
Expand Down
50 changes: 23 additions & 27 deletions src/Plugin/CategoryLayoutPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ class CategoryLayoutPlugin
public function __construct(
FlyweightFactory $themeFactory,
DesignInterface $design,
LayoutProcessorFactory $layoutProcessorFactory
)
LayoutProcessorFactory $layoutProcessorFactory)
{
$this->themeFactory = $themeFactory;
$this->design = $design;
Expand All @@ -66,7 +65,7 @@ private function getLayoutProcessor(): LayoutProcessor
[
'theme' => $this->themeFactory->create(
$this->design->getConfigurationDesignTheme(Area::AREA_FRONTEND)
)
),
]
);
$this->themeFactory = null;
Expand All @@ -88,32 +87,30 @@ private function getLayoutProcessor(): LayoutProcessor
public function afterFetchAvailableFiles(
LayoutUpdateManager $subject,
array $result,
CategoryInterface $category
): array
CategoryInterface $category): array
{
if (!$category->getId()) {
return $result;
}

$handles = $this->getLayoutProcessor()->getAvailableHandles();

return array_merge($result, array_filter(
array_map(
function(string $handle) use ($category) : ?string {
preg_match(
'/^catalog\_category\_view\_selectable\_0\_([a-z0-9]+)/i',
$handle,
$selectable
);
if (!empty($selectable[1])) {
return $selectable[1];
}

return null;
},
$handles
return array_merge(
$result,
array_filter(
array_map(
function (string $handle): ?string {
preg_match(
'/^catalog\_category\_view\_selectable\_0\_([a-z0-9]+)/i',
$handle,
$selectable
);
if (!empty($selectable[1])) {
return $selectable[1];
}

return null;
},
$handles
)
)
));
);
}

/**
Expand All @@ -132,8 +129,7 @@ public function afterExtractCustomSettings(
LayoutUpdateManager $subject,
$result,
CategoryInterface $category,
DataObject $intoSettings
): void
DataObject $intoSettings): void
{
if ($category->getId() && $value = $this->extractAttributeValue($category)) {
$handles = $intoSettings->getPageLayoutHandles() ?? [];
Expand Down
17 changes: 9 additions & 8 deletions src/Plugin/PageLayoutPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Magento\Cms\Api\Data\PageInterface;
use Magento\Cms\Api\PageRepositoryInterface;
use Magento\Cms\Model\Page\CustomLayoutManagerInterface;
use Magento\Cms\Model\Page\CustomLayout\CustomLayoutManager;
use Magento\Cms\Model\Page\CustomLayout\Data\CustomLayoutSelectedInterface;
use Magento\Cms\Model\Page\IdentityMap;
Expand Down Expand Up @@ -95,20 +96,20 @@ private function getLayoutProcessor(): LayoutProcessor
/**
* Fetch list of available global files/handles for the page.
*
* @param CustomLayoutManager $subject
* @param array $handles
* @param CustomLayoutManagerInterface $subject
* @param array $result
* @param PageInterface $page
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterFetchAvailableFiles(
CustomLayoutManager $subject,
array $handles,
CustomLayoutManagerInterface $subject,
array $result,
PageInterface $page
): array {
$handles = $this->getLayoutProcessor()->getAvailableHandles();

return array_filter(
return array_merge($result, array_filter(
array_map(
function(string $handle) : ?string {
preg_match(
Expand All @@ -124,18 +125,18 @@ function(string $handle) : ?string {
},
$handles
)
);
));
}

/**
* @param CustomLayoutManager $subject
* @param CustomLayoutManagerInterface $subject
* @param $result
* @param PageLayout $layout
* @param CustomLayoutSelectedInterface $layoutSelected
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterApplyUpdate(
CustomLayoutManager $subject,
CustomLayoutManagerInterface $subject,
$result,
PageLayout $layout,
CustomLayoutSelectedInterface $layoutSelected
Expand Down
60 changes: 30 additions & 30 deletions src/Plugin/ProductLayoutPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
use Magento\Framework\View\Model\Layout\Merge as LayoutProcessor;
use Magento\Framework\View\Model\Layout\MergeFactory as LayoutProcessorFactory;

class ProductLayoutPlugin {
class ProductLayoutPlugin
{

/**
* @var FlyweightFactory
Expand Down Expand Up @@ -44,8 +45,8 @@ class ProductLayoutPlugin {
public function __construct(
FlyweightFactory $themeFactory,
DesignInterface $design,
LayoutProcessorFactory $layoutProcessorFactory
) {
LayoutProcessorFactory $layoutProcessorFactory)
{
$this->themeFactory = $themeFactory;
$this->design = $design;
$this->layoutProcessorFactory = $layoutProcessorFactory;
Expand All @@ -65,7 +66,7 @@ private function getLayoutProcessor(): LayoutProcessor
[
'theme' => $this->themeFactory->create(
$this->design->getConfigurationDesignTheme(Area::AREA_FRONTEND)
)
),
]
);
$this->themeFactory = null;
Expand All @@ -79,37 +80,36 @@ private function getLayoutProcessor(): LayoutProcessor
* Fetch list of available global files/handles for the product.
*
* @param LayoutUpdateManager $subject
* @param array $handles
* @param CategoryInterface $category
* @param array $result
* @param ProductInterface $product
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterFetchAvailableFiles(
LayoutUpdateManager $subject,
array $handles,
ProductInterface $product
): array {
if (!$product->getSku()) {
return [];
}

array $result,
ProductInterface $product): array
{
$handles = $this->getLayoutProcessor()->getAvailableHandles();

return array_filter(
array_map(
function(string $handle) : ?string {
preg_match(
'/^catalog\_product\_view\_selectable\_0\_([a-z0-9]+)/i',
$handle,
$selectable
);
if (!empty($selectable[1])) {
return $selectable[1];
}

return null;
},
$handles
return array_merge(
$result,
array_filter(
array_map(
function (string $handle): ?string {
preg_match(
'/^catalog\_product\_view\_selectable\_0\_([a-z0-9]+)/i',
$handle,
$selectable
);
if (!empty($selectable[1])) {
return $selectable[1];
}

return null;
},
$handles
)
)
);
}
Expand All @@ -130,8 +130,8 @@ public function afterExtractCustomSettings(
LayoutUpdateManager $subject,
$result,
ProductInterface $product,
DataObject $intoSettings
): void {
DataObject $intoSettings): void
{
if ($product->getSku() && $value = $this->extractAttributeValue($product)) {
$handles = $intoSettings->getPageLayoutHandles() ?? [];
$handles = array_merge_recursive(
Expand Down
2 changes: 1 addition & 1 deletion src/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<type name="Magento\Catalog\Model\Product\Attribute\LayoutUpdateManager">
<plugin name="integernet_global_custom_layout" type="IntegerNet\GlobalCustomLayout\Plugin\ProductLayoutPlugin" />
</type>
<type name="Magento\Cms\Model\Page\CustomLayout\CustomLayoutManager">
<type name="Magento\Cms\Model\Page\CustomLayoutManagerInterface">
<plugin name="integernet_global_custom_layout" type="IntegerNet\GlobalCustomLayout\Plugin\PageLayoutPlugin"/>
</type>
</config>
22 changes: 16 additions & 6 deletions tests/Integration/AbstractFrontendControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

namespace IntegerNet\GlobalCustomLayout\Test\Integration;

use IntegerNet\GlobalCustomLayout\Test\Util\CategoryLayoutUpdateManager;
use Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager;
use IntegerNet\GlobalCustomLayout\Test\src\CategoryLayoutUpdateManager;
use IntegerNet\GlobalCustomLayout\Test\src\PageLayoutUpdateManager;
use IntegerNet\GlobalCustomLayout\Test\src\ProductLayoutUpdateManager;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\TestFramework\Helper\Bootstrap;
Expand All @@ -16,6 +17,14 @@
*/
abstract class AbstractFrontendControllerTest extends AbstractController
{
/** @var int */
const STORE_ID = 0;

const TEST_FILE = 'test-file';

/** @var int */
const GLOBAL_IDENTIFIER = 0;

/**
* @var ObjectManagerInterface
*/
Expand All @@ -24,7 +33,7 @@ abstract class AbstractFrontendControllerTest extends AbstractController
/**
* @var LayoutInterface
*/
protected $layoutInterface;
protected $layoutInterface;

/**
* @inheritdoc
Expand All @@ -44,10 +53,11 @@ private function setUpPreferences(): void
$this->objectManager->configure(
[
'preferences' => [
LayoutUpdateManager::class => CategoryLayoutUpdateManager::class,
]
\Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager::class => CategoryLayoutUpdateManager::class,
\Magento\Catalog\Model\Product\Attribute\LayoutUpdateManager::class => ProductLayoutUpdateManager::class,
\Magento\Cms\Model\Page\CustomLayoutManagerInterface::class => PageLayoutUpdateManager::class,
],
]
);
$this->objectManager->removeSharedInstance(LayoutUpdateManager::class);
}
}
Loading