Skip to content

Commit 6817e68

Browse files
committed
Prevent Category URL Rewrites generation when no Store Group assigned
Add test coverage
1 parent bac82cc commit 6817e68

File tree

1 file changed

+66
-22
lines changed
  • app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Store

1 file changed

+66
-22
lines changed

app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Store/ViewTest.php

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77

88
namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Category\Plugin\Store;
99

10-
use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection;
1110
use Magento\Catalog\Model\Category;
1211
use Magento\Catalog\Model\CategoryFactory;
1312
use Magento\Catalog\Model\Product;
1413
use Magento\Catalog\Model\ProductFactory;
14+
use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection;
1515
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
1616
use Magento\CatalogUrlRewrite\Model\Category\Plugin\Store\View as StoreViewPlugin;
1717
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
1818
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
1919
use Magento\Framework\Model\AbstractModel;
20-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
21-
use Magento\Store\Model\ResourceModel\Store;
20+
use Magento\Store\Model\ResourceModel\Store as StoreResourceModel;
21+
use Magento\Store\Model\Store;
2222
use Magento\UrlRewrite\Model\UrlPersistInterface;
2323
use PHPUnit\Framework\MockObject\MockObject;
2424
use PHPUnit\Framework\TestCase;
@@ -28,11 +28,6 @@
2828
*/
2929
class ViewTest extends TestCase
3030
{
31-
/**
32-
* @var ObjectManager
33-
*/
34-
private $objectManager;
35-
3631
/**
3732
* @var StoreViewPlugin
3833
*/
@@ -44,7 +39,7 @@ class ViewTest extends TestCase
4439
private $abstractModelMock;
4540

4641
/**
47-
* @var Store|MockObject
42+
* @var StoreResourceModel|MockObject
4843
*/
4944
private $subjectMock;
5045

@@ -93,12 +88,11 @@ class ViewTest extends TestCase
9388
*/
9489
protected function setUp(): void
9590
{
96-
$this->objectManager = new ObjectManager($this);
9791
$this->abstractModelMock = $this->getMockBuilder(AbstractModel::class)
9892
->disableOriginalConstructor()
9993
->setMethods(['isObjectNew'])
10094
->getMockForAbstractClass();
101-
$this->subjectMock = $this->getMockBuilder(Store::class)
95+
$this->subjectMock = $this->getMockBuilder(StoreResourceModel::class)
10296
->disableOriginalConstructor()
10397
->getMock();
10498
$this->urlPersistMock = $this->getMockBuilder(UrlPersistInterface::class)
@@ -131,15 +125,12 @@ protected function setUp(): void
131125
->disableOriginalConstructor()
132126
->setMethods(['getCollection'])
133127
->getMock();
134-
$this->plugin = $this->objectManager->getObject(
135-
StoreViewPlugin::class,
136-
[
137-
'urlPersist' => $this->urlPersistMock,
138-
'categoryFactory' => $this->categoryFactoryMock,
139-
'productFactory' => $this->productFactoryMock,
140-
'categoryUrlRewriteGenerator' => $this->categoryUrlRewriteGeneratorMock,
141-
'productUrlRewriteGenerator' => $this->productUrlRewriteGeneratorMock
142-
]
128+
$this->plugin = new StoreViewPlugin(
129+
$this->urlPersistMock,
130+
$this->categoryFactoryMock,
131+
$this->productFactoryMock,
132+
$this->categoryUrlRewriteGeneratorMock,
133+
$this->productUrlRewriteGeneratorMock
143134
);
144135
}
145136

@@ -150,13 +141,19 @@ protected function setUp(): void
150141
*/
151142
public function testAfterSave(): void
152143
{
153-
$origStoreMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
144+
$origStoreMock = $this->getMockBuilder(Store::class)
154145
->disableOriginalConstructor()
155146
->getMock();
156147
$reflectionStore = new \ReflectionClass($this->plugin);
157148
$origStore = $reflectionStore->getProperty('origStore');
158149
$origStore->setAccessible(true);
159150
$origStore->setValue($this->plugin, $origStoreMock);
151+
152+
$origStoreMock->expects($this->atLeastOnce())
153+
->method('getData')
154+
->with('group_id')
155+
->willReturn('1');
156+
160157
$origStoreMock->expects($this->atLeastOnce())
161158
->method('isObjectNew')
162159
->willReturn(true);
@@ -203,7 +200,54 @@ public function testAfterSave(): void
203200

204201
$this->assertSame(
205202
$this->subjectMock,
206-
$this->plugin->afterSave($this->subjectMock, $this->subjectMock, $this->abstractModelMock)
203+
$this->plugin->afterSave($this->subjectMock, $this->subjectMock)
204+
);
205+
}
206+
207+
public function testAfterSaveWhenNoGroupId()
208+
{
209+
$origStoreMock = $this->getMockBuilder(Store::class)
210+
->disableOriginalConstructor()
211+
->getMock();
212+
$reflectionStore = new \ReflectionClass($this->plugin);
213+
$origStore = $reflectionStore->getProperty('origStore');
214+
$origStore->setAccessible(true);
215+
$origStore->setValue($this->plugin, $origStoreMock);
216+
217+
$origStoreMock->expects($this->atLeastOnce())
218+
->method('getData')
219+
->with('group_id')
220+
->willReturn(null);
221+
222+
$origStoreMock->expects($this->any())
223+
->method('isObjectNew')
224+
->willReturn(true);
225+
226+
$this->abstractModelMock->expects($this->any())
227+
->method('isObjectNew')
228+
->willReturn(true);
229+
$categoryCollection = $this->getMockBuilder(CategoryCollection::class)
230+
->disableOriginalConstructor()
231+
->setMethods(['getIterator'])
232+
->getMock();
233+
$categoryCollection->expects($this->any())
234+
->method('getIterator')
235+
->willReturn(new \ArrayIterator([]));
236+
$this->categoryMock->expects($this->never())
237+
->method('getCategories');
238+
$this->categoryFactoryMock->expects($this->never())->method('create');
239+
$this->productFactoryMock->expects($this->never())->method('create');
240+
$this->productMock->expects($this->never())->method('getCollection');
241+
$this->productCollectionMock->expects($this->never())->method('addCategoryIds');
242+
$this->productCollectionMock->expects($this->never())->method('addAttributeToSelect');
243+
$this->productCollectionMock->expects($this->never())->method('addStoreFilter');
244+
245+
$this->productCollectionMock->expects($this->never())->method('getIterator');
246+
$this->productUrlRewriteGeneratorMock->expects($this->never())->method('generate');
247+
248+
$this->assertSame(
249+
$this->subjectMock,
250+
$this->plugin->afterSave($this->subjectMock, $this->subjectMock)
207251
);
208252
}
209253

0 commit comments

Comments
 (0)