Skip to content

Commit 9fb6878

Browse files
committed
Merge remote-tracking branch 'origin/MC-21685' into 2.3-develop-com-pr4
2 parents 296ceb9 + 36faa70 commit 9fb6878

File tree

4 files changed

+361
-114
lines changed

4 files changed

+361
-114
lines changed

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

Lines changed: 144 additions & 47 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);
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);
57+
}
2958

30-
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
31-
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
32-
$this->_product = $productRepository->get('simple');
59+
/**
60+
* @return void
61+
*/
62+
public function testSetLayout(): void
63+
{
64+
$productView = $this->layout->createBlock(View::class);
3365

34-
$objectManager->get(\Magento\Framework\Registry::class)->unregister('product');
35-
$objectManager->get(\Magento\Framework\Registry::class)->register('product', $this->_product);
66+
$this->assertInstanceOf(LayoutInterface::class, $productView->getLayout());
3667
}
3768

38-
public function testSetLayout()
69+
/**
70+
* @return void
71+
*/
72+
public function testGetProduct(): void
3973
{
40-
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
74+
$product = $this->productRepository->get('simple');
75+
$this->registerProduct($product);
4176

42-
/** @var $layout \Magento\Framework\View\Layout */
43-
$layout = $objectManager->get(\Magento\Framework\View\LayoutInterface::class);
77+
$this->assertNotEmpty($this->block->getProduct()->getId());
78+
$this->assertEquals($product->getId(), $this->block->getProduct()->getId());
4479

45-
$productView = $layout->createBlock(\Magento\Catalog\Block\Product\View::class);
80+
$this->registry->unregister('product');
81+
$this->block->setProductId($product->getId());
4682

47-
$this->assertInstanceOf(\Magento\Framework\View\LayoutInterface::class, $productView->getLayout());
83+
$this->assertEquals($product->getId(), $this->block->getProduct()->getId());
4884
}
4985

50-
public function testGetProduct()
86+
/**
87+
* @return void
88+
*/
89+
public function testCanEmailToFriend(): void
5190
{
52-
$this->assertNotEmpty($this->_block->getProduct()->getId());
53-
$this->assertEquals($this->_product->getId(), $this->_block->getProduct()->getId());
54-
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());
91+
$this->assertFalse($this->block->canEmailToFriend());
6092
}
6193

62-
public function testCanEmailToFriend()
94+
/**
95+
* @return void
96+
*/
97+
public function testGetAddToCartUrl(): void
6398
{
64-
$this->assertFalse($this->_block->canEmailToFriend());
65-
}
99+
$product = $this->productRepository->get('simple');
100+
$url = $this->block->getAddToCartUrl($product);
66101

67-
public function testGetAddToCartUrl()
68-
{
69-
$url = $this->_block->getAddToCartUrl($this->_product);
70-
$this->assertStringMatchesFormat('%scheckout/cart/add/%sproduct/' . $this->_product->getId() . '/', $url);
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
{
83-
$this->assertTrue($this->_block->hasOptions());
127+
$product = $this->productRepository->get('simple');
128+
$this->registerProduct($product);
129+
130+
$this->assertTrue($this->block->hasOptions());
84131
}
85132

86-
public function testHasRequiredOptions()
133+
/**
134+
* @return void
135+
*/
136+
public function testHasRequiredOptions(): void
87137
{
88-
$this->assertTrue($this->_block->hasRequiredOptions());
138+
$product = $this->productRepository->get('simple');
139+
$this->registerProduct($product);
140+
141+
$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");
94-
$this->assertFalse($this->_block->startBundleCustomization());
150+
151+
$this->assertFalse($this->block->startBundleCustomization());
152+
}
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);
95192
}
96193
}

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

Lines changed: 127 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,47 @@
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+
1430
/**
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+
41+
/**
42+
* @magentoDataFixture Magento/Catalog/controllers/_files/products.php
1543
* @magentoConfigFixture current_store catalog/seo/product_canonical_tag 1
44+
* @return void
1645
*/
17-
public function testViewActionWithCanonicalTag()
46+
public function testViewActionWithCanonicalTag(): void
1847
{
1948
$this->markTestSkipped(
2049
'MAGETWO-40724: Canonical url from tests sometimes does not equal canonical url from action'
@@ -26,4 +55,98 @@ public function testViewActionWithCanonicalTag()
2655
$this->getResponse()->getBody()
2756
);
2857
}
58+
59+
/**
60+
* @magentoDataFixture Magento/Quote/_files/is_not_salable_product.php
61+
* @return void
62+
*/
63+
public function testDisabledProductInvisibility(): void
64+
{
65+
$product = $this->productRepository->get('simple-99');
66+
$this->dispatch(sprintf('catalog/product/view/id/%s/', $product->getId()));
67+
68+
$this->assert404NotFound();
69+
}
70+
71+
/**
72+
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
73+
* @dataProvider productVisibilityDataProvider
74+
* @param int $visibility
75+
* @return void
76+
*/
77+
public function testProductVisibility(int $visibility): void
78+
{
79+
$product = $this->updateProductVisibility('simple2', $visibility);
80+
$this->dispatch(sprintf('catalog/product/view/id/%s/', $product->getId()));
81+
82+
$this->assertProductIsVisible($product);
83+
}
84+
85+
/**
86+
* @return array
87+
*/
88+
public function productVisibilityDataProvider(): array
89+
{
90+
return [
91+
'catalog_search' => [Visibility::VISIBILITY_BOTH],
92+
'search' => [Visibility::VISIBILITY_IN_SEARCH],
93+
'catalog' => [Visibility::VISIBILITY_IN_CATALOG],
94+
];
95+
}
96+
97+
/**
98+
* @magentoDataFixture Magento/Catalog/_files/simple_products_not_visible_individually.php
99+
*/
100+
public function testProductNotVisibleIndividually(): void
101+
{
102+
$product = $this->updateProductVisibility('simple_not_visible_1', Visibility::VISIBILITY_NOT_VISIBLE);
103+
$this->dispatch(sprintf('catalog/product/view/id/%s/', $product->getId()));
104+
105+
$this->assert404NotFound();
106+
}
107+
108+
/**
109+
* @inheritdoc
110+
*/
111+
public function assert404NotFound()
112+
{
113+
parent::assert404NotFound();
114+
115+
$this->assertNull($this->registry->registry('current_product'));
116+
}
117+
118+
/**
119+
* Assert that product is available in storefront
120+
*
121+
* @param ProductInterface $product
122+
* @return void
123+
*/
124+
private function assertProductIsVisible(ProductInterface $product): void
125+
{
126+
$this->assertEquals(
127+
Response::STATUS_CODE_200,
128+
$this->getResponse()->getHttpResponseCode(),
129+
'Wrong response code is returned'
130+
);
131+
$this->assertEquals(
132+
$product->getSku(),
133+
$this->registry->registry('current_product')->getSku(),
134+
'Wrong product is registered'
135+
);
136+
}
137+
138+
/**
139+
* Update product visibility
140+
*
141+
* @param string $sku
142+
* @param int $visibility
143+
* @return ProductInterface
144+
*/
145+
private function updateProductVisibility(string $sku, int $visibility): ProductInterface
146+
{
147+
$product = $this->productRepository->get($sku);
148+
$product->setVisibility($visibility);
149+
150+
return $this->productRepository->save($product);
151+
}
29152
}

0 commit comments

Comments
 (0)