Skip to content

Commit 5e6c296

Browse files
committed
MC-21685: View simple product on storefront
1 parent e49d6b7 commit 5e6c296

File tree

4 files changed

+327
-108
lines changed

4 files changed

+327
-108
lines changed

dev/tests/integration/testsuite/Magento/Catalog/Block/Product/ViewTest.php

Lines changed: 140 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,94 +3,191 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Catalog\Block\Product;
79

10+
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Framework\ObjectManagerInterface;
13+
use Magento\Framework\Registry;
14+
use Magento\Framework\Serialize\Serializer\Json;
15+
use Magento\Framework\View\LayoutInterface;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use PHPUnit\Framework\TestCase;
18+
819
/**
9-
* Test class for \Magento\Catalog\Block\Product\View.
20+
* Checks product view block.
1021
*
22+
* @see \Magento\Catalog\Block\Product\View
1123
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
24+
* @magentoDbIsolation enabled
1225
*/
13-
class ViewTest extends \PHPUnit\Framework\TestCase
26+
class ViewTest extends TestCase
1427
{
15-
/**
16-
* @var \Magento\Catalog\Block\Product\View
17-
*/
18-
protected $_block;
28+
/** @var ObjectManagerInterface */
29+
private $objectManager;
30+
31+
/** @var View */
32+
private $_block;
33+
34+
/** @var ProductRepositoryInterface */
35+
private $productRepository;
36+
37+
/** @var Registry */
38+
private $registry;
39+
40+
/** @var LayoutInterface */
41+
private $layout;
42+
43+
/** @var Json */
44+
private $json;
1945

2046
/**
21-
* @var \Magento\Catalog\Model\Product
47+
* @inheritdoc
2248
*/
23-
protected $_product;
24-
2549
protected function setUp()
2650
{
27-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
28-
$this->_block = $objectManager->create(\Magento\Catalog\Block\Product\View::class);
29-
30-
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
31-
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
32-
$this->_product = $productRepository->get('simple');
33-
34-
$objectManager->get(\Magento\Framework\Registry::class)->unregister('product');
35-
$objectManager->get(\Magento\Framework\Registry::class)->register('product', $this->_product);
51+
$this->objectManager = Bootstrap::getObjectManager();
52+
$this->_block = $this->objectManager->create(View::class);
53+
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
54+
$this->layout = $this->objectManager->get(LayoutInterface::class);
55+
$this->registry = $this->objectManager->get(Registry::class);
56+
$this->json = $this->objectManager->get(Json::class);
3657
}
3758

38-
public function testSetLayout()
59+
/**
60+
* @return void
61+
*/
62+
public function testSetLayout(): void
3963
{
40-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
64+
$productView = $this->layout->createBlock(View::class);
4165

42-
/** @var $layout \Magento\Framework\View\Layout */
43-
$layout = $objectManager->get(\Magento\Framework\View\LayoutInterface::class);
44-
45-
$productView = $layout->createBlock(\Magento\Catalog\Block\Product\View::class);
46-
47-
$this->assertInstanceOf(\Magento\Framework\View\LayoutInterface::class, $productView->getLayout());
66+
$this->assertInstanceOf(LayoutInterface::class, $productView->getLayout());
4867
}
4968

50-
public function testGetProduct()
69+
/**
70+
* @return void
71+
*/
72+
public function testGetProduct(): void
5173
{
74+
$product = $this->productRepository->get('simple');
75+
$this->registerProduct($product);
76+
5277
$this->assertNotEmpty($this->_block->getProduct()->getId());
53-
$this->assertEquals($this->_product->getId(), $this->_block->getProduct()->getId());
78+
$this->assertEquals($product->getId(), $this->_block->getProduct()->getId());
79+
80+
$this->registry->unregister('product');
81+
$this->_block->setProductId($product->getId());
5482

55-
/** @var $objectManager \Magento\TestFramework\ObjectManager */
56-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
57-
$objectManager->get(\Magento\Framework\Registry::class)->unregister('product');
58-
$this->_block->setProductId($this->_product->getId());
59-
$this->assertEquals($this->_product->getId(), $this->_block->getProduct()->getId());
83+
$this->assertEquals($product->getId(), $this->_block->getProduct()->getId());
6084
}
6185

62-
public function testCanEmailToFriend()
86+
/**
87+
* @return void
88+
*/
89+
public function testCanEmailToFriend(): void
6390
{
6491
$this->assertFalse($this->_block->canEmailToFriend());
6592
}
6693

67-
public function testGetAddToCartUrl()
94+
/**
95+
* @return void
96+
*/
97+
public function testGetAddToCartUrl(): void
6898
{
69-
$url = $this->_block->getAddToCartUrl($this->_product);
70-
$this->assertStringMatchesFormat('%scheckout/cart/add/%sproduct/' . $this->_product->getId() . '/', $url);
99+
$product = $this->productRepository->get('simple');
100+
$url = $this->_block->getAddToCartUrl($product);
101+
102+
$this->assertStringMatchesFormat(
103+
'%scheckout/cart/add/%sproduct/' . $product->getId() . '/',
104+
$url
105+
);
71106
}
72107

73-
public function testGetJsonConfig()
108+
/**
109+
* @return void
110+
*/
111+
public function testGetJsonConfig(): void
74112
{
75-
$config = (array)json_decode($this->_block->getJsonConfig());
113+
$product = $this->productRepository->get('simple');
114+
$this->registerProduct($product);
115+
$config = $this->json->unserialize($this->_block->getJsonConfig());
116+
76117
$this->assertNotEmpty($config);
77118
$this->assertArrayHasKey('productId', $config);
78-
$this->assertEquals($this->_product->getId(), $config['productId']);
119+
$this->assertEquals($product->getId(), $config['productId']);
79120
}
80121

81-
public function testHasOptions()
122+
/**
123+
* @return void
124+
*/
125+
public function testHasOptions(): void
82126
{
127+
$product = $this->productRepository->get('simple');
128+
$this->registerProduct($product);
129+
83130
$this->assertTrue($this->_block->hasOptions());
84131
}
85132

86-
public function testHasRequiredOptions()
133+
/**
134+
* @return void
135+
*/
136+
public function testHasRequiredOptions(): void
87137
{
138+
$product = $this->productRepository->get('simple');
139+
$this->registerProduct($product);
140+
88141
$this->assertTrue($this->_block->hasRequiredOptions());
89142
}
90143

91-
public function testStartBundleCustomization()
144+
/**
145+
* @return void
146+
*/
147+
public function testStartBundleCustomization(): void
92148
{
93149
$this->markTestSkipped("Functionality not implemented in Magento 1.x. Implemented in Magento 2");
150+
94151
$this->assertFalse($this->_block->startBundleCustomization());
95152
}
153+
154+
/**
155+
* @magentoAppArea frontend
156+
*
157+
* @magentoDataFixture Magento/Catalog/_files/product_simple_out_of_stock.php
158+
*/
159+
public function testAddToCartBlockInvisibility(): void
160+
{
161+
$outOfStockProduct = $this->productRepository->get('simple-out-of-stock');
162+
$this->registerProduct($outOfStockProduct);
163+
$this->_block->setTemplate('Magento_Catalog::product/view/addtocart.phtml');
164+
$output = $this->_block->toHtml();
165+
166+
$this->assertNotContains((string)__('Add to Cart'), $output);
167+
}
168+
169+
/**
170+
* @magentoAppArea frontend
171+
*/
172+
public function testAddToCartBlockVisibility(): void
173+
{
174+
$product = $this->productRepository->get('simple');
175+
$this->registerProduct($product);
176+
$this->_block->setTemplate('Magento_Catalog::product/view/addtocart.phtml');
177+
$output = $this->_block->toHtml();
178+
179+
$this->assertContains((string)__('Add to Cart'), $output);
180+
}
181+
182+
/**
183+
* Register the product
184+
*
185+
* @param ProductInterface $product
186+
* @return void
187+
*/
188+
private function registerProduct(ProductInterface $product): void
189+
{
190+
$this->registry->unregister('product');
191+
$this->registry->register('product', $product);
192+
}
96193
}

