Skip to content

Commit 84083ac

Browse files
author
Willem Wigman
committed
#4 add Tests for Product and Page. Fix issue in Page and Product plugin where handles were not merged
1 parent c1270e0 commit 84083ac

8 files changed

+282
-18
lines changed

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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ private function getLayoutProcessor(): LayoutProcessor
7979
* Fetch list of available global files/handles for the product.
8080
*
8181
* @param LayoutUpdateManager $subject
82-
* @param array $handles
83-
* @param CategoryInterface $category
82+
* @param array $result
83+
* @param ProductInterface $product
8484
* @return array
8585
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
8686
*/
8787
public function afterFetchAvailableFiles(
8888
LayoutUpdateManager $subject,
89-
array $handles,
89+
array $result,
9090
ProductInterface $product
9191
): array {
9292
if (!$product->getSku()) {
@@ -95,7 +95,7 @@ public function afterFetchAvailableFiles(
9595

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

98-
return array_filter(
98+
return array_merge($result, array_filter(
9999
array_map(
100100
function(string $handle) : ?string {
101101
preg_match(
@@ -111,7 +111,7 @@ function(string $handle) : ?string {
111111
},
112112
$handles
113113
)
114-
);
114+
));
115115
}
116116

117117
/**

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: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
namespace IntegerNet\GlobalCustomLayout\Test\Integration;
55

66
use IntegerNet\GlobalCustomLayout\Test\Util\CategoryLayoutUpdateManager;
7-
use Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager;
7+
use IntegerNet\GlobalCustomLayout\Test\Util\CustomLayoutManager;
8+
use IntegerNet\GlobalCustomLayout\Test\Util\ProductLayoutUpdateManager;
89
use Magento\Framework\ObjectManagerInterface;
910
use Magento\Framework\View\LayoutInterface;
1011
use Magento\TestFramework\Helper\Bootstrap;
@@ -24,7 +25,7 @@ abstract class AbstractFrontendControllerTest extends AbstractController
2425
/**
2526
* @var LayoutInterface
2627
*/
27-
protected $layoutInterface;
28+
protected $layoutInterface;
2829

2930
/**
3031
* @inheritdoc
@@ -44,10 +45,11 @@ private function setUpPreferences(): void
4445
$this->objectManager->configure(
4546
[
4647
'preferences' => [
47-
LayoutUpdateManager::class => CategoryLayoutUpdateManager::class,
48+
\Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager::class => CategoryLayoutUpdateManager::class,
49+
\Magento\Catalog\Model\Product\Attribute\LayoutUpdateManager::class => ProductLayoutUpdateManager::class,
50+
\Magento\Cms\Model\Page\CustomLayoutManagerInterface::class => CustomLayoutManager::class,
4851
]
4952
]
5053
);
51-
$this->objectManager->removeSharedInstance(LayoutUpdateManager::class);
5254
}
5355
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\GlobalCustomLayout\Test\Integration;
5+
6+
use IntegerNet\GlobalCustomLayout\Test\Util\CustomLayoutManager;
7+
use IntegerNet\GlobalCustomLayout\Test\Util\PageLayoutUpdateManager;
8+
use Magento\Cms\Api\Data\PageInterface;
9+
use Magento\Cms\Model\Page as PageModel;
10+
use Magento\Cms\Model\Page\CustomLayoutRepositoryInterface;
11+
use Magento\Cms\Model\PageFactory as PageModelFactory;
12+
use Magento\Cms\Model\ResourceModel\Page as PageResourceModel;
13+
use Magento\Framework\Exception\AlreadyExistsException;
14+
15+
class PageFrontendControllerTest extends AbstractFrontendControllerTest
16+
{
17+
/** @var CustomLayoutManager */
18+
protected $layoutManager;
19+
20+
/** @var PageModel */
21+
protected $page;
22+
23+
/** @var PageResourceModel */
24+
protected $pageResource;
25+
26+
/** @var pageFactory $pageFactory */
27+
protected $pageFactory;
28+
29+
protected function getPage(int $storeId, string $pageIdentifier): pageModel
30+
{
31+
/** @var CustomLayoutManager $layoutManager */
32+
$layoutManager = $this->getLayoutManager();
33+
34+
/** @var PageResourceModel $pageResource */
35+
$pageResource = $this->getPageResource();
36+
37+
/** @var CustomLayoutRepositoryInterface $layoutRepo */
38+
$layoutRepo = $this->objectManager->create(
39+
CustomLayoutRepositoryInterface::class,
40+
['manager' => $layoutManager]
41+
);
42+
43+
/** @var PageModelFactory $pageFactory */
44+
$pageFactory = $this->objectManager->get(PageModelFactory::class);
45+
46+
/** @var PageModel $page */
47+
$page = $pageFactory->create(['customLayoutRepository' => $layoutRepo]);
48+
49+
$page->setStoreId($storeId);
50+
$pageResource->load($page, $pageIdentifier, PageInterface::IDENTIFIER);
51+
52+
return $page;
53+
}
54+
55+
/**
56+
* Check that custom handles are applied when rendering a page.
57+
*
58+
* @return void
59+
* @throws AlreadyExistsException
60+
* @magentoDataFixture Magento/Cms/_files/pages.php
61+
*/
62+
public function testViewWithGlobalCustomUpdate(): void
63+
{
64+
$file = 'test-file';
65+
$pageIdentifier = 'page100';
66+
$storeId = 0;
67+
68+
/** @var pageModel $page */
69+
$page = $this->getPage($storeId, $pageIdentifier);
70+
/** @var CustomLayoutManager $layoutManager */
71+
$layoutManager = $this->getLayoutManager();
72+
73+
/** @var PageResourceModel $pageResource */
74+
$pageResource = $this->getPageResource();
75+
76+
$pageId = $page->getId();
77+
$layoutManager->fakeAvailableFiles(0, [$file]);
78+
79+
//Updating the custom attribute.
80+
$page->setData('layout_update_selected', $file);
81+
$pageResource->save($page);
82+
83+
$this->dispatch('/cms/page/view/page_id/' . $pageId);
84+
85+
$handles = $this->layoutInterface->getUpdate()->getHandles();
86+
$this->assertContains("cms_page_view_selectable_0_{$file}", $handles);
87+
}
88+
89+
/**
90+
* @return PageResourceModel
91+
*/
92+
protected function getPageResource(): PageResourceModel
93+
{
94+
if (!$this->pageResource) {
95+
$this->pageResource = $this->objectManager->get(PageResourceModel::class);
96+
}
97+
return $this->pageResource;
98+
}
99+
100+
/**
101+
* @return CustomLayoutManager
102+
*/
103+
protected function getLayoutManager(): CustomLayoutManager
104+
{
105+
if (!$this->layoutManager) {
106+
$this->layoutManager = $this->objectManager->get(CustomLayoutManager::class);
107+
}
108+
return $this->layoutManager;
109+
}
110+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\GlobalCustomLayout\Test\Integration;
5+
6+
use IntegerNet\GlobalCustomLayout\Test\Util\ProductLayoutUpdateManager;
7+
use Magento\Catalog\Api\ProductRepositoryInterface;
8+
use Magento\Framework\Exception\CouldNotSaveException;
9+
use Magento\Framework\Exception\InputException;
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Framework\Exception\StateException;
12+
13+
class ProductFrontendControllerTest extends AbstractFrontendControllerTest
14+
{
15+
/**
16+
* Check that Global Custom Layout Update files work for Product views.
17+
*
18+
* @magentoDataFixture Magento/Catalog/controllers/_files/products.php
19+
* @return void
20+
* @throws CouldNotSaveException
21+
* @throws InputException
22+
* @throws NoSuchEntityException
23+
* @throws StateException
24+
*/
25+
public function testViewWithGlobalCustomUpdate(): void
26+
{
27+
//Setting a fake file for the product.
28+
$file = 'test-file';
29+
30+
/** @var ProductRepositoryInterface $repository */
31+
$repository = $this->objectManager->create(ProductRepositoryInterface::class);
32+
$sku = 'simple_product_1';
33+
$product = $repository->get($sku);
34+
$productId = $product->getId();
35+
36+
/** @var ProductLayoutUpdateManager $layoutManager */
37+
$layoutManager = $this->objectManager->get(ProductLayoutUpdateManager::class);
38+
$layoutManager->setFakeFiles(0, [$file]);
39+
40+
//Updating the custom attribute.
41+
$product->setCustomAttribute('custom_layout_update_file', $file);
42+
$repository->save($product);
43+
44+
//Viewing the product
45+
$this->dispatch("catalog/product/view/id/$productId");
46+
47+
//Layout handles must contain the file.
48+
$handles = $this->layoutInterface
49+
->getUpdate()
50+
->getHandles();
51+
$this->assertContains("catalog_product_view_selectable_0_{$file}", $handles);
52+
}
53+
}

tests/Util/CustomLayoutManager.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\GlobalCustomLayout\Test\Util;
5+
6+
use Magento\Cms\Api\Data\PageInterface;
7+
8+
/**
9+
* Manager allowing to fake available files.
10+
*/
11+
class CustomLayoutManager extends \Magento\TestFramework\Cms\Model\CustomLayoutManager
12+
{
13+
/**
14+
* @var string[][]
15+
*/
16+
private $files = [];
17+
18+
/**
19+
* Fake available files for given page.
20+
*
21+
* Pass null to unassign fake files.
22+
*
23+
* @param int $forPageId
24+
* @param string[]|null $files
25+
* @return void
26+
*/
27+
public function fakeAvailableFiles(int $forPageId, ?array $files): void
28+
{
29+
var_dump('added');
30+
if ($files === null) {
31+
unset($this->files[$forPageId]);
32+
} else {
33+
$this->files[$forPageId] = $files;
34+
}
35+
}
36+
37+
/**
38+
* Fetches fake/mock files added through $this->fakeAvailableFiles()
39+
*
40+
* @param PageInterface $page
41+
* @return array
42+
*/
43+
public function fetchAvailableFiles(PageInterface $page): array
44+
{
45+
if (array_key_exists(0, $this->files)) {
46+
return $this->files[0];
47+
}
48+
49+
return parent::fetchAvailableFiles($page);
50+
}
51+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\GlobalCustomLayout\Test\Util;
5+
6+
use Magento\Catalog\Api\Data\ProductInterface;
7+
8+
/**
9+
* Easy way to fake available files.
10+
*/
11+
class ProductLayoutUpdateManager extends \Magento\TestFramework\Catalog\Model\ProductLayoutUpdateManager
12+
{
13+
/**
14+
* @var array Keys are product IDs, values - file names.
15+
*/
16+
private $fakeFiles = [];
17+
18+
/**
19+
* Supply fake files for a product.
20+
*
21+
* @param int $forProductId
22+
* @param string[]|null $files Pass null to reset.
23+
*/
24+
public function setFakeFiles(int $forProductId, ?array $files): void
25+
{
26+
if ($files === null) {
27+
unset($this->fakeFiles[$forProductId]);
28+
} else {
29+
$this->fakeFiles[$forProductId] = $files;
30+
}
31+
}
32+
33+
/**
34+
* Fetches fake/mock files added through $this->setFakeFiles()
35+
*
36+
* @param ProductInterface $product
37+
* @return array
38+
*/
39+
public function fetchAvailableFiles(ProductInterface $product): array
40+
{
41+
if (array_key_exists(0, $this->fakeFiles)) {
42+
return $this->fakeFiles[0];
43+
}
44+
45+
return parent::fetchAvailableFiles($product);
46+
}
47+
}

0 commit comments

Comments
 (0)