Skip to content

Commit

Permalink
Merge pull request magento#5246 from magento-tsg/2.4-develop-com-pr4
Browse files Browse the repository at this point in the history
[TSG-Commerce] Tests for 2.4 (pr4)
  • Loading branch information
zakdma authored Jan 24, 2020
2 parents 3811867 + 499612e commit 0ac3443
Show file tree
Hide file tree
Showing 35 changed files with 2,421 additions and 195 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\TestFramework\Catalog\Model;

use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;

/**
* Load category by category name
*/
class GetCategoryByName
{
/** @var CollectionFactory */
private $categoryCollectionFactory;

/**
* @param CollectionFactory $categoryCollectionFactory
*/
public function __construct(CollectionFactory $categoryCollectionFactory)
{
$this->categoryCollectionFactory = $categoryCollectionFactory;
}

/**
* Load category by name.
*
* @param string $categoryName
* @return CategoryInterface
*/
public function execute(string $categoryName): CategoryInterface
{
$categoryCollection = $this->categoryCollectionFactory->create();

return $categoryCollection->addAttributeToFilter(CategoryInterface::KEY_NAME, $categoryName)
->setPageSize(1)
->getFirstItem();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Controller\Adminhtml\Category\Delete;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Indexer\Category\Flat\State;
use Magento\Catalog\Model\ResourceModel\Category\Flat as CategoryFlatResource;
use Magento\Catalog\Model\ResourceModel\Category\Flat\CollectionFactory;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\TestFramework\TestCase\AbstractBackendController;

/**
* Test cases related to delete category with enabled category flat.
*
* @magentoAppArea adminhtml
* @magentoDbIsolation disabled
*/
class DeleteCategoryWithEnabledFlatTest extends AbstractBackendController
{
/**
* @var IndexerRegistry
*/
private $indexerRegistry;

/**
* @var CategoryRepositoryInterface
*/
private $categoryRepository;

/**
* @var CategoryFlatResource
*/
private $categoryFlatResource;

/**
* @var CollectionFactory
*/
private $categoryFlatCollectionFactory;

/**
* @inheritdoc
*/
protected function setUp()
{
parent::setUp();
$this->indexerRegistry = $this->_objectManager->get(IndexerRegistry::class);
$this->categoryRepository = $this->_objectManager->get(CategoryRepositoryInterface::class);
$this->categoryFlatResource = $this->_objectManager->get(CategoryFlatResource::class);
$this->categoryFlatCollectionFactory = $this->_objectManager->get(CollectionFactory::class);
}

/**
* @inheritdoc
*/
protected function tearDown()
{
parent::tearDown();
$categoryFlatIndexer = $this->indexerRegistry->get(State::INDEXER_ID);
$categoryFlatIndexer->invalidate();
$this->categoryFlatResource->getConnection()->dropTable($this->categoryFlatResource->getMainTable());
}

/**
* Check that product is deleted from flat table.
*
* @magentoConfigFixture current_store catalog/frontend/flat_catalog_category true
*
* @magentoDataFixture Magento/Catalog/_files/category.php
* @magentoDataFixture Magento/Catalog/_files/reindex_catalog_category_flat.php
*
* @return void
*/
public function testDeleteCategory(): void
{
$this->assertEquals(1, $this->getFlatCategoryCollectionSizeByCategoryId(333));
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->getRequest()->setPostValue(['id' => 333]);
$this->dispatch('backend/catalog/category/delete');
$this->assertSessionMessages($this->equalTo([(string)__('You deleted the category.')]));
$this->assertEquals(0, $this->getFlatCategoryCollectionSizeByCategoryId(333));
$this->checkCategoryIsDeleted(333);
}

/**
* Return collection size from category flat collection by category ID.
*
* @param int $categoryId
* @return int
*/
private function getFlatCategoryCollectionSizeByCategoryId(int $categoryId): int
{
$categoryFlatCollection = $this->categoryFlatCollectionFactory->create();
$categoryFlatCollection->addIdFilter($categoryId);

return $categoryFlatCollection->getSize();
}

/**
* Assert that category is deleted.
*
* @param int $categoryId
*/
private function checkCategoryIsDeleted(int $categoryId): void
{
$this->expectExceptionObject(new NoSuchEntityException(__("No such entity with id = {$categoryId}")));
$this->categoryRepository->get($categoryId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Controller\Adminhtml\Category\Save;

use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\TestFramework\TestCase\AbstractBackendController;

/**
* Abstract save category.
*/
class AbstractSaveCategoryTest extends AbstractBackendController
{
/**
* @var SerializerInterface
*/
private $serializer;

/**
* @inheritdoc
*/
protected function setUp()
{
parent::setUp();
$this->serializer = $this->_objectManager->get(SerializerInterface::class);
}

/**
* Perform save category request with category POST data.
*
* @param array $data
* @return array
*/
protected function performSaveCategoryRequest(array $data): array
{
$data['return_session_messages_only'] = true;
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
$this->getRequest()->setPostValue($data);
$this->dispatch('backend/catalog/category/save');

return $this->serializer->unserialize($this->getResponse()->getBody());
}

/**
* Assert that session has message about successfully category save.
*
* @param array $responseData
* @return void
*/
protected function assertRequestIsSuccessfullyPerformed(array $responseData): void
{
$this->assertTrue(isset($responseData['category']['entity_id']));
$this->assertFalse($responseData['error'], 'Response message: ' . $responseData['messages']);
$message = str_replace('.', '\.', (string)__('You saved the category.'));
$this->assertRegExp("/>{$message}</", $responseData['messages']);
}
}
Loading

0 comments on commit 0ac3443

Please sign in to comment.