Skip to content

Commit 6298ab7

Browse files
committed
MC-31766: [AR] Report account is unavailable after URL change
1 parent 161b243 commit 6298ab7

File tree

3 files changed

+265
-12
lines changed

3 files changed

+265
-12
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Analytics\Model\Config\Backend\Enabled\SubscriptionHandler;
99
use Magento\Analytics\Model\Connector;
10+
use Magento\Framework\Exception\NotFoundException;
1011
use Magento\Framework\FlagManager;
1112
use Magento\Framework\App\Config\ReinitableConfigInterface;
1213
use Magento\Framework\App\Config\Storage\WriterInterface;
@@ -57,22 +58,24 @@ public function __construct(
5758
}
5859

5960
/**
60-
* Execute scheduled subscription operation
61+
* Execute scheduled subscription operation.
62+
*
6163
* In case of failure writes message to notifications inbox
6264
*
6365
* @return bool
66+
* @throws NotFoundException
6467
*/
6568
public function execute()
6669
{
67-
$attemptsCount = $this->flagManager->getFlagData(SubscriptionHandler::ATTEMPTS_REVERSE_COUNTER_FLAG_CODE);
70+
$attemptsCount = (int)$this->flagManager->getFlagData(SubscriptionHandler::ATTEMPTS_REVERSE_COUNTER_FLAG_CODE);
6871

69-
if (($attemptsCount === null) || ($attemptsCount <= 0)) {
72+
if ($attemptsCount <= 0) {
7073
$this->deleteAnalyticsCronExpr();
7174
$this->flagManager->deleteFlag(SubscriptionHandler::ATTEMPTS_REVERSE_COUNTER_FLAG_CODE);
7275
return false;
7376
}
7477

75-
$attemptsCount -= 1;
78+
$attemptsCount--;
7679
$this->flagManager->saveFlag(SubscriptionHandler::ATTEMPTS_REVERSE_COUNTER_FLAG_CODE, $attemptsCount);
7780
$signUpResult = $this->connector->execute('signUp');
7881
if ($signUpResult === false) {

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Analytics\Model\AnalyticsToken;
99
use Magento\Analytics\Model\Config\Backend\Baseurl\SubscriptionUpdateHandler;
1010
use Magento\Analytics\Model\Connector;
11+
use Magento\Framework\Exception\NotFoundException;
1112
use Magento\Framework\FlagManager;
1213
use Magento\Framework\App\Config\ReinitableConfigInterface;
1314
use Magento\Framework\App\Config\Storage\WriterInterface;
@@ -67,26 +68,37 @@ public function __construct(
6768
* Execute scheduled update operation
6869
*
6970
* @return bool
71+
* @throws NotFoundException
7072
*/
7173
public function execute()
7274
{
7375
$result = false;
74-
$attemptsCount = $this->flagManager
76+
$attemptsCount = (int)$this->flagManager
7577
->getFlagData(SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE);
7678

77-
if ($attemptsCount) {
78-
$attemptsCount -= 1;
79+
if (($attemptsCount >= 0) && $this->analyticsToken->isTokenExist()) {
80+
$attemptsCount--;
81+
$this->flagManager
82+
->saveFlag(SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE, $attemptsCount);
7983
$result = $this->connector->execute('update');
8084
}
8185

8286
if ($result || ($attemptsCount <= 0) || (!$this->analyticsToken->isTokenExist())) {
83-
$this->flagManager
84-
->deleteFlag(SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE);
85-
$this->flagManager->deleteFlag(SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE);
86-
$this->configWriter->delete(SubscriptionUpdateHandler::UPDATE_CRON_STRING_PATH);
87-
$this->reinitableConfig->reinit();
87+
$this->exitFromUpdateProcess();
8888
}
8989

9090
return $result;
9191
}
92+
93+
/**
94+
* Clean-up flags and refresh configuration
95+
*/
96+
private function exitFromUpdateProcess(): void
97+
{
98+
$this->flagManager
99+
->deleteFlag(SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE);
100+
$this->flagManager->deleteFlag(SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE);
101+
$this->configWriter->delete(SubscriptionUpdateHandler::UPDATE_CRON_STRING_PATH);
102+
$this->reinitableConfig->reinit();
103+
}
92104
}
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
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\Analytics\Cron;
10+
11+
use Magento\Analytics\Model\Config\Backend\Baseurl\SubscriptionUpdateHandler;
12+
use Magento\Analytics\Model\Connector\Http\Client\Curl as CurlClient;
13+
use Magento\Analytics\Model\Connector\Http\ClientInterface;
14+
use Magento\Config\Model\PreparedValueFactory;
15+
use Magento\Config\Model\ResourceModel\Config\Data as ConfigDataResource;
16+
use Magento\Framework\App\Config\ScopeConfigInterface;
17+
use Magento\Framework\FlagManager;
18+
use Magento\Store\Model\Store;
19+
use Magento\TestFramework\Helper\Bootstrap;
20+
use Magento\TestFramework\ObjectManager;
21+
22+
/**
23+
* @magentoAppArea adminhtml
24+
*/
25+
class UpdateTest extends \PHPUnit\Framework\TestCase
26+
{
27+
/**
28+
* @var ObjectManager
29+
*/
30+
private $objectManager;
31+
32+
/**
33+
* @var PreparedValueFactory
34+
*/
35+
private $preparedValueFactory;
36+
37+
/**
38+
* @var ConfigDataResource
39+
*/
40+
private $configValueResourceModel;
41+
42+
/**
43+
* @var Update
44+
*/
45+
private $updateCron;
46+
47+
/**
48+
* @var ClientInterface|\PHPUnit\Framework\MockObject\MockObject
49+
*/
50+
private $httpClient;
51+
52+
/**
53+
* @var FlagManager
54+
*/
55+
private $flagManager;
56+
57+
/**
58+
* @var ScopeConfigInterface
59+
*/
60+
private $scopeConfig;
61+
62+
/**
63+
* @return void
64+
*/
65+
protected function setUp()
66+
{
67+
$this->objectManager = Bootstrap::getObjectManager();
68+
$this->httpClient = $this->getMockBuilder(ClientInterface::class)
69+
->getMockForAbstractClass();
70+
$this->objectManager->addSharedInstance($this->httpClient, CurlClient::class);
71+
$this->preparedValueFactory = $this->objectManager->get(PreparedValueFactory::class);
72+
$this->configValueResourceModel = $this->objectManager->get(ConfigDataResource::class);
73+
$this->updateCron = $this->objectManager->get(Update::class);
74+
$this->scopeConfig = $this->objectManager->get(ScopeConfigInterface::class);
75+
$this->flagManager = $this->objectManager->get(FlagManager::class);
76+
}
77+
78+
/**
79+
* @magentoDbIsolation enabled
80+
* @magentoAppIsolation enabled
81+
* @magentoDataFixture Magento/Analytics/_files/enabled_subscription_with_invalid_token.php
82+
*
83+
*/
84+
public function testSuccessfulAttemptExecute()
85+
{
86+
$this->saveConfigValue(
87+
Store::XML_PATH_SECURE_BASE_URL,
88+
'http://store.com/'
89+
);
90+
91+
$this->mockRequestCall(201, 'URL has been changed');
92+
93+
$this->updateCron->execute();
94+
$this->assertEmpty($this->getUpdateCounterFlag());
95+
$this->assertEmpty($this->getPreviousBaseUrlFlag());
96+
$this->assertEmpty($this->getConfigValue(SubscriptionUpdateHandler::UPDATE_CRON_STRING_PATH));
97+
}
98+
99+
/**
100+
* @magentoDbIsolation enabled
101+
* @magentoAppIsolation enabled
102+
* @magentoDataFixture Magento/Analytics/_files/enabled_subscription_with_invalid_token.php
103+
*
104+
*/
105+
public function testUnsuccessfulAttemptExecute()
106+
{
107+
$this->saveConfigValue(
108+
Store::XML_PATH_SECURE_BASE_URL,
109+
'http://store.com/'
110+
);
111+
112+
$reverseCounter = $this->getUpdateCounterFlag();
113+
$this->mockRequestCall(500, 'Unauthorized access');
114+
115+
$this->updateCron->execute();
116+
$this->assertEquals($reverseCounter - 1, $this->getUpdateCounterFlag());
117+
}
118+
119+
/**
120+
* @magentoDbIsolation enabled
121+
* @magentoAppIsolation enabled
122+
* @magentoDataFixture Magento/Analytics/_files/enabled_subscription_with_invalid_token.php
123+
*
124+
*/
125+
public function testLastUnsuccessfulAttemptExecute()
126+
{
127+
$this->saveConfigValue(
128+
Store::XML_PATH_SECURE_BASE_URL,
129+
'http://store.com/'
130+
);
131+
132+
$this->setUpdateCounterValue(1);
133+
$this->mockRequestCall(500, 'Unauthorized access');
134+
135+
$this->updateCron->execute();
136+
$this->assertEmpty($this->getUpdateCounterFlag());
137+
$this->assertEmpty($this->getPreviousBaseUrlFlag());
138+
$this->assertEmpty($this->getConfigValue(SubscriptionUpdateHandler::UPDATE_CRON_STRING_PATH));
139+
}
140+
141+
/**
142+
* Save configuration value
143+
*
144+
* @param string $path The configuration path in format section/group/field_name
145+
* @param string $value The configuration value
146+
* @param string $scope The configuration scope (default, website, or store)
147+
* @return void
148+
* @throws \Magento\Framework\Exception\RuntimeException
149+
* @throws \Magento\Framework\Exception\AlreadyExistsException
150+
*/
151+
private function saveConfigValue(
152+
string $path,
153+
string $value,
154+
string $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT
155+
) {
156+
$configValue = $this->preparedValueFactory->create(
157+
$path,
158+
$value,
159+
$scope
160+
);
161+
$this->configValueResourceModel->save($configValue);
162+
}
163+
164+
/**
165+
* Get configuration value
166+
*
167+
* @param string $path
168+
* @param string $scopeType
169+
* @return mixed
170+
*/
171+
private function getConfigValue(
172+
string $path,
173+
string $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT
174+
) {
175+
return $this->scopeConfig->getValue(
176+
$path,
177+
$scopeType
178+
);
179+
}
180+
181+
/**
182+
* Get update counter flag value
183+
*
184+
* @return int|null
185+
*/
186+
private function getUpdateCounterFlag(): ?int
187+
{
188+
return $this->flagManager
189+
->getFlagData(SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE);
190+
}
191+
192+
/**
193+
* Get previous URL flag value
194+
*
195+
* @return string|null
196+
*/
197+
private function getPreviousBaseUrlFlag(): ?string
198+
{
199+
return $this->flagManager
200+
->getFlagData(SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE);
201+
}
202+
203+
/**
204+
* Set response mock for the HTTP client
205+
*
206+
* @param int $responseCode
207+
* @param string $responseMessage
208+
*/
209+
private function mockRequestCall(int $responseCode, string $responseMessage): void
210+
{
211+
$response = $this->objectManager->create(
212+
\Zend_Http_Response::class,
213+
[
214+
'code' => $responseCode,
215+
'headers' => [
216+
'Content-Type' => 'application/json'
217+
],
218+
'body' => json_encode(['message' => $responseMessage])
219+
]
220+
);
221+
222+
$this->httpClient
223+
->expects($this->once())
224+
->method('request')
225+
->willReturn($response);
226+
}
227+
228+
/**
229+
* Set value for update counter flag
230+
*
231+
* @param int $value
232+
*/
233+
private function setUpdateCounterValue(int $value): void
234+
{
235+
$this->flagManager
236+
->saveFlag(SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE, $value);
237+
}
238+
}

0 commit comments

Comments
 (0)