Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CategoryUrlPathGeneratorTest.php
Go to the documentation of this file.
1 <?php
7 
12 
13 class CategoryUrlPathGeneratorTest extends \PHPUnit\Framework\TestCase
14 {
17 
19  protected $storeManager;
20 
22  protected $scopeConfig;
23 
26 
28  protected $category;
29 
30  protected function setUp()
31  {
32  $categoryMethods = [
33  '__wakeup',
34  'getUrlPath',
35  'getParentId',
36  'getLevel',
37  'dataHasChangedFor',
38  'getUrlKey',
39  'getStoreId',
40  'getId',
41  'formatUrlKey',
42  'getName',
43  'isObjectNew'
44  ];
45  $this->category = $this->createPartialMock(\Magento\Catalog\Model\Category::class, $categoryMethods);
46  $this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
47  $this->scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
48  $this->categoryRepository = $this->createMock(\Magento\Catalog\Api\CategoryRepositoryInterface::class);
49 
50  $this->categoryUrlPathGenerator = (new ObjectManager($this))->getObject(
51  \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator::class,
52  [
53  'storeManager' => $this->storeManager,
54  'scopeConfig' => $this->scopeConfig,
55  'categoryRepository' => $this->categoryRepository,
56  ]
57  );
58  }
59 
70  public function testGetUrlPath(
71  $parentId,
72  $urlPath,
73  $level,
74  $urlKey,
75  $dataChangedForUrlKey,
76  $dataChangedForParentId,
77  $result
78  ) {
79  $this->category->expects($this->any())->method('getParentId')->will($this->returnValue($parentId));
80  $this->category->expects($this->any())->method('isObjectNew')->will($this->returnValue(false));
81  $this->category->expects($this->any())->method('getLevel')->will($this->returnValue($level));
82  $this->category->expects($this->any())->method('getUrlPath')->will($this->returnValue($urlPath));
83  $this->category->expects($this->any())->method('getUrlKey')->will($this->returnValue($urlKey));
84  $this->category->expects($this->any())->method('dataHasChangedFor')
85  ->will($this->returnValueMap([['url_key', $dataChangedForUrlKey], ['parent_id', $dataChangedForParentId]]));
86 
87  $this->assertEquals($result, $this->categoryUrlPathGenerator->getUrlPath($this->category));
88  }
89 
93  public function getUrlPathDataProvider()
94  {
96  return [
97  [Category::TREE_ROOT_ID, 'url-path', $noGenerationLevel, '', false, false, ''],
98  [13, 'url-path', $noGenerationLevel, '', false, false, 'url-path'],
99  [13, 'url-path', $noGenerationLevel, 'url-key', true, false, 'url-key'],
100  [13, 'url-path', $noGenerationLevel, 'url-key', false, true, 'url-key'],
101  ];
102  }
103 
108  {
111  return [
112  ['url-key', false, $requireGenerationLevel, 13, 'parent-path', 'parent-path/url-key'],
113  ['url-key', false, $requireGenerationLevel, Category::TREE_ROOT_ID, null, 'url-key'],
114  ['url-key', true, $noGenerationLevel, Category::TREE_ROOT_ID, null, 'url-key'],
115  ];
116  }
117 
127  public function testGetUrlPathWithParent(
128  $urlKey,
129  $isCategoryNew,
130  $level,
131  $parentCategoryParentId,
132  $parentUrlPath,
133  $result
134  ) {
135  $urlPath = null;
137  $this->category->expects($this->any())->method('getParentId')
138  ->will($this->returnValue(13));
139  $this->category->expects($this->any())->method('getLevel')
140  ->will($this->returnValue($level));
141  $this->category->expects($this->any())->method('getUrlPath')->will($this->returnValue($urlPath));
142  $this->category->expects($this->any())->method('getUrlKey')->will($this->returnValue($urlKey));
143  $this->category->expects($this->any())->method('isObjectNew')->will($this->returnValue($isCategoryNew));
144 
145  $methods = ['__wakeup', 'getUrlPath', 'getParentId', 'getLevel', 'dataHasChangedFor', 'load'];
146  $parentCategory = $this->createPartialMock(\Magento\Catalog\Model\Category::class, $methods);
147  $parentCategory->expects($this->any())->method('getParentId')
148  ->will($this->returnValue($parentCategoryParentId));
149  $parentCategory->expects($this->any())->method('getLevel')->will($this->returnValue($parentLevel));
150  $parentCategory->expects($this->any())->method('getUrlPath')->will($this->returnValue($parentUrlPath));
151  $parentCategory->expects($this->any())->method('dataHasChangedFor')
152  ->will($this->returnValueMap([['url_key', false], ['path_ids', false]]));
153 
154  $this->categoryRepository->expects($this->any())->method('get')->with(13)
155  ->will($this->returnValue($parentCategory));
156 
157  $this->assertEquals($result, $this->categoryUrlPathGenerator->getUrlPath($this->category));
158  }
159 
164  {
165  return [
166  ['url-path', 1, null, '.html', 'url-path.html'],
167  ['url-path', null, 1, '.html', 'url-path.html'],
168  ];
169  }
170 
179  public function testGetUrlPathWithSuffixAndStore($urlPath, $storeId, $categoryStoreId, $suffix, $result)
180  {
181  $this->category->expects($this->any())->method('getStoreId')->will($this->returnValue($categoryStoreId));
182  $this->category->expects($this->once())->method('getParentId')->will($this->returnValue(123));
183  $this->category->expects($this->once())->method('getUrlPath')->will($this->returnValue($urlPath));
184  $this->category->expects($this->exactly(2))->method('dataHasChangedFor')
185  ->will($this->returnValueMap([['url_key', false], ['path_ids', false]]));
186 
187  $passedStoreId = $storeId ? $storeId : $categoryStoreId;
188  $this->scopeConfig->expects($this->once())->method('getValue')
190  ->will($this->returnValue($suffix));
191 
192  $this->assertEquals(
193  $result,
194  $this->categoryUrlPathGenerator->getUrlPathWithSuffix($this->category, $storeId)
195  );
196  }
197 
199  {
200  $urlPath = 'url-path';
201  $storeId = null;
202  $currentStoreId = 1;
203  $suffix = '.html';
204  $result = 'url-path.html';
205 
206  $this->category->expects($this->any())->method('getStoreId')->will($this->returnValue($storeId));
207  $this->category->expects($this->once())->method('getParentId')->will($this->returnValue(2));
208  $this->category->expects($this->once())->method('getUrlPath')->will($this->returnValue($urlPath));
209  $this->category->expects($this->exactly(2))->method('dataHasChangedFor')
210  ->will($this->returnValueMap([['url_key', false], ['path_ids', false]]));
211 
212  $store = $this->createMock(\Magento\Store\Model\Store::class);
213  $store->expects($this->once())->method('getId')->will($this->returnValue($currentStoreId));
214  $this->storeManager->expects($this->once())->method('getStore')->will($this->returnValue($store));
215  $this->scopeConfig->expects($this->once())->method('getValue')
217  ->will($this->returnValue($suffix));
218 
219  $this->assertEquals(
220  $result,
221  $this->categoryUrlPathGenerator->getUrlPathWithSuffix($this->category, $storeId)
222  );
223  }
224 
225  public function testGetCanonicalUrlPath()
226  {
227  $this->category->expects($this->once())->method('getId')->will($this->returnValue(1));
228  $this->assertEquals(
229  'catalog/category/view/id/1',
230  $this->categoryUrlPathGenerator->getCanonicalUrlPath($this->category)
231  );
232  }
233 
237  public function getUrlKeyDataProvider()
238  {
239  return [
240  ['url-key', null, 'url-key'],
241  ['', 'category-name', 'category-name'],
242  ];
243  }
244 
251  public function testGetUrlKey($urlKey, $name, $result)
252  {
253  $this->category->expects($this->once())->method('getUrlKey')->will($this->returnValue($urlKey));
254  $this->category->expects($this->any())->method('getName')->will($this->returnValue($name));
255  $this->category->expects($this->once())->method('formatUrlKey')->will($this->returnArgument(0));
256 
257  $this->assertEquals($result, $this->categoryUrlPathGenerator->getUrlKey($this->category));
258  }
259 }
$suffix
Definition: name.phtml:27
$methods
Definition: billing.phtml:71
testGetUrlPath( $parentId, $urlPath, $level, $urlKey, $dataChangedForUrlKey, $dataChangedForParentId, $result)
testGetUrlPathWithSuffixAndStore($urlPath, $storeId, $categoryStoreId, $suffix, $result)
testGetUrlPathWithParent( $urlKey, $isCategoryNew, $level, $parentCategoryParentId, $parentUrlPath, $result)
if(!isset($_GET['name'])) $name
Definition: log.php:14