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 4 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
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
10 changes: 5 additions & 5 deletions src/Plugin/ProductLayoutPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ 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,
array $result,
ProductInterface $product
): array {
if (!$product->getSku()) {
Expand All @@ -95,7 +95,7 @@ public function afterFetchAvailableFiles(

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

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

/**
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>
10 changes: 6 additions & 4 deletions tests/Integration/AbstractFrontendControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
namespace IntegerNet\GlobalCustomLayout\Test\Integration;

use IntegerNet\GlobalCustomLayout\Test\Util\CategoryLayoutUpdateManager;
use Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager;
use IntegerNet\GlobalCustomLayout\Test\Util\CustomLayoutManager;
use IntegerNet\GlobalCustomLayout\Test\Util\ProductLayoutUpdateManager;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\TestFramework\Helper\Bootstrap;
Expand All @@ -24,7 +25,7 @@ abstract class AbstractFrontendControllerTest extends AbstractController
/**
* @var LayoutInterface
*/
protected $layoutInterface;
protected $layoutInterface;

/**
* @inheritdoc
Expand All @@ -44,10 +45,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 => CustomLayoutManager::class,
]
]
);
$this->objectManager->removeSharedInstance(LayoutUpdateManager::class);
}
}
110 changes: 110 additions & 0 deletions tests/Integration/PageFrontendControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
declare(strict_types=1);

namespace IntegerNet\GlobalCustomLayout\Test\Integration;

use IntegerNet\GlobalCustomLayout\Test\Util\CustomLayoutManager;
use IntegerNet\GlobalCustomLayout\Test\Util\PageLayoutUpdateManager;
use Magento\Cms\Api\Data\PageInterface;
use Magento\Cms\Model\Page as PageModel;
use Magento\Cms\Model\Page\CustomLayoutRepositoryInterface;
use Magento\Cms\Model\PageFactory as PageModelFactory;
use Magento\Cms\Model\ResourceModel\Page as PageResourceModel;
use Magento\Framework\Exception\AlreadyExistsException;

class PageFrontendControllerTest extends AbstractFrontendControllerTest
{
/** @var CustomLayoutManager */
protected $layoutManager;

/** @var PageModel */
protected $page;

/** @var PageResourceModel */
protected $pageResource;

/** @var pageFactory $pageFactory */
protected $pageFactory;

protected function getPage(int $storeId, string $pageIdentifier): pageModel
{
/** @var CustomLayoutManager $layoutManager */
$layoutManager = $this->getLayoutManager();

/** @var PageResourceModel $pageResource */
$pageResource = $this->getPageResource();

/** @var CustomLayoutRepositoryInterface $layoutRepo */
$layoutRepo = $this->objectManager->create(
CustomLayoutRepositoryInterface::class,
['manager' => $layoutManager]
);

/** @var PageModelFactory $pageFactory */
$pageFactory = $this->objectManager->get(PageModelFactory::class);

/** @var PageModel $page */
$page = $pageFactory->create(['customLayoutRepository' => $layoutRepo]);

$page->setStoreId($storeId);
$pageResource->load($page, $pageIdentifier, PageInterface::IDENTIFIER);

return $page;
}

/**
* Check that custom handles are applied when rendering a page.
*
* @return void
* @throws AlreadyExistsException
* @magentoDataFixture Magento/Cms/_files/pages.php
*/
public function testViewWithGlobalCustomUpdate(): void
{
$file = 'test-file';
$pageIdentifier = 'page100';
$storeId = 0;

/** @var pageModel $page */
$page = $this->getPage($storeId, $pageIdentifier);
/** @var CustomLayoutManager $layoutManager */
$layoutManager = $this->getLayoutManager();

/** @var PageResourceModel $pageResource */
$pageResource = $this->getPageResource();

$pageId = $page->getId();
$layoutManager->fakeAvailableFiles(0, [$file]);

//Updating the custom attribute.
$page->setData('layout_update_selected', $file);
$pageResource->save($page);

$this->dispatch('/cms/page/view/page_id/' . $pageId);

$handles = $this->layoutInterface->getUpdate()->getHandles();
$this->assertContains("cms_page_view_selectable_0_{$file}", $handles);
}

/**
* @return PageResourceModel
*/
protected function getPageResource(): PageResourceModel
{
if (!$this->pageResource) {
$this->pageResource = $this->objectManager->get(PageResourceModel::class);
}
return $this->pageResource;
}

/**
* @return CustomLayoutManager
*/
protected function getLayoutManager(): CustomLayoutManager
{
if (!$this->layoutManager) {
$this->layoutManager = $this->objectManager->get(CustomLayoutManager::class);
}
return $this->layoutManager;
}
}
53 changes: 53 additions & 0 deletions tests/Integration/ProductFrontendControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);

namespace IntegerNet\GlobalCustomLayout\Test\Integration;

use IntegerNet\GlobalCustomLayout\Test\Util\ProductLayoutUpdateManager;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;

class ProductFrontendControllerTest extends AbstractFrontendControllerTest
{
/**
* Check that Global Custom Layout Update files work for Product views.
*
* @magentoDataFixture Magento/Catalog/controllers/_files/products.php
* @return void
* @throws CouldNotSaveException
* @throws InputException
* @throws NoSuchEntityException
* @throws StateException
*/
public function testViewWithGlobalCustomUpdate(): void
{
//Setting a fake file for the product.
$file = 'test-file';

/** @var ProductRepositoryInterface $repository */
$repository = $this->objectManager->create(ProductRepositoryInterface::class);
$sku = 'simple_product_1';
$product = $repository->get($sku);
$productId = $product->getId();

/** @var ProductLayoutUpdateManager $layoutManager */
$layoutManager = $this->objectManager->get(ProductLayoutUpdateManager::class);
$layoutManager->setFakeFiles(0, [$file]);

//Updating the custom attribute.
$product->setCustomAttribute('custom_layout_update_file', $file);
$repository->save($product);

//Viewing the product
$this->dispatch("catalog/product/view/id/$productId");

//Layout handles must contain the file.
$handles = $this->layoutInterface
->getUpdate()
->getHandles();
$this->assertContains("catalog_product_view_selectable_0_{$file}", $handles);
}
}
50 changes: 50 additions & 0 deletions tests/Util/CustomLayoutManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);

namespace IntegerNet\GlobalCustomLayout\Test\Util;

use Magento\Cms\Api\Data\PageInterface;

/**
* Manager allowing to fake available files.
*/
class CustomLayoutManager extends \Magento\TestFramework\Cms\Model\CustomLayoutManager
{
/**
* @var string[][]
*/
private $files = [];

/**
* Fake available files for given page.
*
* Pass null to unassign fake files.
*
* @param int $forPageId
* @param string[]|null $files
* @return void
*/
public function fakeAvailableFiles(int $forPageId, ?array $files): void
{
if ($files === null) {
unset($this->files[$forPageId]);
} else {
$this->files[$forPageId] = $files;
}
}

/**
* Fetches fake/mock files added through $this->fakeAvailableFiles()
*
* @param PageInterface $page
* @return array
*/
public function fetchAvailableFiles(PageInterface $page): array
{
if (array_key_exists(0, $this->files)) {
return $this->files[0];
}

return parent::fetchAvailableFiles($page);
}
}
Loading