|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\CatalogUrlRewrite\Test\Unit\Observer; |
| 8 | + |
| 9 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 10 | +use Magento\CatalogImportExport\Model\Import\Product as ImportProduct; |
| 11 | +use Magento\Store\Model\Store; |
| 12 | +use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator; |
| 13 | +use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; |
| 14 | + |
| 15 | +/** |
| 16 | + * Class ProductProcessUrlRewriteSavingObserverTest |
| 17 | + * |
| 18 | + * @SuppressWarnings(PHPMD.TooManyFields) |
| 19 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 20 | + */ |
| 21 | +class ProductProcessUrlRewriteSavingObserverTest extends \PHPUnit_Framework_TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var \Magento\UrlRewrite\Model\UrlPersistInterface|\PHPUnit_Framework_MockObject_MockObject |
| 25 | + */ |
| 26 | + protected $urlPersist; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var \Magento\Framework\Event|\PHPUnit_Framework_MockObject_MockObject |
| 30 | + */ |
| 31 | + protected $event; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var \Magento\Framework\Event\Observer|\PHPUnit_Framework_MockObject_MockObject |
| 35 | + */ |
| 36 | + protected $observer; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject |
| 40 | + */ |
| 41 | + protected $product; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator|\PHPUnit_Framework_MockObject_MockObject |
| 45 | + */ |
| 46 | + protected $productUrlRewriteGenerator; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var ObjectManager |
| 50 | + */ |
| 51 | + protected $objectManager; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var \Magento\CatalogUrlRewrite\Observer\ProductProcessUrlRewriteSavingObserver |
| 55 | + */ |
| 56 | + protected $model; |
| 57 | + |
| 58 | + /** |
| 59 | + * Set up |
| 60 | + */ |
| 61 | + protected function setUp() |
| 62 | + { |
| 63 | + $this->urlPersist = $this->getMock(\Magento\UrlRewrite\Model\UrlPersistInterface::class, [], [], '', false); |
| 64 | + $this->product = $this->getMock( |
| 65 | + \Magento\Catalog\Model\Product::class, |
| 66 | + [ |
| 67 | + 'getId', |
| 68 | + 'dataHasChangedFor', |
| 69 | + 'isVisibleInSiteVisibility', |
| 70 | + 'getIsChangedWebsites', |
| 71 | + 'getIsChangedCategories' |
| 72 | + ], |
| 73 | + [], |
| 74 | + '', |
| 75 | + false |
| 76 | + ); |
| 77 | + $this->product->expects($this->any())->method('getId')->will($this->returnValue(3)); |
| 78 | + $this->event = $this->getMock(\Magento\Framework\Event::class, ['getProduct'], [], '', false); |
| 79 | + $this->event->expects($this->any())->method('getProduct')->willReturn($this->product); |
| 80 | + $this->observer = $this->getMock(\Magento\Framework\Event\Observer::class, ['getEvent'], [], '', false); |
| 81 | + $this->observer->expects($this->any())->method('getEvent')->willReturn($this->event); |
| 82 | + $this->productUrlRewriteGenerator = $this->getMock( |
| 83 | + \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::class, |
| 84 | + ['generate'], |
| 85 | + [], |
| 86 | + '', |
| 87 | + false |
| 88 | + ); |
| 89 | + $this->productUrlRewriteGenerator->expects($this->any()) |
| 90 | + ->method('generate') |
| 91 | + ->will($this->returnValue([3 => 'rewrite'])); |
| 92 | + $this->objectManager = new ObjectManager($this); |
| 93 | + $this->model = $this->objectManager->getObject( |
| 94 | + \Magento\CatalogUrlRewrite\Observer\ProductProcessUrlRewriteSavingObserver::class, |
| 95 | + [ |
| 96 | + 'productUrlRewriteGenerator' => $this->productUrlRewriteGenerator, |
| 97 | + 'urlPersist' => $this->urlPersist |
| 98 | + ] |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Data provider |
| 104 | + * |
| 105 | + * @return array |
| 106 | + */ |
| 107 | + public function testUrlKeyDataProvider() |
| 108 | + { |
| 109 | + return [ |
| 110 | + 'url changed' => [ |
| 111 | + 'isChangedUrlKey' => true, |
| 112 | + 'isChangedVisibility' => false, |
| 113 | + 'isChangedWebsites' => false, |
| 114 | + 'isChangedCategories' => false, |
| 115 | + 'visibilityResult' => true, |
| 116 | + 'expectedDeleteCount' => 1, |
| 117 | + 'expectedReplaceCount' => 1 |
| 118 | + ], |
| 119 | + 'no chnages' => [ |
| 120 | + 'isChangedUrlKey' => false, |
| 121 | + 'isChangedVisibility' => false, |
| 122 | + 'isChangedWebsites' => false, |
| 123 | + 'isChangedCategories' => false, |
| 124 | + 'visibilityResult' => true, |
| 125 | + 'expectedDeleteCount' => 0, |
| 126 | + 'expectedReplaceCount' => 0 |
| 127 | + ], |
| 128 | + 'visibility changed' => [ |
| 129 | + 'isChangedUrlKey' => false, |
| 130 | + 'isChangedVisibility' => true, |
| 131 | + 'isChangedWebsites' => false, |
| 132 | + 'isChangedCategories' => false, |
| 133 | + 'visibilityResult' => true, |
| 134 | + 'expectedDeleteCount' => 1, |
| 135 | + 'expectedReplaceCount' => 1 |
| 136 | + ], |
| 137 | + 'websites changed' => [ |
| 138 | + 'isChangedUrlKey' => false, |
| 139 | + 'isChangedVisibility' => false, |
| 140 | + 'isChangedWebsites' => true, |
| 141 | + 'isChangedCategories' => false, |
| 142 | + 'visibilityResult' => true, |
| 143 | + 'expectedDeleteCount' => 1, |
| 144 | + 'expectedReplaceCount' => 1 |
| 145 | + ], |
| 146 | + 'categories changed' => [ |
| 147 | + 'isChangedUrlKey' => false, |
| 148 | + 'isChangedVisibility' => false, |
| 149 | + 'isChangedWebsites' => false, |
| 150 | + 'isChangedCategories' => true, |
| 151 | + 'visibilityResult' => true, |
| 152 | + 'expectedDeleteCount' => 1, |
| 153 | + 'expectedReplaceCount' => 1 |
| 154 | + ], |
| 155 | + 'url changed invisible' => [ |
| 156 | + 'isChangedUrlKey' => true, |
| 157 | + 'isChangedVisibility' => false, |
| 158 | + 'isChangedWebsites' => false, |
| 159 | + 'isChangedCategories' => false, |
| 160 | + 'visibilityResult' => false, |
| 161 | + 'expectedDeleteCount' => 1, |
| 162 | + 'expectedReplaceCount' => 0 |
| 163 | + ], |
| 164 | + ]; |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * @param bool $isChangedUrlKey |
| 169 | + * @param bool $isChangedVisibility |
| 170 | + * @param bool $isChangedWebsites |
| 171 | + * @param bool $isChangedCategories |
| 172 | + * @param bool $visibilityResult |
| 173 | + * @param int $expectedDeleteCount |
| 174 | + * @param int $expectedReplaceCount |
| 175 | + * |
| 176 | + * @dataProvider testUrlKeyDataProvider |
| 177 | + */ |
| 178 | + public function testExecuteUrlKey( |
| 179 | + $isChangedUrlKey, |
| 180 | + $isChangedVisibility, |
| 181 | + $isChangedWebsites, |
| 182 | + $isChangedCategories, |
| 183 | + $visibilityResult, |
| 184 | + $expectedDeleteCount, |
| 185 | + $expectedReplaceCount |
| 186 | + ) { |
| 187 | + $this->product->expects($this->any()) |
| 188 | + ->method('dataHasChangedFor') |
| 189 | + ->will($this->returnValueMap( |
| 190 | + [ |
| 191 | + ['visibility', $isChangedVisibility], |
| 192 | + ['url_key', $isChangedUrlKey] |
| 193 | + ] |
| 194 | + )); |
| 195 | + |
| 196 | + $this->product->expects($this->any()) |
| 197 | + ->method('getIsChangedWebsites') |
| 198 | + ->will($this->returnValue($isChangedWebsites)); |
| 199 | + |
| 200 | + $this->product->expects($this->any()) |
| 201 | + ->method('getIsChangedCategories') |
| 202 | + ->will($this->returnValue($isChangedCategories)); |
| 203 | + |
| 204 | + $this->urlPersist->expects($this->exactly($expectedDeleteCount))->method('deleteByData')->with([ |
| 205 | + UrlRewrite::ENTITY_ID => $this->product->getId(), |
| 206 | + UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE, |
| 207 | + UrlRewrite::REDIRECT_TYPE => 0, |
| 208 | + ]); |
| 209 | + |
| 210 | + $this->product->expects($this->any()) |
| 211 | + ->method('isVisibleInSiteVisibility') |
| 212 | + ->will($this->returnValue($visibilityResult)); |
| 213 | + |
| 214 | + $this->urlPersist->expects($this->exactly($expectedReplaceCount)) |
| 215 | + ->method('replace') |
| 216 | + ->with([3 => 'rewrite']); |
| 217 | + |
| 218 | + $this->model->execute($this->observer); |
| 219 | + } |
| 220 | +} |
0 commit comments