dev/tests/integration/testsuite/Magento/Catalog/Controller/Product/ViewTest.php

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,43 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Catalog\Controller\Product;
79

10+
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Catalog\Model\Product\Visibility;
13+
use Magento\Framework\Registry;
14+
use Magento\TestFramework\Response;
15+
use Magento\TestFramework\TestCase\AbstractController;
16+
817
/**
9-
* @magentoDataFixture Magento/Catalog/controllers/_files/products.php
10-
* @magentoDbIsolation disabled
18+
* Checks product visibility on storefront
19+
*
20+
* @magentoDbIsolation enabled
1121
*/
12-
class ViewTest extends \Magento\TestFramework\TestCase\AbstractController
22+
class ViewTest extends AbstractController
1323
{
24+
/** @var ProductRepositoryInterface */
25+
private $productRepository;
26+
27+
/** @var Registry */
28+
private $registry;
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
protected function setUp()
34+
{
35+
parent::setUp();
36+
37+
$this->productRepository = $this->_objectManager->create(ProductRepositoryInterface::class);
38+
$this->registry = $this->_objectManager->get(Registry::class);
39+
}
40+
1441
/**
42+
* @magentoDataFixture Magento/Catalog/controllers/_files/products.php
1543
* @magentoConfigFixture current_store catalog/seo/product_canonical_tag 1
1644
*/
1745
public function testViewActionWithCanonicalTag()
@@ -26,4 +54,74 @@ public function testViewActionWithCanonicalTag()
2654
$this->getResponse()->getBody()
2755
);
2856
}
57+
58+
/**
59+
* @magentoDataFixture Magento/Quote/_files/is_not_salable_product.php
60+
* @return void
61+
*/
62+
public function testDisabledProductInvisibility(): void
63+
{
64+
$product = $this->productRepository->get('simple-99');
65+
$this->dispatch(sprintf('catalog/product/view/id/%s/', $product->getId()));
66+
67+
$this->assert404NotFound();
68+
}
69+
70+
/**
71+
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
72+
* @dataProvider productVisibilityDataProvider
73+
* @param int $visibility
74+
* @return void
75+
*/
76+
public function testProductVisibility(int $visibility): void
77+
{
78+
$product = $this->productRepository->get('simple2');
79+
$product->setVisibility($visibility);
80+
$this->productRepository->save($product);
81+
$this->dispatch(sprintf('catalog/product/view/id/%s/', $product->getId()));
82+
83+
$this->assertProductIsVisible($product);
84+
}
85+
86+
/**
87+
* @return array
88+
*/
89+
public function productVisibilityDataProvider(): array
90+
{
91+
return [
92+
'catalog_search' => [Visibility::VISIBILITY_BOTH],
93+
'search' => [Visibility::VISIBILITY_IN_SEARCH],
94+
'catalog' => [Visibility::VISIBILITY_IN_CATALOG],
95+
];
96+
}
97+
98+
/**
99+
* @inheritdoc
100+
*/
101+
public function assert404NotFound()
102+
{
103+
parent::assert404NotFound();
104+
105+
$this->assertNull($this->registry->registry('current_product'));
106+
}
107+
108+
/**
109+
* Assert that product is available in storefront
110+
*
111+
* @param ProductInterface $product
112+
* @return void
113+
*/
114+
private function assertProductIsVisible(ProductInterface $product): void
115+
{
116+
$this->assertEquals(
117+
Response::STATUS_CODE_200,
118+
$this->getResponse()->getHttpResponseCode(),
119+
'Wrong response code is returned'
120+
);
121+
$this->assertEquals(
122+
$product->getSku(),
123+
$this->registry->registry('current_product')->getSku(),
124+
'Wrong product is registered'
125+
);
126+
}
29127
}

0 commit comments

Comments
 (0)