Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductExternalTest.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Catalog\Model;
7 
19 class ProductExternalTest extends \PHPUnit\Framework\TestCase
20 {
24  protected $objectManager;
25 
29  protected $productRepository;
30 
34  protected $_model;
35 
36  protected function setUp()
37  {
39 
40  $this->productRepository = $this->objectManager->create(
41  \Magento\Catalog\Api\ProductRepositoryInterface::class
42  );
43 
44  $this->_model = $this->objectManager->create(
45  \Magento\Catalog\Model\Product::class
46  );
47  }
48 
49  public function testGetStoreId()
50  {
51  $this->assertEquals(
52  $this->objectManager->get(
53  \Magento\Store\Model\StoreManagerInterface::class
54  )->getStore()->getId(),
55  $this->_model->getStoreId()
56  );
57  $this->_model->setData('store_id', 999);
58  $this->assertEquals(999, $this->_model->getStoreId());
59  }
60 
61  public function testGetLinkInstance()
62  {
63  $model = $this->_model->getLinkInstance();
64  $this->assertInstanceOf(\Magento\Catalog\Model\Product\Link::class, $model);
65  $this->assertSame($model, $this->_model->getLinkInstance());
66  }
67 
68  public function testGetCategoryId()
69  {
70  $this->assertFalse($this->_model->getCategoryId());
71  $category = new \Magento\Framework\DataObject(['id' => 5]);
72 
73  $this->objectManager->get(\Magento\Framework\Registry::class)->register('current_category', $category);
74  try {
75  $this->assertEquals(5, $this->_model->getCategoryId());
76  $this->objectManager->get(\Magento\Framework\Registry::class)->unregister('current_category');
77  } catch (\Exception $e) {
78  $this->objectManager->get(\Magento\Framework\Registry::class)->unregister('current_category');
79  throw $e;
80  }
81  }
82 
83  public function testGetCategory()
84  {
85  $this->assertEmpty($this->_model->getCategory());
86 
87  $this->objectManager->get(\Magento\Framework\Registry::class)
88  ->register('current_category', new \Magento\Framework\DataObject(['id' => 3]));
89  // fixture
90  try {
91  $category = $this->_model->getCategory();
92  $this->assertInstanceOf(\Magento\Catalog\Model\Category::class, $category);
93  $this->assertEquals(3, $category->getId());
94  $this->objectManager->get(\Magento\Framework\Registry::class)->unregister('current_category');
95  } catch (\Exception $e) {
96  $this->objectManager->get(\Magento\Framework\Registry::class)->unregister('current_category');
97  throw $e;
98  }
99 
100  $categoryTwo = new \StdClass();
101  $this->_model->setCategory($categoryTwo);
102  $this->assertSame($categoryTwo, $this->_model->getCategory());
103  }
104 
105  public function testGetCategoryIds()
106  {
107  // none
109  $model = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
110  $this->assertEquals([], $model->getCategoryIds());
111 
112  // fixture
113  $this->_model->setId(
114  $this->productRepository->get('simple')->getId()
115  );
116  $this->assertEquals([2, 3, 4, 13], $this->_model->getCategoryIds());
117  }
118 
119  public function testGetCategoryCollection()
120  {
121  // empty
122  $collection = $this->_model->getCategoryCollection();
123  $this->assertInstanceOf(\Magento\Catalog\Model\ResourceModel\Category\Collection::class, $collection);
124 
125  // fixture
126  $this->_model->setId(
127  $this->productRepository->get('simple')->getId()
128  );
129  $fixtureCollection = $this->_model->getCategoryCollection();
130  $this->assertInstanceOf(\Magento\Catalog\Model\ResourceModel\Category\Collection::class, $fixtureCollection);
131  $this->assertNotSame($fixtureCollection, $collection);
132  $ids = [];
133  foreach ($fixtureCollection as $category) {
134  $ids[] = $category->getId();
135  }
136  $this->assertEquals([2, 3, 4, 13], $ids);
137  }
138 
139  public function testGetWebsiteIds()
140  {
141  // set
143  $model = $this->objectManager->create(
144  \Magento\Catalog\Model\Product::class,
145  ['data' => ['website_ids' => [1, 2]]]
146  );
147  $this->assertEquals([1, 2], $model->getWebsiteIds());
148 
149  // fixture
150  $this->_model->setId(
151  $this->productRepository->get('simple')->getId()
152  );
153  $this->assertEquals([1], $this->_model->getWebsiteIds());
154  }
155 
156  public function testGetStoreIds()
157  {
158  // set
160  $model = $this->objectManager->create(
161  \Magento\Catalog\Model\Product::class,
162  ['data' => ['store_ids' => [1, 2]]]
163  );
164  $this->assertEquals([1, 2], $model->getStoreIds());
165 
166  // fixture
167  $this->_model->setId(
168  $this->productRepository->get('simple')->getId()
169  );
170  $this->assertEquals([1], $this->_model->getStoreIds());
171  }
172 
179  public function testRelatedProductsApi()
180  {
181  $this->assertEquals([], $this->_model->getRelatedProducts());
182  $this->assertEquals([], $this->_model->getRelatedProductIds());
183 
184  $collection = $this->_model->getRelatedProductCollection();
185  $this->assertInstanceOf(\Magento\Catalog\Model\ResourceModel\Product\Collection::class, $collection);
186  $this->assertSame($this->_model, $collection->getProduct());
187 
188  $linkCollection = $this->_model->getRelatedLinkCollection();
189  $this->assertInstanceOf(\Magento\Catalog\Model\ResourceModel\Product\Link\Collection::class, $linkCollection);
190  $this->assertSame($this->_model, $linkCollection->getProduct());
191  }
192 
199  public function testUpSellProductsApi()
200  {
201  $this->assertEquals([], $this->_model->getUpSellProducts());
202  $this->assertEquals([], $this->_model->getUpSellProductIds());
203 
204  $collection = $this->_model->getUpSellProductCollection();
205  $this->assertInstanceOf(\Magento\Catalog\Model\ResourceModel\Product\Collection::class, $collection);
206  $this->assertSame($this->_model, $collection->getProduct());
207 
208  $linkCollection = $this->_model->getUpSellLinkCollection();
209  $this->assertInstanceOf(\Magento\Catalog\Model\ResourceModel\Product\Link\Collection::class, $linkCollection);
210  $this->assertSame($this->_model, $linkCollection->getProduct());
211  }
212 
219  public function testCrossSellProductsApi()
220  {
221  $this->assertEquals([], $this->_model->getCrossSellProducts());
222  $this->assertEquals([], $this->_model->getCrossSellProductIds());
223 
224  $collection = $this->_model->getCrossSellProductCollection();
225  $this->assertInstanceOf(\Magento\Catalog\Model\ResourceModel\Product\Collection::class, $collection);
226  $this->assertSame($this->_model, $collection->getProduct());
227 
228  $linkCollection = $this->_model->getCrossSellLinkCollection();
229  $this->assertInstanceOf(\Magento\Catalog\Model\ResourceModel\Product\Link\Collection::class, $linkCollection);
230  $this->assertSame($this->_model, $linkCollection->getProduct());
231  }
232 
237  public function testGetProductUrl()
238  {
239  $this->assertStringEndsWith('catalog/product/view/', $this->_model->getProductUrl());
240  $this->assertStringEndsWith('catalog/product/view/', $this->_model->getUrlInStore());
241  $this->_model->setId(999);
242  $url = $this->_model->getProductUrl();
243  $this->assertContains('catalog/product/view', $url);
244  $this->assertContains('id/999', $url);
245  $storeUrl = $this->_model->getUrlInStore();
246  $this->assertEquals($storeUrl, $url);
247  }
248 
252  public function testFormatUrlKey()
253  {
254  $this->assertEquals('test', $this->_model->formatUrlKey('test'));
255  }
256 
257  public function testGetUrlPath()
258  {
259  $this->_model->setUrlPath('test');
260  $this->assertEquals('test', $this->_model->getUrlPath());
261 
262  $urlPathGenerator = $this->objectManager->create(
263  \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::class
264  );
265 
267  $category = $this->objectManager->create(
268  \Magento\Catalog\Model\Category::class,
269  ['data' => ['url_path' => 'category', 'entity_id' => 5, 'path_ids' => [2, 3, 5]]]
270  );
271  $category->setOrigData();
272  $this->assertEquals('category/test', $urlPathGenerator->getUrlPath($this->_model, $category));
273  }
274 
280  public function testOptionApi()
281  {
282  $this->assertNull($this->_model->getOptions());
283 
284  $optionId = uniqid();
285  $option = $this->objectManager->create(
286  \Magento\Catalog\Model\Product\Option::class,
287  ['data' => ['key' => 'value']]
288  );
289  $option->setId($optionId);
290  $this->_model->setOptions([$option]);
291 
292  $this->assertSame($option, $this->_model->getOptionById($optionId));
293  $this->assertEquals([$option], $this->_model->getOptions());
294  }
295 
303  public function testCustomOptionsApi()
304  {
305  $this->assertEquals([], $this->_model->getCustomOptions());
306  $this->assertFalse($this->_model->hasCustomOptions());
307 
308  $this->_model->setId(99);
309  $this->_model->addCustomOption('one', 'value1');
310  $option = $this->_model->getCustomOption('one');
311  $this->assertInstanceOf(\Magento\Framework\DataObject::class, $option);
312  $this->assertEquals($this->_model->getId(), $option->getProductId());
313  $this->assertSame($option->getProduct(), $this->_model);
314  $this->assertEquals('one', $option->getCode());
315  $this->assertEquals('value1', $option->getValue());
316 
317  $this->assertEquals(['one' => $option], $this->_model->getCustomOptions());
318  $this->assertTrue($this->_model->hasCustomOptions());
319 
320  $this->_model->setCustomOptions(['test']);
321  $this->assertTrue(is_array($this->_model->getCustomOptions()));
322  }
323 
324  public function testCanBeShowInCategory()
325  {
326  $this->_model->load(
327  $this->productRepository->get('simple')->getId()
328  );
329 
330  // fixture
331  $this->assertFalse((bool)$this->_model->canBeShowInCategory(6));
332  $this->assertTrue((bool)$this->_model->canBeShowInCategory(3));
333  }
334 
336  {
337  $this->assertEquals([], $this->_model->getAvailableInCategories());
338 
339  $this->_model->load(
340  $this->productRepository->get('simple-4')->getId()
341  );
342  // fixture
343  $actualCategoryIds = $this->_model->getAvailableInCategories();
344  sort($actualCategoryIds);
345  // not depend on the order of items
346  $this->assertEquals([10, 11, 12, 13], $actualCategoryIds);
347  //Check not visible product
348  $this->_model->load(
349  $this->productRepository->get('simple-3')->getId()
350  );
351  $this->assertEmpty($this->_model->getAvailableInCategories());
352  }
353 }