Skip to content

Commit bf2207a

Browse files
committed
MC-40059: Create automated test for: "Subscribe to price alert"
1 parent 1e846a4 commit bf2207a

File tree

12 files changed

+652
-23
lines changed

12 files changed

+652
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Alerts;
9+
10+
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
11+
use Magento\Framework\App\RequestInterface;
12+
use Magento\Framework\Module\Manager;
13+
use Magento\Framework\ObjectManagerInterface;
14+
use Magento\Store\Model\Store;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* Base alert's tab test logic
21+
*/
22+
abstract class AbstractAlertTest extends TestCase
23+
{
24+
/** @var ObjectManagerInterface */
25+
protected $objectManager;
26+
27+
/** @var RequestInterface */
28+
protected $request;
29+
30+
/** @var StoreManagerInterface */
31+
private $storeManager;
32+
33+
/** @var ProductResource */
34+
private $productResource;
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public static function setUpBeforeClass(): void
40+
{
41+
parent::setUpBeforeClass();
42+
43+
$objectManager = Bootstrap::getObjectManager();
44+
/** @var Manager $moduleManager */
45+
$moduleManager = $objectManager->get(Manager::class);
46+
//This check is needed because module Magento_Catalog is independent of Magento_ProductAlert
47+
if (!$moduleManager->isEnabled('Magento_ProductAlert')) {
48+
self::markTestSkipped('Magento_ProductAlert module disabled.');
49+
}
50+
}
51+
52+
/**
53+
* @inheritdoc
54+
*/
55+
protected function setUp(): void
56+
{
57+
parent::setUp();
58+
59+
$this->objectManager = Bootstrap::getObjectManager();
60+
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
61+
$this->request = $this->objectManager->get(RequestInterface::class);
62+
$this->productResource = $this->objectManager->get(ProductResource::class);
63+
}
64+
65+
/**
66+
* Prepare request
67+
*
68+
* @param string|null $sku
69+
* @param string|null $storeCode
70+
* @return void
71+
*/
72+
protected function prepareRequest(?string $sku = null, ?string $storeCode = null): void
73+
{
74+
$productId = (int)$this->productResource->getIdBySku($sku);
75+
$storeId = $storeCode ? (int)$this->storeManager->getStore($storeCode)->getId() : null;
76+
$this->request->setParams(['id' => $productId, 'store' => $storeId]);
77+
}
78+
79+
/**
80+
* Assert grid url
81+
*
82+
* @param string $url
83+
* @param string|null $storeCode
84+
* @return void
85+
*/
86+
protected function assertGridUrl(string $url, ?string $storeCode): void
87+
{
88+
$storeId = $storeCode ? (int)$this->storeManager->getStore($storeCode)->getId() : Store::DEFAULT_STORE_ID;
89+
$this->assertStringContainsString(sprintf('/store/%s', $storeId), $url);
90+
}
91+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Alerts;
9+
10+
use Magento\Framework\View\LayoutInterface;
11+
12+
/**
13+
* Check price alert grid
14+
*
15+
* @see \Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Alerts\Price
16+
*
17+
* @magentoAppArea adminhtml
18+
*/
19+
class PriceTest extends AbstractAlertTest
20+
{
21+
/** @var Price */
22+
private $block;
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
protected function setUp(): void
28+
{
29+
parent::setUp();
30+
31+
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Price::class);
32+
}
33+
34+
/**
35+
* @dataProvider alertsDataProvider
36+
*
37+
* @magentoDbIsolation disabled
38+
*
39+
* @magentoDataFixture Magento/ProductAlert/_files/product_alert.php
40+
* @magentoDataFixture Magento/ProductAlert/_files/price_alert_on_second_website.php
41+
*
42+
* @param string $sku
43+
* @param string $expectedEmail
44+
* @param string|null $storeCode
45+
* @return void
46+
*/
47+
public function testGridCollectionWithStoreId(string $sku, string $expectedEmail, ?string $storeCode = null): void
48+
{
49+
$this->prepareRequest($sku, $storeCode);
50+
$collection = $this->block->getPreparedCollection();
51+
$this->assertCount(1, $collection);
52+
$this->assertEquals($expectedEmail, $collection->getFirstItem()->getEmail());
53+
}
54+
55+
/**
56+
* @return array
57+
*/
58+
public function alertsDataProvider(): array
59+
{
60+
return [
61+
'without_store_id_filter' => [
62+
'product_sku' => 'simple',
63+
'expected_customer_emails' => '[email protected]',
64+
],
65+
'with_store_id_filter' => [
66+
'product_sku' => 'simple_on_second_website_for_price_alert',
67+
'expected_customer_emails' => '[email protected]',
68+
'store_code' => 'fixture_third_store',
69+
],
70+
];
71+
}
72+
73+
/**
74+
* @dataProvider storeProvider
75+
*
76+
* @param string|null $storeCode
77+
* @return void
78+
*/
79+
public function testGetGridUrl(?string $storeCode): void
80+
{
81+
$this->prepareRequest(null, $storeCode);
82+
$this->assertGridUrl($this->block->getGridUrl(), $storeCode);
83+
}
84+
85+
/**
86+
* @return array
87+
*/
88+
public function storeProvider(): array
89+
{
90+
return [
91+
'without_store_id_param' => [
92+
'store_code' => null,
93+
],
94+
'with_store_id_param' => [
95+
'store_code' => 'default',
96+
],
97+
];
98+
}
99+
}

