PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/integration/testsuite/Magento/Catalog/Model/ProductExternalTest.php

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