Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductUrlPathGeneratorTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
13 
14 class ProductUrlPathGeneratorTest extends \PHPUnit\Framework\TestCase
15 {
18 
20  protected $storeManager;
21 
23  protected $scopeConfig;
24 
27 
29  protected $product;
30 
32  protected $productRepository;
33 
35  protected $category;
36 
40  protected function setUp(): void
41  {
42  $this->category = $this->createMock(\Magento\Catalog\Model\Category::class);
43  $productMethods = [
44  '__wakeup',
45  'getData',
46  'getUrlKey',
47  'getName',
48  'formatUrlKey',
49  'getId',
50  'load',
51  'setStoreId',
52  ];
53 
54  $this->product = $this->createPartialMock(\Magento\Catalog\Model\Product::class, $productMethods);
55  $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
56  $this->scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
57  $this->categoryUrlPathGenerator = $this->createMock(
58  \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator::class
59  );
60  $this->productRepository = $this->createMock(\Magento\Catalog\Api\ProductRepositoryInterface::class);
61  $this->productRepository->expects($this->any())->method('getById')->willReturn($this->product);
62 
63  $this->productUrlPathGenerator = (new ObjectManager($this))->getObject(
64  \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::class,
65  [
66  'storeManager' => $this->storeManager,
67  'scopeConfig' => $this->scopeConfig,
68  'categoryUrlPathGenerator' => $this->categoryUrlPathGenerator,
69  'productRepository' => $this->productRepository,
70  ]
71  );
72  }
73 
77  public function getUrlPathDataProvider(): array
78  {
79  return [
80  'path based on url key' => ['url-key', null, 'url-key'],
81  'path based on product name 1' => ['', 'product-name', 'product-name'],
82  'path based on product name 2' => [null, 'product-name', 'product-name'],
83  'path based on product name 3' => [false, 'product-name', 'product-name']
84  ];
85  }
86 
94  public function testGetUrlPath($urlKey, $productName, $result): void
95  {
96  $this->product->expects($this->once())->method('getData')->with('url_path')
97  ->will($this->returnValue(null));
98  $this->product->expects($this->any())->method('getUrlKey')->will($this->returnValue($urlKey));
99  $this->product->expects($this->any())->method('getName')->will($this->returnValue($productName));
100  $this->product->expects($this->once())->method('formatUrlKey')->will($this->returnArgument(0));
101 
102  $this->assertEquals($result, $this->productUrlPathGenerator->getUrlPath($this->product, null));
103  }
104 
111  public function testGetUrlKey($productUrlKey, $expectedUrlKey): void
112  {
113  $this->product->expects($this->any())->method('getUrlKey')->will($this->returnValue($productUrlKey));
114  $this->product->expects($this->any())->method('formatUrlKey')->will($this->returnValue($productUrlKey));
115  $this->assertSame($expectedUrlKey, $this->productUrlPathGenerator->getUrlKey($this->product));
116  }
117 
121  public function getUrlKeyDataProvider(): array
122  {
123  return [
124  'URL Key use default' => [false, null],
125  'URL Key empty' => ['product-url', 'product-url'],
126  ];
127  }
128 
136  public function testGetUrlPathDefaultUrlKey($storedUrlKey, $productName, $expectedUrlKey): void
137  {
138  $this->product->expects($this->once())->method('getData')->with('url_path')
139  ->will($this->returnValue(null));
140  $this->product->expects($this->any())->method('getUrlKey')->willReturnOnConsecutiveCalls(false, $storedUrlKey);
141  $this->product->expects($this->any())->method('getName')->will($this->returnValue($productName));
142  $this->product->expects($this->any())->method('formatUrlKey')->will($this->returnArgument(0));
143  $this->assertEquals($expectedUrlKey, $this->productUrlPathGenerator->getUrlPath($this->product, null));
144  }
145 
149  public function getUrlPathDefaultUrlKeyDataProvider(): array
150  {
151  return [
152  ['default-store-view-url-key', null, 'default-store-view-url-key'],
153  [false, 'default-store-view-product-name', 'default-store-view-product-name']
154  ];
155  }
156 
160  public function testGetUrlPathWithCategory(): void
161  {
162  $this->product->expects($this->once())->method('getData')->with('url_path')
163  ->will($this->returnValue('product-path'));
164  $this->categoryUrlPathGenerator->expects($this->once())->method('getUrlPath')
165  ->will($this->returnValue('category-url-path'));
166 
167  $this->assertEquals(
168  'category-url-path/product-path',
169  $this->productUrlPathGenerator->getUrlPath($this->product, $this->category)
170  );
171  }
172 
176  public function testGetUrlPathWithSuffix(): void
177  {
178  $storeId = 1;
179  $this->product->expects($this->once())->method('getData')->with('url_path')
180  ->will($this->returnValue('product-path'));
181  $store = $this->createMock(\Magento\Store\Model\Store::class);
182  $store->expects($this->once())->method('getId')->will($this->returnValue($storeId));
183  $this->storeManager->expects($this->once())->method('getStore')->will($this->returnValue($store));
184  $this->scopeConfig->expects($this->once())->method('getValue')
186  ->will($this->returnValue('.html'));
187 
188  $this->assertEquals(
189  'product-path.html',
190  $this->productUrlPathGenerator->getUrlPathWithSuffix($this->product, null)
191  );
192  }
193 
198  {
199  $storeId = 1;
200  $this->product->expects($this->once())->method('getData')->with('url_path')
201  ->will($this->returnValue('product-path'));
202  $this->categoryUrlPathGenerator->expects($this->once())->method('getUrlPath')
203  ->will($this->returnValue('category-url-path'));
204  $this->storeManager->expects($this->never())->method('getStore');
205  $this->scopeConfig->expects($this->once())->method('getValue')
207  ->will($this->returnValue('.html'));
208 
209  $this->assertEquals(
210  'category-url-path/product-path.html',
211  $this->productUrlPathGenerator->getUrlPathWithSuffix($this->product, $storeId, $this->category)
212  );
213  }
214 
218  public function testGetCanonicalUrlPath(): void
219  {
220  $this->product->expects($this->once())->method('getId')->will($this->returnValue(1));
221 
222  $this->assertEquals(
223  'catalog/product/view/id/1',
224  $this->productUrlPathGenerator->getCanonicalUrlPath($this->product)
225  );
226  }
227 
231  public function testGetCanonicalUrlPathWithCategory(): void
232  {
233  $this->product->expects($this->once())->method('getId')->will($this->returnValue(1));
234  $this->category->expects($this->once())->method('getId')->will($this->returnValue(1));
235 
236  $this->assertEquals(
237  'catalog/product/view/id/1/category/1',
238  $this->productUrlPathGenerator->getCanonicalUrlPath($this->product, $this->category)
239  );
240  }
241 }