dev/tests/integration/testsuite/Magento/Catalog/Block/Adminhtml/Product/Edit/Tab/Alerts/StockTest.php

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77

88
namespace Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Alerts;
99

10-
use Magento\Catalog\Api\ProductRepositoryInterface;
11-
use Magento\Framework\ObjectManagerInterface;
1210
use Magento\Framework\View\LayoutInterface;
13-
use Magento\Store\Model\StoreManagerInterface;
14-
use Magento\TestFramework\Helper\Bootstrap;
15-
use PHPUnit\Framework\TestCase;
1611

1712
/**
1813
* Check stock alert grid
@@ -21,31 +16,19 @@
2116
*
2217
* @magentoAppArea adminhtml
2318
*/
24-
class StockTest extends TestCase
19+
class StockTest extends AbstractAlertTest
2520
{
26-
/** @var ObjectManagerInterface */
27-
private $objectManager;
28-
2921
/** @var Stock */
3022
private $block;
3123

32-
/** @var StoreManagerInterface */
33-
private $storeManager;
34-
35-
/** @var ProductRepositoryInterface */
36-
private $productRepository;
37-
3824
/**
3925
* @inheritdoc
4026
*/
4127
protected function setUp(): void
4228
{
4329
parent::setUp();
4430

45-
$this->objectManager = Bootstrap::getObjectManager();
4631
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Stock::class);
47-
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
48-
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
4932
}
5033

5134
/**
@@ -62,9 +45,7 @@ protected function setUp(): void
6245
*/
6346
public function testGridCollectionWithStoreId(string $sku, string $expectedEmail, ?string $storeCode = null): void
6447
{
65-
$productId = (int)$this->productRepository->get($sku)->getId();
66-
$storeId = $storeCode ? (int)$this->storeManager->getStore($storeCode)->getId() : null;
67-
$this->block->getRequest()->setParams(['id' => $productId, 'store' => $storeId]);
48+
$this->prepareRequest($sku, $storeCode);
6849
$collection = $this->block->getPreparedCollection();
6950
$this->assertCount(1, $collection);
7051
$this->assertEquals($expectedEmail, $collection->getFirstItem()->getEmail());
@@ -87,4 +68,31 @@ public function alertsDataProvider(): array
8768
],
8869
];
8970
}
71+
72+
/**
73+
* @dataProvider storeProvider
74+
*
75+
* @param string|null $storeCode
76+
* @return void
77+
*/
78+
public function testGetGridUrl(?string $storeCode): void
79+
{
80+
$this->prepareRequest(null, $storeCode);
81+
$this->assertGridUrl($this->block->getGridUrl(), $storeCode);
82+
}
83+
84+
/**
85+
* @return array
86+
*/
87+
public function storeProvider(): array
88+
{
89+
return [
90+
'without_store_id_param' => [
91+
'store_code' => null,
92+
],
93+
'with_store_id_param' => [
94+
'store_code' => 'default',
95+
],
96+
];
97+
}
9098
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Controller\Adminhtml\Product;
9+
10+
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
11+
use Magento\Framework\App\Request\Http as HttpRequest;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
use Magento\TestFramework\Helper\Xpath;
14+
use Magento\TestFramework\TestCase\AbstractBackendController;
15+
16+
/**
17+
* Class contains of base logic for alert controllers tests
18+
*/
19+
abstract class AbstractAlertTest extends AbstractBackendController
20+
{
21+
/** @var ProductResource */
22+
private $productResource;
23+
24+
/** @var StoreManagerInterface */
25+
private $storeManager;
26+
27+
/**
28+
* @inheritdoc
29+
*/
30+
protected function setUp(): void
31+
{
32+
parent::setUp();
33+
34+
$this->productResource = $this->_objectManager->get(ProductResource::class);
35+
$this->storeManager = $this->_objectManager->get(StoreManagerInterface::class);
36+
}
37+
38+
/**
39+
* Prepare request
40+
*
41+
* @param string $productSku
42+
* @param string $storeCode
43+
* @param int|null $limit
44+
* @return void
45+
*/
46+
protected function prepareRequest(string $productSku, string $storeCode, ?int $limit): void
47+
{
48+
$productId = $this->productResource->getIdBySku($productSku);
49+
$storeId = $this->storeManager->getStore($storeCode)->getId();
50+
$this->getRequest()->setMethod(HttpRequest::METHOD_GET);
51+
$this->getRequest()->setParams(['id' => $productId, 'store' => $storeId, 'limit' => $limit]);
52+
}
53+
54+
/**
55+
* Assert alert grid records count related to provided email
56+
*
57+
* @param string $email
58+
* @param int $expectedCount
59+
* @return void
60+
*/
61+
protected function assertGridRecords(string $email, int $expectedCount): void
62+
{
63+
$content = $this->getResponse()->getContent();
64+
$this->assertEquals(
65+
$expectedCount,
66+
Xpath::getElementsCountForXpath(sprintf($this->getRecordXpathTemplate(), $email), $content)
67+
);
68+
}
69+
70+
/**
71+
* Get alert grid record xpath template
72+
*
73+
* @return string
74+
*/
75+
abstract protected function getRecordXpathTemplate(): string;
76+
}

0 commit comments

Comments
 (0)