Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 85bdbbd

Browse files
committed
MAGETWO-72467: Move Signifyd to CE
- Add integration tests
2 parents 36c4361 + f983831 commit 85bdbbd

22 files changed

+2486
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Signifyd\Block\Adminhtml;
7+
8+
use Magento\Framework\App\RequestInterface;
9+
use Magento\Framework\ObjectManagerInterface;
10+
use Magento\Framework\View\Element\Template\Context;
11+
use Magento\Framework\View\LayoutInterface;
12+
use Magento\Sales\Model\Order;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
15+
class CaseInfoTest extends \PHPUnit\Framework\TestCase
16+
{
17+
/**
18+
* @var ObjectManagerInterface
19+
*/
20+
private $objectManager;
21+
22+
/**
23+
* @var Order
24+
*/
25+
private $order;
26+
27+
/**
28+
* @var LayoutInterface
29+
*/
30+
private $layout;
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
protected function setUp()
36+
{
37+
$this->objectManager = Bootstrap::getObjectManager();
38+
$this->order = $this->objectManager->create(Order::class);
39+
$this->layout = $this->objectManager->get(LayoutInterface::class);
40+
}
41+
42+
/**
43+
* Checks that block has contents when case entity for order is exists
44+
* even if Signifyd module is inactive.
45+
*
46+
* @magentoConfigFixture current_store fraud_protection/signifyd/active 0
47+
* @magentoDataFixture Magento/Signifyd/_files/case.php
48+
* @magentoAppArea adminhtml
49+
*/
50+
public function testModuleIsInactive()
51+
{
52+
$this->order->loadByIncrementId('100000001');
53+
54+
self::assertNotEmpty($this->getBlock()->toHtml());
55+
}
56+
57+
/**
58+
* Checks that block does not give contents
59+
* if there is no case entity created for order.
60+
*
61+
* @covers \Magento\Signifyd\Block\Adminhtml\CaseInfo::getCaseEntity
62+
* @magentoConfigFixture current_store fraud_protection/signifyd/active 1
63+
* @magentoDataFixture Magento/Signifyd/_files/order_with_customer_and_two_simple_products.php
64+
* @magentoAppArea adminhtml
65+
*/
66+
public function testCaseEntityNotExists()
67+
{
68+
$this->order->loadByIncrementId('100000001');
69+
70+
self::assertEmpty($this->getBlock()->toHtml());
71+
}
72+
73+
/**
74+
* Checks that:
75+
* - block give contents
76+
* - block contents guarantee decision field
77+
*
78+
* @covers \Magento\Signifyd\Block\Adminhtml\CaseInfo::getScoreClass
79+
* @magentoConfigFixture current_store fraud_protection/signifyd/active 1
80+
* @magentoDataFixture Magento/Signifyd/_files/case.php
81+
* @magentoAppArea adminhtml
82+
*/
83+
public function testCaseEntityExists()
84+
{
85+
$this->order->loadByIncrementId('100000001');
86+
87+
$block = $this->getBlock();
88+
self::assertNotEmpty($block->toHtml());
89+
self::assertContains((string) $block->getCaseGuaranteeDisposition(), $block->toHtml());
90+
}
91+
92+
/**
93+
* Gets block.
94+
*
95+
* @return CaseInfo
96+
*/
97+
private function getBlock()
98+
{
99+
$this->layout->addContainer('order_additional_info', 'Container');
100+
101+
/** @var CaseInfo $block */
102+
$block = $this->layout->addBlock(CaseInfo::class, 'order_case_info', 'order_additional_info');
103+
$block->setAttribute('context', $this->getContext());
104+
$block->setTemplate('Magento_Signifyd::case_info.phtml');
105+
106+
return $block;
107+
}
108+
109+
/**
110+
* Creates template context with necessary order id param.
111+
*
112+
* @return Context
113+
*/
114+
private function getContext()
115+
{
116+
/** @var RequestInterface $request */
117+
$request = $this->objectManager->get(RequestInterface::class);
118+
$request->setParams(['order_id' => $this->order->getEntityId()]);
119+
120+
return $this->objectManager->create(Context::class, ['request' => $request]);
121+
}
122+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Signifyd\Block;
7+
8+
use Magento\Framework\App\Area;
9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\View\LayoutInterface;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
13+
class FingerprintTest extends \PHPUnit\Framework\TestCase
14+
{
15+
/**
16+
* @var ObjectManager
17+
*/
18+
private $objectManager;
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
protected function setUp()
24+
{
25+
$bootstrap = Bootstrap::getInstance();
26+
$bootstrap->loadArea(Area::AREA_FRONTEND);
27+
28+
$this->objectManager = Bootstrap::getObjectManager();
29+
}
30+
31+
/**
32+
* Checks if session id attribute is present when the module is enabled.
33+
*
34+
* @magentoConfigFixture current_store fraud_protection/signifyd/active 1
35+
*/
36+
public function testSessionIdPresent()
37+
{
38+
self::assertContains('data-order-session-id', $this->getBlockContents());
39+
}
40+
41+
/**
42+
* Checks if block is an empty when the module is disabled.
43+
*
44+
* @magentoConfigFixture current_store fraud_protection/signifyd/active 0
45+
*/
46+
public function testBlockEmpty()
47+
{
48+
self::assertEmpty($this->getBlockContents());
49+
}
50+
51+
/**
52+
* Renders block contents.
53+
*
54+
* @return string
55+
*/
56+
private function getBlockContents()
57+
{
58+
$block = $this->objectManager->get(LayoutInterface::class)
59+
->createBlock(Fingerprint::class);
60+
61+
return $block->fetchView($block->getTemplateFile());
62+
}
63+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Signifyd\Controller\Webhooks;
7+
8+
use Magento\TestFramework\TestCase\AbstractController;
9+
use Magento\Signifyd\Model\SignifydGateway\Response\WebhookRequest;
10+
use Magento\Signifyd\Api\CaseRepositoryInterface;
11+
use Magento\Signifyd\Api\Data\CaseInterface;
12+
use Magento\Sales\Api\Data\OrderStatusHistoryInterface;
13+
use Magento\Sales\Api\OrderRepositoryInterface;
14+
15+
/**
16+
* Class tests handling webhook post from Signifyd service.
17+
*/
18+
class HandlerTest extends AbstractController
19+
{
20+
/**
21+
* @var string
22+
*/
23+
private static $entryPoint = 'signifyd/webhooks/handler';
24+
25+
/**
26+
* Tests handling webhook message of cases/rescore type.
27+
* Checks updated case entity and comment in order history.
28+
*
29+
* @covers \Magento\Signifyd\Controller\Webhooks\Handler::execute
30+
* @magentoConfigFixture current_store fraud_protection/signifyd/active 1
31+
* @magentoConfigFixture current_store fraud_protection/signifyd/api_key ApFZZvxGgIxuP8BazSm3v8eGN
32+
* @magentoDataFixture Magento/Signifyd/_files/case.php
33+
*/
34+
public function testExecuteSuccess()
35+
{
36+
$caseId = 123;
37+
$webhookRequest = $this->getWebhookRequest();
38+
$this->_objectManager->addSharedInstance($webhookRequest, WebhookRequest::class);
39+
40+
$this->dispatch(self::$entryPoint);
41+
42+
/** @var CaseRepositoryInterface $caseManagement */
43+
$caseRepository = $this->_objectManager->get(CaseRepositoryInterface::class);
44+
/** @var CaseInterface $caseEntity */
45+
$caseEntity = $caseRepository->getByCaseId($caseId);
46+
$orderEntityId = $caseEntity->getOrderId();
47+
48+
self::assertNotEmpty($caseEntity);
49+
self::assertEquals('2017-01-06 12:47:03', $caseEntity->getCreatedAt());
50+
self::assertEquals('2017-01-06 12:47:03', $caseEntity->getUpdatedAt());
51+
self::assertEquals('Magento', $caseEntity->getAssociatedTeam()['teamName']);
52+
self::assertEquals(true, $caseEntity->isGuaranteeEligible());
53+
self::assertEquals(CaseInterface::STATUS_OPEN, $caseEntity->getStatus());
54+
self::assertEquals($orderEntityId, $caseEntity->getOrderId());
55+
56+
/** @var OrderRepositoryInterface $orderRepository */
57+
$orderRepository = $this->_objectManager->get(OrderRepositoryInterface::class);
58+
$order = $orderRepository->get($caseEntity->getOrderId());
59+
$histories = $order->getStatusHistories();
60+
self::assertNotEmpty($histories);
61+
62+
/** @var OrderStatusHistoryInterface $caseCreationComment */
63+
$caseComment = array_pop($histories);
64+
self::assertInstanceOf(OrderStatusHistoryInterface::class, $caseComment);
65+
66+
self::assertEquals(
67+
"Case Update: New score for the order is 384. Previous score was 553.",
68+
$caseComment->getComment()
69+
);
70+
71+
$this->_objectManager->removeSharedInstance(WebhookRequest::class);
72+
}
73+
74+
/**
75+
* Tests handling webhook message of cases/test type.
76+
* Controller should response with code 200.
77+
*
78+
* @covers \Magento\Signifyd\Controller\Webhooks\Handler::execute
79+
* @magentoConfigFixture current_store fraud_protection/signifyd/active 1
80+
*/
81+
public function testExecuteTestSuccess()
82+
{
83+
$webhookRequest = $this->getTestWebhookRequest();
84+
$this->_objectManager->addSharedInstance($webhookRequest, WebhookRequest::class);
85+
$this->dispatch(self::$entryPoint);
86+
$this->assertEquals(200, $this->getResponse()->getHttpResponseCode());
87+
$this->_objectManager->removeSharedInstance(WebhookRequest::class);
88+
}
89+
90+
/**
91+
* Returns mocked WebhookRequest
92+
*
93+
* @return WebhookRequest|\PHPUnit\Framework\MockObject_MockObject
94+
*/
95+
private function getWebhookRequest()
96+
{
97+
$webhookRequest = $this->getMockBuilder(WebhookRequest::class)
98+
->disableOriginalConstructor()
99+
->getMock();
100+
$webhookRequest->expects($this->any())
101+
->method('getBody')
102+
->willReturn(file_get_contents(__DIR__ . '/../../_files/webhook_body.json'));
103+
$webhookRequest->expects($this->any())
104+
->method('getEventTopic')
105+
->willReturn('cases/rescore');
106+
$webhookRequest->expects($this->any())
107+
->method('getHash')
108+
->willReturn('m/X29RcHWPSCDPgQuSXjnyTfKISJDopcdGbVsRLeqy8=');
109+
110+
return $webhookRequest;
111+
}
112+
113+
/**
114+
* Returns mocked test WebhookRequest
115+
*
116+
* @return WebhookRequest|\PHPUnit\Framework\MockObject_MockObject
117+
*/
118+
private function getTestWebhookRequest()
119+
{
120+
$webhookRequest = $this->getMockBuilder(WebhookRequest::class)
121+
->disableOriginalConstructor()
122+
->getMock();
123+
$webhookRequest->expects($this->any())
124+
->method('getBody')
125+
->willReturn(file_get_contents(__DIR__ . '/../../_files/webhook_body.json'));
126+
$webhookRequest->expects($this->any())
127+
->method('getEventTopic')
128+
->willReturn('cases/test');
129+
$webhookRequest->expects($this->any())
130+
->method('getHash')
131+
->willReturn('wyG0r9mOmv1IqVlN6ZqJ5sgA635yKW6lbSsqlYF2b8U=');
132+
133+
return $webhookRequest;
134+
}
135+
}

0 commit comments

Comments
 (0)