-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '2.4-develop' of https://github.com/mage-os/mirror-magento2
Showing
16 changed files
with
845 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
app/code/Magento/Authorization/Test/Unit/Model/Acl/Loader/MissingDeclineRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
/** | ||
* Copyright 2024 Adobe | ||
* All Rights Reserved. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Authorization\Test\Unit\Model\Acl\Loader; | ||
|
||
use Magento\Authorization\Model\Acl\Loader\Rule; | ||
use Magento\Framework\Acl; | ||
use Magento\Framework\Acl\Data\CacheInterface; | ||
use Magento\Framework\Acl\RootResource; | ||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\Serialize\Serializer\Json; | ||
use PHPUnit\Framework\MockObject\Exception; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers \Magento\Authorization\Model\Acl\Loader\Rule | ||
*/ | ||
class MissingDeclineRuleTest extends TestCase | ||
{ | ||
/** | ||
* @var Rule | ||
*/ | ||
private $model; | ||
|
||
/** | ||
* @var RootResource | ||
*/ | ||
private $rootResource; | ||
|
||
/** | ||
* @var ResourceConnection|MockObject | ||
*/ | ||
private $resourceMock; | ||
|
||
/** | ||
* @var CacheInterface|MockObject | ||
*/ | ||
private $aclDataCacheMock; | ||
|
||
/** | ||
* @var Json|MockObject | ||
*/ | ||
private $serializerMock; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function setUp(): void | ||
{ | ||
$this->rootResource = new RootResource('Magento_Backend::all'); | ||
|
||
$this->resourceMock = $this->getMockBuilder(ResourceConnection::class) | ||
->addMethods(['getTable']) | ||
->onlyMethods(['getConnection']) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->aclDataCacheMock = $this->createMock(CacheInterface::class); | ||
$this->serializerMock = $this->createPartialMock( | ||
Json::class, | ||
['serialize', 'unserialize'] | ||
); | ||
|
||
$this->serializerMock->method('serialize') | ||
->willReturnCallback( | ||
static function ($value) { | ||
return json_encode($value); | ||
} | ||
); | ||
|
||
$this->serializerMock->method('unserialize') | ||
->willReturnCallback( | ||
static function ($value) { | ||
return json_decode($value, true); | ||
} | ||
); | ||
|
||
$this->model = new Rule( | ||
$this->rootResource, | ||
$this->resourceMock, | ||
$this->aclDataCacheMock, | ||
$this->serializerMock | ||
); | ||
} | ||
|
||
/** | ||
* This test ensures that any new resources, which have not been explicitly defined in the authorization_rule table, | ||
* are automatically denied for all roles unless explicitly allowed. | ||
* | ||
* @return void | ||
* @throws Exception | ||
*/ | ||
public function testDenyAbsentResources(): void | ||
{ | ||
// Vendor_MyModule::menu and Vendor_MyModule::report permissions are not present in the authorization_rules | ||
// table for role 3, and should be denied by default | ||
$authorizationRulesData = [ | ||
['rule_id' => 1, 'role_id' => 2, 'resource_id' => 'Magento_Backend::all', 'permission' => 'deny'], | ||
['rule_id' => 2, 'role_id' => 2, 'resource_id' => 'Vendor_MyModule::index', 'permission' => 'allow'], | ||
['rule_id' => 3, 'role_id' => 2, 'resource_id' => 'Vendor_MyModule::menu', 'permission' => 'deny'], | ||
['rule_id' => 4, 'role_id' => 2, 'resource_id' => 'Vendor_MyModule::report', 'permission' => 'deny'], | ||
['rule_id' => 5, 'role_id' => 3, 'resource_id' => 'Magento_Backend::all', 'permission' => 'deny'], | ||
['rule_id' => 6, 'role_id' => 3, 'resource_id' => 'Vendor_MyModule::index', 'permission' => 'allow'], | ||
]; | ||
|
||
// Vendor_MyModule::configuration is a new resource that has not been defined in the authorization_rules table | ||
// for any role, and should be denied by default | ||
$getAclResourcesData = array_unique(array_column($authorizationRulesData, 'resource_id')); | ||
$getAclResourcesData[] = 'Vendor_MyModule::configuration'; | ||
|
||
$this->aclDataCacheMock->expects($this->once()) | ||
->method('load') | ||
->with(Rule::ACL_RULE_CACHE_KEY) | ||
->willReturn( | ||
json_encode($authorizationRulesData) | ||
); | ||
|
||
$aclMock = $this->createMock(Acl::class); | ||
$aclMock->method('hasResource')->willReturn(true); | ||
|
||
$aclMock | ||
->expects($this->exactly(2)) | ||
->method('allow'); | ||
|
||
$aclMock | ||
->expects($this->exactly(7)) | ||
->method('deny'); | ||
|
||
$aclMock | ||
->method('getResources') | ||
->willReturn($getAclResourcesData); | ||
|
||
$this->model->populateAcl($aclMock); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
...ode/Magento/CustomerGraphQl/Test/Unit/Model/Resolver/Cache/Customer/ModelHydratorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
<?php | ||
/** | ||
* Copyright 2024 Adobe. | ||
* All Rights Reserved. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CustomerGraphQl\Test\Unit\Model\Resolver\Cache\Customer; | ||
|
||
use Magento\Customer\Model\Data\Customer; | ||
use Magento\Customer\Model\Data\CustomerFactory; | ||
use Magento\CustomerGraphQl\Model\Resolver\Cache\Customer\ModelHydrator; | ||
use Magento\Framework\EntityManager\HydratorInterface; | ||
use Magento\Framework\EntityManager\HydratorPool; | ||
use PHPUnit\Framework\MockObject\Exception; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ModelHydratorTest extends TestCase | ||
{ | ||
/** | ||
* @var CustomerFactory|MockObject | ||
*/ | ||
private $customerFactory; | ||
|
||
/** | ||
* @var HydratorPool|MockObject | ||
*/ | ||
private $hydratorPool; | ||
|
||
/** | ||
* @var HydratorInterface|MockObject | ||
*/ | ||
private $hydrator; | ||
|
||
/** | ||
* @var ModelHydrator | ||
*/ | ||
private $modelHydrator; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->customerFactory = $this->createMock(CustomerFactory::class); | ||
$this->hydratorPool = $this->createMock(HydratorPool::class); | ||
$this->hydrator = $this->createMock(HydratorInterface::class); | ||
|
||
$this->modelHydrator = new ModelHydrator( | ||
$this->customerFactory, | ||
$this->hydratorPool | ||
); | ||
} | ||
|
||
/** | ||
* Test hydrate method with existing model | ||
* | ||
* @return void | ||
* @throws Exception | ||
*/ | ||
|
||
public function testHydrateWithExistingModel() | ||
{ | ||
$customer = $this->createMock(Customer::class); | ||
$resolverData = [ | ||
'model_id' => 1, | ||
'model_entity_type' => 'customer', | ||
'model_data' => ['id' => 1] | ||
]; | ||
|
||
$this->customerFactory | ||
->method('create') | ||
->willReturn($customer); | ||
$this->hydrator | ||
->method('hydrate') | ||
->with($customer, $resolverData['model_data']) | ||
->willReturnSelf(); | ||
$this->hydratorPool | ||
->method('getHydrator') | ||
->with('customer') | ||
->willReturn($this->hydrator); | ||
|
||
$this->modelHydrator->hydrate($resolverData); | ||
$this->modelHydrator->hydrate($resolverData); | ||
$this->assertSame($customer, $resolverData['model']); | ||
} | ||
|
||
/** | ||
* Test hydrate method with new model | ||
* | ||
* @return void | ||
* @throws Exception | ||
*/ | ||
|
||
public function testHydrateWithNewModel() | ||
{ | ||
$customer = $this->createMock(Customer::class); | ||
$resolverData = [ | ||
'model_id' => 1, | ||
'model_entity_type' => 'customer', | ||
'model_data' => ['id' => 1] | ||
]; | ||
|
||
$this->customerFactory | ||
->method('create') | ||
->willReturn($customer); | ||
$this->hydratorPool | ||
->method('getHydrator') | ||
->willReturn($this->hydrator); | ||
$this->hydrator->expects($this->once()) | ||
->method('hydrate') | ||
->with($customer, $resolverData['model_data']); | ||
|
||
$this->modelHydrator->hydrate($resolverData); | ||
$this->assertSame($customer, $resolverData['model']); | ||
} | ||
|
||
/** | ||
* Test that resetState method resets the state of the modelHydrator | ||
* | ||
* @return void | ||
* @throws Exception | ||
* | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function testResetState() | ||
{ | ||
$customer1 = $this->createMock(Customer::class); | ||
$customer2 = $this->createMock(Customer::class); | ||
|
||
$resolverData1 = [ | ||
'model_id' => 1, | ||
'model_entity_type' => 'customer', | ||
'model_data' => ['id' => 1] | ||
]; | ||
|
||
$resolverData2 = [ | ||
'model_id' => 2, | ||
'model_entity_type' => 'customer', | ||
'model_data' => ['id' => 2] | ||
]; | ||
|
||
$this->customerFactory | ||
->method('create') | ||
->willReturnOnConsecutiveCalls($customer1, $customer2); | ||
$this->hydratorPool | ||
->method('getHydrator') | ||
->willReturn($this->hydrator); | ||
|
||
$matcher = $this->exactly(2); | ||
$expected1 = $resolverData1['model_data']; | ||
$expected2 = $resolverData2['model_data']; | ||
|
||
$this->hydrator | ||
->expects($matcher) | ||
->method('hydrate') | ||
->willReturnCallback(function ($model, $data) use ($matcher, $expected1, $expected2) { | ||
match ($matcher->numberOfInvocations()) { | ||
1 => $this->assertEquals($expected1, $data), | ||
2 => $this->assertEquals($expected2, $data), | ||
}; | ||
}); | ||
|
||
$this->modelHydrator->hydrate($resolverData1); | ||
|
||
$this->assertArrayHasKey('model', $resolverData1); | ||
$this->assertEquals(1, $resolverData1['model_id']); | ||
|
||
$this->modelHydrator->_resetState(); | ||
|
||
$this->modelHydrator->hydrate($resolverData2); | ||
|
||
$this->assertArrayHasKey('model', $resolverData2); | ||
$this->assertEquals(2, $resolverData2['model_id']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters