Skip to content

Commit 1fb4f35

Browse files
author
olysenko
committed
Merge remote-tracking branch 'origin/MAGETWO-65001' into bugfixes
2 parents f1dc91b + 1332427 commit 1fb4f35

13 files changed

+371
-16
lines changed

app/code/Magento/Analytics/Cron/CollectData.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
namespace Magento\Analytics\Cron;
77

8-
use Magento\Analytics\Model\ExportDataHandler;
8+
use Magento\Analytics\Model\ExportDataHandlerInterface;
99
use Magento\Analytics\Model\SubscriptionStatusProvider;
1010

1111
/**
@@ -16,7 +16,7 @@ class CollectData
1616
/**
1717
* Resource for the handling of a new data collection.
1818
*
19-
* @var ExportDataHandler
19+
* @var ExportDataHandlerInterface
2020
*/
2121
private $exportDataHandler;
2222

@@ -28,11 +28,11 @@ class CollectData
2828
private $subscriptionStatus;
2929

3030
/**
31-
* @param ExportDataHandler $exportDataHandler
31+
* @param ExportDataHandlerInterface $exportDataHandler
3232
* @param SubscriptionStatusProvider $subscriptionStatus
3333
*/
3434
public function __construct(
35-
ExportDataHandler $exportDataHandler,
35+
ExportDataHandlerInterface $exportDataHandler,
3636
SubscriptionStatusProvider $subscriptionStatus
3737
) {
3838
$this->exportDataHandler = $exportDataHandler;
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Analytics\Model\Connector;
7+
8+
use Magento\Analytics\Model\AnalyticsToken;
9+
use Magento\Framework\HTTP\ZendClient;
10+
use Magento\Config\Model\Config;
11+
use Psr\Log\LoggerInterface;
12+
use Magento\Store\Model\Store;
13+
14+
/**
15+
* Command notifies MBI about that data collection was finished.
16+
*/
17+
class NotifyDataChangedCommand implements CommandInterface
18+
{
19+
/**
20+
* @var string
21+
*/
22+
private $notifyDataChangedUrlPath = 'analytics/url/notify_data_changed';
23+
24+
/**
25+
* @var AnalyticsToken
26+
*/
27+
private $analyticsToken;
28+
29+
/**
30+
* @var Http\ClientInterface
31+
*/
32+
private $httpClient;
33+
34+
/**
35+
* @var Config
36+
*/
37+
private $config;
38+
39+
/**
40+
* @var LoggerInterface
41+
*/
42+
private $logger;
43+
44+
/**
45+
* NotifyDataChangedCommand constructor.
46+
* @param AnalyticsToken $analyticsToken
47+
* @param Http\ClientInterface $httpClient
48+
* @param Config $config
49+
* @param LoggerInterface $logger
50+
*/
51+
public function __construct(
52+
AnalyticsToken $analyticsToken,
53+
Http\ClientInterface $httpClient,
54+
Config $config,
55+
LoggerInterface $logger
56+
) {
57+
$this->analyticsToken = $analyticsToken;
58+
$this->httpClient = $httpClient;
59+
$this->config = $config;
60+
$this->logger = $logger;
61+
}
62+
63+
/**
64+
* Notify MBI about that data collection was finished
65+
* @return bool
66+
*/
67+
public function execute()
68+
{
69+
$result = false;
70+
try {
71+
if ($this->analyticsToken->isTokenExist()) {
72+
$this->httpClient->request(
73+
ZendClient::POST,
74+
$this->config->getConfigDataValue($this->notifyDataChangedUrlPath),
75+
$this->getRequestJson(),
76+
['Content-Type: application/json']
77+
);
78+
$result = true;
79+
}
80+
} catch (\Exception $e) {
81+
$this->logger->critical($e);
82+
}
83+
return $result;
84+
}
85+
86+
/**
87+
* Prepares request data in JSON format.
88+
* @return string
89+
*/
90+
private function getRequestJson()
91+
{
92+
return json_encode(
93+
[
94+
"access-token" => $this->analyticsToken->getToken(),
95+
"url" => $this->config->getConfigDataValue(
96+
Store::XML_PATH_SECURE_BASE_URL
97+
),
98+
]
99+
);
100+
}
101+
}

app/code/Magento/Analytics/Model/ExportDataHandler.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* Class for the handling of a new data collection for MBI.
1717
*/
18-
class ExportDataHandler
18+
class ExportDataHandler implements ExportDataHandlerInterface
1919
{
2020
/**
2121
* Subdirectory path for all temporary files.
@@ -84,9 +84,7 @@ public function __construct(
8484
}
8585

8686
/**
87-
* Execute collecting new data for MBI.
88-
*
89-
* @return bool
87+
* @inheritdoc
9088
*/
9189
public function prepareExportData()
9290
{
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Analytics\Model;
7+
8+
/**
9+
* The interface represents the type of classes that handling of a new data collection for MBI.
10+
*/
11+
interface ExportDataHandlerInterface
12+
{
13+
/**
14+
* Execute collecting new data for MBI.
15+
*
16+
* @return bool
17+
*/
18+
public function prepareExportData();
19+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Analytics\Model;
7+
8+
use Magento\Analytics\Model\ExportDataHandler;
9+
10+
/**
11+
* Class which add notification behaviour to classes that handling of a new data collection for MBI.
12+
*/
13+
class ExportDataHandlerNotification implements ExportDataHandlerInterface
14+
{
15+
/**
16+
* @var ExportDataHandler
17+
*/
18+
private $exportDataHandler;
19+
20+
/**
21+
* @var Connector
22+
*/
23+
private $analyticsConnector;
24+
25+
/**
26+
* ExportDataHandlerNotification constructor.
27+
*
28+
* @param ExportDataHandlerInterface $exportDataHandler
29+
* @param Connector $connector
30+
*/
31+
public function __construct(ExportDataHandler $exportDataHandler, Connector $connector)
32+
{
33+
$this->exportDataHandler = $exportDataHandler;
34+
$this->analyticsConnector = $connector;
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
* Execute notification command.
40+
*
41+
* @return bool
42+
*/
43+
public function prepareExportData()
44+
{
45+
$result = $this->exportDataHandler->prepareExportData();
46+
$this->analyticsConnector->execute('notifyDataChanged');
47+
return $result;
48+
}
49+
}

app/code/Magento/Analytics/Model/LinkProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __construct(
5252
public function get()
5353
{
5454
$fileInfo = $this->fileInfoManager->load();
55-
if ($fileInfo->getPath() === null || $fileInfo->getInitializationVector() === null) {
55+
if (!$fileInfo->getPath() || !$fileInfo->getInitializationVector()) {
5656
throw new Exception(__('File is not ready yet.'), 0, Exception::HTTP_NOT_FOUND);
5757
}
5858
$link = $this->linkInterfaceFactory->create();

app/code/Magento/Analytics/Test/Unit/Cron/CollectDataTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Magento\Analytics\Test\Unit\Cron;
77

88
use Magento\Analytics\Cron\CollectData;
9-
use Magento\Analytics\Model\ExportDataHandler;
9+
use Magento\Analytics\Model\ExportDataHandlerInterface;
1010
use Magento\Analytics\Model\SubscriptionStatusProvider;
1111
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1212

@@ -16,7 +16,7 @@
1616
class CollectDataTest extends \PHPUnit_Framework_TestCase
1717
{
1818
/**
19-
* @var ExportDataHandler|\PHPUnit_Framework_MockObject_MockObject
19+
* @var ExportDataHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
2020
*/
2121
private $exportDataHandlerMock;
2222

@@ -40,9 +40,8 @@ class CollectDataTest extends \PHPUnit_Framework_TestCase
4040
*/
4141
protected function setUp()
4242
{
43-
$this->exportDataHandlerMock = $this->getMockBuilder(ExportDataHandler::class)
44-
->disableOriginalConstructor()
45-
->getMock();
43+
$this->exportDataHandlerMock = $this->getMockBuilder(ExportDataHandlerInterface::class)
44+
->getMockForAbstractClass();
4645

4746
$this->subscriptionStatusMock = $this->getMockBuilder(SubscriptionStatusProvider::class)
4847
->disableOriginalConstructor()
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Analytics\Test\Unit\Model\Connector;
7+
8+
use Magento\Analytics\Model\AnalyticsToken;
9+
use Magento\Framework\HTTP\ZendClient;
10+
use Magento\Config\Model\Config;
11+
use Psr\Log\LoggerInterface;
12+
use Magento\Analytics\Model\Connector\NotifyDataChangedCommand;
13+
use Magento\Analytics\Model\Connector\Http\ClientInterface;
14+
15+
class NotifyDataChangedCommandTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* @var NotifyDataChangedCommand
19+
*/
20+
private $notifyDataChangedCommand;
21+
22+
/**
23+
* @var AnalyticsToken|\PHPUnit_Framework_MockObject_MockObject
24+
*/
25+
private $analyticsTokenMock;
26+
27+
/**
28+
* @var ClientInterface|\PHPUnit_Framework_MockObject_MockObject
29+
*/
30+
private $httpClientMock;
31+
32+
/**
33+
* @var Config|\PHPUnit_Framework_MockObject_MockObject
34+
*/
35+
public $configMock;
36+
37+
/**
38+
* @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
39+
*/
40+
private $loggerMock;
41+
42+
/**
43+
* @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
44+
*/
45+
private $responseMock;
46+
47+
protected function setUp()
48+
{
49+
$this->analyticsTokenMock = $this->getMockBuilder(AnalyticsToken::class)
50+
->disableOriginalConstructor()
51+
->getMock();
52+
53+
$this->httpClientMock = $this->getMockBuilder(ClientInterface::class)
54+
->disableOriginalConstructor()
55+
->getMock();
56+
57+
$this->configMock = $this->getMockBuilder(Config::class)
58+
->disableOriginalConstructor()
59+
->getMock();
60+
61+
$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
62+
->disableOriginalConstructor()
63+
->getMock();
64+
65+
$this->responseMock = $this->getMockBuilder(\Zend_Http_Response::class)
66+
->disableOriginalConstructor()
67+
->getMock();
68+
69+
$this->notifyDataChangedCommand = new NotifyDataChangedCommand(
70+
$this->analyticsTokenMock,
71+
$this->httpClientMock,
72+
$this->configMock,
73+
$this->loggerMock
74+
);
75+
}
76+
77+
public function testExecuteSuccess()
78+
{
79+
$configVal = "Config val";
80+
$token = "Secret token!";
81+
$requestJson = sprintf('{"access-token":"%s","url":"%s"}', $token, $configVal);
82+
$this->analyticsTokenMock->expects($this->once())
83+
->method('isTokenExist')
84+
->willReturn(true);
85+
$this->configMock->expects($this->any())
86+
->method('getConfigDataValue')
87+
->willReturn($configVal);
88+
$this->analyticsTokenMock->expects($this->once())
89+
->method('getToken')
90+
->willReturn($token);
91+
$this->httpClientMock->expects($this->once())
92+
->method('request')
93+
->with(
94+
ZendClient::POST,
95+
$configVal,
96+
$requestJson,
97+
['Content-Type: application/json']
98+
)->willReturn($this->responseMock);
99+
$this->assertTrue($this->notifyDataChangedCommand->execute());
100+
}
101+
102+
public function testExecuteWithoutToken()
103+
{
104+
$this->analyticsTokenMock->expects($this->once())
105+
->method('isTokenExist')
106+
->willReturn(false);
107+
$this->assertFalse($this->notifyDataChangedCommand->execute());
108+
}
109+
}

0 commit comments

Comments
 (0)