Skip to content

Commit 346f7ff

Browse files
committed
AC-14352::Integration and Unit test coverage for Delete multiple catalog price rules at once
1 parent b3460d1 commit 346f7ff

File tree

3 files changed

+122
-1
lines changed

3 files changed

+122
-1
lines changed

app/code/Magento/CatalogRule/Controller/Adminhtml/Promo/Catalog/MassDelete.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function execute()
4848
foreach ($ids as $id) {
4949
$this->ruleRepository->deleteById($id);
5050
}
51-
$this->messageManager->addSuccessMessage(__('You deleted a total of %1 records.', count($ids)));
51+
$this->messageManager->addSuccessMessage(__('A total of %1 record(s) were deleted.', count($ids)));
5252
} else {
5353
$this->messageManager->addErrorMessage(__('Please select a catalog price rule(s)'));
5454
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogRule\Test\Unit\Controller\Adminhtml\Promo\Catalog;
9+
10+
use Magento\Backend\Model\View\Result\Redirect;
11+
use Magento\CatalogRule\Controller\Adminhtml\Promo\Catalog\MassDelete;
12+
use Magento\Framework\App\RequestInterface;
13+
use Magento\Framework\Controller\ResultFactory;
14+
use Magento\Framework\Message\ManagerInterface;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use Magento\Backend\App\Action\Context;
17+
use PHPUnit\Framework\TestCase;
18+
use Magento\CatalogRule\Api\Data\RuleInterface;
19+
use Magento\CatalogRule\Model\Rule;
20+
use Magento\CatalogRule\Api\CatalogRuleRepositoryInterface;
21+
22+
class MassDeleteTest extends TestCase
23+
{
24+
/**
25+
* @var CatalogRuleRepositoryInterface|MockObject
26+
*/
27+
private $catalogRuleRepositoryMock;
28+
29+
/**
30+
* @var Context|MockObject
31+
*/
32+
protected $contextMock;
33+
34+
/**
35+
* @var MassDelete
36+
*/
37+
protected $delete;
38+
39+
/**
40+
* @var RequestInterface|MockObject
41+
*/
42+
protected $requestMock;
43+
44+
/**
45+
* @var Rule|MockObject
46+
*/
47+
protected $ruleMock;
48+
49+
/**
50+
* @var ManagerInterface|MockObject
51+
*/
52+
protected $messageManagerMock;
53+
54+
/**
55+
* @var Redirect|MockObject
56+
*/
57+
protected $resultRedirectMock;
58+
59+
/**
60+
* @var ResultFactory|MockObject
61+
*/
62+
protected $resultFactory;
63+
64+
protected function setUp(): void
65+
{
66+
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
67+
->onlyMethods(['getParam'])
68+
->getMockForAbstractClass();
69+
$this->contextMock = $this->createMock(Context::class);
70+
71+
$this->messageManagerMock = $this->getMockBuilder(ManagerInterface::class)
72+
->getMockForAbstractClass();
73+
74+
$this->resultRedirectMock = $this->getMockBuilder(Redirect::class)
75+
->onlyMethods(['setPath'])
76+
->disableOriginalConstructor()
77+
->getMock();
78+
79+
$this->resultFactory = $this->createMock(ResultFactory::class);
80+
$this->resultFactory->method('create')->willReturn($this->resultRedirectMock);
81+
$this->contextMock->method('getResultFactory')->willReturn($this->resultFactory);
82+
$this->contextMock->method('getMessageManager')->willReturn($this->messageManagerMock);
83+
$this->contextMock->method('getRequest')->willReturn($this->requestMock);
84+
$this->catalogRuleRepositoryMock = $this->createMock(
85+
CatalogRuleRepositoryInterface::class
86+
);
87+
$this->delete = new MassDelete($this->contextMock, $this->catalogRuleRepositoryMock);
88+
}
89+
90+
public function testExecute()
91+
{
92+
$data = [1];
93+
$this->requestMock->expects(self::any())
94+
->method('getParam')
95+
->willReturn($data);
96+
$catalogRuleMock = $this->getMockForAbstractClass(RuleInterface::class);
97+
$this->catalogRuleRepositoryMock->expects($this->once())
98+
->method('deleteById')
99+
->with(1)
100+
->willReturn($catalogRuleMock);
101+
$this->messageManagerMock->expects($this->once())
102+
->method('addSuccessMessage')
103+
->with(__('A total of %1 record(s) were deleted.', $data));
104+
$this->delete->execute();
105+
}
106+
}

dev/tests/integration/testsuite/Magento/CatalogRule/Controller/Adminhtml/Promo/Catalog/MassActionTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,21 @@ public function testMassInActivatedRule()
8585
self::assertEquals(0, $afterActivateRule2->getIsActive());
8686
}
8787

88+
#[DataFixture(CatalogRuleFixture::class, ['is_active' => 1], 'cr1')]
89+
#[DataFixture(CatalogRuleFixture::class, ['is_active' => 1], 'cr2')]
90+
public function testMassDeleteRule()
91+
{
92+
$rule1 = FixtureManager::getStorage()->get('cr1');
93+
$rule2 = FixtureManager::getStorage()->get('cr2');
94+
$params = ['catalogpricerule' => [$rule1->getId(), $rule2->getId()]];
95+
$request = $this->getRequest();
96+
$request->setParams($params);
97+
$request->setMethod(HttpRequest::METHOD_POST);
98+
$url = 'backend/catalog_rule/promo_catalog/massDelete';
99+
$this->dispatch($url);
100+
self::assertEquals('A total of 2 record(s) were deleted.', $this->getSuccessMessage());
101+
}
102+
88103
/**
89104
* Gets success message after dispatching the controller.
90105
*

0 commit comments

Comments
 (0)