Skip to content

Commit

Permalink
🔃 [Magento Community Engineering] Community Contributions - 2.4-devel…
Browse files Browse the repository at this point in the history
…op expedited

Accepted Community Pull Requests:
 - magento#26076: [Search] Cover SynonymActions Column by Unit Test (by @edenduong)
 - magento#26009: Refactor: Add information about the path that is not allowed (by @lbajsarowicz)
 - magento#26082: [GiftMessage] Cover Observer SalesEventOrderItemToQuoteItemObserver by Unit Test (by @edenduong)
 - magento#26068: [GoogleAnalytics] covered Helper Data by Unit Test (by @srsathish92)
 - magento#25759: fixed issue 25433 (by @Ashna-Jahan)


Fixed GitHub Issues:
 - magento#25433: Close (X) not working when error come for qty (reported by @renard123) has been fixed in magento#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
  • Loading branch information
VladimirZaets authored Dec 19, 2019
2 parents 6149eac + ccc0c71 commit abac538
Show file tree
Hide file tree
Showing 6 changed files with 658 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\GiftMessage\Test\Unit\Observer;

use Magento\Framework\Event;
use Magento\Framework\Event\Observer;
use Magento\Framework\Message\MessageInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\GiftMessage\Helper\Message as MessageHelper;
use Magento\GiftMessage\Model\Message as MessageModel;
use Magento\GiftMessage\Model\MessageFactory;
use Magento\GiftMessage\Observer\SalesEventOrderItemToQuoteItemObserver;
use Magento\Quote\Model\Quote\Item as QuoteItem;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Item as OrderItem;
use Magento\Store\Model\Store;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class SalesEventOrderItemToQuoteItemObserverTest extends TestCase
{
/**
* Stub message id
*/
private const STUB_MESSAGE_ID = 1;

/**
* Stub new message id
*/
private const STUB_NEW_MESSAGE_ID = 2;

/**
* @var SalesEventOrderItemToQuoteItemObserver
*/
private $observer;

/**
* @var MessageFactory|MockObject
*/
private $messageFactoryMock;

/**
* @var MessageHelper|MockObject
*/
private $giftMessageHelperMock;

/**
* @var Observer|MockObject
*/
private $observerMock;

/**
* @var Event|MockObject
*/
private $eventMock;

/**
* @var Order|MockObject
*/
private $orderMock;

/**
* @var OrderItem|MockObject
*/
private $orderItemMock;

/**
* @var Store|MockObject
*/
private $storeMock;

/**
* @var MessageInterface|MockObject
*/
private $messageMock;

/**
* @var QuoteItem|MockObject
*/
private $quoteItemMock;

/**
* Prepare environment for test
*/
public function setUp(): void
{
$this->messageFactoryMock = $this->getMockBuilder(MessageFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$this->giftMessageHelperMock = $this->createMock(MessageHelper::class);
$this->observerMock = $this->createMock(Observer::class);
$this->eventMock = $this->createPartialMock(Event::class, ['getOrderItem', 'getQuoteItem']);
$this->orderItemMock = $this->createPartialMock(
OrderItem::class,
['getOrder', 'getStoreId', 'getGiftMessageId']
);
$this->quoteItemMock = $this->createPartialMock(QuoteItem::class, ['setGiftMessageId']);
$this->orderMock = $this->createPartialMock(Order::class, ['getReordered']);
$this->storeMock = $this->createMock(Store::class);
$this->messageMock = $this->createMock(MessageModel::class);

$this->eventMock->expects($this->atLeastOnce())
->method('getOrderItem')
->willReturn($this->orderItemMock);

$this->orderItemMock->expects($this->atLeastOnce())
->method('getOrder')
->willReturn($this->orderMock);

$this->observerMock->expects($this->atLeastOnce())
->method('getEvent')
->willReturn($this->eventMock);

$objectManager = new ObjectManager($this);

$this->observer = $objectManager->getObject(
SalesEventOrderItemToQuoteItemObserver::class,
[
'messageFactory' => $this->messageFactoryMock,
'giftMessageMessage' => $this->giftMessageHelperMock
]
);
}

/**
* Test when the order is reorder
*/
public function testReorder()
{
$this->orderMock->expects($this->once())
->method('getReordered')
->willReturn(true);

$this->giftMessageHelperMock->expects($this->never())
->method('isMessagesAllowed');

$this->eventMock
->expects($this->never())
->method('getQuoteItem')
->willReturn($this->quoteItemMock);

/** Run observer */
$this->observer->execute($this->observerMock);
}

/**
* Test when the order is new reorder and gift message is not allowed
*/
public function testNewOrderWhenGiftMessageIsNotAllowed()
{
$this->orderMock->expects($this->once())
->method('getReordered')
->willReturn(false);

$this->giftMessageHelperMock->expects($this->once())
->method('isMessagesAllowed')
->willReturn(false);

$this->eventMock
->expects($this->never())
->method('getQuoteItem')
->willReturn($this->quoteItemMock);

/** Run observer */
$this->observer->execute($this->observerMock);
}

/**
* Test when the order is new reorder and gift message is allowed
*/
public function testNewOrderWhenGiftMessageIsAllowed()
{
$this->orderMock->expects($this->once())
->method('getReordered')
->willReturn(false);

$this->giftMessageHelperMock->expects($this->once())
->method('isMessagesAllowed')
->willReturn(true);

$this->eventMock
->expects($this->atLeastOnce())
->method('getQuoteItem')
->willReturn($this->quoteItemMock);

$this->orderItemMock->expects($this->once())
->method('getGiftMessageId')
->willReturn(self::STUB_MESSAGE_ID);

$this->messageFactoryMock->expects($this->once())
->method('create')
->willReturn($this->messageMock);
$this->messageMock->expects($this->once())
->method('load')
->with(self::STUB_MESSAGE_ID)
->willReturnSelf();
$this->messageMock->expects($this->once())
->method('setId')
->with(null)
->willReturnSelf();
$this->messageMock->expects($this->once())
->method('save')
->willReturnSelf();
$this->messageMock->expects($this->once())
->method('getId')
->willReturn(self::STUB_NEW_MESSAGE_ID);
$this->quoteItemMock->expects($this->once())
->method('setGiftMessageId')
->with(self::STUB_NEW_MESSAGE_ID)
->willReturnSelf();

/** Run observer */
$this->observer->execute($this->observerMock);
}
}
120 changes: 120 additions & 0 deletions app/code/Magento/GoogleAnalytics/Test/Unit/Helper/DataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\GoogleAnalytics\Test\Unit\Helper;

use Magento\GoogleAnalytics\Helper\Data as HelperData;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;

/**
* Unit test for Magento\GoogleAnalytics\Helper\Data
*/
class DataTest extends TestCase
{
/**
* @var HelperData
*/
private $helper;

/**
* @var ScopeConfigInterface|MockObject
*/
private $scopeConfigMock;

/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
->setMethods(['getValue', 'isSetFlag'])
->disableOriginalConstructor()
->getMockForAbstractClass();

$objectManager = new ObjectManager($this);
$this->helper = $objectManager->getObject(
HelperData::class,
[
'scopeConfig' => $this->scopeConfigMock
]
);
}

/**
* Test for isGoogleAnalyticsAvailable()
*
* @param string $value
* @param bool $flag
* @param bool $result
* @return void
* @dataProvider gaDataProvider
*/
public function testIsGoogleAnalyticsAvailable($value, $flag, $result): void
{
$this->scopeConfigMock->expects($this->once())
->method('getValue')
->with(HelperData::XML_PATH_ACCOUNT, ScopeInterface::SCOPE_STORE)
->willReturn($value);

$this->scopeConfigMock->expects($this->any())
->method('isSetFlag')
->with(HelperData::XML_PATH_ACTIVE, ScopeInterface::SCOPE_STORE)
->willReturn($flag);

$this->assertEquals($result, $this->helper->isGoogleAnalyticsAvailable());
}

/**
* Data provider for isGoogleAnalyticsAvailable()
*
* @return array
*/
public function gaDataProvider(): array
{
return [
['GA-XXXX', true, true],
['GA-XXXX', false, false],
['', true, false]
];
}

/**
* Test for isAnonymizedIpActive()
*
* @param string $value
* @param bool $result
* @return void
* @dataProvider yesNoDataProvider
*/
public function testIsAnonymizedIpActive($value, $result): void
{
$this->scopeConfigMock->expects($this->once())
->method('getValue')
->with(HelperData::XML_PATH_ANONYMIZE, ScopeInterface::SCOPE_STORE)
->willReturn($value);
$this->assertEquals($result, $this->helper->isAnonymizedIpActive());
}

/**
* Data provider for isAnonymizedIpActive()
*
* @return array
*/
public function yesNoDataProvider(): array
{
return [
['Yes' => '1', 'result' => true],
['No' => '0', 'result' => false]
];
}
}
Loading

0 comments on commit abac538

Please sign in to comment.