8
8
namespace Magento \CatalogUrlRewrite \Test \Unit \Model \Category \Plugin ;
9
9
10
10
use Magento \CatalogUrlRewrite \Model \Category \Plugin \Storage as CategoryStoragePlugin ;
11
- use Magento \CatalogUrlRewrite \Model \Category \Product ;
12
11
use 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 ;
14
15
use Magento \UrlRewrite \Model \StorageInterface ;
15
16
use Magento \UrlRewrite \Model \UrlFinderInterface ;
16
17
use Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ;
17
18
use PHPUnit \Framework \MockObject \MockObject ;
18
19
use PHPUnit \Framework \TestCase ;
19
20
20
- /**
21
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
22
- */
23
21
class StorageTest extends TestCase
24
22
{
25
23
/**
@@ -37,66 +35,164 @@ class StorageTest extends TestCase
37
35
*/
38
36
private $ storage ;
39
37
40
- /**
41
- * @var Product|MockObject
42
- */
43
- private $ product ;
44
-
45
38
/**
46
39
* @var ProductResourceModel|MockObject
47
40
*/
48
41
private $ productResourceModel ;
49
42
50
- /**
51
- * @var UrlRewrite|MockObject
52
- */
53
- private $ urlRewrite ;
54
-
55
43
protected function setUp (): void
56
44
{
57
45
$ this ->storage = $ this ->getMockBuilder (StorageInterface::class)
58
46
->getMockForAbstractClass ();
59
47
$ this ->urlFinder = $ this ->getMockBuilder (UrlFinderInterface::class)
60
48
->getMockForAbstractClass ();
61
- $ this ->product = $ this ->getMockBuilder (Product::class)
62
- ->disableOriginalConstructor ()
63
- ->getMock ();
64
49
$ this ->productResourceModel = $ this ->getMockBuilder (ProductResourceModel::class)
65
50
->disableOriginalConstructor ()
66
51
->getMock ();
67
- $ this ->urlRewrite = $ this ->getMockBuilder (UrlRewrite::class)
68
- ->disableOriginalConstructor ()
69
- ->setMethods (['getMetadata ' , 'getEntityType ' , 'getIsAutogenerated ' , 'getUrlRewriteId ' , 'getEntityId ' ])
70
- ->getMock ();
71
52
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
78
65
);
79
66
}
80
67
81
68
/**
82
69
* test AfterReplace method
70
+ *
71
+ * @dataProvider afterReplaceDataProvider
83
72
*/
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
+ }
94
100
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
+ }
96
109
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
+ }
98
119
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
+ ];
100
196
}
101
197
102
198
/**
0 commit comments