|
| 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\Product; |
| 9 | + |
| 10 | +use Laminas\Stdlib\Parameters; |
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper; |
| 13 | +use Magento\Catalog\Model\Product; |
| 14 | +use Magento\Framework\App\Request\Http as HttpRequest; |
| 15 | +use Magento\Framework\Exception\AuthorizationException; |
| 16 | +use Magento\Framework\ObjectManagerInterface; |
| 17 | +use Magento\TestFramework\Helper\Bootstrap; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +/** |
| 21 | + * Verify additional authorization for product operations |
| 22 | + * |
| 23 | + * @magentoAppArea adminhtml |
| 24 | + */ |
| 25 | +class AuthorizationTest extends TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var ObjectManagerInterface |
| 29 | + */ |
| 30 | + private $objectManager; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var Helper |
| 34 | + */ |
| 35 | + private $initializationHelper; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var HttpRequest |
| 39 | + */ |
| 40 | + private $request; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var ProductRepositoryInterface |
| 44 | + */ |
| 45 | + private $productRepository; |
| 46 | + |
| 47 | + /** |
| 48 | + * @inheritdoc |
| 49 | + */ |
| 50 | + protected function setUp(): void |
| 51 | + { |
| 52 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 53 | + $this->initializationHelper = $this->objectManager->get(Helper::class); |
| 54 | + $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class); |
| 55 | + $this->request = $this->objectManager->get(HttpRequest::class); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Verify AuthorizedSavingOf |
| 60 | + * |
| 61 | + * @magentoDataFixture Magento/Catalog/_files/product_simple.php |
| 62 | + * @param array $data |
| 63 | + * |
| 64 | + * @dataProvider postRequestData |
| 65 | + */ |
| 66 | + public function testAuthorizedSavingOf(array $data): void |
| 67 | + { |
| 68 | + $this->request->setPost(new Parameters($data)); |
| 69 | + |
| 70 | + /** @var Product $product */ |
| 71 | + $product = $this->productRepository->get('simple'); |
| 72 | + |
| 73 | + $product = $this->initializationHelper->initialize($product); |
| 74 | + $this->assertEquals('simple_new', $product->getName()); |
| 75 | + $this->assertEquals( |
| 76 | + 'container2', |
| 77 | + $product->getCustomAttribute('options_container')->getValue() |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @return array |
| 83 | + */ |
| 84 | + public function postRequestData(): array |
| 85 | + { |
| 86 | + return [ |
| 87 | + [ |
| 88 | + [ |
| 89 | + 'product' => [ |
| 90 | + 'name' => 'simple_new', |
| 91 | + 'custom_design' => '', |
| 92 | + 'page_layout' => '', |
| 93 | + 'options_container' => 'container2', |
| 94 | + 'custom_layout_update' => '', |
| 95 | + 'custom_design_from' => '', |
| 96 | + 'custom_design_to' => '', |
| 97 | + 'custom_layout_update_file' => '', |
| 98 | + ], |
| 99 | + 'use_default' => [ |
| 100 | + 'custom_design' => '1', |
| 101 | + 'page_layout' => '1', |
| 102 | + 'options_container' => '1', |
| 103 | + 'custom_layout' => '1', |
| 104 | + 'custom_design_from' => '1', |
| 105 | + 'custom_design_to' => '1', |
| 106 | + 'custom_layout_update_file' => '1', |
| 107 | + ], |
| 108 | + ], |
| 109 | + ], |
| 110 | + [ |
| 111 | + [ |
| 112 | + 'product' => [ |
| 113 | + 'name' => 'simple_new', |
| 114 | + 'page_layout' => '', |
| 115 | + 'options_container' => 'container2', |
| 116 | + 'custom_design' => '', |
| 117 | + 'custom_design_from' => '', |
| 118 | + 'custom_design_to' => '', |
| 119 | + 'custom_layout' => '', |
| 120 | + 'custom_layout_update_file' => '__no_update__', |
| 121 | + ], |
| 122 | + 'use_default' => null, |
| 123 | + ], |
| 124 | + ], |
| 125 | + ]; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Verify AuthorizedSavingOf when change design attributes |
| 130 | + * |
| 131 | + * @magentoDataFixture Magento/Catalog/_files/product_simple.php |
| 132 | + * @param array $data |
| 133 | + * |
| 134 | + * @dataProvider postRequestDataException |
| 135 | + */ |
| 136 | + public function testAuthorizedSavingOfWithException(array $data): void |
| 137 | + { |
| 138 | + $this->expectException(AuthorizationException::class); |
| 139 | + $this->expectErrorMessage('Not allowed to edit the product\'s design attributes'); |
| 140 | + $this->request->setPost(new Parameters($data)); |
| 141 | + |
| 142 | + /** @var Product $product */ |
| 143 | + $product = $this->productRepository->get('simple'); |
| 144 | + |
| 145 | + $this->initializationHelper->initialize($product); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @return array |
| 150 | + */ |
| 151 | + public function postRequestDataException(): array |
| 152 | + { |
| 153 | + return [ |
| 154 | + [ |
| 155 | + [ |
| 156 | + 'product' => [ |
| 157 | + 'name' => 'simple_new', |
| 158 | + 'page_layout' => '1column', |
| 159 | + 'options_container' => 'container2', |
| 160 | + 'custom_design' => '', |
| 161 | + 'custom_design_from' => '', |
| 162 | + 'custom_design_to' => '', |
| 163 | + 'custom_layout' => '', |
| 164 | + 'custom_layout_update_file' => '__no_update__', |
| 165 | + ], |
| 166 | + 'use_default' => null, |
| 167 | + ], |
| 168 | + ], |
| 169 | + ]; |
| 170 | + } |
| 171 | +} |
0 commit comments