Skip to content

Commit d46371a

Browse files
committed
MC-37543: Create automated test for "Add static block on a category page"
1 parent 48801ae commit d46371a

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

dev/tests/integration/testsuite/Magento/Catalog/Block/Category/ViewTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\ObjectManagerInterface;
1313
use Magento\Framework\Registry;
1414
use Magento\Framework\View\LayoutInterface;
15+
use Magento\Store\Model\StoreManagerInterface;
1516
use Magento\TestFramework\Catalog\Model\GetCategoryByName;
1617
use Magento\TestFramework\Helper\Bootstrap;
1718
use PHPUnit\Framework\TestCase;
@@ -39,6 +40,9 @@ class ViewTest extends TestCase
3940
/** @var LayoutInterface */
4041
private $layout;
4142

43+
/** @var StoreManagerInterface */
44+
private $storeManager;
45+
4246
/**
4347
* @inheritdoc
4448
*/
@@ -51,6 +55,7 @@ protected function setUp(): void
5155
$this->getCategoryByName = $this->objectManager->get(GetCategoryByName::class);
5256
$this->categoryRepository = $this->objectManager->get(CategoryRepositoryInterface::class);
5357
$this->layout = $this->objectManager->get(LayoutInterface::class);
58+
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
5459
}
5560

5661
/**
@@ -70,8 +75,9 @@ protected function tearDown(): void
7075
*/
7176
public function testCmsBlockDisplayedOnCategory(): void
7277
{
78+
$storeId = (int)$this->storeManager->getStore('default')->getId();
7379
$categoryId = $this->getCategoryByName->execute('Category with cms block')->getId();
74-
$category = $this->categoryRepository->get($categoryId, 1);
80+
$category = $this->categoryRepository->get($categoryId, $storeId);
7581
$this->registerCategory($category);
7682
$block = $this->layout->createBlock(View::class)->setTemplate('Magento_Catalog::category/cms.phtml');
7783
$this->assertStringContainsString('<h1>Fixture Block Title</h1>', $block->toHtml());

dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Category/Save/SaveCategoryTest.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
use Magento\Catalog\Model\Category;
1313
use Magento\Cms\Api\GetBlockByIdentifierInterface;
1414
use Magento\Framework\Exception\NoSuchEntityException;
15+
use Magento\Store\Model\StoreManagerInterface;
1516

1617
/**
1718
* Test cases for save category controller.
1819
*
1920
* @magentoAppArea adminhtml
20-
* @magentoDbIsolation disabled
21+
* @magentoDbIsolation enabled
2122
*/
2223
class SaveCategoryTest extends AbstractSaveCategoryTest
2324
{
@@ -30,6 +31,9 @@ class SaveCategoryTest extends AbstractSaveCategoryTest
3031
/** @var string */
3132
private $createdCategoryId;
3233

34+
/** @var StoreManagerInterface */
35+
private $storeManager;
36+
3337
/**
3438
* @inheritdoc
3539
*/
@@ -39,19 +43,22 @@ protected function setUp(): void
3943

4044
$this->categoryRepository = $this->_objectManager->get(CategoryRepositoryInterface::class);
4145
$this->getBlockByIdentifier = $this->_objectManager->get(GetBlockByIdentifierInterface::class);
46+
$this->storeManager = $this->_objectManager->get(StoreManagerInterface::class);
4247
}
4348

4449
/**
4550
* @inheritdoc
4651
*/
4752
protected function tearDown(): void
4853
{
49-
try {
50-
$this->categoryRepository->deleteByIdentifier($this->createdCategoryId);
51-
} catch (NoSuchEntityException $e) {
52-
//Category already deleted.
54+
if(!empty($this->createdCategoryId)) {
55+
try {
56+
$this->categoryRepository->deleteByIdentifier($this->createdCategoryId);
57+
} catch (NoSuchEntityException $e) {
58+
//Category already deleted.
59+
}
60+
$this->createdCategoryId = null;
5361
}
54-
$this->createdCategoryId = null;
5562

5663
parent::tearDown();
5764
}
@@ -63,15 +70,16 @@ protected function tearDown(): void
6370
*/
6471
public function testCreateCategoryWithCmsBlock(): void
6572
{
66-
$blockId = $this->getBlockByIdentifier->execute('fixture_block', 1)->getId();
73+
$storeId = (int)$this->storeManager->getStore('default')->getId();
74+
$blockId = $this->getBlockByIdentifier->execute('fixture_block', $storeId)->getId();
6775
$postData = [
6876
CategoryInterface::KEY_NAME => 'Category with cms block',
6977
CategoryInterface::KEY_IS_ACTIVE => 1,
7078
CategoryInterface::KEY_INCLUDE_IN_MENU => 1,
7179
'display_mode' => Category::DM_MIXED,
7280
'landing_page' => $blockId,
73-
'available_sort_by' => 1,
74-
'default_sort_by' => 1,
81+
CategoryInterface::KEY_AVAILABLE_SORT_BY => ['position'],
82+
'default_sort_by' => 'position',
7583
];
7684
$responseData = $this->performSaveCategoryRequest($postData);
7785
$this->assertRequestIsSuccessfullyPerformed($responseData);

dev/tests/integration/testsuite/Magento/Catalog/_files/category_with_cms_block.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
$getBlockByIdentifier = $objectManager->get(GetBlockByIdentifierInterface::class);
3232
$block = $getBlockByIdentifier->execute('fixture_block', $currentStoreId);
3333

34-
$storeManager->setCurrentStore(Store::DEFAULT_STORE_ID);
3534
$category = $categoryFactory->create();
3635
$category->setName('Category with cms block')
3736
->setParentId($categoryHelper->getId())
@@ -42,5 +41,9 @@
4241
->setPosition(1)
4342
->setDisplayMode(Category::DM_MIXED)
4443
->setLandingPage($block->getId());
45-
$categoryRepository->save($category);
46-
$storeManager->setCurrentStore($currentStoreId);
44+
try {
45+
$storeManager->setCurrentStore(Store::DEFAULT_STORE_ID);
46+
$categoryRepository->save($category);
47+
} finally {
48+
$storeManager->setCurrentStore($currentStoreId);
49+
}

0 commit comments

Comments
 (0)