40 $this->productRepository = $this->objectManager->create(
41 \
Magento\Catalog\Api\ProductRepositoryInterface::class
44 $this->_model = $this->objectManager->create(
45 \
Magento\Catalog\Model\Product::class
52 $this->objectManager->get(
53 \
Magento\Store\Model\StoreManagerInterface::class
54 )->getStore()->getId(),
55 $this->_model->getStoreId()
57 $this->_model->setData(
'store_id', 999);
58 $this->assertEquals(999, $this->_model->getStoreId());
63 $model = $this->_model->getLinkInstance();
65 $this->assertSame(
$model, $this->_model->getLinkInstance());
70 $this->assertFalse($this->_model->getCategoryId());
71 $category = new \Magento\Framework\DataObject([
'id' => 5]);
73 $this->objectManager->get(\
Magento\Framework\Registry::class)->register(
'current_category',
$category);
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');
85 $this->assertEmpty($this->_model->getCategory());
87 $this->objectManager->get(\
Magento\Framework\Registry::class)
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');
100 $categoryTwo = new \StdClass();
101 $this->_model->setCategory($categoryTwo);
102 $this->assertSame($categoryTwo, $this->_model->getCategory());
105 public function testGetCategoryIds()
109 $model = $this->objectManager->create(\
Magento\Catalog\Model\Product::class);
110 $this->assertEquals([],
$model->getCategoryIds());
113 $this->_model->setId(
114 $this->productRepository->get(
'simple')->getId()
116 $this->assertEquals([2, 3, 4, 13], $this->_model->getCategoryIds());
122 $collection = $this->_model->getCategoryCollection();
126 $this->_model->setId(
127 $this->productRepository->get(
'simple')->getId()
129 $fixtureCollection = $this->_model->getCategoryCollection();
131 $this->assertNotSame($fixtureCollection,
$collection);
133 foreach ($fixtureCollection as
$category) {
136 $this->assertEquals([2, 3, 4, 13], $ids);
139 public function testGetWebsiteIds()
143 $model = $this->objectManager->create(
144 \
Magento\Catalog\Model\Product::class,
145 [
'data' => [
'website_ids' => [1, 2]]]
147 $this->assertEquals([1, 2],
$model->getWebsiteIds());
150 $this->_model->setId(
151 $this->productRepository->get(
'simple')->getId()
153 $this->assertEquals([1], $this->_model->getWebsiteIds());
156 public function testGetStoreIds()
160 $model = $this->objectManager->create(
161 \
Magento\Catalog\Model\Product::class,
162 [
'data' => [
'store_ids' => [1, 2]]]
164 $this->assertEquals([1, 2],
$model->getStoreIds());
167 $this->_model->setId(
168 $this->productRepository->get(
'simple')->getId()
170 $this->assertEquals([1], $this->_model->getStoreIds());
181 $this->assertEquals([], $this->_model->getRelatedProducts());
182 $this->assertEquals([], $this->_model->getRelatedProductIds());
184 $collection = $this->_model->getRelatedProductCollection();
186 $this->assertSame($this->_model,
$collection->getProduct());
201 $this->assertEquals([], $this->_model->getUpSellProducts());
202 $this->assertEquals([], $this->_model->getUpSellProductIds());
204 $collection = $this->_model->getUpSellProductCollection();
206 $this->assertSame($this->_model,
$collection->getProduct());
221 $this->assertEquals([], $this->_model->getCrossSellProducts());
222 $this->assertEquals([], $this->_model->getCrossSellProductIds());
224 $collection = $this->_model->getCrossSellProductCollection();
226 $this->assertSame($this->_model,
$collection->getProduct());
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);
254 $this->assertEquals(
'test', $this->_model->formatUrlKey(
'test'));
257 public function testGetUrlPath()
259 $this->_model->setUrlPath(
'test');
260 $this->assertEquals(
'test', $this->_model->getUrlPath());
262 $urlPathGenerator = $this->objectManager->create(
263 \
Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator::class
267 $category = $this->objectManager->create(
268 \
Magento\Catalog\Model\Category::class,
269 [
'data' => [
'url_path' =>
'category',
'entity_id' => 5,
'path_ids' => [2, 3, 5]]]
272 $this->assertEquals(
'category/test', $urlPathGenerator->getUrlPath($this->_model,
$category));
282 $this->assertNull($this->_model->getOptions());
285 $option = $this->objectManager->create(
287 [
'data' => [
'key' =>
'value']]
290 $this->_model->setOptions([
$option]);
293 $this->assertEquals([
$option], $this->_model->getOptions());
305 $this->assertEquals([], $this->_model->getCustomOptions());
306 $this->assertFalse($this->_model->hasCustomOptions());
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());
314 $this->assertEquals(
'one',
$option->getCode());
315 $this->assertEquals(
'value1',
$option->getValue());
317 $this->assertEquals([
'one' =>
$option], $this->_model->getCustomOptions());
318 $this->assertTrue($this->_model->hasCustomOptions());
320 $this->_model->setCustomOptions([
'test']);
321 $this->assertTrue(is_array($this->_model->getCustomOptions()));
327 $this->productRepository->get(
'simple')->getId()
331 $this->assertFalse((
bool)$this->_model->canBeShowInCategory(6));
332 $this->assertTrue((
bool)$this->_model->canBeShowInCategory(3));
337 $this->assertEquals([], $this->_model->getAvailableInCategories());
340 $this->productRepository->get(
'simple-4')->getId()
343 $actualCategoryIds = $this->_model->getAvailableInCategories();
344 sort($actualCategoryIds);
346 $this->assertEquals([10, 11, 12, 13], $actualCategoryIds);
349 $this->productRepository->get(
'simple-3')->getId()
351 $this->assertEmpty($this->_model->getAvailableInCategories());
testGetCategoryCollection()
testGetAvailableInCategories()
testCrossSellProductsApi()
testCanBeShowInCategory()
static getObjectManager()