|
| 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 | +} |
0 commit comments