PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/magento/module-catalog-url-rewrite/Test/Unit/Model/Product/CurrentUrlRewritesRegeneratorTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 320 lines | 265 code | 28 blank | 27 comment | 0 complexity | 507b9f0a1d214494ba60d1745f231aeb MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Product;
  7. use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. use Magento\UrlRewrite\Model\OptionProvider;
  10. use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
  11. class CurrentUrlRewritesRegeneratorTest extends \PHPUnit_Framework_TestCase
  12. {
  13. /** @var \Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator */
  14. protected $currentUrlRewritesRegenerator;
  15. /** @var \PHPUnit_Framework_MockObject_MockObject */
  16. protected $filter;
  17. /** @var \Magento\UrlRewrite\Model\UrlFinderInterface|\PHPUnit_Framework_MockObject_MockObject */
  18. protected $urlFinder;
  19. /** @var \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator|\PHPUnit_Framework_MockObject_MockObject */
  20. protected $productUrlPathGenerator;
  21. /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $product;
  23. /** @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject */
  24. protected $category;
  25. /** @var \Magento\CatalogUrlRewrite\Model\ObjectRegistry|\PHPUnit_Framework_MockObject_MockObject */
  26. protected $objectRegistry;
  27. /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory|\PHPUnit_Framework_MockObject_MockObject */
  28. protected $urlRewriteFactory;
  29. /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite|\PHPUnit_Framework_MockObject_MockObject */
  30. protected $urlRewrite;
  31. protected function setUp()
  32. {
  33. $this->urlRewriteFactory = $this->getMockBuilder('Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory')
  34. ->setMethods(['create'])
  35. ->disableOriginalConstructor()->getMock();
  36. $this->urlRewrite = $this->getMockBuilder('Magento\UrlRewrite\Service\V1\Data\UrlRewrite')
  37. ->disableOriginalConstructor()->getMock();
  38. $this->product = $this->getMockBuilder('Magento\Catalog\Model\Product')
  39. ->disableOriginalConstructor()->getMock();
  40. $this->category = $this->getMockBuilder('Magento\Catalog\Model\Category')
  41. ->disableOriginalConstructor()->getMock();
  42. $this->objectRegistry = $this->getMockBuilder('\Magento\CatalogUrlRewrite\Model\ObjectRegistry')
  43. ->disableOriginalConstructor()->getMock();
  44. $this->filter = $this->getMockBuilder('Magento\UrlRewrite\Service\V1\Data\Filter')
  45. ->disableOriginalConstructor()->getMock();
  46. $this->filter->expects($this->any())->method('setStoreId')->will($this->returnSelf());
  47. $this->filter->expects($this->any())->method('setEntityId')->will($this->returnSelf());
  48. $this->urlFinder = $this->getMockBuilder('Magento\UrlRewrite\Model\UrlFinderInterface')
  49. ->disableOriginalConstructor()->getMock();
  50. $this->productUrlPathGenerator = $this->getMockBuilder(
  51. 'Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator'
  52. )->disableOriginalConstructor()->getMock();
  53. $this->currentUrlRewritesRegenerator = (new ObjectManager($this))->getObject(
  54. 'Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator',
  55. [
  56. 'urlFinder' => $this->urlFinder,
  57. 'productUrlPathGenerator' => $this->productUrlPathGenerator,
  58. 'urlRewriteFactory' => $this->urlRewriteFactory
  59. ]
  60. );
  61. }
  62. public function testIsAutogeneratedWithoutSaveRewriteHistory()
  63. {
  64. $this->urlFinder->expects($this->once())->method('findAllByData')
  65. ->will($this->returnValue($this->getCurrentRewritesMocks([[UrlRewrite::IS_AUTOGENERATED => 1]])));
  66. $this->product->expects($this->once())->method('getData')->with('save_rewrites_history')
  67. ->will($this->returnValue(false));
  68. $this->assertEquals(
  69. [],
  70. $this->currentUrlRewritesRegenerator->generate('store_id', $this->product, $this->objectRegistry)
  71. );
  72. }
  73. public function testSkipGenerationForAutogenerated()
  74. {
  75. $this->urlFinder->expects($this->once())->method('findAllByData')
  76. ->will($this->returnValue($this->getCurrentRewritesMocks([
  77. [UrlRewrite::IS_AUTOGENERATED => 1, UrlRewrite::REQUEST_PATH => 'same-path'],
  78. ])));
  79. $this->product->expects($this->once())->method('getData')->with('save_rewrites_history')
  80. ->will($this->returnValue(true));
  81. $this->productUrlPathGenerator->expects($this->once())->method('getUrlPathWithSuffix')
  82. ->will($this->returnValue('same-path'));
  83. $this->assertEquals(
  84. [],
  85. $this->currentUrlRewritesRegenerator->generate('store_id', $this->product, $this->objectRegistry)
  86. );
  87. }
  88. public function testIsAutogeneratedWithoutCategory()
  89. {
  90. $requestPath = 'autogenerated.html';
  91. $targetPath = 'some-path.html';
  92. $storeId = 2;
  93. $productId = 12;
  94. $description = 'description';
  95. $this->urlFinder->expects($this->once())->method('findAllByData')
  96. ->will($this->returnValue($this->getCurrentRewritesMocks([
  97. [
  98. UrlRewrite::REQUEST_PATH => $requestPath,
  99. UrlRewrite::TARGET_PATH => 'custom-target-path',
  100. UrlRewrite::STORE_ID => $storeId,
  101. UrlRewrite::IS_AUTOGENERATED => 1,
  102. UrlRewrite::METADATA => [],
  103. UrlRewrite::DESCRIPTION => $description,
  104. ],
  105. ])));
  106. $this->product->expects($this->any())->method('getId')->will($this->returnValue($productId));
  107. $this->product->expects($this->once())->method('getData')->with('save_rewrites_history')
  108. ->will($this->returnValue(true));
  109. $this->productUrlPathGenerator->expects($this->once())->method('getUrlPathWithSuffix')
  110. ->will($this->returnValue($targetPath));
  111. $this->prepareUrlRewriteMock(
  112. $storeId,
  113. $productId,
  114. $requestPath,
  115. $targetPath,
  116. OptionProvider::PERMANENT,
  117. [],
  118. $description
  119. );
  120. $this->assertEquals(
  121. [$this->urlRewrite],
  122. $this->currentUrlRewritesRegenerator->generate($storeId, $this->product, $this->objectRegistry)
  123. );
  124. }
  125. public function testIsAutogeneratedWithCategory()
  126. {
  127. $productId = 12;
  128. $requestPath = 'autogenerated.html';
  129. $targetPath = 'simple-product.html';
  130. $storeId = 2;
  131. $metadata = ['category_id' => 2, 'some_another_data' => 1];
  132. $description = 'description';
  133. $this->urlFinder->expects($this->once())->method('findAllByData')
  134. ->will($this->returnValue($this->getCurrentRewritesMocks([
  135. [
  136. UrlRewrite::REQUEST_PATH => $requestPath,
  137. UrlRewrite::TARGET_PATH => 'some-path.html',
  138. UrlRewrite::STORE_ID => $storeId,
  139. UrlRewrite::IS_AUTOGENERATED => 1,
  140. UrlRewrite::METADATA => $metadata,
  141. UrlRewrite::DESCRIPTION => $description,
  142. ],
  143. ])));
  144. $this->product->expects($this->any())->method('getId')->will($this->returnValue($productId));
  145. $this->product->expects($this->once())->method('getData')->with('save_rewrites_history')
  146. ->will($this->returnValue(true));
  147. $this->productUrlPathGenerator->expects($this->once())->method('getUrlPathWithSuffix')
  148. ->will($this->returnValue($targetPath));
  149. $this->objectRegistry->expects($this->once())->method('get')->will($this->returnValue($this->category));
  150. $this->prepareUrlRewriteMock(
  151. $storeId,
  152. $productId,
  153. $requestPath,
  154. $targetPath,
  155. OptionProvider::PERMANENT,
  156. $metadata,
  157. $description
  158. );
  159. $this->assertEquals(
  160. [$this->urlRewrite],
  161. $this->currentUrlRewritesRegenerator->generate($storeId, $this->product, $this->objectRegistry)
  162. );
  163. }
  164. public function testSkipGenerationForCustom()
  165. {
  166. $this->urlFinder->expects($this->once())->method('findAllByData')
  167. ->will($this->returnValue($this->getCurrentRewritesMocks([
  168. [
  169. UrlRewrite::IS_AUTOGENERATED => 0,
  170. UrlRewrite::REQUEST_PATH => 'same-path',
  171. UrlRewrite::REDIRECT_TYPE => 1,
  172. ],
  173. ])));
  174. $this->productUrlPathGenerator->expects($this->once())->method('getUrlPathWithSuffix')
  175. ->will($this->returnValue('same-path'));
  176. $this->assertEquals(
  177. [],
  178. $this->currentUrlRewritesRegenerator->generate('store_id', $this->product, $this->objectRegistry)
  179. );
  180. }
  181. public function testGenerationForCustomWithoutTargetPathGeneration()
  182. {
  183. $storeId = 12;
  184. $productId = 123;
  185. $requestPath = 'generate-for-custom-without-redirect-type.html';
  186. $targetPath = 'custom-target-path.html';
  187. $description = 'description';
  188. $this->urlFinder->expects($this->once())->method('findAllByData')
  189. ->will($this->returnValue($this->getCurrentRewritesMocks([
  190. [
  191. UrlRewrite::REQUEST_PATH => $requestPath,
  192. UrlRewrite::TARGET_PATH => $targetPath,
  193. UrlRewrite::REDIRECT_TYPE => 0,
  194. UrlRewrite::IS_AUTOGENERATED => 0,
  195. UrlRewrite::DESCRIPTION => $description,
  196. UrlRewrite::METADATA => [],
  197. ],
  198. ])));
  199. $this->productUrlPathGenerator->expects($this->never())->method('getUrlPathWithSuffix');
  200. $this->product->expects($this->any())->method('getId')->will($this->returnValue($productId));
  201. $this->prepareUrlRewriteMock($storeId, $productId, $requestPath, $targetPath, 0, [], $description);
  202. $this->assertEquals(
  203. [$this->urlRewrite],
  204. $this->currentUrlRewritesRegenerator->generate($storeId, $this->product, $this->objectRegistry)
  205. );
  206. }
  207. public function testGenerationForCustomWithTargetPathGeneration()
  208. {
  209. $storeId = 12;
  210. $productId = 123;
  211. $requestPath = 'generate-for-custom-without-redirect-type.html';
  212. $targetPath = 'generated-target-path.html';
  213. $description = 'description';
  214. $this->urlFinder->expects($this->once())->method('findAllByData')
  215. ->will($this->returnValue($this->getCurrentRewritesMocks([
  216. [
  217. UrlRewrite::REQUEST_PATH => $requestPath,
  218. UrlRewrite::TARGET_PATH => 'custom-target-path.html',
  219. UrlRewrite::REDIRECT_TYPE => 'code',
  220. UrlRewrite::IS_AUTOGENERATED => 0,
  221. UrlRewrite::DESCRIPTION => $description,
  222. UrlRewrite::METADATA => [],
  223. ],
  224. ])));
  225. $this->productUrlPathGenerator->expects($this->any())->method('getUrlPathWithSuffix')
  226. ->will($this->returnValue($targetPath));
  227. $this->product->expects($this->any())->method('getId')->will($this->returnValue($productId));
  228. $this->prepareUrlRewriteMock($storeId, $productId, $requestPath, $targetPath, 'code', [], $description);
  229. $this->assertEquals(
  230. [$this->urlRewrite],
  231. $this->currentUrlRewritesRegenerator->generate($storeId, $this->product, $this->objectRegistry)
  232. );
  233. }
  234. /**
  235. * @param array $currentRewrites
  236. * @return array
  237. */
  238. protected function getCurrentRewritesMocks($currentRewrites)
  239. {
  240. $rewrites = [];
  241. foreach ($currentRewrites as $urlRewrite) {
  242. /** @var \PHPUnit_Framework_MockObject_MockObject */
  243. $url = $this->getMockBuilder('Magento\UrlRewrite\Service\V1\Data\UrlRewrite')
  244. ->disableOriginalConstructor()->getMock();
  245. foreach ($urlRewrite as $key => $value) {
  246. $url->expects($this->any())
  247. ->method('get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))))
  248. ->will($this->returnValue($value));
  249. }
  250. $rewrites[] = $url;
  251. }
  252. return $rewrites;
  253. }
  254. /**
  255. * @param mixed $storeId
  256. * @param mixed $productId
  257. * @param mixed $requestPath
  258. * @param mixed $targetPath
  259. * @param mixed $redirectType
  260. * @param mixed $metadata
  261. * @param mixed $description
  262. */
  263. protected function prepareUrlRewriteMock(
  264. $storeId,
  265. $productId,
  266. $requestPath,
  267. $targetPath,
  268. $redirectType,
  269. $metadata,
  270. $description
  271. ) {
  272. $this->urlRewrite->expects($this->any())->method('setStoreId')->with($storeId)
  273. ->will($this->returnSelf());
  274. $this->urlRewrite->expects($this->any())->method('setEntityId')->with($productId)
  275. ->will($this->returnSelf());
  276. $this->urlRewrite->expects($this->any())->method('setEntityType')
  277. ->with(ProductUrlRewriteGenerator::ENTITY_TYPE)->will($this->returnSelf());
  278. $this->urlRewrite->expects($this->any())->method('setRequestPath')->with($requestPath)
  279. ->will($this->returnSelf());
  280. $this->urlRewrite->expects($this->any())->method('setTargetPath')->with($targetPath)
  281. ->will($this->returnSelf());
  282. $this->urlRewrite->expects($this->any())->method('setIsAutogenerated')->with(0)
  283. ->will($this->returnSelf());
  284. $this->urlRewrite->expects($this->any())->method('setRedirectType')->with($redirectType)
  285. ->will($this->returnSelf());
  286. $this->urlRewrite->expects($this->any())->method('setMetadata')->with($metadata)
  287. ->will($this->returnSelf());
  288. $this->urlRewriteFactory->expects($this->any())->method('create')->will($this->returnValue($this->urlRewrite));
  289. $this->urlRewrite->expects($this->once())->method('setDescription')->with($description)
  290. ->will($this->returnSelf());
  291. }
  292. }