|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\CatalogUrlRewrite\Test\Unit\Plugin\Catalog\Block\Adminhtml\Category\Tab; |
| 10 | + |
| 11 | +use Magento\Catalog\Model\Category; |
| 12 | +use Magento\Catalog\Model\Category\DataProvider as CategoryDataProvider; |
| 13 | +use Magento\CatalogUrlRewrite\Plugin\Catalog\Block\Adminhtml\Category\Tab\Attributes; |
| 14 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 15 | +use PHPUnit\Framework\MockObject\MockObject; |
| 16 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +/** |
| 20 | + * Test for \Magento\CatalogUrlRewrite\Plugin\Catalog\Block\Adminhtml\Category\Tab\Attributes. |
| 21 | + */ |
| 22 | +class AttributesTest extends TestCase |
| 23 | +{ |
| 24 | + private const STUB_CATEGORY_META = ['url_key' => 'url_key_test']; |
| 25 | + private const STUB_URL_KEY = 'url_key_777'; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var Attributes |
| 29 | + */ |
| 30 | + private $model; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var Category|MockObject |
| 34 | + */ |
| 35 | + private $categoryMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var CategoryDataProvider|MockObject |
| 39 | + */ |
| 40 | + private $dataProviderMock; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var ScopeConfigInterface|MockObject |
| 44 | + */ |
| 45 | + private $scopeConfigMock; |
| 46 | + |
| 47 | + /** |
| 48 | + * @inheritdoc |
| 49 | + */ |
| 50 | + protected function setUp(): void |
| 51 | + { |
| 52 | + $objectManager = new ObjectManager($this); |
| 53 | + |
| 54 | + $this->categoryMock = $this->createMock(Category::class); |
| 55 | + $this->dataProviderMock = $this->createMock(CategoryDataProvider::class); |
| 56 | + $this->dataProviderMock->expects($this->any()) |
| 57 | + ->method('getCurrentCategory') |
| 58 | + ->willReturn($this->categoryMock); |
| 59 | + |
| 60 | + $this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class); |
| 61 | + $this->model = $objectManager->getObject(Attributes::class, ['scopeConfig' => $this->scopeConfigMock]); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Test get attributes meta |
| 66 | + * |
| 67 | + * @dataProvider attributesMetaDataProvider |
| 68 | + * |
| 69 | + * @param bool $configEnabled |
| 70 | + * @param bool|string $expected |
| 71 | + * @return void |
| 72 | + */ |
| 73 | + public function testGetAttributesMeta(bool $configEnabled, $expected): void |
| 74 | + { |
| 75 | + $this->categoryMock->expects($this->once()) |
| 76 | + ->method('getId') |
| 77 | + ->willReturn(1); |
| 78 | + $this->categoryMock->expects($this->once()) |
| 79 | + ->method('getLevel') |
| 80 | + ->willReturn(2); |
| 81 | + $this->categoryMock->expects($this->atMost(2)) |
| 82 | + ->method('getUrlKey') |
| 83 | + ->willReturn(self::STUB_URL_KEY); |
| 84 | + $this->scopeConfigMock->expects($this->once()) |
| 85 | + ->method('isSetFlag') |
| 86 | + ->willReturn($configEnabled); |
| 87 | + $this->categoryMock->expects($this->once()) |
| 88 | + ->method('getStoreId') |
| 89 | + ->willReturn(1); |
| 90 | + |
| 91 | + $result = $this->model->afterGetAttributesMeta($this->dataProviderMock, self::STUB_CATEGORY_META); |
| 92 | + |
| 93 | + $this->assertArrayHasKey('url_key_create_redirect', $result); |
| 94 | + |
| 95 | + $this->assertArrayHasKey('value', $result['url_key_create_redirect']); |
| 96 | + $this->assertEquals(self::STUB_URL_KEY, $result['url_key_create_redirect']['value']); |
| 97 | + |
| 98 | + $this->assertArrayHasKey('valueMap', $result['url_key_create_redirect']); |
| 99 | + $this->assertArrayHasKey('true', $result['url_key_create_redirect']['valueMap']); |
| 100 | + $this->assertEquals($expected, $result['url_key_create_redirect']['valueMap']['true']); |
| 101 | + |
| 102 | + $this->assertArrayHasKey('disabled', $result['url_key_create_redirect']); |
| 103 | + $this->assertTrue($result['url_key_create_redirect']['disabled']); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * DataProvider for testGetAttributesMeta |
| 108 | + * |
| 109 | + * @return array |
| 110 | + */ |
| 111 | + public function attributesMetaDataProvider(): array |
| 112 | + { |
| 113 | + return [ |
| 114 | + 'save rewrite history config enabled' => [true, self::STUB_URL_KEY], |
| 115 | + 'save rewrite history config disabled' => [false, false] |
| 116 | + ]; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Test get category without id attributes meta |
| 121 | + * |
| 122 | + * @return void |
| 123 | + */ |
| 124 | + public function testGetAttributesMetaWithoutCategoryId(): void |
| 125 | + { |
| 126 | + $this->categoryMock->expects($this->once()) |
| 127 | + ->method('getId') |
| 128 | + ->willReturn(null); |
| 129 | + |
| 130 | + $result = $this->model->afterGetAttributesMeta($this->dataProviderMock, self::STUB_CATEGORY_META); |
| 131 | + |
| 132 | + $this->assertArrayHasKey('url_key_create_redirect', $result); |
| 133 | + $this->assertArrayHasKey('visible', $result['url_key_create_redirect']); |
| 134 | + $this->assertFalse($result['url_key_create_redirect']['visible']); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Test get root category attributes meta |
| 139 | + * |
| 140 | + * @return void |
| 141 | + */ |
| 142 | + public function testGetAttributesMetaRootCategory(): void |
| 143 | + { |
| 144 | + $this->categoryMock->expects($this->once()) |
| 145 | + ->method('getId') |
| 146 | + ->willReturn(1); |
| 147 | + $this->categoryMock->expects($this->once()) |
| 148 | + ->method('getLevel') |
| 149 | + ->willReturn(1); |
| 150 | + |
| 151 | + $result = $this->model->afterGetAttributesMeta($this->dataProviderMock, self::STUB_CATEGORY_META); |
| 152 | + |
| 153 | + $this->assertArrayHasKey('url_key_group', $result); |
| 154 | + $this->assertArrayHasKey('componentDisabled', $result['url_key_group']); |
| 155 | + $this->assertTrue($result['url_key_group']['componentDisabled']); |
| 156 | + } |
| 157 | +} |
0 commit comments