Skip to content

Commit abac538

Browse files
🔃 [Magento Community Engineering] Community Contributions - 2.4-develop expedited
Accepted Community Pull Requests: - #26076: [Search] Cover SynonymActions Column by Unit Test (by @edenduong) - #26009: Refactor: Add information about the path that is not allowed (by @lbajsarowicz) - #26082: [GiftMessage] Cover Observer SalesEventOrderItemToQuoteItemObserver by Unit Test (by @edenduong) - #26068: [GoogleAnalytics] covered Helper Data by Unit Test (by @srsathish92) - #25759: fixed issue 25433 (by @Ashna-Jahan) Fixed GitHub Issues: - #25433: Close (X) not working when error come for qty (reported by @renard123) has been fixed in #25759 by @Ashna-Jahan in 2.4-develop branch Related commits: 1. 841bb75 2. e2d6d58 3. 4c0f128 4. 2bd319b 5. db5332e 6. d904436 7. 78cdd70 8. 56d198d
2 parents 6149eac + ccc0c71 commit abac538

File tree

6 files changed

+658
-87
lines changed

6 files changed

+658
-87
lines changed
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\GiftMessage\Test\Unit\Observer;
10+
11+
use Magento\Framework\Event;
12+
use Magento\Framework\Event\Observer;
13+
use Magento\Framework\Message\MessageInterface;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use Magento\GiftMessage\Helper\Message as MessageHelper;
16+
use Magento\GiftMessage\Model\Message as MessageModel;
17+
use Magento\GiftMessage\Model\MessageFactory;
18+
use Magento\GiftMessage\Observer\SalesEventOrderItemToQuoteItemObserver;
19+
use Magento\Quote\Model\Quote\Item as QuoteItem;
20+
use Magento\Sales\Model\Order;
21+
use Magento\Sales\Model\Order\Item as OrderItem;
22+
use Magento\Store\Model\Store;
23+
use PHPUnit\Framework\MockObject\MockObject;
24+
use PHPUnit\Framework\TestCase;
25+
26+
/**
27+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
28+
*/
29+
class SalesEventOrderItemToQuoteItemObserverTest extends TestCase
30+
{
31+
/**
32+
* Stub message id
33+
*/
34+
private const STUB_MESSAGE_ID = 1;
35+
36+
/**
37+
* Stub new message id
38+
*/
39+
private const STUB_NEW_MESSAGE_ID = 2;
40+
41+
/**
42+
* @var SalesEventOrderItemToQuoteItemObserver
43+
*/
44+
private $observer;
45+
46+
/**
47+
* @var MessageFactory|MockObject
48+
*/
49+
private $messageFactoryMock;
50+
51+
/**
52+
* @var MessageHelper|MockObject
53+
*/
54+
private $giftMessageHelperMock;
55+
56+
/**
57+
* @var Observer|MockObject
58+
*/
59+
private $observerMock;
60+
61+
/**
62+
* @var Event|MockObject
63+
*/
64+
private $eventMock;
65+
66+
/**
67+
* @var Order|MockObject
68+
*/
69+
private $orderMock;
70+
71+
/**
72+
* @var OrderItem|MockObject
73+
*/
74+
private $orderItemMock;
75+
76+
/**
77+
* @var Store|MockObject
78+
*/
79+
private $storeMock;
80+
81+
/**
82+
* @var MessageInterface|MockObject
83+
*/
84+
private $messageMock;
85+
86+
/**
87+
* @var QuoteItem|MockObject
88+
*/
89+
private $quoteItemMock;
90+
91+
/**
92+
* Prepare environment for test
93+
*/
94+
public function setUp(): void
95+
{
96+
$this->messageFactoryMock = $this->getMockBuilder(MessageFactory::class)
97+
->disableOriginalConstructor()
98+
->setMethods(['create'])
99+
->getMock();
100+
$this->giftMessageHelperMock = $this->createMock(MessageHelper::class);
101+
$this->observerMock = $this->createMock(Observer::class);
102+
$this->eventMock = $this->createPartialMock(Event::class, ['getOrderItem', 'getQuoteItem']);
103+
$this->orderItemMock = $this->createPartialMock(
104+
OrderItem::class,
105+
['getOrder', 'getStoreId', 'getGiftMessageId']
106+
);
107+
$this->quoteItemMock = $this->createPartialMock(QuoteItem::class, ['setGiftMessageId']);
108+
$this->orderMock = $this->createPartialMock(Order::class, ['getReordered']);
109+
$this->storeMock = $this->createMock(Store::class);
110+
$this->messageMock = $this->createMock(MessageModel::class);
111+
112+
$this->eventMock->expects($this->atLeastOnce())
113+
->method('getOrderItem')
114+
->willReturn($this->orderItemMock);
115+
116+
$this->orderItemMock->expects($this->atLeastOnce())
117+
->method('getOrder')
118+
->willReturn($this->orderMock);
119+
120+
$this->observerMock->expects($this->atLeastOnce())
121+
->method('getEvent')
122+
->willReturn($this->eventMock);
123+
124+
$objectManager = new ObjectManager($this);
125+
126+
$this->observer = $objectManager->getObject(
127+
SalesEventOrderItemToQuoteItemObserver::class,
128+
[
129+
'messageFactory' => $this->messageFactoryMock,
130+
'giftMessageMessage' => $this->giftMessageHelperMock
131+
]
132+
);
133+
}
134+
135+
/**
136+
* Test when the order is reorder
137+
*/
138+
public function testReorder()
139+
{
140+
$this->orderMock->expects($this->once())
141+
->method('getReordered')
142+
->willReturn(true);
143+
144+
$this->giftMessageHelperMock->expects($this->never())
145+
->method('isMessagesAllowed');
146+
147+
$this->eventMock
148+
->expects($this->never())
149+
->method('getQuoteItem')
150+
->willReturn($this->quoteItemMock);
151+
152+
/** Run observer */
153+
$this->observer->execute($this->observerMock);
154+
}
155+
156+
/**
157+
* Test when the order is new reorder and gift message is not allowed
158+
*/
159+
public function testNewOrderWhenGiftMessageIsNotAllowed()
160+
{
161+
$this->orderMock->expects($this->once())
162+
->method('getReordered')
163+
->willReturn(false);
164+
165+
$this->giftMessageHelperMock->expects($this->once())
166+
->method('isMessagesAllowed')
167+
->willReturn(false);
168+
169+
$this->eventMock
170+
->expects($this->never())
171+
->method('getQuoteItem')
172+
->willReturn($this->quoteItemMock);
173+
174+
/** Run observer */
175+
$this->observer->execute($this->observerMock);
176+
}
177+
178+
/**
179+
* Test when the order is new reorder and gift message is allowed
180+
*/
181+
public function testNewOrderWhenGiftMessageIsAllowed()
182+
{
183+
$this->orderMock->expects($this->once())
184+
->method('getReordered')
185+
->willReturn(false);
186+
187+
$this->giftMessageHelperMock->expects($this->once())
188+
->method('isMessagesAllowed')
189+
->willReturn(true);
190+
191+
$this->eventMock
192+
->expects($this->atLeastOnce())
193+
->method('getQuoteItem')
194+
->willReturn($this->quoteItemMock);
195+
196+
$this->orderItemMock->expects($this->once())
197+
->method('getGiftMessageId')
198+
->willReturn(self::STUB_MESSAGE_ID);
199+
200+
$this->messageFactoryMock->expects($this->once())
201+
->method('create')
202+
->willReturn($this->messageMock);
203+
$this->messageMock->expects($this->once())
204+
->method('load')
205+
->with(self::STUB_MESSAGE_ID)
206+
->willReturnSelf();
207+
$this->messageMock->expects($this->once())
208+
->method('setId')
209+
->with(null)
210+
->willReturnSelf();
211+
$this->messageMock->expects($this->once())
212+
->method('save')
213+
->willReturnSelf();
214+
$this->messageMock->expects($this->once())
215+
->method('getId')
216+
->willReturn(self::STUB_NEW_MESSAGE_ID);
217+
$this->quoteItemMock->expects($this->once())
218+
->method('setGiftMessageId')
219+
->with(self::STUB_NEW_MESSAGE_ID)
220+
->willReturnSelf();
221+
222+
/** Run observer */
223+
$this->observer->execute($this->observerMock);
224+
}
225+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Magento\GoogleAnalytics\Test\Unit\Helper;
11+
12+
use Magento\GoogleAnalytics\Helper\Data as HelperData;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
use Magento\Store\Model\ScopeInterface;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
16+
use PHPUnit\Framework\TestCase;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
19+
/**
20+
* Unit test for Magento\GoogleAnalytics\Helper\Data
21+
*/
22+
class DataTest extends TestCase
23+
{
24+
/**
25+
* @var HelperData
26+
*/
27+
private $helper;
28+
29+
/**
30+
* @var ScopeConfigInterface|MockObject
31+
*/
32+
private $scopeConfigMock;
33+
34+
/**
35+
* @inheritDoc
36+
*/
37+
protected function setUp(): void
38+
{
39+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
40+
->setMethods(['getValue', 'isSetFlag'])
41+
->disableOriginalConstructor()
42+
->getMockForAbstractClass();
43+
44+
$objectManager = new ObjectManager($this);
45+
$this->helper = $objectManager->getObject(
46+
HelperData::class,
47+
[
48+
'scopeConfig' => $this->scopeConfigMock
49+
]
50+
);
51+
}
52+
53+
/**
54+
* Test for isGoogleAnalyticsAvailable()
55+
*
56+
* @param string $value
57+
* @param bool $flag
58+
* @param bool $result
59+
* @return void
60+
* @dataProvider gaDataProvider
61+
*/
62+
public function testIsGoogleAnalyticsAvailable($value, $flag, $result): void
63+
{
64+
$this->scopeConfigMock->expects($this->once())
65+
->method('getValue')
66+
->with(HelperData::XML_PATH_ACCOUNT, ScopeInterface::SCOPE_STORE)
67+
->willReturn($value);
68+
69+
$this->scopeConfigMock->expects($this->any())
70+
->method('isSetFlag')
71+
->with(HelperData::XML_PATH_ACTIVE, ScopeInterface::SCOPE_STORE)
72+
->willReturn($flag);
73+
74+
$this->assertEquals($result, $this->helper->isGoogleAnalyticsAvailable());
75+
}
76+
77+
/**
78+
* Data provider for isGoogleAnalyticsAvailable()
79+
*
80+
* @return array
81+
*/
82+
public function gaDataProvider(): array
83+
{
84+
return [
85+
['GA-XXXX', true, true],
86+
['GA-XXXX', false, false],
87+
['', true, false]
88+
];
89+
}
90+
91+
/**
92+
* Test for isAnonymizedIpActive()
93+
*
94+
* @param string $value
95+
* @param bool $result
96+
* @return void
97+
* @dataProvider yesNoDataProvider
98+
*/
99+
public function testIsAnonymizedIpActive($value, $result): void
100+
{
101+
$this->scopeConfigMock->expects($this->once())
102+
->method('getValue')
103+
->with(HelperData::XML_PATH_ANONYMIZE, ScopeInterface::SCOPE_STORE)
104+
->willReturn($value);
105+
$this->assertEquals($result, $this->helper->isAnonymizedIpActive());
106+
}
107+
108+
/**
109+
* Data provider for isAnonymizedIpActive()
110+
*
111+
* @return array
112+
*/
113+
public function yesNoDataProvider(): array
114+
{
115+
return [
116+
['Yes' => '1', 'result' => true],
117+
['No' => '0', 'result' => false]
118+
];
119+
}
120+
}

0 commit comments

Comments
 (0)