Skip to content

Commit 1e846a4

Browse files
authored
Merge pull request #6519 from magento-tsg/2.4-develop-sidecar-pr11
[Sidecar] Fixes for 2.4 (pr11)
2 parents 3fef6d1 + 7c2fc63 commit 1e846a4

13 files changed

+901
-1
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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\Product;
9+
10+
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Framework\App\Request\Http as HttpRequest;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Framework\Message\MessageInterface;
15+
use Magento\TestFramework\TestCase\AbstractBackendController;
16+
17+
/**
18+
* Test class for Product duplicate action
19+
*
20+
* @magentoAppArea adminhtml
21+
* @see \Magento\Catalog\Controller\Adminhtml\Product\Duplicate
22+
*/
23+
class DuplicateTest extends AbstractBackendController
24+
{
25+
/**
26+
* @var ProductRepositoryInterface
27+
*/
28+
private $productRepository;
29+
30+
/**
31+
* @var string
32+
*/
33+
private $duplicatedProductSku;
34+
35+
/**
36+
* @var array
37+
*/
38+
private $dataKeys = ['name', 'description', 'short_description', 'price', 'weight', 'attribute_set_id'];
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
protected function setUp(): void
44+
{
45+
parent::setUp();
46+
47+
$this->productRepository = $this->_objectManager->get(ProductRepositoryInterface::class);
48+
$this->productRepository->cleanCache();
49+
}
50+
51+
/**
52+
* @inheritdoc
53+
*/
54+
protected function tearDown(): void
55+
{
56+
try {
57+
$this->productRepository->deleteById($this->duplicatedProductSku);
58+
} catch (NoSuchEntityException $e) {
59+
// product already deleted
60+
}
61+
62+
parent::tearDown();
63+
}
64+
65+
/**
66+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
67+
*
68+
* @return void
69+
*/
70+
public function testDuplicateAction(): void
71+
{
72+
$product = $this->productRepository->get('simple');
73+
$this->getRequest()->setMethod(HttpRequest::METHOD_GET);
74+
$this->getRequest()->setParams(
75+
[
76+
'id' => $product->getId(),
77+
'attribute_set_id' => $product->getAttributeSetId(),
78+
]
79+
);
80+
$this->dispatch('backend/catalog/product/duplicate');
81+
$this->assertSessionMessages(
82+
$this->containsEqual((string)__('You duplicated the product.')),
83+
MessageInterface::TYPE_SUCCESS
84+
);
85+
$this->assertRedirect($this->stringContains('catalog/product/edit/'));
86+
$productId = $this->getIdFromRedirectedUrl();
87+
$this->assertNotEmpty($productId, 'Id not found');
88+
$duplicatedProduct = $this->productRepository->getById((int)$productId);
89+
$this->duplicatedProductSku = $duplicatedProduct->getSku();
90+
$this->assertProductDuplicated($product, $duplicatedProduct);
91+
}
92+
93+
/**
94+
* Get id value from redirected url
95+
*
96+
* @return string
97+
*/
98+
private function getIdFromRedirectedUrl(): string
99+
{
100+
$url = $this->getResponse()
101+
->getHeader('Location')
102+
->getFieldValue();
103+
$pattern = '!/id/(.*?)/!';
104+
$result = preg_match($pattern, $url, $matches);
105+
106+
return $result ? $matches[1] : '';
107+
}
108+
109+
/**
110+
* Checks that duplicate product was created from the first product
111+
*
112+
* @param ProductInterface $product
113+
* @param ProductInterface $duplicatedProduct
114+
* @return void
115+
*/
116+
private function assertProductDuplicated(ProductInterface $product, ProductInterface $duplicatedProduct): void
117+
{
118+
foreach ($this->dataKeys as $key) {
119+
$this->assertEquals($product->getData($key), $duplicatedProduct->getData($key));
120+
}
121+
}
122+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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\Model\Category;
9+
10+
/**
11+
* Mock for authorization process
12+
*/
13+
class AuthorizationMock extends \Magento\Framework\Authorization
14+
{
15+
/**
16+
* @inheritdoc
17+
*/
18+
public function isAllowed($resource, $privilege = null)
19+
{
20+
return false;
21+
}
22+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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\Model\Category;
9+
10+
use Magento\Catalog\Api\Data\CategoryInterface;
11+
use Magento\Catalog\Api\Data\CategoryInterfaceFactory;
12+
use Magento\Catalog\Model\Category\Authorization as CategoryAuthorization;
13+
use Magento\Framework\Authorization;
14+
use Magento\Framework\Exception\AuthorizationException;
15+
use Magento\Framework\Exception\NoSuchEntityException;
16+
use Magento\Framework\ObjectManagerInterface;
17+
use Magento\TestFramework\Helper\Bootstrap;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* Checks authorization for category design attributes edit
22+
*
23+
* @magentoDbIsolation enabled
24+
*/
25+
class AuthorizationTest extends TestCase
26+
{
27+
/** @var ObjectManagerInterface */
28+
private $objectManager;
29+
30+
/** @var CategoryAuthorization */
31+
private $model;
32+
33+
/** @var CategoryInterfaceFactory */
34+
private $categoryFactory;
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
protected function setUp(): void
40+
{
41+
parent::setUp();
42+
43+
$this->objectManager = Bootstrap::getObjectManager();
44+
$this->objectManager->addSharedInstance(
45+
$this->objectManager->get(AuthorizationMock::class),
46+
Authorization::class
47+
);
48+
$this->model = $this->objectManager->get(CategoryAuthorization::class);
49+
$this->categoryFactory = $this->objectManager->get(CategoryInterfaceFactory::class);
50+
}
51+
52+
/**
53+
* @inheritdoc
54+
*/
55+
protected function tearDown(): void
56+
{
57+
$this->objectManager->removeSharedInstance(Authorization::class);
58+
59+
parent::tearDown();
60+
}
61+
62+
/**
63+
* @magentoDataFixture Magento/Catalog/_files/category.php
64+
*
65+
* @return void
66+
*/
67+
public function testAuthorizationWithoutPermissions(): void
68+
{
69+
$category = $this->createCategoryWithData(['entity_id' => 333, 'custom_use_parent_settings' => true]);
70+
$this->expectException(AuthorizationException::class);
71+
$this->expectExceptionMessage((string)__('Not allowed to edit the category\'s design attributes'));
72+
$this->model->authorizeSavingOf($category);
73+
}
74+
75+
/**
76+
* @return void
77+
*/
78+
public function testAuthorizationWithWrongCategoryId(): void
79+
{
80+
$wrongCategoryId = 56464654;
81+
$category = $this->createCategoryWithData(['entity_id' => $wrongCategoryId]);
82+
$this->expectExceptionObject(NoSuchEntityException::singleField('id', $wrongCategoryId));
83+
$this->model->authorizeSavingOf($category);
84+
}
85+
86+
/**
87+
* Create category instance with provided data
88+
*
89+
* @param array $data
90+
* @return CategoryInterface
91+
*/
92+
private function createCategoryWithData(array $data): CategoryInterface
93+
{
94+
$category = $this->categoryFactory->create();
95+
$category->addData($data);
96+
97+
return $category;
98+
}
99+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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\Observer\Compare;
9+
10+
use Magento\Catalog\Model\Product\Compare\ListCompareFactory;
11+
use Magento\Customer\Model\Session;
12+
use Magento\Customer\Model\Visitor;
13+
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
14+
use Magento\Framework\ObjectManagerInterface;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* Checks customer and visitor compare list merging after customer login
20+
*
21+
* @see \Magento\Catalog\Observer\Compare\BindCustomerLoginObserver
22+
*
23+
* @magentoAppArea frontend
24+
* @magentoDbIsolation enabled
25+
*/
26+
class BindCustomerLoginObserverTest extends TestCase
27+
{
28+
/** @var ObjectManagerInterface */
29+
private $objectManager;
30+
31+
/** @var Session */
32+
private $session;
33+
34+
/** @var Visitor */
35+
private $visitor;
36+
37+
/** @var ListCompareFactory */
38+
private $listCompareFactory;
39+
40+
/**
41+
* @inheritdoc
42+
*/
43+
protected function setUp(): void
44+
{
45+
parent::setUp();
46+
47+
$this->objectManager = Bootstrap::getObjectManager();
48+
$this->session = $this->objectManager->get(Session::class);
49+
$this->visitor = $this->objectManager->get(Visitor::class);
50+
$this->listCompareFactory = $this->objectManager->get(ListCompareFactory::class);
51+
}
52+
53+
/**
54+
* @inheritdoc
55+
*/
56+
protected function tearDown(): void
57+
{
58+
$this->session->logout();
59+
$this->visitor->setId(null);
60+
61+
parent::tearDown();
62+
}
63+
64+
/**
65+
* @magentoDataFixture Magento/Catalog/_files/visitor_compare_list.php
66+
* @magentoDataFixture Magento/Customer/_files/customer.php
67+
*
68+
* @return void
69+
*/
70+
public function testExecute(): void
71+
{
72+
$this->visitor->setId(123);
73+
$this->session->loginById(1);
74+
$this->assertCustomerItems(1, ['simple']);
75+
$this->assertVisitorItems(123, []);
76+
}
77+
78+
/**
79+
* @magentoDataFixture Magento/Catalog/_files/customer_compare_list_with_simple_product.php
80+
* @magentoDataFixture Magento/Catalog/_files/product_in_compare_list_with_customer.php
81+
* @magentoDataFixture Magento/Catalog/_files/visitor_compare_list.php
82+
*
83+
* @return void
84+
*/
85+
public function testExecuteWithSameProducts(): void
86+
{
87+
$this->visitor->setId(123);
88+
$this->session->loginById(1);
89+
$this->assertCustomerItems(1, ['simple', 'simple2']);
90+
$this->assertVisitorItems(123, []);
91+
}
92+
93+
/**
94+
* Check customer compare items
95+
*
96+
* @param int $customerId
97+
* @param array $expectedProductSkus
98+
* @return void
99+
*/
100+
private function assertCustomerItems(int $customerId, array $expectedProductSkus): void
101+
{
102+
$collection = $this->listCompareFactory->create()->getItemCollection()->useProductItem()
103+
->setCustomerId($customerId);
104+
$this->checkCollection($collection, $expectedProductSkus);
105+
}
106+
107+
/**
108+
* Checks visitor compare items
109+
*
110+
* @param int $visitorId
111+
* @param array $expectedProductSkus
112+
* @return void
113+
*/
114+
private function assertVisitorItems(int $visitorId, array $expectedProductSkus): void
115+
{
116+
$collection = $this->listCompareFactory->create()->getItemCollection()->useProductItem()
117+
->setVisitorId($visitorId);
118+
$collection->addFieldToFilter('customer_id', ['null' => true]);
119+
$this->checkCollection($collection, $expectedProductSkus);
120+
}
121+
122+
/**
123+
* Check collection
124+
*
125+
* @param AbstractCollection $collection
126+
* @param array $expectedSkus
127+
* @return void
128+
*/
129+
private function checkCollection(AbstractCollection $collection, array $expectedSkus): void
130+
{
131+
$this->assertCount(count($expectedSkus), $collection);
132+
foreach ($expectedSkus as $expectedSku) {
133+
$this->assertNotNull($collection->getItemByColumnValue('sku', $expectedSku));
134+
}
135+
}
136+
}

0 commit comments

Comments
 (0)