Skip to content

Commit 0536a32

Browse files
authored
Merge pull request #5 from integer-net/tests
#4 Add test for Category views
2 parents d80aef6 + c1270e0 commit 0536a32

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed
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\CategoryLayoutUpdateManager;
7+
use Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager;
8+
use Magento\Framework\ObjectManagerInterface;
9+
use Magento\Framework\View\LayoutInterface;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
use Magento\TestFramework\TestCase\AbstractController;
12+
13+
/**
14+
* @magentoAppIsolation enabled
15+
* @magentoAppArea frontend
16+
*/
17+
abstract class AbstractFrontendControllerTest extends AbstractController
18+
{
19+
/**
20+
* @var ObjectManagerInterface
21+
*/
22+
protected $objectManager;
23+
24+
/**
25+
* @var LayoutInterface
26+
*/
27+
protected $layoutInterface;
28+
29+
/**
30+
* @inheritdoc
31+
*/
32+
protected function setUp()
33+
{
34+
$this->objectManager = Bootstrap::getObjectManager();
35+
$this->layoutInterface = $this->objectManager->get(LayoutInterface::class);
36+
37+
$this->setUpPreferences();
38+
39+
parent::setUp();
40+
}
41+
42+
private function setUpPreferences(): void
43+
{
44+
$this->objectManager->configure(
45+
[
46+
'preferences' => [
47+
LayoutUpdateManager::class => CategoryLayoutUpdateManager::class,
48+
]
49+
]
50+
);
51+
$this->objectManager->removeSharedInstance(LayoutUpdateManager::class);
52+
}
53+
}
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\CategoryLayoutUpdateManager;
7+
use Magento\Catalog\Api\CategoryRepositoryInterface;
8+
use Magento\Framework\Exception\CouldNotSaveException;
9+
use Magento\Framework\Exception\NoSuchEntityException;
10+
11+
/**
12+
* Tests whether global layout handles are correctly saved on categories
13+
* and retrieved on the frontend on Category views
14+
*/
15+
class CategoryFrontendControllerTest extends AbstractFrontendControllerTest
16+
{
17+
/**
18+
* Check that Global Custom Layout Update files work for Category views.
19+
*
20+
* @return void
21+
* @throws CouldNotSaveException
22+
* @throws NoSuchEntityException
23+
*
24+
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_product_ids.php
25+
*/
26+
public function testViewWithGlobalCustomUpdate(): void
27+
{
28+
//Setting a fake file for the category.
29+
$file = 'test-file';
30+
$categoryId = 5;
31+
32+
/** @var CategoryLayoutUpdateManager $layoutManager */
33+
$layoutManager = $this->objectManager->get(CategoryLayoutUpdateManager::class);
34+
$layoutManager->setCategoryFakeFiles(0, [$file]);
35+
36+
/** @var CategoryRepositoryInterface $categoryRepo */
37+
$categoryRepo = $this->objectManager->create(CategoryRepositoryInterface::class);
38+
$category = $categoryRepo->get($categoryId);
39+
40+
//Updating the custom attribute.
41+
$category->setCustomAttribute('custom_layout_update_file', $file);
42+
$categoryRepo->save($category);
43+
44+
//Viewing the category
45+
$this->dispatch("catalog/category/view/id/$categoryId");
46+
47+
//Layout handles must contain the file.
48+
$handles = $this->layoutInterface
49+
->getUpdate()
50+
->getHandles();
51+
$this->assertContains("catalog_category_view_selectable_0_{$file}", $handles);
52+
}
53+
}
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)