PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/magento2-base/dev/tests/integration/testsuite/Magento/Catalog/Model/ProductExternalTest.php

https://gitlab.com/daigiangaitu91/magento
PHP | 321 lines | 211 code | 39 blank | 71 comment | 0 complexity | 8cffca76508212671888270a005e2c16 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2015 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model;
  7. /**
  8. * Tests product model:
  9. * - external interaction is tested
  10. *
  11. * @see \Magento\Catalog\Model\ProductTest
  12. * @see \Magento\Catalog\Model\ProductPriceTest
  13. * @magentoDataFixture Magento/Catalog/_files/categories.php
  14. */
  15. class ProductExternalTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @var \Magento\Catalog\Model\Product
  19. */
  20. protected $_model;
  21. protected function setUp()
  22. {
  23. $this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  24. 'Magento\Catalog\Model\Product'
  25. );
  26. }
  27. public function testGetStoreId()
  28. {
  29. $this->assertEquals(
  30. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  31. 'Magento\Store\Model\StoreManagerInterface'
  32. )->getStore()->getId(),
  33. $this->_model->getStoreId()
  34. );
  35. $this->_model->setData('store_id', 999);
  36. $this->assertEquals(999, $this->_model->getStoreId());
  37. }
  38. public function testGetLinkInstance()
  39. {
  40. $model = $this->_model->getLinkInstance();
  41. $this->assertInstanceOf('Magento\Catalog\Model\Product\Link', $model);
  42. $this->assertSame($model, $this->_model->getLinkInstance());
  43. }
  44. public function testGetCategoryId()
  45. {
  46. $this->assertFalse($this->_model->getCategoryId());
  47. $category = new \Magento\Framework\DataObject(['id' => 5]);
  48. /** @var $objectManager \Magento\TestFramework\ObjectManager */
  49. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  50. $objectManager->get('Magento\Framework\Registry')->register('current_category', $category);
  51. try {
  52. $this->assertEquals(5, $this->_model->getCategoryId());
  53. $objectManager->get('Magento\Framework\Registry')->unregister('current_category');
  54. } catch (\Exception $e) {
  55. $objectManager->get('Magento\Framework\Registry')->unregister('current_category');
  56. throw $e;
  57. }
  58. }
  59. public function testGetCategory()
  60. {
  61. $this->assertEmpty($this->_model->getCategory());
  62. /** @var $objectManager \Magento\TestFramework\ObjectManager */
  63. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  64. $objectManager->get('Magento\Framework\Registry')
  65. ->register('current_category', new \Magento\Framework\DataObject(['id' => 3]));
  66. // fixture
  67. try {
  68. $category = $this->_model->getCategory();
  69. $this->assertInstanceOf('Magento\Catalog\Model\Category', $category);
  70. $this->assertEquals(3, $category->getId());
  71. $objectManager->get('Magento\Framework\Registry')->unregister('current_category');
  72. } catch (\Exception $e) {
  73. $objectManager->get('Magento\Framework\Registry')->unregister('current_category');
  74. throw $e;
  75. }
  76. $categoryTwo = new \StdClass();
  77. $this->_model->setCategory($categoryTwo);
  78. $this->assertSame($categoryTwo, $this->_model->getCategory());
  79. }
  80. public function testGetCategoryIds()
  81. {
  82. // none
  83. /** @var $model \Magento\Catalog\Model\Product */
  84. $model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\Catalog\Model\Product');
  85. $this->assertEquals([], $model->getCategoryIds());
  86. // fixture
  87. $this->_model->setId(1);
  88. $this->assertEquals([2, 3, 4], $this->_model->getCategoryIds());
  89. }
  90. public function testGetCategoryCollection()
  91. {
  92. // empty
  93. $collection = $this->_model->getCategoryCollection();
  94. $this->assertInstanceOf('Magento\Catalog\Model\ResourceModel\Category\Collection', $collection);
  95. // fixture
  96. $this->_model->setId(1);
  97. $fixtureCollection = $this->_model->getCategoryCollection();
  98. $this->assertInstanceOf('Magento\Catalog\Model\ResourceModel\Category\Collection', $fixtureCollection);
  99. $this->assertNotSame($fixtureCollection, $collection);
  100. $ids = [];
  101. foreach ($fixtureCollection as $category) {
  102. $ids[] = $category->getId();
  103. }
  104. $this->assertEquals([2, 3, 4], $ids);
  105. }
  106. public function testGetWebsiteIds()
  107. {
  108. // set
  109. /** @var $model \Magento\Catalog\Model\Product */
  110. $model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  111. 'Magento\Catalog\Model\Product',
  112. ['data' => ['website_ids' => [1, 2]]]
  113. );
  114. $this->assertEquals([1, 2], $model->getWebsiteIds());
  115. // fixture
  116. $this->_model->setId(1);
  117. $this->assertEquals([1], $this->_model->getWebsiteIds());
  118. }
  119. public function testGetStoreIds()
  120. {
  121. // set
  122. /** @var $model \Magento\Catalog\Model\Product */
  123. $model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  124. 'Magento\Catalog\Model\Product',
  125. ['data' => ['store_ids' => [1, 2]]]
  126. );
  127. $this->assertEquals([1, 2], $model->getStoreIds());
  128. // fixture
  129. $this->_model->setId(1);
  130. $this->assertEquals([1], $this->_model->getStoreIds());
  131. }
  132. /**
  133. * @covers \Magento\Catalog\Model\Product::getRelatedProducts
  134. * @covers \Magento\Catalog\Model\Product::getRelatedProductIds
  135. * @covers \Magento\Catalog\Model\Product::getRelatedProductCollection
  136. * @covers \Magento\Catalog\Model\Product::getRelatedLinkCollection
  137. */
  138. public function testRelatedProductsApi()
  139. {
  140. $this->assertEquals([], $this->_model->getRelatedProducts());
  141. $this->assertEquals([], $this->_model->getRelatedProductIds());
  142. $collection = $this->_model->getRelatedProductCollection();
  143. $this->assertInstanceOf('Magento\Catalog\Model\ResourceModel\Product\Collection', $collection);
  144. $this->assertSame($this->_model, $collection->getProduct());
  145. $linkCollection = $this->_model->getRelatedLinkCollection();
  146. $this->assertInstanceOf('Magento\Catalog\Model\ResourceModel\Product\Link\Collection', $linkCollection);
  147. $this->assertSame($this->_model, $linkCollection->getProduct());
  148. }
  149. /**
  150. * @covers \Magento\Catalog\Model\Product::getUpSellProducts
  151. * @covers \Magento\Catalog\Model\Product::getUpSellProductIds
  152. * @covers \Magento\Catalog\Model\Product::getUpSellProductCollection
  153. * @covers \Magento\Catalog\Model\Product::getUpSellLinkCollection
  154. */
  155. public function testUpSellProductsApi()
  156. {
  157. $this->assertEquals([], $this->_model->getUpSellProducts());
  158. $this->assertEquals([], $this->_model->getUpSellProductIds());
  159. $collection = $this->_model->getUpSellProductCollection();
  160. $this->assertInstanceOf('Magento\Catalog\Model\ResourceModel\Product\Collection', $collection);
  161. $this->assertSame($this->_model, $collection->getProduct());
  162. $linkCollection = $this->_model->getUpSellLinkCollection();
  163. $this->assertInstanceOf('Magento\Catalog\Model\ResourceModel\Product\Link\Collection', $linkCollection);
  164. $this->assertSame($this->_model, $linkCollection->getProduct());
  165. }
  166. /**
  167. * @covers \Magento\Catalog\Model\Product::getCrossSellProducts
  168. * @covers \Magento\Catalog\Model\Product::getCrossSellProductIds
  169. * @covers \Magento\Catalog\Model\Product::getCrossSellProductCollection
  170. * @covers \Magento\Catalog\Model\Product::getCrossSellLinkCollection
  171. */
  172. public function testCrossSellProductsApi()
  173. {
  174. $this->assertEquals([], $this->_model->getCrossSellProducts());
  175. $this->assertEquals([], $this->_model->getCrossSellProductIds());
  176. $collection = $this->_model->getCrossSellProductCollection();
  177. $this->assertInstanceOf('Magento\Catalog\Model\ResourceModel\Product\Collection', $collection);
  178. $this->assertSame($this->_model, $collection->getProduct());
  179. $linkCollection = $this->_model->getCrossSellLinkCollection();
  180. $this->assertInstanceOf('Magento\Catalog\Model\ResourceModel\Product\Link\Collection', $linkCollection);
  181. $this->assertSame($this->_model, $linkCollection->getProduct());
  182. }
  183. /**
  184. * @covers \Magento\Catalog\Model\Product::getProductUrl
  185. * @covers \Magento\Catalog\Model\Product::getUrlInStore
  186. */
  187. public function testGetProductUrl()
  188. {
  189. $this->assertStringEndsWith('catalog/product/view/', $this->_model->getProductUrl());
  190. $this->assertStringEndsWith('catalog/product/view/', $this->_model->getUrlInStore());
  191. $this->_model->setId(999);
  192. $url = $this->_model->getProductUrl();
  193. $this->assertContains('catalog/product/view', $url);
  194. $this->assertContains('id/999', $url);
  195. $storeUrl = $this->_model->getUrlInStore();
  196. $this->assertEquals($storeUrl, $url);
  197. }
  198. /**
  199. * @see \Magento\Catalog\Model\Product\UrlTest
  200. */
  201. public function testFormatUrlKey()
  202. {
  203. $this->assertEquals('test', $this->_model->formatUrlKey('test'));
  204. }
  205. public function testGetUrlPath()
  206. {
  207. $this->_model->setUrlPath('test');
  208. $this->assertEquals('test', $this->_model->getUrlPath());
  209. $urlPathGenerator = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  210. 'Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator'
  211. );
  212. /** @var $category \Magento\Catalog\Model\Category */
  213. $category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  214. 'Magento\Catalog\Model\Category',
  215. ['data' => ['url_path' => 'category', 'entity_id' => 5, 'path_ids' => [2, 3, 5]]]
  216. );
  217. $category->setOrigData();
  218. $this->assertEquals('category/test', $urlPathGenerator->getUrlPath($this->_model, $category));
  219. }
  220. /**
  221. * @covers \Magento\Catalog\Model\Product::addOption
  222. * @covers \Magento\Catalog\Model\Product::getOptionById
  223. * @covers \Magento\Catalog\Model\Product::getOptions
  224. */
  225. public function testOptionApi()
  226. {
  227. $this->assertEquals([], $this->_model->getOptions());
  228. $optionId = uniqid();
  229. $option = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  230. 'Magento\Catalog\Model\Product\Option',
  231. ['data' => ['key' => 'value']]
  232. );
  233. $option->setId($optionId);
  234. $this->_model->addOption($option);
  235. $this->assertSame($option, $this->_model->getOptionById($optionId));
  236. $this->assertEquals([$optionId => $option], $this->_model->getOptions());
  237. }
  238. /**
  239. * @covers \Magento\Catalog\Model\Product::addCustomOption
  240. * @covers \Magento\Catalog\Model\Product::setCustomOptions
  241. * @covers \Magento\Catalog\Model\Product::getCustomOptions
  242. * @covers \Magento\Catalog\Model\Product::getCustomOption
  243. * @covers \Magento\Catalog\Model\Product::hasCustomOptions
  244. */
  245. public function testCustomOptionsApi()
  246. {
  247. $this->assertEquals([], $this->_model->getCustomOptions());
  248. $this->assertFalse($this->_model->hasCustomOptions());
  249. $this->_model->setId(99);
  250. $this->_model->addCustomOption('one', 'value1');
  251. $option = $this->_model->getCustomOption('one');
  252. $this->assertInstanceOf('Magento\Framework\DataObject', $option);
  253. $this->assertEquals($this->_model->getId(), $option->getProductId());
  254. $this->assertSame($option->getProduct(), $this->_model);
  255. $this->assertEquals('one', $option->getCode());
  256. $this->assertEquals('value1', $option->getValue());
  257. $this->assertEquals(['one' => $option], $this->_model->getCustomOptions());
  258. $this->assertTrue($this->_model->hasCustomOptions());
  259. $this->_model->setCustomOptions(['test']);
  260. $this->assertTrue(is_array($this->_model->getCustomOptions()));
  261. }
  262. public function testCanBeShowInCategory()
  263. {
  264. $this->_model->load(1);
  265. // fixture
  266. $this->assertFalse((bool)$this->_model->canBeShowInCategory(6));
  267. $this->assertTrue((bool)$this->_model->canBeShowInCategory(3));
  268. }
  269. public function testGetAvailableInCategories()
  270. {
  271. $this->assertEquals([], $this->_model->getAvailableInCategories());
  272. $this->_model->load(4);
  273. // fixture
  274. $actualCategoryIds = $this->_model->getAvailableInCategories();
  275. sort($actualCategoryIds);
  276. // not depend on the order of items
  277. $this->assertEquals([2, 10, 11, 12], $actualCategoryIds);
  278. //Check not visible product
  279. $this->_model->load(3);
  280. $this->assertEmpty($this->_model->getAvailableInCategories());
  281. }
  282. }