Skip to content

Commit 40ec1d5

Browse files
author
Willem Wigman
committed
#4 Add test for Category views
1 parent 7b1fb99 commit 40ec1d5

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\GlobalCustomLayout\Test\Integration;
5+
6+
use IntegerNet\GlobalCustomLayout\Test\Util\CategoryLayoutUpdateManager;
7+
use Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager;
8+
use Magento\Catalog\Model\Session;
9+
use Magento\Framework\ObjectManagerInterface;
10+
use Magento\Framework\Registry;
11+
use Magento\Framework\View\LayoutInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\TestCase\AbstractController;
14+
15+
/**
16+
* @magentoAppIsolation enabled
17+
* @magentoAppArea frontend
18+
*/
19+
abstract class AbstractFrontendControllerTest extends AbstractController
20+
{
21+
/**
22+
* @var ObjectManagerInterface
23+
*/
24+
private $objectManager;
25+
26+
/**
27+
* @var Registry
28+
*/
29+
private $registry;
30+
31+
/**
32+
* @var Session
33+
*/
34+
private $session;
35+
36+
/**
37+
* @var LayoutInterface
38+
*/
39+
private $layout;
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
protected function setUp()
45+
{
46+
$this->objectManager = Bootstrap::getObjectManager();
47+
48+
$this->setUpPreferences();
49+
50+
parent::setUp();
51+
52+
$this->registry = $this->objectManager->get(Registry::class);
53+
$this->layout = $this->objectManager->get(LayoutInterface::class);
54+
$this->session = $this->objectManager->get(Session::class);
55+
}
56+
57+
private function setUpPreferences(): void
58+
{
59+
$this->objectManager->configure(
60+
[
61+
'preferences' => [
62+
LayoutUpdateManager::class => CategoryLayoutUpdateManager::class,
63+
]
64+
]
65+
);
66+
$this->objectManager->removeSharedInstance(LayoutUpdateManager::class);
67+
}
68+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\GlobalCustomLayout\Test\Integration;
5+
6+
use IntegerNet\GlobalCustomLayout\Test\Util\CategoryLayoutUpdateManager;
7+
use Magento\Catalog\Api\CategoryRepositoryInterface;
8+
use Magento\TestFramework\Helper\Bootstrap;
9+
10+
/**
11+
* Tests whether global layout handles are correctly saved on categories
12+
* and retrieved on the frontend on Category views
13+
*/
14+
class CategoryFrontendControllerTest extends AbstractFrontendControllerTest
15+
{
16+
/**
17+
* Check that Global Custom Layout Update files work for Category views.
18+
*
19+
* @return void
20+
* @throws \Magento\Framework\Exception\CouldNotSaveException
21+
* @throws \Magento\Framework\Exception\NoSuchEntityException
22+
*
23+
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_product_ids.php
24+
*/
25+
public function testViewWithGlobalCustomUpdate(): void
26+
{
27+
//Setting a fake file for the category.
28+
$file = 'test-file';
29+
$categoryId = 5;
30+
31+
/** @var CategoryLayoutUpdateManager $layoutManager */
32+
$layoutManager = Bootstrap::getObjectManager()->get(CategoryLayoutUpdateManager::class);
33+
$layoutManager->setCategoryFakeFiles(0, [$file]);
34+
35+
/** @var CategoryRepositoryInterface $categoryRepo */
36+
$categoryRepo = Bootstrap::getObjectManager()->create(CategoryRepositoryInterface::class);
37+
$category = $categoryRepo->get($categoryId);
38+
39+
//Updating the custom attribute.
40+
$category->setCustomAttribute('custom_layout_update_file', $file);
41+
$categoryRepo->save($category);
42+
43+
//Viewing the category
44+
$this->dispatch("catalog/category/view/id/$categoryId");
45+
46+
//Layout handles must contain the file.
47+
$handles = Bootstrap::getObjectManager()->get(\Magento\Framework\View\LayoutInterface::class)
48+
->getUpdate()
49+
->getHandles();
50+
$this->assertContains("catalog_category_view_selectable_0_{$file}", $handles);
51+
}
52+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace IntegerNet\GlobalCustomLayout\Test\Util;
5+
6+
use Magento\Catalog\Api\Data\CategoryInterface;
7+
8+
/**
9+
* Easy way to fake available files.
10+
*/
11+
class CategoryLayoutUpdateManager extends \Magento\TestFramework\Catalog\Model\CategoryLayoutUpdateManager
12+
{
13+
/**
14+
* @var array Keys are category IDs, values - file names.
15+
*/
16+
private $fakeFiles = [];
17+
18+
/**
19+
* Supply fake files for a category.
20+
*
21+
* @param int $forCategoryId
22+
* @param string[]|null $files Pass null to reset.
23+
*/
24+
public function setCategoryFakeFiles(int $forCategoryId, ?array $files): void
25+
{
26+
if ($files === null) {
27+
unset($this->fakeFiles[$forCategoryId]);
28+
} else {
29+
$this->fakeFiles[$forCategoryId] = $files;
30+
}
31+
}
32+
33+
/**
34+
* Fetches fake/mock files added through $this->setCategoryFakeFiles()
35+
*
36+
* @param CategoryInterface $category
37+
* @return array
38+
*/
39+
public function fetchAvailableFiles(CategoryInterface $category): array
40+
{
41+
if (array_key_exists(0, $this->fakeFiles)) {
42+
return $this->fakeFiles[0];
43+
}
44+
return parent::fetchAvailableFiles($category);
45+
}
46+
}

0 commit comments

Comments
 (0)