88namespace Magento \CatalogUrlRewrite \Test \Unit \Model \Category \Plugin ;
99
1010use Magento \CatalogUrlRewrite \Model \Category \Plugin \Storage as CategoryStoragePlugin ;
11- use Magento \CatalogUrlRewrite \Model \Category \Product ;
1211use Magento \CatalogUrlRewrite \Model \ResourceModel \Category \Product as ProductResourceModel ;
13- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
12+ use Magento \Framework \Serialize \Serializer \Json ;
13+ use Magento \UrlRewrite \Model \MergeDataProvider ;
14+ use Magento \UrlRewrite \Model \MergeDataProviderFactory ;
1415use Magento \UrlRewrite \Model \StorageInterface ;
1516use Magento \UrlRewrite \Model \UrlFinderInterface ;
1617use Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ;
1718use PHPUnit \Framework \MockObject \MockObject ;
1819use PHPUnit \Framework \TestCase ;
1920
20- /**
21- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
22- */
2321class StorageTest extends TestCase
2422{
2523 /**
@@ -37,66 +35,164 @@ class StorageTest extends TestCase
3735 */
3836 private $ storage ;
3937
40- /**
41- * @var Product|MockObject
42- */
43- private $ product ;
44-
4538 /**
4639 * @var ProductResourceModel|MockObject
4740 */
4841 private $ productResourceModel ;
4942
50- /**
51- * @var UrlRewrite|MockObject
52- */
53- private $ urlRewrite ;
54-
5543 protected function setUp (): void
5644 {
5745 $ this ->storage = $ this ->getMockBuilder (StorageInterface::class)
5846 ->getMockForAbstractClass ();
5947 $ this ->urlFinder = $ this ->getMockBuilder (UrlFinderInterface::class)
6048 ->getMockForAbstractClass ();
61- $ this ->product = $ this ->getMockBuilder (Product::class)
62- ->disableOriginalConstructor ()
63- ->getMock ();
6449 $ this ->productResourceModel = $ this ->getMockBuilder (ProductResourceModel::class)
6550 ->disableOriginalConstructor ()
6651 ->getMock ();
67- $ this ->urlRewrite = $ this ->getMockBuilder (UrlRewrite::class)
68- ->disableOriginalConstructor ()
69- ->setMethods (['getMetadata ' , 'getEntityType ' , 'getIsAutogenerated ' , 'getUrlRewriteId ' , 'getEntityId ' ])
70- ->getMock ();
7152
72- $ this ->plugin = (new ObjectManager ($ this ))->getObject (
73- CategoryStoragePlugin::class,
74- [
75- 'urlFinder ' => $ this ->urlFinder ,
76- 'productResource ' => $ this ->productResourceModel
77- ]
53+ $ mergeDataProviderFactory = $ this ->createMock (MergeDataProviderFactory::class);
54+ $ mergeDataProviderFactory ->method ('create ' )
55+ ->willReturnCallback (
56+ function () {
57+ return new MergeDataProvider ();
58+ }
59+ );
60+
61+ $ this ->plugin = new CategoryStoragePlugin (
62+ $ this ->urlFinder ,
63+ $ this ->productResourceModel ,
64+ $ mergeDataProviderFactory
7865 );
7966 }
8067
8168 /**
8269 * test AfterReplace method
70+ *
71+ * @dataProvider afterReplaceDataProvider
8372 */
84- public function testAfterReplace ()
85- {
86- $ this ->urlRewrite ->expects (static ::any ())->method ('getMetadata ' )->willReturn (['category_id ' => '5 ' ]);
87- $ this ->urlRewrite ->expects (static ::once ())->method ('getEntityTYpe ' )->willReturn ('product ' );
88- $ this ->urlRewrite ->expects (static ::once ())->method ('getIsAutogenerated ' )->willReturn (1 );
89- $ this ->urlRewrite ->expects (static ::once ())->method ('getUrlRewriteId ' )->willReturn ('4 ' );
90- $ this ->urlRewrite ->expects (static ::once ())->method ('getEntityId ' )->willReturn ('2 ' );
91- $ this ->urlRewrite ->setData ('request_path ' , 'test ' );
92- $ this ->urlRewrite ->setData ('store_id ' , '1 ' );
93- $ productUrls = ['targetPath ' => $ this ->urlRewrite ];
73+ public function testAfterReplace (
74+ array $ oldUrlsData ,
75+ array $ newUrlsData ,
76+ array $ expected
77+ ) {
78+ $ serializer = new Json ();
79+ $ categoryId = 4 ;
80+ $ defaultUrlData = [
81+ UrlRewrite::URL_REWRITE_ID => null ,
82+ UrlRewrite::ENTITY_ID => null ,
83+ UrlRewrite::ENTITY_TYPE => 'product ' ,
84+ UrlRewrite::IS_AUTOGENERATED => 1 ,
85+ UrlRewrite::REQUEST_PATH => null ,
86+ UrlRewrite::TARGET_PATH => null ,
87+ UrlRewrite::STORE_ID => 1 ,
88+ UrlRewrite::REDIRECT_TYPE => 0 ,
89+ UrlRewrite::DESCRIPTION => null ,
90+ UrlRewrite::METADATA => $ serializer ->serialize (['category_id ' => $ categoryId ]),
91+ ];
92+ $ newUrlRewrites = [];
93+ $ oldUrlRewrites = [];
94+ foreach ($ oldUrlsData as $ urlData ) {
95+ $ oldUrlRewrites [] = new UrlRewrite ($ urlData + $ defaultUrlData , $ serializer );
96+ }
97+ foreach ($ newUrlsData as $ urlData ) {
98+ $ newUrlRewrites [] = new UrlRewrite ($ urlData + $ defaultUrlData , $ serializer );
99+ }
94100
95- $ this ->urlFinder ->expects (static ::once ())->method ('findAllByData ' )->willReturn ([$ this ->urlRewrite ]);
101+ if ($ expected ['findAllByData ' ]) {
102+ $ this ->urlFinder ->expects ($ this ->once ())
103+ ->method ('findAllByData ' )
104+ ->willReturn ($ oldUrlRewrites );
105+ } else {
106+ $ this ->urlFinder ->expects ($ this ->never ())
107+ ->method ('findAllByData ' );
108+ }
96109
97- $ this ->productResourceModel ->expects (static ::once ())->method ('saveMultiple ' )->willReturnSelf ();
110+ if ($ expected ['saveMultiple ' ]) {
111+ $ this ->productResourceModel ->expects ($ this ->once ())
112+ ->method ('saveMultiple ' )
113+ ->with ($ expected ['saveMultiple ' ])
114+ ->willReturnSelf ();
115+ } else {
116+ $ this ->productResourceModel ->expects ($ this ->never ())
117+ ->method ('saveMultiple ' );
118+ }
98119
99- $ this ->plugin ->afterReplace ($ this ->storage , $ productUrls , $ productUrls );
120+ $ this ->plugin ->afterReplace ($ this ->storage , $ newUrlRewrites , $ newUrlRewrites );
121+ }
122+
123+ public function afterReplaceDataProvider (): array
124+ {
125+ return [
126+ [
127+ [
128+ [
129+ UrlRewrite::URL_REWRITE_ID => 1 ,
130+ UrlRewrite::ENTITY_ID => 1 ,
131+ UrlRewrite::ENTITY_TYPE => 'category ' ,
132+ UrlRewrite::REQUEST_PATH => 'cat1/cat11.html ' ,
133+ UrlRewrite::STORE_ID => 1 ,
134+ ]
135+ ],
136+ [
137+ [
138+ UrlRewrite::ENTITY_TYPE => 'category ' ,
139+ UrlRewrite::REQUEST_PATH => 'cat1/cat11.html ' ,
140+ UrlRewrite::STORE_ID => 1 ,
141+ ]
142+ ],
143+ [
144+ 'findAllByData ' => false ,
145+ 'saveMultiple ' => false ,
146+ ]
147+ ],
148+ [
149+ [
150+ [
151+ UrlRewrite::URL_REWRITE_ID => 1 ,
152+ UrlRewrite::ENTITY_ID => 1 ,
153+ UrlRewrite::REQUEST_PATH => 'cat1/cat11/simple1.html ' ,
154+ UrlRewrite::STORE_ID => 1 ,
155+ ],
156+ [
157+ UrlRewrite::URL_REWRITE_ID => 2 ,
158+ UrlRewrite::ENTITY_ID => 1 ,
159+ UrlRewrite::REQUEST_PATH => 'cat1/cat11/simple1.html ' ,
160+ UrlRewrite::STORE_ID => 2 ,
161+ ],
162+ [
163+ UrlRewrite::URL_REWRITE_ID => 3 ,
164+ UrlRewrite::ENTITY_ID => 2 ,
165+ UrlRewrite::REQUEST_PATH => 'cat1/cat11/simple2.html ' ,
166+ UrlRewrite::STORE_ID => 2 ,
167+ ],
168+ ],
169+ [
170+ [
171+ UrlRewrite::REQUEST_PATH => 'cat1/cat11/simple1.html ' ,
172+ UrlRewrite::STORE_ID => 1 ,
173+ ],
174+ [
175+ UrlRewrite::REQUEST_PATH => 'cat1/cat11/simple2.html ' ,
176+ UrlRewrite::STORE_ID => 2 ,
177+ ],
178+ ],
179+ [
180+ 'findAllByData ' => true ,
181+ 'saveMultiple ' => [
182+ [
183+ 'url_rewrite_id ' => 1 ,
184+ 'category_id ' => 4 ,
185+ 'product_id ' => 1 ,
186+ ],
187+ [
188+ 'url_rewrite_id ' => 3 ,
189+ 'category_id ' => 4 ,
190+ 'product_id ' => 2 ,
191+ ]
192+ ],
193+ ]
194+ ]
195+ ];
100196 }
101197
102198 /**
0 commit comments