Skip to content

Commit

Permalink
Merge branch '2.4-develop' of github.com:magento/magento2 into 28569_…
Browse files Browse the repository at this point in the history
…adding_stores_to_store_config
  • Loading branch information
gallyamov committed Jun 24, 2020
2 parents ac5835b + c35093a commit 5ea15d1
Show file tree
Hide file tree
Showing 101 changed files with 5,346 additions and 454 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ public function getFailedItems()
}
}

/**
* Clear validation messages to prevent wrong validation for subsequent price update.
* Work around for backward compatible changes.
*/
$this->failedItems = [];

return $failedItems;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
use Magento\Catalog\Model\Category;

/**
* Aggregate count for parent category after deleting child category
*
* Class AggregateCount
*/
class AggregateCount
{
/**
* Reduces children count for parent categories
*
* @param Category $category
* @return void
*/
Expand All @@ -25,9 +29,7 @@ public function processDelete(Category $category)
*/
$parentIds = $category->getParentIds();
if ($parentIds) {
$childDecrease = $category->getChildrenCount() + 1;
// +1 is itself
$data = ['children_count' => new \Zend_Db_Expr('children_count - ' . $childDecrease)];
$data = ['children_count' => new \Zend_Db_Expr('children_count - 1')];
$where = ['entity_id IN(?)' => $parentIds];
$resourceModel->getConnection()->update($resourceModel->getEntityTable(), $data, $where);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Category;

use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\ResourceModel\Category\AggregateCount;
use Magento\Catalog\Model\ResourceModel\Category as ResourceCategory;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Aggregate count model test
*/
class AggregateCountTest extends TestCase
{

/**
* @var AggregateCount
*/
protected $aggregateCount;

/**
* @var ObjectManagerHelper
*/
protected $objectManagerHelper;

/**
* @var Category|MockObject
*/
protected $categoryMock;

/**
* @var ResourceCategory|MockObject
*/
protected $resourceCategoryMock;

/**
* @var AdapterInterface|MockObject
*/
protected $connectionMock;

/**
* {@inheritdoc}
*/
public function setUp(): void
{
$this->categoryMock = $this->createMock(Category::class);
$this->resourceCategoryMock = $this->createMock(ResourceCategory::class);
$this->connectionMock = $this->getMockBuilder(AdapterInterface::class)
->getMockForAbstractClass();
$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->aggregateCount = $this->objectManagerHelper->getObject(AggregateCount::class);
}

/**
* @return void
*/
public function testProcessDelete(): void
{
$parentIds = 3;
$table = 'catalog_category_entity';

$this->categoryMock->expects($this->once())
->method('getResource')
->willReturn($this->resourceCategoryMock);
$this->categoryMock->expects($this->once())
->method('getParentIds')
->willReturn($parentIds);
$this->resourceCategoryMock->expects($this->any())
->method('getEntityTable')
->willReturn($table);
$this->resourceCategoryMock->expects($this->once())
->method('getConnection')
->willReturn($this->connectionMock);
$this->connectionMock->expects($this->once())
->method('update')
->with(
$table,
['children_count' => new \Zend_Db_Expr('children_count - 1')],
['entity_id IN(?)' => $parentIds]
);
$this->aggregateCount->processDelete($this->categoryMock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@

namespace Magento\Catalog\Test\Unit\Ui\DataProvider\Product\Form\Modifier;

use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Categories;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection;
use Magento\Framework\AuthorizationInterface;
use Magento\Framework\DB\Helper as DbHelper;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\Store;
use Magento\Backend\Model\Auth\Session;
use Magento\Authorization\Model\Role;
use Magento\User\Model\User;
use PHPUnit\Framework\MockObject\MockObject;

/**
Expand Down Expand Up @@ -51,6 +54,11 @@ class CategoriesTest extends AbstractModifierTest
*/
private $authorizationMock;

/**
* @var Session|MockObject
*/
private $sessionMock;

protected function setUp(): void
{
parent::setUp();
Expand All @@ -72,7 +80,10 @@ protected function setUp(): void
$this->authorizationMock = $this->getMockBuilder(AuthorizationInterface::class)
->disableOriginalConstructor()
->getMockForAbstractClass();

$this->sessionMock = $this->getMockBuilder(Session::class)
->setMethods(['getUser'])
->disableOriginalConstructor()
->getMock();
$this->categoryCollectionFactoryMock->expects($this->any())
->method('create')
->willReturn($this->categoryCollectionMock);
Expand All @@ -88,6 +99,26 @@ protected function setUp(): void
$this->categoryCollectionMock->expects($this->any())
->method('getIterator')
->willReturn(new \ArrayIterator([]));

$roleAdmin = $this->getMockBuilder(Role::class)
->setMethods(['getId'])
->disableOriginalConstructor()
->getMock();
$roleAdmin->expects($this->any())
->method('getId')
->willReturn(0);

$userAdmin = $this->getMockBuilder(User::class)
->setMethods(['getRole'])
->disableOriginalConstructor()
->getMock();
$userAdmin->expects($this->any())
->method('getRole')
->willReturn($roleAdmin);

$this->sessionMock->expects($this->any())
->method('getUser')
->willReturn($userAdmin);
}

/**
Expand All @@ -101,11 +132,28 @@ protected function createModel()
'locator' => $this->locatorMock,
'categoryCollectionFactory' => $this->categoryCollectionFactoryMock,
'arrayManager' => $this->arrayManagerMock,
'authorization' => $this->authorizationMock
'authorization' => $this->authorizationMock,
'session' => $this->sessionMock
]
);
}

/**
* @param object $object
* @param string $method
* @param array $args
* @return mixed
* @throws \ReflectionException
*/
private function invokeMethod($object, $method, $args = [])
{
$class = new \ReflectionClass(Categories::class);
$method = $class->getMethod($method);
$method->setAccessible(true);

return $method->invokeArgs($object, $args);
}

public function testModifyData()
{
$this->assertSame([], $this->getModel()->modifyData([]));
Expand Down Expand Up @@ -176,4 +224,44 @@ public function modifyMetaLockedDataProvider()
{
return [[true], [false]];
}

/**
* Asserts that a user with an ACL role ID of 0 and a user with an ACL role ID of 1 do not have the same cache IDs
* Assumes a store ID of 0
*
* @throws \ReflectionException
*/
public function testAclCacheIds()
{
$categoriesAdmin = $this->createModel();
$cacheIdAdmin = $this->invokeMethod($categoriesAdmin, 'getCategoriesTreeCacheId', [0]);

$roleAclUser = $this->getMockBuilder(Role::class)
->disableOriginalConstructor()
->getMock();
$roleAclUser->expects($this->any())
->method('getId')
->willReturn(1);

$userAclUser = $this->getMockBuilder(User::class)
->disableOriginalConstructor()
->getMock();
$userAclUser->expects($this->any())
->method('getRole')
->will($this->returnValue($roleAclUser));

$this->sessionMock = $this->getMockBuilder(Session::class)
->setMethods(['getUser'])
->disableOriginalConstructor()
->getMock();

$this->sessionMock->expects($this->any())
->method('getUser')
->will($this->returnValue($userAclUser));

$categoriesAclUser = $this->createModel();
$cacheIdAclUser = $this->invokeMethod($categoriesAclUser, 'getCategoriesTreeCacheId', [0]);

$this->assertNotEquals($cacheIdAdmin, $cacheIdAclUser);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
use Magento\Framework\UrlInterface;
use Magento\Framework\Stdlib\ArrayManager;
use Magento\Framework\AuthorizationInterface;
use Magento\Backend\Model\Auth\Session;

/**
* Data provider for categories field of product page
*
* @api
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
* @since 101.0.0
*/
class Categories extends AbstractModifier
Expand Down Expand Up @@ -86,6 +88,11 @@ class Categories extends AbstractModifier
*/
private $authorization;

/**
* @var Session
*/
private $session;

/**
* @param LocatorInterface $locator
* @param CategoryCollectionFactory $categoryCollectionFactory
Expand All @@ -94,6 +101,7 @@ class Categories extends AbstractModifier
* @param ArrayManager $arrayManager
* @param SerializerInterface $serializer
* @param AuthorizationInterface $authorization
* @param Session $session
*/
public function __construct(
LocatorInterface $locator,
Expand All @@ -102,7 +110,8 @@ public function __construct(
UrlInterface $urlBuilder,
ArrayManager $arrayManager,
SerializerInterface $serializer = null,
AuthorizationInterface $authorization = null
AuthorizationInterface $authorization = null,
Session $session = null
) {
$this->locator = $locator;
$this->categoryCollectionFactory = $categoryCollectionFactory;
Expand All @@ -111,6 +120,7 @@ public function __construct(
$this->arrayManager = $arrayManager;
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
$this->authorization = $authorization ?: ObjectManager::getInstance()->get(AuthorizationInterface::class);
$this->session = $session ?: ObjectManager::getInstance()->get(Session::class);
}

/**
Expand Down Expand Up @@ -370,10 +380,16 @@ protected function getCategoriesTree($filter = null)
* @param string $filter
* @return string
*/
private function getCategoriesTreeCacheId(int $storeId, string $filter = '') : string
private function getCategoriesTreeCacheId(int $storeId, string $filter = ''): string
{
if ($this->session->getUser() !== null) {
return self::CATEGORY_TREE_ID
. '_' . (string)$storeId
. '_' . $this->session->getUser()->getAclRole()
. '_' . $filter;
}
return self::CATEGORY_TREE_ID
. '_' . (string) $storeId
. '_' . (string)$storeId
. '_' . $filter;
}

Expand Down
5 changes: 5 additions & 0 deletions app/code/Magento/Catalog/etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@
<index referenceId="CATALOG_PRODUCT_ENTITY_INT_STORE_ID" indexType="btree">
<column name="store_id"/>
</index>
<index referenceId="CATALOG_PRODUCT_ENTITY_INT_ATTRIBUTE_ID_STORE_ID_VALUE" indexType="btree">
<column name="attribute_id"/>
<column name="store_id"/>
<column name="value"/>
</index>
</table>
<table name="catalog_product_entity_text" resource="default" engine="innodb"
comment="Catalog Product Text Attribute Backend Table">
Expand Down
3 changes: 2 additions & 1 deletion app/code/Magento/Catalog/etc/db_schema_whitelist.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
},
"index": {
"CATALOG_PRODUCT_ENTITY_INT_ATTRIBUTE_ID": true,
"CATALOG_PRODUCT_ENTITY_INT_STORE_ID": true
"CATALOG_PRODUCT_ENTITY_INT_STORE_ID": true,
"CATALOG_PRODUCT_ENTITY_INT_ATTRIBUTE_ID_STORE_ID_VALUE": true
},
"constraint": {
"PRIMARY": true,
Expand Down
Loading

0 comments on commit 5ea15d1

Please sign in to comment.