-
Notifications
You must be signed in to change notification settings - Fork 9
#4 Add test for Category views #5
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
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace IntegerNet\GlobalCustomLayout\Test\Integration; | ||
|
||
use IntegerNet\GlobalCustomLayout\Test\Util\CategoryLayoutUpdateManager; | ||
use Magento\Catalog\Model\Category\Attribute\LayoutUpdateManager; | ||
use Magento\Catalog\Model\Session; | ||
use Magento\Framework\ObjectManagerInterface; | ||
use Magento\Framework\Registry; | ||
use Magento\Framework\View\LayoutInterface; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\TestFramework\TestCase\AbstractController; | ||
|
||
/** | ||
* @magentoAppIsolation enabled | ||
* @magentoAppArea frontend | ||
*/ | ||
abstract class AbstractFrontendControllerTest extends AbstractController | ||
{ | ||
/** | ||
* @var ObjectManagerInterface | ||
*/ | ||
private $objectManager; | ||
|
||
/** | ||
* @var Registry | ||
*/ | ||
private $registry; | ||
|
||
/** | ||
* @var Session | ||
*/ | ||
private $session; | ||
|
||
/** | ||
* @var LayoutInterface | ||
*/ | ||
private $layout; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->objectManager = Bootstrap::getObjectManager(); | ||
|
||
$this->setUpPreferences(); | ||
|
||
parent::setUp(); | ||
|
||
$this->registry = $this->objectManager->get(Registry::class); | ||
$this->layout = $this->objectManager->get(LayoutInterface::class); | ||
$this->session = $this->objectManager->get(Session::class); | ||
} | ||
|
||
private function setUpPreferences(): void | ||
{ | ||
$this->objectManager->configure( | ||
[ | ||
'preferences' => [ | ||
LayoutUpdateManager::class => CategoryLayoutUpdateManager::class, | ||
] | ||
] | ||
); | ||
$this->objectManager->removeSharedInstance(LayoutUpdateManager::class); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace IntegerNet\GlobalCustomLayout\Test\Integration; | ||
|
||
use IntegerNet\GlobalCustomLayout\Test\Util\CategoryLayoutUpdateManager; | ||
use Magento\Catalog\Api\CategoryRepositoryInterface; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
|
||
/** | ||
* Tests whether global layout handles are correctly saved on categories | ||
* and retrieved on the frontend on Category views | ||
*/ | ||
class CategoryFrontendControllerTest extends AbstractFrontendControllerTest | ||
{ | ||
/** | ||
* Check that Global Custom Layout Update files work for Category views. | ||
* | ||
* @return void | ||
* @throws \Magento\Framework\Exception\CouldNotSaveException | ||
* @throws \Magento\Framework\Exception\NoSuchEntityException | ||
* | ||
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories_with_product_ids.php | ||
*/ | ||
public function testViewWithGlobalCustomUpdate(): void | ||
{ | ||
//Setting a fake file for the category. | ||
$file = 'test-file'; | ||
$categoryId = 5; | ||
|
||
/** @var CategoryLayoutUpdateManager $layoutManager */ | ||
$layoutManager = Bootstrap::getObjectManager()->get(CategoryLayoutUpdateManager::class); | ||
wigman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$layoutManager->setCategoryFakeFiles(0, [$file]); | ||
|
||
/** @var CategoryRepositoryInterface $categoryRepo */ | ||
$categoryRepo = Bootstrap::getObjectManager()->create(CategoryRepositoryInterface::class); | ||
$category = $categoryRepo->get($categoryId); | ||
|
||
//Updating the custom attribute. | ||
$category->setCustomAttribute('custom_layout_update_file', $file); | ||
$categoryRepo->save($category); | ||
|
||
//Viewing the category | ||
$this->dispatch("catalog/category/view/id/$categoryId"); | ||
|
||
//Layout handles must contain the file. | ||
$handles = Bootstrap::getObjectManager()->get(\Magento\Framework\View\LayoutInterface::class) | ||
->getUpdate() | ||
->getHandles(); | ||
$this->assertContains("catalog_category_view_selectable_0_{$file}", $handles); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace IntegerNet\GlobalCustomLayout\Test\Util; | ||
|
||
use Magento\Catalog\Api\Data\CategoryInterface; | ||
|
||
/** | ||
* Easy way to fake available files. | ||
*/ | ||
class CategoryLayoutUpdateManager extends \Magento\TestFramework\Catalog\Model\CategoryLayoutUpdateManager | ||
{ | ||
/** | ||
* @var array Keys are category IDs, values - file names. | ||
*/ | ||
private $fakeFiles = []; | ||
|
||
/** | ||
* Supply fake files for a category. | ||
* | ||
* @param int $forCategoryId | ||
* @param string[]|null $files Pass null to reset. | ||
*/ | ||
public function setCategoryFakeFiles(int $forCategoryId, ?array $files): void | ||
{ | ||
if ($files === null) { | ||
unset($this->fakeFiles[$forCategoryId]); | ||
} else { | ||
$this->fakeFiles[$forCategoryId] = $files; | ||
} | ||
} | ||
|
||
/** | ||
* Fetches fake/mock files added through $this->setCategoryFakeFiles() | ||
* | ||
* @param CategoryInterface $category | ||
* @return array | ||
*/ | ||
public function fetchAvailableFiles(CategoryInterface $category): array | ||
{ | ||
if (array_key_exists(0, $this->fakeFiles)) { | ||
return $this->fakeFiles[0]; | ||
} | ||
return parent::fetchAvailableFiles($category); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.