5
5
*/
6
6
namespace Magento \CatalogUrlRewrite \Test \Unit \Model ;
7
7
8
- use \Magento \CatalogUrlRewrite \Model \ProductUrlPathGenerator ;
9
-
10
- use Magento \Store \Model \ScopeInterface ;
8
+ use Magento \CatalogUrlRewrite \Model \ProductUrlPathGenerator ;
11
9
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
10
+ use Magento \Store \Model \ScopeInterface ;
12
11
13
12
class ProductUrlPathGeneratorTest extends \PHPUnit_Framework_TestCase
14
13
{
@@ -27,13 +26,26 @@ class ProductUrlPathGeneratorTest extends \PHPUnit_Framework_TestCase
27
26
/** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */
28
27
protected $ product ;
29
28
29
+ /** @var \Magento\Catalog\Api\ProductRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
30
+ protected $ productRepository ;
31
+
30
32
/** @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject */
31
33
protected $ category ;
32
34
33
35
protected function setUp ()
34
36
{
35
37
$ this ->category = $ this ->getMock ('Magento\Catalog\Model\Category ' , [], [], '' , false );
36
- $ productMethods = ['__wakeup ' , 'getData ' , 'getUrlKey ' , 'getName ' , 'formatUrlKey ' , 'getId ' ];
38
+ $ productMethods = [
39
+ '__wakeup ' ,
40
+ 'getData ' ,
41
+ 'getUrlKey ' ,
42
+ 'getName ' ,
43
+ 'formatUrlKey ' ,
44
+ 'getId ' ,
45
+ 'load ' ,
46
+ 'setStoreId ' ,
47
+ ];
48
+
37
49
$ this ->product = $ this ->getMock ('Magento\Catalog\Model\Product ' , $ productMethods , [], '' , false );
38
50
$ this ->storeManager = $ this ->getMock ('Magento\Store\Model\StoreManagerInterface ' );
39
51
$ this ->scopeConfig = $ this ->getMock ('Magento\Framework\App\Config\ScopeConfigInterface ' );
@@ -44,13 +56,16 @@ protected function setUp()
44
56
'' ,
45
57
false
46
58
);
59
+ $ this ->productRepository = $ this ->getMock ('Magento\Catalog\Api\ProductRepositoryInterface ' );
60
+ $ this ->productRepository ->expects ($ this ->any ())->method ('getById ' )->willReturn ($ this ->product );
47
61
48
62
$ this ->productUrlPathGenerator = (new ObjectManager ($ this ))->getObject (
49
63
'Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator ' ,
50
64
[
51
65
'storeManager ' => $ this ->storeManager ,
52
66
'scopeConfig ' => $ this ->scopeConfig ,
53
- 'categoryUrlPathGenerator ' => $ this ->categoryUrlPathGenerator
67
+ 'categoryUrlPathGenerator ' => $ this ->categoryUrlPathGenerator ,
68
+ 'productRepository ' => $ this ->productRepository ,
54
69
]
55
70
);
56
71
}
@@ -64,6 +79,7 @@ public function getUrlPathDataProvider()
64
79
'path based on url key ' => ['url-key ' , null , 'url-key ' ],
65
80
'path based on product name 1 ' => ['' , 'product-name ' , 'product-name ' ],
66
81
'path based on product name 2 ' => [null , 'product-name ' , 'product-name ' ],
82
+ 'path based on product name 3 ' => [false , 'product-name ' , 'product-name ' ]
67
83
];
68
84
}
69
85
@@ -77,13 +93,34 @@ public function testGenerateUrlPath($urlKey, $productName, $result)
77
93
{
78
94
$ this ->product ->expects ($ this ->once ())->method ('getData ' )->with ('url_path ' )
79
95
->will ($ this ->returnValue (null ));
80
- $ this ->product ->expects ($ this ->once ())->method ('getUrlKey ' )->will ($ this ->returnValue ($ urlKey ));
96
+ $ this ->product ->expects ($ this ->any ())->method ('getUrlKey ' )->will ($ this ->returnValue ($ urlKey ));
81
97
$ this ->product ->expects ($ this ->any ())->method ('getName ' )->will ($ this ->returnValue ($ productName ));
82
98
$ this ->product ->expects ($ this ->once ())->method ('formatUrlKey ' )->will ($ this ->returnArgument (0 ));
83
99
84
100
$ this ->assertEquals ($ result , $ this ->productUrlPathGenerator ->getUrlPath ($ this ->product , null ));
85
101
}
86
102
103
+ /**
104
+ * @param $productUrlKey
105
+ * @param $expectedUrlKey
106
+ *
107
+ * @dataProvider generateUrlKeyDataProvider
108
+ */
109
+ public function testGenerateUrlKey ($ productUrlKey , $ expectedUrlKey )
110
+ {
111
+ $ this ->product ->expects ($ this ->any ())->method ('getUrlKey ' )->will ($ this ->returnValue ($ productUrlKey ));
112
+ $ this ->product ->expects ($ this ->any ())->method ('formatUrlKey ' )->will ($ this ->returnValue ($ productUrlKey ));
113
+ $ this ->assertEquals ($ expectedUrlKey , $ this ->productUrlPathGenerator ->generateUrlKey ($ this ->product ));
114
+ }
115
+
116
+ public function generateUrlKeyDataProvider ()
117
+ {
118
+ return [
119
+ 'URL Key use default ' => [false , false ],
120
+ 'URL Key empty ' => ['product-url ' , 'product-url ' ],
121
+ ];
122
+ }
123
+
87
124
public function testGetUrlPath ()
88
125
{
89
126
$ this ->product ->expects ($ this ->once ())->method ('getData ' )->with ('url_path ' )
@@ -93,6 +130,29 @@ public function testGetUrlPath()
93
130
$ this ->assertEquals ('url-path ' , $ this ->productUrlPathGenerator ->getUrlPath ($ this ->product , null ));
94
131
}
95
132
133
+ /**
134
+ *
135
+ * @dataProvider getUrlPathDefaultUrlKeyDataProvider
136
+ */
137
+ public function testGetUrlPathDefaultUrlKey ($ storedUrlKey , $ productName , $ expectedUrlKey )
138
+ {
139
+ $ this ->product ->expects ($ this ->once ())->method ('getData ' )->with ('url_path ' )
140
+ ->will ($ this ->returnValue (null ));
141
+ $ this ->product ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturnOnConsecutiveCalls (false , $ storedUrlKey );
142
+ $ this ->product ->expects ($ this ->any ())->method ('getName ' )->will ($ this ->returnValue ($ productName ));
143
+ $ this ->product ->expects ($ this ->any ())->method ('formatUrlKey ' )->will ($ this ->returnArgument (0 ));
144
+ $ this ->assertEquals ($ expectedUrlKey , $ this ->productUrlPathGenerator ->getUrlPath ($ this ->product , null ));
145
+ }
146
+
147
+ public function getUrlPathDefaultUrlKeyDataProvider ()
148
+ {
149
+ return [
150
+ ['default-store-view-url-key ' , null , 'default-store-view-url-key ' ],
151
+ [false , 'default-store-view-product-name ' , 'default-store-view-product-name ' ]
152
+ ];
153
+
154
+ }
155
+
96
156
public function testGetUrlPathWithCategory ()
97
157
{
98
158
$ this ->product ->expects ($ this ->once ())->method ('getData ' )->with ('url_path ' )
@@ -124,7 +184,7 @@ public function testGetUrlPathWithSuffix()
124
184
);
125
185
}
126
186
127
- public function testGetUrlPathWithSuffixAndCategoryAnsStore ()
187
+ public function testGetUrlPathWithSuffixAndCategoryAndStore ()
128
188
{
129
189
$ storeId = 1 ;
130
190
$ this ->product ->expects ($ this ->once ())->method ('getData ' )->with ('url_path ' )
0 commit comments