Skip to content

Commit 0ac3443

Browse files
authored
Merge pull request #5246 from magento-tsg/2.4-develop-com-pr4
[TSG-Commerce] Tests for 2.4 (pr4)
2 parents 3811867 + 499612e commit 0ac3443

File tree

35 files changed

+2421
-195
lines changed

35 files changed

+2421
-195
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TestFramework\Catalog\Model;
9+
10+
use Magento\Catalog\Api\Data\CategoryInterface;
11+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
12+
13+
/**
14+
* Load category by category name
15+
*/
16+
class GetCategoryByName
17+
{
18+
/** @var CollectionFactory */
19+
private $categoryCollectionFactory;
20+
21+
/**
22+
* @param CollectionFactory $categoryCollectionFactory
23+
*/
24+
public function __construct(CollectionFactory $categoryCollectionFactory)
25+
{
26+
$this->categoryCollectionFactory = $categoryCollectionFactory;
27+
}
28+
29+
/**
30+
* Load category by name.
31+
*
32+
* @param string $categoryName
33+
* @return CategoryInterface
34+
*/
35+
public function execute(string $categoryName): CategoryInterface
36+
{
37+
$categoryCollection = $this->categoryCollectionFactory->create();
38+
39+
return $categoryCollection->addAttributeToFilter(CategoryInterface::KEY_NAME, $categoryName)
40+
->setPageSize(1)
41+
->getFirstItem();
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Controller\Adminhtml\Category\Delete;
9+
10+
use Magento\Catalog\Api\CategoryRepositoryInterface;
11+
use Magento\Catalog\Model\Indexer\Category\Flat\State;
12+
use Magento\Catalog\Model\ResourceModel\Category\Flat as CategoryFlatResource;
13+
use Magento\Catalog\Model\ResourceModel\Category\Flat\CollectionFactory;
14+
use Magento\Framework\App\Request\Http as HttpRequest;
15+
use Magento\Framework\Exception\NoSuchEntityException;
16+
use Magento\Framework\Indexer\IndexerRegistry;
17+
use Magento\TestFramework\TestCase\AbstractBackendController;
18+
19+
/**
20+
* Test cases related to delete category with enabled category flat.
21+
*
22+
* @magentoAppArea adminhtml
23+
* @magentoDbIsolation disabled
24+
*/
25+
class DeleteCategoryWithEnabledFlatTest extends AbstractBackendController
26+
{
27+
/**
28+
* @var IndexerRegistry
29+
*/
30+
private $indexerRegistry;
31+
32+
/**
33+
* @var CategoryRepositoryInterface
34+
*/
35+
private $categoryRepository;
36+
37+
/**
38+
* @var CategoryFlatResource
39+
*/
40+
private $categoryFlatResource;
41+
42+
/**
43+
* @var CollectionFactory
44+
*/
45+
private $categoryFlatCollectionFactory;
46+
47+
/**
48+
* @inheritdoc
49+
*/
50+
protected function setUp()
51+
{
52+
parent::setUp();
53+
$this->indexerRegistry = $this->_objectManager->get(IndexerRegistry::class);
54+
$this->categoryRepository = $this->_objectManager->get(CategoryRepositoryInterface::class);
55+
$this->categoryFlatResource = $this->_objectManager->get(CategoryFlatResource::class);
56+
$this->categoryFlatCollectionFactory = $this->_objectManager->get(CollectionFactory::class);
57+
}
58+
59+
/**
60+
* @inheritdoc
61+
*/
62+
protected function tearDown()
63+
{
64+
parent::tearDown();
65+
$categoryFlatIndexer = $this->indexerRegistry->get(State::INDEXER_ID);
66+
$categoryFlatIndexer->invalidate();
67+
$this->categoryFlatResource->getConnection()->dropTable($this->categoryFlatResource->getMainTable());
68+
}
69+
70+
/**
71+
* Check that product is deleted from flat table.
72+
*
73+
* @magentoConfigFixture current_store catalog/frontend/flat_catalog_category true
74+
*
75+
* @magentoDataFixture Magento/Catalog/_files/category.php
76+
* @magentoDataFixture Magento/Catalog/_files/reindex_catalog_category_flat.php
77+
*
78+
* @return void
79+
*/
80+
public function testDeleteCategory(): void
81+
{
82+
$this->assertEquals(1, $this->getFlatCategoryCollectionSizeByCategoryId(333));
83+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
84+
$this->getRequest()->setPostValue(['id' => 333]);
85+
$this->dispatch('backend/catalog/category/delete');
86+
$this->assertSessionMessages($this->equalTo([(string)__('You deleted the category.')]));
87+
$this->assertEquals(0, $this->getFlatCategoryCollectionSizeByCategoryId(333));
88+
$this->checkCategoryIsDeleted(333);
89+
}
90+
91+
/**
92+
* Return collection size from category flat collection by category ID.
93+
*
94+
* @param int $categoryId
95+
* @return int
96+
*/
97+
private function getFlatCategoryCollectionSizeByCategoryId(int $categoryId): int
98+
{
99+
$categoryFlatCollection = $this->categoryFlatCollectionFactory->create();
100+
$categoryFlatCollection->addIdFilter($categoryId);
101+
102+
return $categoryFlatCollection->getSize();
103+
}
104+
105+
/**
106+
* Assert that category is deleted.
107+
*
108+
* @param int $categoryId
109+
*/
110+
private function checkCategoryIsDeleted(int $categoryId): void
111+
{
112+
$this->expectExceptionObject(new NoSuchEntityException(__("No such entity with id = {$categoryId}")));
113+
$this->categoryRepository->get($categoryId);
114+
}
115+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Controller\Adminhtml\Category\Save;
9+
10+
use Magento\Framework\App\Request\Http as HttpRequest;
11+
use Magento\Framework\Serialize\SerializerInterface;
12+
use Magento\TestFramework\TestCase\AbstractBackendController;
13+
14+
/**
15+
* Abstract save category.
16+
*/
17+
class AbstractSaveCategoryTest extends AbstractBackendController
18+
{
19+
/**
20+
* @var SerializerInterface
21+
*/
22+
private $serializer;
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
protected function setUp()
28+
{
29+
parent::setUp();
30+
$this->serializer = $this->_objectManager->get(SerializerInterface::class);
31+
}
32+
33+
/**
34+
* Perform save category request with category POST data.
35+
*
36+
* @param array $data
37+
* @return array
38+
*/
39+
protected function performSaveCategoryRequest(array $data): array
40+
{
41+
$data['return_session_messages_only'] = true;
42+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
43+
$this->getRequest()->setPostValue($data);
44+
$this->dispatch('backend/catalog/category/save');
45+
46+
return $this->serializer->unserialize($this->getResponse()->getBody());
47+
}
48+
49+
/**
50+
* Assert that session has message about successfully category save.
51+
*
52+
* @param array $responseData
53+
* @return void
54+
*/
55+
protected function assertRequestIsSuccessfullyPerformed(array $responseData): void
56+
{
57+
$this->assertTrue(isset($responseData['category']['entity_id']));
58+
$this->assertFalse($responseData['error'], 'Response message: ' . $responseData['messages']);
59+
$message = str_replace('.', '\.', (string)__('You saved the category.'));
60+
$this->assertRegExp("/>{$message}</", $responseData['messages']);
61+
}
62+
}

0 commit comments

Comments
 (0)