|
| 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\CatalogUrlRewrite\Test\Unit\Observer; |
| 9 | + |
| 10 | +use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator; |
| 11 | +use Magento\CatalogUrlRewrite\Observer\CategoryProcessUrlRewriteSavingObserver; |
| 12 | +use Magento\CatalogUrlRewrite\Model\UrlRewriteBunchReplacer; |
| 13 | +use Magento\CatalogUrlRewrite\Observer\UrlRewriteHandler; |
| 14 | +use Magento\CatalogUrlRewrite\Model\Map\DatabaseMapPool; |
| 15 | +use Magento\Store\Model\ResourceModel\Group\CollectionFactory; |
| 16 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 17 | +use Magento\Catalog\Model\Category; |
| 18 | + |
| 19 | +/** |
| 20 | + * Unit tests for \Magento\CatalogUrlRewrite\Observer\CategoryProcessUrlRewriteSavingObserver class. |
| 21 | + */ |
| 22 | +class CategoryProcessUrlRewriteSavingObserverTest extends \PHPUnit\Framework\TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject |
| 26 | + */ |
| 27 | + private $observer; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject |
| 31 | + */ |
| 32 | + private $category; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var CategoryProcessUrlRewriteSavingObserver |
| 36 | + */ |
| 37 | + private $categoryProcessUrlRewriteSavingObserver; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var CategoryUrlRewriteGenerator|\PHPUnit_Framework_MockObject_MockObject |
| 41 | + */ |
| 42 | + private $categoryUrlRewriteGeneratorMock; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var UrlRewriteBunchReplacer|\PHPUnit_Framework_MockObject_MockObject |
| 46 | + */ |
| 47 | + private $urlRewriteBunchReplacerMock; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var UrlRewriteHandler|\PHPUnit_Framework_MockObject_MockObject |
| 51 | + */ |
| 52 | + private $urlRewriteHandlerMock; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var DatabaseMapPool|\PHPUnit_Framework_MockObject_MockObject |
| 56 | + */ |
| 57 | + private $databaseMapPoolMock; |
| 58 | + |
| 59 | + /** |
| 60 | + * @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject |
| 61 | + */ |
| 62 | + private $storeGroupFactory; |
| 63 | + |
| 64 | + /** |
| 65 | + * {@inheritDoc} |
| 66 | + */ |
| 67 | + protected function setUp() |
| 68 | + { |
| 69 | + $this->observer = $this->createPartialMock( |
| 70 | + \Magento\Framework\Event\Observer::class, |
| 71 | + ['getEvent', 'getData'] |
| 72 | + ); |
| 73 | + $this->category = $this->createPartialMock(Category::class, [ |
| 74 | + 'hasData', |
| 75 | + 'getParentId', |
| 76 | + 'dataHasChangedFor', |
| 77 | + 'getIsChangedProductList', |
| 78 | + ]); |
| 79 | + $this->observer->expects($this->any()) |
| 80 | + ->method('getEvent') |
| 81 | + ->willReturnSelf(); |
| 82 | + $this->observer->expects($this->any()) |
| 83 | + ->method('getData') |
| 84 | + ->with('category') |
| 85 | + ->willReturn($this->category); |
| 86 | + |
| 87 | + $this->categoryUrlRewriteGeneratorMock = $this->getMockBuilder(CategoryUrlRewriteGenerator::class) |
| 88 | + ->disableOriginalConstructor() |
| 89 | + ->getMock(); |
| 90 | + $this->urlRewriteBunchReplacerMock = $this->getMockBuilder(UrlRewriteBunchReplacer::class) |
| 91 | + ->disableOriginalConstructor() |
| 92 | + ->getMock(); |
| 93 | + $this->urlRewriteHandlerMock = $this->getMockBuilder(UrlRewriteHandler::class) |
| 94 | + ->disableOriginalConstructor() |
| 95 | + ->getMock(); |
| 96 | + $this->databaseMapPoolMock = $this->getMockBuilder(DatabaseMapPool::class) |
| 97 | + ->disableOriginalConstructor() |
| 98 | + ->getMock(); |
| 99 | + $this->storeGroupFactory = $this->getMockBuilder(CollectionFactory::class) |
| 100 | + ->setMethods(['create']) |
| 101 | + ->disableOriginalConstructor() |
| 102 | + ->getMock(); |
| 103 | + |
| 104 | + $this->categoryProcessUrlRewriteSavingObserver = (new ObjectManagerHelper($this))->getObject( |
| 105 | + CategoryProcessUrlRewriteSavingObserver::class, |
| 106 | + [ |
| 107 | + 'categoryUrlRewriteGenerator' => $this->categoryUrlRewriteGeneratorMock, |
| 108 | + 'urlRewriteHandler' => $this->urlRewriteHandlerMock, |
| 109 | + 'urlRewriteBunchReplacer' => $this->urlRewriteBunchReplacerMock, |
| 110 | + 'databaseMapPool' => $this->databaseMapPoolMock, |
| 111 | + 'storeGroupFactory' => $this->storeGroupFactory, |
| 112 | + ] |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + public function testExecuteForRootDirectory() |
| 117 | + { |
| 118 | + $this->category->expects($this->once()) |
| 119 | + ->method('getParentId') |
| 120 | + ->willReturn(Category::TREE_ROOT_ID); |
| 121 | + $this->category->expects($this->never()) |
| 122 | + ->method('hasData'); |
| 123 | + |
| 124 | + $this->categoryProcessUrlRewriteSavingObserver->execute($this->observer); |
| 125 | + } |
| 126 | + |
| 127 | + public function testExecuteHasStoreId() |
| 128 | + { |
| 129 | + $this->category->expects($this->once()) |
| 130 | + ->method('getParentId') |
| 131 | + ->willReturn(2); |
| 132 | + $this->category->expects($this->once()) |
| 133 | + ->method('hasData') |
| 134 | + ->willReturn(true); |
| 135 | + $this->storeGroupFactory->expects($this->never()) |
| 136 | + ->method('create'); |
| 137 | + $this->category->expects($this->any()) |
| 138 | + ->method('dataHasChangedFor') |
| 139 | + ->willReturnMap( |
| 140 | + [ |
| 141 | + ['url_key', false], |
| 142 | + ['is_anchor', false], |
| 143 | + ] |
| 144 | + ); |
| 145 | + $this->category->expects($this->once()) |
| 146 | + ->method('getIsChangedProductList') |
| 147 | + ->willReturn(false); |
| 148 | + |
| 149 | + $this->categoryProcessUrlRewriteSavingObserver->execute($this->observer); |
| 150 | + } |
| 151 | + |
| 152 | + public function testExecuteHasNotChanges() |
| 153 | + { |
| 154 | + $this->category->expects($this->once()) |
| 155 | + ->method('getParentId') |
| 156 | + ->willReturn(2); |
| 157 | + $this->category->expects($this->once()) |
| 158 | + ->method('hasData') |
| 159 | + ->willReturn(false); |
| 160 | + $this->storeGroupFactory->expects($this->once()) |
| 161 | + ->method('create') |
| 162 | + ->willReturn([]); |
| 163 | + $this->category->expects($this->any()) |
| 164 | + ->method('dataHasChangedFor') |
| 165 | + ->willReturnMap( |
| 166 | + [ |
| 167 | + ['url_key', false], |
| 168 | + ['is_anchor', false], |
| 169 | + ] |
| 170 | + ); |
| 171 | + $this->category->expects($this->once()) |
| 172 | + ->method('getIsChangedProductList') |
| 173 | + ->willReturn(false); |
| 174 | + $this->databaseMapPoolMock->expects($this->never()) |
| 175 | + ->method('resetMap'); |
| 176 | + |
| 177 | + $this->categoryProcessUrlRewriteSavingObserver->execute($this->observer); |
| 178 | + } |
| 179 | + |
| 180 | + public function testExecuteHasChanges() |
| 181 | + { |
| 182 | + $this->category->expects($this->once()) |
| 183 | + ->method('getParentId') |
| 184 | + ->willReturn(2); |
| 185 | + $this->category->expects($this->once()) |
| 186 | + ->method('hasData') |
| 187 | + ->willReturn(false); |
| 188 | + $this->storeGroupFactory->expects($this->once()) |
| 189 | + ->method('create') |
| 190 | + ->willReturn([]); |
| 191 | + $this->category->expects($this->any()) |
| 192 | + ->method('dataHasChangedFor') |
| 193 | + ->willReturnMap( |
| 194 | + [ |
| 195 | + ['url_key', true], |
| 196 | + ['is_anchor', false], |
| 197 | + ] |
| 198 | + ); |
| 199 | + $this->category->expects($this->any()) |
| 200 | + ->method('getIsChangedProductList') |
| 201 | + ->willReturn(false); |
| 202 | + |
| 203 | + $result1 = ['test']; |
| 204 | + $this->categoryUrlRewriteGeneratorMock->expects($this->once()) |
| 205 | + ->method('generate') |
| 206 | + ->with($this->category) |
| 207 | + ->willReturn($result1); |
| 208 | + $this->urlRewriteBunchReplacerMock->expects($this->at(0)) |
| 209 | + ->method('doBunchReplace') |
| 210 | + ->with($result1) |
| 211 | + ->willReturn(null); |
| 212 | + |
| 213 | + $result2 = ['test2']; |
| 214 | + $this->urlRewriteHandlerMock->expects($this->once()) |
| 215 | + ->method('generateProductUrlRewrites') |
| 216 | + ->with($this->category) |
| 217 | + ->willReturn($result2); |
| 218 | + $this->urlRewriteBunchReplacerMock->expects($this->at(1)) |
| 219 | + ->method('doBunchReplace') |
| 220 | + ->with($result2) |
| 221 | + ->willReturn(null); |
| 222 | + |
| 223 | + $this->databaseMapPoolMock->expects($this->any()) |
| 224 | + ->method('resetMap'); |
| 225 | + |
| 226 | + $this->categoryProcessUrlRewriteSavingObserver->execute($this->observer); |
| 227 | + } |
| 228 | +} |
0 commit comments