diff --git a/app/code/Magento/Analytics/Controller/Adminhtml/Reports/Show.php b/app/code/Magento/Analytics/Controller/Adminhtml/Reports/Show.php index 7f0aade798e18..1b0e5c92420de 100644 --- a/app/code/Magento/Analytics/Controller/Adminhtml/Reports/Show.php +++ b/app/code/Magento/Analytics/Controller/Adminhtml/Reports/Show.php @@ -5,6 +5,7 @@ */ namespace Magento\Analytics\Controller\Adminhtml\Reports; +use Magento\Analytics\Model\Exception\State\SubscriptionUpdateException; use Magento\Analytics\Model\ReportUrlProvider; use Magento\Backend\App\Action; use Magento\Backend\App\Action\Context; @@ -55,6 +56,9 @@ public function execute() $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); try { $resultRedirect->setUrl($this->reportUrlProvider->getUrl()); + } catch (SubscriptionUpdateException $e) { + $this->getMessageManager()->addNoticeMessage($e->getMessage()); + $resultRedirect->setPath('adminhtml'); } catch (LocalizedException $e) { $this->getMessageManager()->addExceptionMessage($e, $e->getMessage()); $resultRedirect->setPath('adminhtml'); diff --git a/app/code/Magento/Analytics/Cron/Update.php b/app/code/Magento/Analytics/Cron/Update.php index 36e6c3e59e5c7..9062a7bac7551 100644 --- a/app/code/Magento/Analytics/Cron/Update.php +++ b/app/code/Magento/Analytics/Cron/Update.php @@ -5,14 +5,14 @@ */ namespace Magento\Analytics\Cron; +use Magento\Analytics\Model\AnalyticsToken; +use Magento\Analytics\Model\Config\Backend\Baseurl\SubscriptionUpdateHandler; use Magento\Analytics\Model\Connector; -use Magento\Analytics\Model\Plugin\BaseUrlConfigPlugin; use Magento\Framework\FlagManager; use Magento\Framework\App\Config\ReinitableConfigInterface; use Magento\Framework\App\Config\Storage\WriterInterface; /** - * Class Update * Executes by cron schedule in case base url was changed */ class Update @@ -28,8 +28,6 @@ class Update private $configWriter; /** - * Reinitable Config Model. - * * @var ReinitableConfigInterface */ private $reinitableConfig; @@ -40,22 +38,29 @@ class Update private $flagManager; /** - * Update constructor. + * @var AnalyticsToken + */ + private $analyticsToken; + + /** * @param Connector $connector * @param WriterInterface $configWriter * @param ReinitableConfigInterface $reinitableConfig * @param FlagManager $flagManager + * @param AnalyticsToken $analyticsToken */ public function __construct( Connector $connector, WriterInterface $configWriter, ReinitableConfigInterface $reinitableConfig, - FlagManager $flagManager + FlagManager $flagManager, + AnalyticsToken $analyticsToken ) { $this->connector = $connector; $this->configWriter = $configWriter; $this->reinitableConfig = $reinitableConfig; $this->flagManager = $flagManager; + $this->analyticsToken = $analyticsToken; } /** @@ -65,13 +70,23 @@ public function __construct( */ public function execute() { - $updateResult = $this->connector->execute('update'); - if ($updateResult === false) { - return false; + $result = false; + $attemptsCount = $this->flagManager + ->getFlagData(SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE); + + if ($attemptsCount) { + $attemptsCount -= 1; + $result = $this->connector->execute('update'); + } + + if ($result || ($attemptsCount <= 0) || (!$this->analyticsToken->isTokenExist())) { + $this->flagManager + ->deleteFlag(SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE); + $this->flagManager->deleteFlag(SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE); + $this->configWriter->delete(SubscriptionUpdateHandler::UPDATE_CRON_STRING_PATH); + $this->reinitableConfig->reinit(); } - $this->configWriter->delete(BaseUrlConfigPlugin::UPDATE_CRON_STRING_PATH); - $this->flagManager->deleteFlag(BaseUrlConfigPlugin::OLD_BASE_URL_FLAG_CODE); - $this->reinitableConfig->reinit(); - return true; + + return $result; } } diff --git a/app/code/Magento/Analytics/Model/Config/Backend/Baseurl/SubscriptionUpdateHandler.php b/app/code/Magento/Analytics/Model/Config/Backend/Baseurl/SubscriptionUpdateHandler.php new file mode 100644 index 0000000000000..6e6f008d49f7e --- /dev/null +++ b/app/code/Magento/Analytics/Model/Config/Backend/Baseurl/SubscriptionUpdateHandler.php @@ -0,0 +1,107 @@ +analyticsToken = $analyticsToken; + $this->flagManager = $flagManager; + $this->reinitableConfig = $reinitableConfig; + $this->configWriter = $configWriter; + } + + /** + * Activate process of subscription update handling. + * + * @param string $url + * @return bool + */ + public function processUrlUpdate(string $url) + { + if ($this->analyticsToken->isTokenExist()) { + if (!$this->flagManager->getFlagData(self::PREVIOUS_BASE_URL_FLAG_CODE)) { + $this->flagManager->saveFlag(self::PREVIOUS_BASE_URL_FLAG_CODE, $url); + } + + $this->flagManager + ->saveFlag(self::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE, $this->attemptsInitValue); + $this->configWriter->save(self::UPDATE_CRON_STRING_PATH, $this->cronExpression); + $this->reinitableConfig->reinit(); + } + + return true; + } +} diff --git a/app/code/Magento/Analytics/Model/Connector/NotifyDataChangedCommand.php b/app/code/Magento/Analytics/Model/Connector/NotifyDataChangedCommand.php index 177ee64c82c5b..f1a8ea6460f9d 100644 --- a/app/code/Magento/Analytics/Model/Connector/NotifyDataChangedCommand.php +++ b/app/code/Magento/Analytics/Model/Connector/NotifyDataChangedCommand.php @@ -6,8 +6,8 @@ namespace Magento\Analytics\Model\Connector; use Magento\Analytics\Model\AnalyticsToken; +use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\HTTP\ZendClient; -use Magento\Config\Model\Config; use Psr\Log\LoggerInterface; use Magento\Store\Model\Store; use Magento\Analytics\Model\Connector\Http\ResponseResolver; @@ -33,7 +33,7 @@ class NotifyDataChangedCommand implements CommandInterface private $httpClient; /** - * @var Config + * @var ScopeConfigInterface */ private $config; @@ -51,14 +51,14 @@ class NotifyDataChangedCommand implements CommandInterface * NotifyDataChangedCommand constructor. * @param AnalyticsToken $analyticsToken * @param Http\ClientInterface $httpClient - * @param Config $config + * @param ScopeConfigInterface $config * @param ResponseResolver $responseResolver * @param LoggerInterface $logger */ public function __construct( AnalyticsToken $analyticsToken, Http\ClientInterface $httpClient, - Config $config, + ScopeConfigInterface $config, ResponseResolver $responseResolver, LoggerInterface $logger ) { @@ -80,16 +80,14 @@ public function execute() if ($this->analyticsToken->isTokenExist()) { $response = $this->httpClient->request( ZendClient::POST, - $this->config->getConfigDataValue($this->notifyDataChangedUrlPath), + $this->config->getValue($this->notifyDataChangedUrlPath), [ "access-token" => $this->analyticsToken->getToken(), - "url" => $this->config->getConfigDataValue( - Store::XML_PATH_SECURE_BASE_URL - ), + "url" => $this->config->getValue(Store::XML_PATH_SECURE_BASE_URL), ] ); $result = $this->responseResolver->getResult($response); } - return $result; + return (bool)$result; } } diff --git a/app/code/Magento/Analytics/Model/Connector/SignUpCommand.php b/app/code/Magento/Analytics/Model/Connector/SignUpCommand.php index 367b46de17c4b..a1f23637e04b1 100644 --- a/app/code/Magento/Analytics/Model/Connector/SignUpCommand.php +++ b/app/code/Magento/Analytics/Model/Connector/SignUpCommand.php @@ -8,7 +8,7 @@ use Magento\Analytics\Model\AnalyticsToken; use Magento\Analytics\Model\Connector\Http\ResponseResolver; use Magento\Analytics\Model\IntegrationManager; -use Magento\Config\Model\Config; +use Magento\Framework\App\Config\ScopeConfigInterface; use Psr\Log\LoggerInterface; use Magento\Framework\HTTP\ZendClient; use Magento\Store\Model\Store; @@ -36,7 +36,7 @@ class SignUpCommand implements CommandInterface private $integrationManager; /** - * @var Config + * @var ScopeConfigInterface */ private $config; @@ -60,7 +60,7 @@ class SignUpCommand implements CommandInterface * * @param AnalyticsToken $analyticsToken * @param IntegrationManager $integrationManager - * @param Config $config + * @param ScopeConfigInterface $config * @param Http\ClientInterface $httpClient * @param LoggerInterface $logger * @param ResponseResolver $responseResolver @@ -68,7 +68,7 @@ class SignUpCommand implements CommandInterface public function __construct( AnalyticsToken $analyticsToken, IntegrationManager $integrationManager, - Config $config, + ScopeConfigInterface $config, Http\ClientInterface $httpClient, LoggerInterface $logger, ResponseResolver $responseResolver @@ -101,12 +101,10 @@ public function execute() $this->integrationManager->activateIntegration(); $response = $this->httpClient->request( ZendClient::POST, - $this->config->getConfigDataValue($this->signUpUrlPath), + $this->config->getValue($this->signUpUrlPath), [ "token" => $integrationToken->getData('token'), - "url" => $this->config->getConfigDataValue( - Store::XML_PATH_SECURE_BASE_URL - ) + "url" => $this->config->getValue(Store::XML_PATH_SECURE_BASE_URL), ] ); @@ -121,6 +119,6 @@ public function execute() } } - return $result; + return (bool)$result; } } diff --git a/app/code/Magento/Analytics/Model/Connector/UpdateCommand.php b/app/code/Magento/Analytics/Model/Connector/UpdateCommand.php index a2d64a98e0409..8f05f1107e87e 100644 --- a/app/code/Magento/Analytics/Model/Connector/UpdateCommand.php +++ b/app/code/Magento/Analytics/Model/Connector/UpdateCommand.php @@ -6,9 +6,9 @@ namespace Magento\Analytics\Model\Connector; use Magento\Analytics\Model\AnalyticsToken; +use Magento\Analytics\Model\Config\Backend\Baseurl\SubscriptionUpdateHandler; use Magento\Analytics\Model\Connector\Http\ResponseResolver; -use Magento\Analytics\Model\Plugin\BaseUrlConfigPlugin; -use Magento\Config\Model\Config; +use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\FlagManager; use Magento\Framework\HTTP\ZendClient; use Magento\Store\Model\Store; @@ -36,7 +36,7 @@ class UpdateCommand implements CommandInterface private $httpClient; /** - * @var Config + * @var ScopeConfigInterface */ private $config; @@ -58,7 +58,7 @@ class UpdateCommand implements CommandInterface /** * @param AnalyticsToken $analyticsToken * @param Http\ClientInterface $httpClient - * @param Config $config + * @param ScopeConfigInterface $config * @param LoggerInterface $logger * @param FlagManager $flagManager * @param ResponseResolver $responseResolver @@ -66,7 +66,7 @@ class UpdateCommand implements CommandInterface public function __construct( AnalyticsToken $analyticsToken, Http\ClientInterface $httpClient, - Config $config, + ScopeConfigInterface $config, LoggerInterface $logger, FlagManager $flagManager, ResponseResolver $responseResolver @@ -90,12 +90,11 @@ public function execute() if ($this->analyticsToken->isTokenExist()) { $response = $this->httpClient->request( ZendClient::PUT, - $this->config->getConfigDataValue($this->updateUrlPath), + $this->config->getValue($this->updateUrlPath), [ - "url" => $this->flagManager->getFlagData(BaseUrlConfigPlugin::OLD_BASE_URL_FLAG_CODE), - "new-url" => $this->config->getConfigDataValue( - Store::XML_PATH_SECURE_BASE_URL - ), + "url" => $this->flagManager + ->getFlagData(SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE), + "new-url" => $this->config->getValue(Store::XML_PATH_SECURE_BASE_URL), "access-token" => $this->analyticsToken->getToken(), ] ); @@ -110,6 +109,6 @@ public function execute() } } - return $result; + return (bool)$result; } } diff --git a/app/code/Magento/Analytics/Model/Exception/State/SubscriptionUpdateException.php b/app/code/Magento/Analytics/Model/Exception/State/SubscriptionUpdateException.php new file mode 100644 index 0000000000000..5d127037afea9 --- /dev/null +++ b/app/code/Magento/Analytics/Model/Exception/State/SubscriptionUpdateException.php @@ -0,0 +1,17 @@ +flagManager = $flagManager; - $this->subscriptionStatusProvider = $subscriptionStatusProvider; - $this->configWriter = $configWriter; + $this->subscriptionUpdateHandler = $subscriptionUpdateHandler; } /** - * Sets update analytics cron job if base url was changed. + * Add additional handling after config value was saved. * - * @param \Magento\Config\Model\Config\Backend\Baseurl $subject - * @param \Magento\Config\Model\Config\Backend\Baseurl $result - * @return \Magento\Config\Model\Config\Backend\Baseurl + * @param Value $subject + * @param Value $result + * @return Value * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterAfterSave( - \Magento\Config\Model\Config\Backend\Baseurl $subject, - \Magento\Config\Model\Config\Backend\Baseurl $result + Value $subject, + Value $result ) { - if (!$result->isValueChanged()) { - return $result; - } - if ($this->isPluginApplicable($result)) { - $this->flagManager->saveFlag(static::OLD_BASE_URL_FLAG_CODE, $result->getOldValue()); - $this->configWriter->save(self::UPDATE_CRON_STRING_PATH, $this->cronExpression); + $this->subscriptionUpdateHandler->processUrlUpdate($result->getOldValue()); } return $result; } /** - * @param \Magento\Config\Model\Config\Backend\Baseurl $result - * + * @param Value $result * @return bool */ - private function isPluginApplicable(\Magento\Config\Model\Config\Backend\Baseurl $result) + private function isPluginApplicable(Value $result) { - return $result->getData('path') === \Magento\Store\Model\Store::XML_PATH_SECURE_BASE_URL - && $this->subscriptionStatusProvider->getStatus() === SubscriptionStatusProvider::ENABLED; + return $result->isValueChanged() + && ($result->getPath() === Store::XML_PATH_SECURE_BASE_URL) + && ($result->getScope() === ScopeConfigInterface::SCOPE_TYPE_DEFAULT); } } diff --git a/app/code/Magento/Analytics/Model/ReportUrlProvider.php b/app/code/Magento/Analytics/Model/ReportUrlProvider.php index b72374f4d9f62..e7fdf6f9e8132 100644 --- a/app/code/Magento/Analytics/Model/ReportUrlProvider.php +++ b/app/code/Magento/Analytics/Model/ReportUrlProvider.php @@ -5,8 +5,11 @@ */ namespace Magento\Analytics\Model; +use Magento\Analytics\Model\Config\Backend\Baseurl\SubscriptionUpdateHandler; use Magento\Analytics\Model\Connector\OTPRequest; +use Magento\Analytics\Model\Exception\State\SubscriptionUpdateException; use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Framework\FlagManager; /** * Provide URL on resource with reports. @@ -32,6 +35,11 @@ class ReportUrlProvider */ private $config; + /** + * @var FlagManager + */ + private $flagManager; + /** * Path to config value with URL which provide reports. * @@ -43,24 +51,35 @@ class ReportUrlProvider * @param AnalyticsToken $analyticsToken * @param OTPRequest $otpRequest * @param ScopeConfigInterface $config + * @param FlagManager $flagManager */ public function __construct( AnalyticsToken $analyticsToken, OTPRequest $otpRequest, - ScopeConfigInterface $config + ScopeConfigInterface $config, + FlagManager $flagManager ) { $this->analyticsToken = $analyticsToken; $this->otpRequest = $otpRequest; $this->config = $config; + $this->flagManager = $flagManager; } /** * Provide URL on resource with reports. * * @return string + * @throws SubscriptionUpdateException */ public function getUrl() { + if ($this->flagManager->getFlagData(SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE)) { + throw new SubscriptionUpdateException(__( + 'Your Base URL has been changed and your reports are being updated. ' + . 'Advanced Reporting will be available once this change has been processed. Please try again later.' + )); + } + $url = $this->config->getValue($this->urlReportConfigPath); if ($this->analyticsToken->isTokenExist()) { $otp = $this->otpRequest->call(); diff --git a/app/code/Magento/Analytics/Model/SubscriptionStatusProvider.php b/app/code/Magento/Analytics/Model/SubscriptionStatusProvider.php index 8fe548013c206..1dd831a672faa 100644 --- a/app/code/Magento/Analytics/Model/SubscriptionStatusProvider.php +++ b/app/code/Magento/Analytics/Model/SubscriptionStatusProvider.php @@ -5,6 +5,7 @@ */ namespace Magento\Analytics\Model; +use Magento\Analytics\Model\Config\Backend\Baseurl\SubscriptionUpdateHandler; use Magento\Analytics\Model\Config\Backend\Enabled\SubscriptionHandler; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\FlagManager; @@ -93,6 +94,10 @@ public function getStatus() public function getStatusForEnabledSubscription() { $status = static::ENABLED; + if ($this->flagManager->getFlagData(SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE)) { + $status = self::PENDING; + } + if (!$this->analyticsToken->isTokenExist()) { $status = static::PENDING; if ($this->flagManager->getFlagData(SubscriptionHandler::ATTEMPTS_REVERSE_COUNTER_FLAG_CODE) === null) { diff --git a/app/code/Magento/Analytics/Test/Unit/Controller/Adminhtml/Reports/ShowTest.php b/app/code/Magento/Analytics/Test/Unit/Controller/Adminhtml/Reports/ShowTest.php index eaab952178daa..99de92cc63905 100644 --- a/app/code/Magento/Analytics/Test/Unit/Controller/Adminhtml/Reports/ShowTest.php +++ b/app/code/Magento/Analytics/Test/Unit/Controller/Adminhtml/Reports/ShowTest.php @@ -6,6 +6,7 @@ namespace Magento\Analytics\Test\Unit\Controller\Adminhtml\Reports; use Magento\Analytics\Controller\Adminhtml\Reports\Show; +use Magento\Analytics\Model\Exception\State\SubscriptionUpdateException; use Magento\Analytics\Model\ReportUrlProvider; use Magento\Framework\Controller\Result\Redirect; use Magento\Framework\Controller\ResultFactory; @@ -152,4 +153,33 @@ public function executeWithExceptionDataProvider() 'ExecuteWithException' => [new \Exception('TestMessage')], ]; } + + /** + * @return void + */ + public function testExecuteWithSubscriptionUpdateException() + { + $exception = new SubscriptionUpdateException(__('TestMessage')); + $this->resultFactoryMock + ->expects($this->once()) + ->method('create') + ->with(ResultFactory::TYPE_REDIRECT) + ->willReturn($this->redirectMock); + $this->reportUrlProviderMock + ->expects($this->once()) + ->method('getUrl') + ->with() + ->willThrowException($exception); + $this->messageManagerMock + ->expects($this->once()) + ->method('addNoticeMessage') + ->with($exception->getMessage()) + ->willReturnSelf(); + $this->redirectMock + ->expects($this->once()) + ->method('setPath') + ->with('adminhtml') + ->willReturnSelf(); + $this->assertSame($this->redirectMock, $this->showController->execute()); + } } diff --git a/app/code/Magento/Analytics/Test/Unit/Cron/UpdateTest.php b/app/code/Magento/Analytics/Test/Unit/Cron/UpdateTest.php index 9a5355de62a5c..23ba59a90ce7b 100644 --- a/app/code/Magento/Analytics/Test/Unit/Cron/UpdateTest.php +++ b/app/code/Magento/Analytics/Test/Unit/Cron/UpdateTest.php @@ -6,8 +6,9 @@ namespace Magento\Analytics\Test\Unit\Cron; use Magento\Analytics\Cron\Update; +use Magento\Analytics\Model\AnalyticsToken; +use Magento\Analytics\Model\Config\Backend\Baseurl\SubscriptionUpdateHandler; use Magento\Analytics\Model\Connector; -use Magento\Analytics\Model\Plugin\BaseUrlConfigPlugin; use Magento\Framework\App\Config\ReinitableConfigInterface; use Magento\Framework\App\Config\Storage\WriterInterface; use Magento\Framework\FlagManager; @@ -37,6 +38,11 @@ class UpdateTest extends \PHPUnit_Framework_TestCase */ private $reinitableConfigMock; + /** + * @var AnalyticsToken|\PHPUnit_Framework_MockObject_MockObject + */ + private $analyticsTokenMock; + /** * @var Update */ @@ -56,38 +62,153 @@ protected function setUp() $this->reinitableConfigMock = $this->getMockBuilder(ReinitableConfigInterface::class) ->disableOriginalConstructor() ->getMock(); + $this->analyticsTokenMock = $this->getMockBuilder(AnalyticsToken::class) + ->disableOriginalConstructor() + ->getMock(); $this->update = new Update( $this->connectorMock, $this->configWriterMock, $this->reinitableConfigMock, - $this->flagManagerMock + $this->flagManagerMock, + $this->analyticsTokenMock ); } - public function testExecute() + /** + * @return void + */ + public function testExecuteWithoutToken() { - $this->connectorMock->expects($this->once()) + $this->flagManagerMock + ->method('getFlagData') + ->with(SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE) + ->willReturn(10); + $this->connectorMock + ->expects($this->once()) ->method('execute') ->with('update') - ->willReturn(true); - $this->configWriterMock->expects($this->once()) - ->method('delete') - ->with(BaseUrlConfigPlugin::UPDATE_CRON_STRING_PATH); - $this->flagManagerMock->expects($this->once()) + ->willReturn(false); + $this->analyticsTokenMock + ->expects($this->once()) + ->method('isTokenExist') + ->willReturn(false); + $this->addFinalOutputAsserts(); + $this->assertFalse($this->update->execute()); + } + + /** + * @param bool $isExecuted + */ + private function addFinalOutputAsserts(bool $isExecuted = true) + { + $this->flagManagerMock + ->expects($this->exactly(2 * $isExecuted)) ->method('deleteFlag') - ->with(BaseUrlConfigPlugin::OLD_BASE_URL_FLAG_CODE); - $this->reinitableConfigMock->expects($this->once()) - ->method('reinit'); - $this->assertTrue($this->update->execute()); + ->withConsecutive( + [SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE], + [SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE] + ); + $this->configWriterMock + ->expects($this->exactly((int)$isExecuted)) + ->method('delete') + ->with(SubscriptionUpdateHandler::UPDATE_CRON_STRING_PATH); + $this->reinitableConfigMock + ->expects($this->exactly((int)$isExecuted)) + ->method('reinit') + ->with(); } - public function testExecuteUnsuccess() + /** + * @param $counterData + * @return void + * @dataProvider executeWithEmptyReverseCounterDataProvider + */ + public function testExecuteWithEmptyReverseCounter($counterData) { - $this->connectorMock->expects($this->once()) + $this->flagManagerMock + ->method('getFlagData') + ->with(SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE) + ->willReturn($counterData); + $this->connectorMock + ->expects($this->never()) ->method('execute') ->with('update') ->willReturn(false); + $this->analyticsTokenMock + ->method('isTokenExist') + ->willReturn(true); + $this->addFinalOutputAsserts(); $this->assertFalse($this->update->execute()); } + + /** + * Provides empty states of the reverse counter. + * + * @return array + */ + public function executeWithEmptyReverseCounterDataProvider() + { + return [ + [null], + [0] + ]; + } + + /** + * @param int $reverseCount + * @param bool $commandResult + * @param bool $finalConditionsIsExpected + * @param bool $functionResult + * @return void + * @dataProvider executeRegularScenarioDataProvider + */ + public function testExecuteRegularScenario( + int $reverseCount, + bool $commandResult, + bool $finalConditionsIsExpected, + bool $functionResult + ) { + $this->flagManagerMock + ->method('getFlagData') + ->with(SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE) + ->willReturn($reverseCount); + $this->connectorMock + ->expects($this->once()) + ->method('execute') + ->with('update') + ->willReturn($commandResult); + $this->analyticsTokenMock + ->method('isTokenExist') + ->willReturn(true); + $this->addFinalOutputAsserts($finalConditionsIsExpected); + $this->assertSame($functionResult, $this->update->execute()); + } + + /** + * @return array + */ + public function executeRegularScenarioDataProvider() + { + return [ + 'The last attempt with command execution result False' => [ + 'Reverse count' => 1, + 'Command result' => false, + 'Executed final output conditions' => true, + 'Function result' => false, + ], + 'Not the last attempt with command execution result False' => [ + 'Reverse count' => 10, + 'Command result' => false, + 'Executed final output conditions' => false, + 'Function result' => false, + ], + 'Command execution result True' => [ + 'Reverse count' => 10, + 'Command result' => true, + 'Executed final output conditions' => true, + 'Function result' => true, + ], + ]; + } } diff --git a/app/code/Magento/Analytics/Test/Unit/Model/Config/Backend/Baseurl/SubscriptionUpdateHandlerTest.php b/app/code/Magento/Analytics/Test/Unit/Model/Config/Backend/Baseurl/SubscriptionUpdateHandlerTest.php new file mode 100644 index 0000000000000..865ad236fc057 --- /dev/null +++ b/app/code/Magento/Analytics/Test/Unit/Model/Config/Backend/Baseurl/SubscriptionUpdateHandlerTest.php @@ -0,0 +1,178 @@ +reinitableConfigMock = $this->getMockBuilder(ReinitableConfigInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->analyticsTokenMock = $this->getMockBuilder(AnalyticsToken::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->flagManagerMock = $this->getMockBuilder(FlagManager::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->configWriterMock = $this->getMockBuilder(WriterInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->objectManagerHelper = new ObjectManagerHelper($this); + + $this->subscriptionUpdateHandler = $this->objectManagerHelper->getObject( + SubscriptionUpdateHandler::class, + [ + 'reinitableConfig' => $this->reinitableConfigMock, + 'analyticsToken' => $this->analyticsTokenMock, + 'flagManager' => $this->flagManagerMock, + 'configWriter' => $this->configWriterMock, + ] + ); + } + + /** + * @return void + */ + public function testTokenDoesNotExist() + { + $this->analyticsTokenMock + ->expects($this->once()) + ->method('isTokenExist') + ->with() + ->willReturn(false); + $this->flagManagerMock + ->expects($this->never()) + ->method('saveFlag'); + $this->configWriterMock + ->expects($this->never()) + ->method('save'); + $this->assertTrue($this->subscriptionUpdateHandler->processUrlUpdate('http://store.com')); + } + + /** + * @return void + */ + public function testTokenAndPreviousBaseUrlExist() + { + $url = 'https://store.com'; + $this->analyticsTokenMock + ->expects($this->once()) + ->method('isTokenExist') + ->with() + ->willReturn(true); + $this->flagManagerMock + ->expects($this->once()) + ->method('getFlagData') + ->with(SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE) + ->willReturn(true); + $this->flagManagerMock + ->expects($this->once()) + ->method('saveFlag') + ->withConsecutive( + [SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE, $this->attemptsInitValue], + [SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE, $url] + ); + $this->configWriterMock + ->expects($this->once()) + ->method('save') + ->with(SubscriptionUpdateHandler::UPDATE_CRON_STRING_PATH, $this->cronExpression); + $this->reinitableConfigMock + ->expects($this->once()) + ->method('reinit') + ->with(); + $this->assertTrue($this->subscriptionUpdateHandler->processUrlUpdate($url)); + } + + /** + * @return void + */ + public function testTokenExistAndWithoutPreviousBaseUrl() + { + $url = 'https://store.com'; + $this->analyticsTokenMock + ->expects($this->once()) + ->method('isTokenExist') + ->with() + ->willReturn(true); + $this->flagManagerMock + ->expects($this->once()) + ->method('getFlagData') + ->with(SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE) + ->willReturn(false); + $this->flagManagerMock + ->expects($this->exactly(2)) + ->method('saveFlag') + ->withConsecutive( + [SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE, $url], + [SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE, $this->attemptsInitValue] + ); + $this->configWriterMock + ->expects($this->once()) + ->method('save') + ->with(SubscriptionUpdateHandler::UPDATE_CRON_STRING_PATH, $this->cronExpression); + $this->reinitableConfigMock + ->expects($this->once()) + ->method('reinit') + ->with(); + $this->assertTrue($this->subscriptionUpdateHandler->processUrlUpdate($url)); + } +} diff --git a/app/code/Magento/Analytics/Test/Unit/Model/Connector/NotifyDataChangedCommandTest.php b/app/code/Magento/Analytics/Test/Unit/Model/Connector/NotifyDataChangedCommandTest.php index a30211c7f3475..5b86dd6557d69 100644 --- a/app/code/Magento/Analytics/Test/Unit/Model/Connector/NotifyDataChangedCommandTest.php +++ b/app/code/Magento/Analytics/Test/Unit/Model/Connector/NotifyDataChangedCommandTest.php @@ -8,8 +8,8 @@ use Magento\Analytics\Model\AnalyticsToken; use Magento\Analytics\Model\Connector\Http\JsonConverter; use Magento\Analytics\Model\Connector\Http\ResponseResolver; +use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\HTTP\ZendClient; -use Magento\Config\Model\Config; use Psr\Log\LoggerInterface; use Magento\Analytics\Model\Connector\NotifyDataChangedCommand; use Magento\Analytics\Model\Connector\Http\ClientInterface; @@ -32,7 +32,7 @@ class NotifyDataChangedCommandTest extends \PHPUnit_Framework_TestCase private $httpClientMock; /** - * @var Config|\PHPUnit_Framework_MockObject_MockObject + * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */ public $configMock; @@ -51,7 +51,7 @@ protected function setUp() ->disableOriginalConstructor() ->getMock(); - $this->configMock = $this->getMockBuilder(Config::class) + $this->configMock = $this->getMockBuilder(ScopeConfigInterface::class) ->disableOriginalConstructor() ->getMock(); @@ -80,7 +80,7 @@ public function testExecuteSuccess() ->method('isTokenExist') ->willReturn(true); $this->configMock->expects($this->any()) - ->method('getConfigDataValue') + ->method('getValue') ->willReturn($configVal); $this->analyticsTokenMock->expects($this->once()) ->method('getToken') diff --git a/app/code/Magento/Analytics/Test/Unit/Model/Connector/SignUpCommandTest.php b/app/code/Magento/Analytics/Test/Unit/Model/Connector/SignUpCommandTest.php index 624728f3440c9..cc9eba99b3d48 100644 --- a/app/code/Magento/Analytics/Test/Unit/Model/Connector/SignUpCommandTest.php +++ b/app/code/Magento/Analytics/Test/Unit/Model/Connector/SignUpCommandTest.php @@ -11,7 +11,7 @@ use Magento\Analytics\Model\Connector\SignUpCommand; use Magento\Analytics\Model\AnalyticsToken; use Magento\Analytics\Model\IntegrationManager; -use Magento\Config\Model\Config; +use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Integration\Model\Oauth\Token as IntegrationToken; use Psr\Log\LoggerInterface; @@ -41,7 +41,7 @@ class SignUpCommandTest extends \PHPUnit_Framework_TestCase private $integrationToken; /** - * @var Config|\PHPUnit_Framework_MockObject_MockObject + * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */ private $configMock; @@ -71,7 +71,7 @@ protected function setUp() $this->integrationToken = $this->getMockBuilder(IntegrationToken::class) ->disableOriginalConstructor() ->getMock(); - $this->configMock = $this->getMockBuilder(Config::class) + $this->configMock = $this->getMockBuilder(ScopeConfigInterface::class) ->disableOriginalConstructor() ->getMock(); $this->httpClientMock = $this->getMockBuilder(ClientInterface::class) @@ -105,7 +105,7 @@ public function testExecuteSuccess() $data = $this->getTestData(); $this->configMock->expects($this->any()) - ->method('getConfigDataValue') + ->method('getValue') ->willReturn($data['url']); $this->integrationToken->expects($this->any()) ->method('getData') diff --git a/app/code/Magento/Analytics/Test/Unit/Model/Connector/UpdateCommandTest.php b/app/code/Magento/Analytics/Test/Unit/Model/Connector/UpdateCommandTest.php index c5f96a96b4432..eb22461d789c8 100644 --- a/app/code/Magento/Analytics/Test/Unit/Model/Connector/UpdateCommandTest.php +++ b/app/code/Magento/Analytics/Test/Unit/Model/Connector/UpdateCommandTest.php @@ -6,12 +6,12 @@ namespace Magento\Analytics\Test\Unit\Model\Connector; use Magento\Analytics\Model\AnalyticsToken; +use Magento\Analytics\Model\Config\Backend\Baseurl\SubscriptionUpdateHandler; use Magento\Analytics\Model\Connector\Http\ResponseResolver; +use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\FlagManager; use Magento\Framework\HTTP\ZendClient; -use Magento\Config\Model\Config; use Psr\Log\LoggerInterface; -use Magento\Analytics\Model\Plugin\BaseUrlConfigPlugin; use Magento\Analytics\Model\Connector\UpdateCommand; use Magento\Analytics\Model\Connector\Http\ClientInterface; @@ -36,7 +36,7 @@ class UpdateCommandTest extends \PHPUnit_Framework_TestCase private $httpClientMock; /** - * @var Config|\PHPUnit_Framework_MockObject_MockObject + * @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */ public $configMock; @@ -65,7 +65,7 @@ protected function setUp() ->disableOriginalConstructor() ->getMock(); - $this->configMock = $this->getMockBuilder(Config::class) + $this->configMock = $this->getMockBuilder(ScopeConfigInterface::class) ->disableOriginalConstructor() ->getMock(); @@ -101,12 +101,12 @@ public function testExecuteSuccess() ->willReturn(true); $this->configMock->expects($this->any()) - ->method('getConfigDataValue') + ->method('getValue') ->willReturn($configVal); $this->flagManagerMock->expects($this->once()) ->method('getFlagData') - ->with(BaseUrlConfigPlugin::OLD_BASE_URL_FLAG_CODE) + ->with(SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE) ->willReturn($url); $this->analyticsTokenMock->expects($this->once()) diff --git a/app/code/Magento/Analytics/Test/Unit/Model/Plugin/BaseUrlConfigPluginTest.php b/app/code/Magento/Analytics/Test/Unit/Model/Plugin/BaseUrlConfigPluginTest.php index caf81b77fbdf9..38d073a4b4550 100644 --- a/app/code/Magento/Analytics/Test/Unit/Model/Plugin/BaseUrlConfigPluginTest.php +++ b/app/code/Magento/Analytics/Test/Unit/Model/Plugin/BaseUrlConfigPluginTest.php @@ -5,12 +5,13 @@ */ namespace Magento\Analytics\Test\Unit\Model\Plugin; +use Magento\Analytics\Model\Config\Backend\Baseurl\SubscriptionUpdateHandler; use Magento\Analytics\Model\Plugin\BaseUrlConfigPlugin; use Magento\Analytics\Model\SubscriptionStatusProvider; -use Magento\Config\Model\Config\Backend\Baseurl; -use Magento\Framework\FlagManager; -use Magento\Framework\App\Config\Storage\WriterInterface; +use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Framework\App\Config\Value; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; +use Magento\Store\Model\ScopeInterface; use Magento\Store\Model\Store; /** @@ -19,25 +20,15 @@ class BaseUrlConfigPluginTest extends \PHPUnit_Framework_TestCase { /** - * @var FlagManager | \PHPUnit_Framework_MockObject_MockObject + * @var SubscriptionUpdateHandler | \PHPUnit_Framework_MockObject_MockObject */ - private $flagManagerMock; + private $subscriptionUpdateHandlerMock; /** - * @var BaseUrl | \PHPUnit_Framework_MockObject_MockObject + * @var Value | \PHPUnit_Framework_MockObject_MockObject */ private $configValueMock; - /** - * @var SubscriptionStatusProvider | \PHPUnit_Framework_MockObject_MockObject - */ - private $subscriptionStatusProvider; - - /** - * @var WriterInterface | \PHPUnit_Framework_MockObject_MockObject - */ - private $configWriterMock; - /** * @var ObjectManagerHelper */ @@ -53,70 +44,42 @@ class BaseUrlConfigPluginTest extends \PHPUnit_Framework_TestCase */ protected function setUp() { - $this->flagManagerMock = $this->getMockBuilder(FlagManager::class) - ->disableOriginalConstructor() - ->getMock(); - $this->configValueMock = $this->getMockBuilder(Baseurl::class) + $this->subscriptionUpdateHandlerMock = $this->getMockBuilder(SubscriptionUpdateHandler::class) ->disableOriginalConstructor() ->getMock(); - $this->subscriptionStatusProvider = $this->getMockBuilder(SubscriptionStatusProvider::class) + $this->configValueMock = $this->getMockBuilder(Value::class) ->disableOriginalConstructor() + ->setMethods(['isValueChanged', 'getPath', 'getScope', 'getOldValue']) ->getMock(); - $this->configWriterMock = $this->getMockBuilder(WriterInterface::class) - ->disableOriginalConstructor() - ->getMockForAbstractClass(); $this->objectManagerHelper = new ObjectManagerHelper($this); $this->plugin = $this->objectManagerHelper->getObject( BaseUrlConfigPlugin::class, [ - 'flagManager' => $this->flagManagerMock, - 'subscriptionStatusProvider' => $this->subscriptionStatusProvider, - 'configWriter' => $this->configWriterMock + 'subscriptionUpdateHandler' => $this->subscriptionUpdateHandlerMock, ] ); } /** - * @param array $testData - * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $saveConfigInvokeMatcher - * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $oldValueInvokeMatcher - * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $saveFlagInvokeMatcher - * @param \PHPUnit_Framework_MockObject_Matcher_InvokedCount $configValueGetPathMatcher - * + * @param array $configValueData * @return void - * @dataProvider pluginDataProvider + * @dataProvider afterSavePluginIsNotApplicableDataProvider */ - public function testPluginForAfterSave( - array $testData, - \PHPUnit_Framework_MockObject_Matcher_InvokedCount $saveConfigInvokeMatcher, - \PHPUnit_Framework_MockObject_Matcher_InvokedCount $oldValueInvokeMatcher, - \PHPUnit_Framework_MockObject_Matcher_InvokedCount $saveFlagInvokeMatcher, - \PHPUnit_Framework_MockObject_Matcher_InvokedCount $configValueGetPathMatcher + public function testAfterSavePluginIsNotApplicable( + array $configValueData ) { - $this->configValueMock->expects($this->once()) + $this->configValueMock ->method('isValueChanged') - ->willReturn($testData['isValueChanged']); - - $this->configValueMock->expects($configValueGetPathMatcher) - ->method('getData') - ->with('path') - ->willReturn($testData['path']); - $this->subscriptionStatusProvider->expects($this->any())->method('getStatus') - ->willReturn($testData['subscriptionStatus']); - - $oldUrl = 'mage.dev'; - $this->configValueMock->expects($oldValueInvokeMatcher) - ->method('getOldValue') - ->willReturn($oldUrl); - $this->flagManagerMock->expects($saveFlagInvokeMatcher) - ->method('saveFlag') - ->with(BaseUrlConfigPlugin::OLD_BASE_URL_FLAG_CODE, $oldUrl); - - $this->configWriterMock->expects($saveConfigInvokeMatcher)->method('save') - ->with( - BaseUrlConfigPlugin::UPDATE_CRON_STRING_PATH, - '0 * * * *' - ); + ->willReturn($configValueData['isValueChanged']); + $this->configValueMock + ->method('getPath') + ->willReturn($configValueData['path']); + $this->configValueMock + ->method('getScope') + ->willReturn($configValueData['scope']); + $this->subscriptionUpdateHandlerMock + ->expects($this->never()) + ->method('processUrlUpdate'); $this->assertEquals( $this->configValueMock, @@ -127,64 +90,58 @@ public function testPluginForAfterSave( /** * @return array */ - public function pluginDataProvider() + public function afterSavePluginIsNotApplicableDataProvider() { return [ - 'setup_subscription_update_cron_job' => [ - 'testData' => [ - 'isValueChanged' => true, - 'subscriptionStatus' => SubscriptionStatusProvider::ENABLED, - 'path' => Store::XML_PATH_SECURE_BASE_URL - ], - 'saveConfigInvokeMatcher' => $this->once(), - 'oldValueInvokeMatcher' => $this->once(), - 'saveFlagInvokeMatcher' => $this->once(), - 'configValueGetPathMatcher' => $this->once(), - ], - 'base_url_not_changed' => [ - 'testData' => [ + 'Value has not been changed' => [ + 'Config Value Data' => [ 'isValueChanged' => false, - 'subscriptionStatus' => SubscriptionStatusProvider::ENABLED, - 'path' => Store::XML_PATH_SECURE_BASE_URL + 'path' => Store::XML_PATH_SECURE_BASE_URL, + 'scope' => ScopeConfigInterface::SCOPE_TYPE_DEFAULT ], - 'saveConfigInvokeMatcher' => $this->never(), - 'oldValueInvokeMatcher' => $this->never(), - 'saveFlagInvokeMatcher' => $this->never(), - 'configValueGetPathMatcher' => $this->never(), ], - 'analytics_disabled' => [ - 'testData' => [ + 'Unsecure URL has been changed' => [ + 'Config Value Data' => [ 'isValueChanged' => true, - 'subscriptionStatus' => SubscriptionStatusProvider::DISABLED, - 'path' => Store::XML_PATH_SECURE_BASE_URL + 'path' => Store::XML_PATH_UNSECURE_BASE_URL, + 'scope' => ScopeConfigInterface::SCOPE_TYPE_DEFAULT ], - 'saveConfigInvokeMatcher' => $this->never(), - 'oldValueInvokeMatcher' => $this->never(), - 'saveFlagInvokeMatcher' => $this->never(), - 'configValueGetPathMatcher' => $this->once(), ], - 'analytics_pending' => [ - 'testData' => [ + 'Secure URL has been changed not in the Default scope' => [ + 'Config Value Data' => [ 'isValueChanged' => true, - 'subscriptionStatus' => SubscriptionStatusProvider::PENDING, - 'path' => Store::XML_PATH_SECURE_BASE_URL + 'path' => Store::XML_PATH_SECURE_BASE_URL, + 'scope' => ScopeInterface::SCOPE_STORES ], - 'saveConfigInvokeMatcher' => $this->never(), - 'oldValueInvokeMatcher' => $this->never(), - 'saveFlagInvokeMatcher' => $this->never(), - 'configValueGetPathMatcher' => $this->once(), ], - 'unsecure_url_changed' => [ - 'testData' => [ - 'isValueChanged' => true, - 'subscriptionStatus' => SubscriptionStatusProvider::PENDING, - 'path' => Store::XML_PATH_UNSECURE_BASE_URL - ], - 'saveConfigInvokeMatcher' => $this->never(), - 'oldValueInvokeMatcher' => $this->never(), - 'saveFlagInvokeMatcher' => $this->never(), - 'configValueGetPathMatcher' => $this->once(), - ] ]; } + + /** + * @return void + */ + public function testAfterSavePluginIsApplicable() + { + $this->configValueMock + ->method('isValueChanged') + ->willReturn(true); + $this->configValueMock + ->method('getPath') + ->willReturn(Store::XML_PATH_SECURE_BASE_URL); + $this->configValueMock + ->method('getScope') + ->willReturn(ScopeConfigInterface::SCOPE_TYPE_DEFAULT); + $this->configValueMock + ->method('getOldValue') + ->willReturn('http://store.com'); + $this->subscriptionUpdateHandlerMock + ->expects($this->once()) + ->method('processUrlUpdate') + ->with('http://store.com'); + + $this->assertEquals( + $this->configValueMock, + $this->plugin->afterAfterSave($this->configValueMock, $this->configValueMock) + ); + } } diff --git a/app/code/Magento/Analytics/Test/Unit/Model/ReportUrlProviderTest.php b/app/code/Magento/Analytics/Test/Unit/Model/ReportUrlProviderTest.php index 1be0524d72b7f..ee507d88c68db 100644 --- a/app/code/Magento/Analytics/Test/Unit/Model/ReportUrlProviderTest.php +++ b/app/code/Magento/Analytics/Test/Unit/Model/ReportUrlProviderTest.php @@ -6,9 +6,12 @@ namespace Magento\Analytics\Test\Unit\Model; use Magento\Analytics\Model\AnalyticsToken; +use Magento\Analytics\Model\Config\Backend\Baseurl\SubscriptionUpdateHandler; use Magento\Analytics\Model\Connector\OTPRequest; +use Magento\Analytics\Model\Exception\State\SubscriptionUpdateException; use Magento\Analytics\Model\ReportUrlProvider; use Magento\Framework\App\Config\ScopeConfigInterface; +use Magento\Framework\FlagManager; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; /** @@ -31,6 +34,11 @@ class ReportUrlProviderTest extends \PHPUnit_Framework_TestCase */ private $otpRequestMock; + /** + * @var FlagManager|\PHPUnit_Framework_MockObject_MockObject + */ + private $flagManagerMock; + /** * @var ObjectManagerHelper */ @@ -63,6 +71,10 @@ protected function setUp() ->disableOriginalConstructor() ->getMock(); + $this->flagManagerMock = $this->getMockBuilder(FlagManager::class) + ->disableOriginalConstructor() + ->getMock(); + $this->objectManagerHelper = new ObjectManagerHelper($this); $this->reportUrlProvider = $this->objectManagerHelper->getObject( @@ -71,6 +83,7 @@ protected function setUp() 'config' => $this->configMock, 'analyticsToken' => $this->analyticsTokenMock, 'otpRequest' => $this->otpRequestMock, + 'flagManager' => $this->flagManagerMock, 'urlReportConfigPath' => $this->urlReportConfigPath, ] ); @@ -119,4 +132,22 @@ public function getUrlDataProvider() 'TokenExistAndOtpValid' => [true, '249e6b658877bde2a77bc4ab'], ]; } + + /** + * @return void + */ + public function testGetUrlWhenSubscriptionUpdateRunning() + { + $this->flagManagerMock + ->expects($this->once()) + ->method('getFlagData') + ->with(SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE) + ->willReturn('http://store.com'); + $this->setExpectedException( + SubscriptionUpdateException::class, + 'Your Base URL has been changed and your reports are being updated. ' + . 'Advanced Reporting will be available once this change has been processed. Please try again later.' + ); + $this->reportUrlProvider->getUrl(); + } } diff --git a/app/code/Magento/Analytics/Test/Unit/Model/SubscriptionStatusProviderTest.php b/app/code/Magento/Analytics/Test/Unit/Model/SubscriptionStatusProviderTest.php index 90707b2db7e1a..2e52a13f90bbf 100644 --- a/app/code/Magento/Analytics/Test/Unit/Model/SubscriptionStatusProviderTest.php +++ b/app/code/Magento/Analytics/Test/Unit/Model/SubscriptionStatusProviderTest.php @@ -6,6 +6,7 @@ namespace Magento\Analytics\Test\Unit\Model; use Magento\Analytics\Model\AnalyticsToken; +use Magento\Analytics\Model\Config\Backend\Baseurl\SubscriptionUpdateHandler; use Magento\Analytics\Model\Config\Backend\Enabled\SubscriptionHandler; use Magento\Analytics\Model\SubscriptionStatusProvider; use Magento\Framework\App\Config\ScopeConfigInterface; @@ -70,7 +71,11 @@ protected function setUp() ); } - public function testGetStatusShouldBeFailed() + /** + * @param array $flagManagerData + * @dataProvider getStatusShouldBeFailedDataProvider + */ + public function testGetStatusShouldBeFailed(array $flagManagerData) { $this->analyticsTokenMock->expects($this->once()) ->method('isTokenExist') @@ -80,26 +85,86 @@ public function testGetStatusShouldBeFailed() ->with('analytics/subscription/enabled') ->willReturn(true); - $this->expectFlagCounterReturn(null); + $this->expectFlagManagerReturn($flagManagerData); $this->assertEquals(SubscriptionStatusProvider::FAILED, $this->statusProvider->getStatus()); } - public function testGetStatusShouldBePending() + /** + * @return array + */ + public function getStatusShouldBeFailedDataProvider() + { + return [ + 'Subscription update doesn\'t active' => [ + 'Flag Manager data mapping' => [ + [SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE, null], + [SubscriptionHandler::ATTEMPTS_REVERSE_COUNTER_FLAG_CODE, null] + ], + ], + 'Subscription update is active' => [ + 'Flag Manager data mapping' => [ + [SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE, 'http://store.com'], + [SubscriptionHandler::ATTEMPTS_REVERSE_COUNTER_FLAG_CODE, null] + ], + ], + ]; + } + + /** + * @param array $flagManagerData + * @param bool $isTokenExist + * @dataProvider getStatusShouldBePendingDataProvider + */ + public function testGetStatusShouldBePending(array $flagManagerData, bool $isTokenExist) { $this->analyticsTokenMock->expects($this->once()) ->method('isTokenExist') - ->willReturn(false); + ->willReturn($isTokenExist); $this->scopeConfigMock->expects($this->once()) ->method('getValue') ->with('analytics/subscription/enabled') ->willReturn(true); - $this->expectFlagCounterReturn(45); + $this->expectFlagManagerReturn($flagManagerData); $this->assertEquals(SubscriptionStatusProvider::PENDING, $this->statusProvider->getStatus()); } + /** + * @return array + */ + public function getStatusShouldBePendingDataProvider() + { + return [ + 'Subscription update doesn\'t active and the token does not exist' => [ + 'Flag Manager data mapping' => [ + [SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE, null], + [SubscriptionHandler::ATTEMPTS_REVERSE_COUNTER_FLAG_CODE, 45] + ], + 'isTokenExist' => false, + ], + 'Subscription update is active and the token does not exist' => [ + 'Flag Manager data mapping' => [ + [SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE, 'http://store.com'], + [SubscriptionHandler::ATTEMPTS_REVERSE_COUNTER_FLAG_CODE, 45] + ], + 'isTokenExist' => false, + ], + 'Subscription update is active and token exist' => [ + 'Flag Manager data mapping' => [ + [SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE, 'http://store.com'], + [SubscriptionHandler::ATTEMPTS_REVERSE_COUNTER_FLAG_CODE, null] + ], + 'isTokenExist' => true, + ], + ]; + } + public function testGetStatusShouldBeEnabled() { + $this->flagManagerMock + ->method('getFlagData') + ->with(SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE) + ->willReturn(null); $this->analyticsTokenMock->expects($this->once()) ->method('isTokenExist') ->willReturn(true); @@ -120,15 +185,12 @@ public function testGetStatusShouldBeDisabled() } /** - * @param null|int $value + * @param array $mapping */ - private function expectFlagCounterReturn($value) + private function expectFlagManagerReturn(array $mapping) { - $this->flagManagerMock->expects($this->once())->method('getFlagData') - ->willReturnMap( - [ - [SubscriptionHandler::ATTEMPTS_REVERSE_COUNTER_FLAG_CODE, $value], - ] - ); + $this->flagManagerMock + ->method('getFlagData') + ->willReturnMap($mapping); } } diff --git a/dev/tests/integration/testsuite/Magento/Analytics/Model/Connector/Http/ReSignUpResponseResolverTest.php b/dev/tests/integration/testsuite/Magento/Analytics/Model/Connector/Http/ReSignUpResponseResolverTest.php index f69c751e1357f..4578286a5d071 100644 --- a/dev/tests/integration/testsuite/Magento/Analytics/Model/Connector/Http/ReSignUpResponseResolverTest.php +++ b/dev/tests/integration/testsuite/Magento/Analytics/Model/Connector/Http/ReSignUpResponseResolverTest.php @@ -5,6 +5,7 @@ */ namespace Magento\Analytics\Model\Connector\Http; +use Magento\Analytics\Model\Config\Backend\Baseurl\SubscriptionUpdateHandler; use Magento\Analytics\Model\Config\Backend\Enabled\SubscriptionHandler; use Magento\Framework\FlagManager; use Magento\Framework\App\Config\ScopeConfigInterface; @@ -30,22 +31,38 @@ class ReSignUpResponseResolverTest extends \PHPUnit_Framework_TestCase */ private $converter; + /** + * @var ResponseResolver + */ + private $notifyDataChangedResponseResolver; + + /** + * @var FlagManager + */ + private $flagManager; + /** * @return void */ protected function setUp() { - $this->otpResponseResolver = Bootstrap::getObjectManager()->get( + $objectManager = Bootstrap::getObjectManager(); + $this->otpResponseResolver = $objectManager->get( 'OtpResponseResolver' ); - $this->updateResponseResolver = Bootstrap::getObjectManager()->get( + $this->updateResponseResolver = $objectManager->get( 'UpdateResponseResolver' ); - $this->converter = Bootstrap::getObjectManager()->get(ConverterInterface::class); + $this->notifyDataChangedResponseResolver = $objectManager->get( + 'NotifyDataChangedResponseResolver' + ); + $this->converter = $objectManager->get(ConverterInterface::class); + $this->flagManager = $objectManager->get(FlagManager::class); } /** * @magentoDataFixture Magento/Analytics/_files/enabled_subscription_with_invalid_token.php + * @magentoDbIsolation enabled */ public function testReSignUpOnOtp() { @@ -57,6 +74,7 @@ public function testReSignUpOnOtp() /** * @magentoDataFixture Magento/Analytics/_files/enabled_subscription_with_invalid_token.php + * @magentoDbIsolation enabled */ public function testReSignOnOtpWasNotCalled() { @@ -68,6 +86,7 @@ public function testReSignOnOtpWasNotCalled() /** * @magentoDataFixture Magento/Analytics/_files/enabled_subscription_with_invalid_token.php + * @magentoDbIsolation enabled */ public function testReSignUpOnUpdateWasCalled() { @@ -79,8 +98,9 @@ public function testReSignUpOnUpdateWasCalled() /** * @magentoDataFixture Magento/Analytics/_files/enabled_subscription_with_invalid_token.php + * @magentoDbIsolation enabled */ - public function testReSignUpOnUpdateWasnotCalled() + public function testReSignUpOnUpdateWasNotCalled() { $body = $this->converter->toBody(['test' => '42']); $successResponse = new \Zend_Http_Response(201, [$this->converter->getContentTypeHeader()], $body); @@ -88,6 +108,23 @@ public function testReSignUpOnUpdateWasnotCalled() $this->assertCronWasNotSet(); } + /** + * @magentoDataFixture Magento/Analytics/_files/enabled_subscription_with_invalid_token.php + * @magentoDbIsolation enabled + */ + public function testReSignUpOnNotifyDataChangedWasNotCalledWhenSubscriptionUpdateIsRunning() + { + $this->flagManager + ->saveFlag( + SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE, + 'https://previous.example.com/' + ); + $body = $this->converter->toBody(['test' => '42']); + $retryResponse = new \Zend_Http_Response(401, [$this->converter->getContentTypeHeader()], $body); + $this->notifyDataChangedResponseResolver->getResult($retryResponse); + $this->assertCronWasNotSet(); + } + /** * @return string|null */ diff --git a/dev/tests/integration/testsuite/Magento/Analytics/Model/Plugin/BaseUrlConfigPluginTest.php b/dev/tests/integration/testsuite/Magento/Analytics/Model/Plugin/BaseUrlConfigPluginTest.php new file mode 100644 index 0000000000000..6082712cfabff --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Analytics/Model/Plugin/BaseUrlConfigPluginTest.php @@ -0,0 +1,207 @@ +objectManager = Bootstrap::getObjectManager(); + $this->preparedValueFactory = $this->objectManager->get(PreparedValueFactory::class); + $this->configValueResourceModel = $this->objectManager->get(ConfigData::class); + $this->scopeConfig = $this->objectManager->get(ScopeConfigInterface::class); + $this->flagManager = $this->objectManager->get(FlagManager::class); + } + + /** + * @magentoDbIsolation enabled + */ + public function testAfterSaveNotSecureUrl() + { + $this->saveConfigValue( + Store::XML_PATH_UNSECURE_BASE_URL, + 'http://store.com/', + ScopeConfigInterface::SCOPE_TYPE_DEFAULT + ); + $this->assertCronWasNotSet(); + } + + /** + * @magentoDbIsolation enabled + */ + public function testAfterSaveSecureUrlNotInDefaultScope() + { + $this->saveConfigValue( + Store::XML_PATH_SECURE_BASE_URL, + 'https://store.com/', + ScopeInterface::SCOPE_STORES + ); + $this->assertCronWasNotSet(); + } + + /** + * @magentoDbIsolation enabled + * @magentoAdminConfigFixture web/secure/base_url https://previous.example.com/ + */ + public function testAfterSaveSecureUrlInDefaultScopeOnDoesNotRegisteredInstance() + { + $this->saveConfigValue( + Store::XML_PATH_SECURE_BASE_URL, + 'https://store.com/', + ScopeConfigInterface::SCOPE_TYPE_DEFAULT + ); + $this->assertCronWasNotSet(); + } + + /** + * @magentoDbIsolation enabled + * @magentoAdminConfigFixture web/secure/base_url https://previous.example.com/ + * @magentoAdminConfigFixture analytics/general/token MBI_token + */ + public function testAfterSaveSecureUrlInDefaultScopeOnRegisteredInstance() + { + $this->saveConfigValue( + Store::XML_PATH_SECURE_BASE_URL, + 'https://store.com/', + ScopeConfigInterface::SCOPE_TYPE_DEFAULT + ); + $this->assertCronWasSet(); + } + + /** + * @magentoDbIsolation enabled + * @magentoAdminConfigFixture web/secure/base_url https://previous.example.com/ + * @magentoAdminConfigFixture analytics/general/token MBI_token + */ + public function testAfterSaveMultipleBaseUrlChanges() + { + $this->saveConfigValue( + Store::XML_PATH_SECURE_BASE_URL, + 'https://store.com/', + ScopeConfigInterface::SCOPE_TYPE_DEFAULT + ); + + $this->saveConfigValue( + Store::XML_PATH_SECURE_BASE_URL, + 'https://store10.com/', + ScopeConfigInterface::SCOPE_TYPE_DEFAULT + ); + $this->assertCronWasSet(); + } + + /** + * @param string $path The configuration path in format section/group/field_name + * @param string $value The configuration value + * @param string $scope The configuration scope (default, website, or store) + * @return void + */ + private function saveConfigValue(string $path, string $value, string $scope) + { + $value = $this->preparedValueFactory->create( + $path, + $value, + $scope + ); + $this->configValueResourceModel->save($value); + } + + /** + * @return void + */ + private function assertCronWasNotSet() + { + $this->assertNull($this->getSubscriptionUpdateSchedule()); + $this->assertNull($this->getPreviousUpdateUrl()); + $this->assertNull($this->getUpdateReverseCounter()); + } + + /** + * @return void + */ + private function assertCronWasSet() + { + $this->assertSame( + '0 * * * *', + $this->getSubscriptionUpdateSchedule(), + 'Subscription update schedule has not been set' + ); + $this->assertSame( + 'https://previous.example.com/', + $this->getPreviousUpdateUrl(), + 'The previous URL stored for update is not correct' + ); + $this->assertSame(48, $this->getUpdateReverseCounter()); + } + + /** + * @return mixed + */ + private function getSubscriptionUpdateSchedule() + { + return $this->scopeConfig->getValue( + SubscriptionUpdateHandler::UPDATE_CRON_STRING_PATH, + ScopeConfigInterface::SCOPE_TYPE_DEFAULT + ); + } + + /** + * @return mixed + */ + private function getPreviousUpdateUrl() + { + return $this->flagManager->getFlagData(SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE); + } + + /** + * @return mixed + */ + private function getUpdateReverseCounter() + { + return $this->flagManager + ->getFlagData(SubscriptionUpdateHandler::SUBSCRIPTION_UPDATE_REVERSE_COUNTER_FLAG_CODE); + } +} diff --git a/dev/tests/integration/testsuite/Magento/Analytics/Model/ReportUrlProviderTest.php b/dev/tests/integration/testsuite/Magento/Analytics/Model/ReportUrlProviderTest.php new file mode 100644 index 0000000000000..97d9b7cab0675 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Analytics/Model/ReportUrlProviderTest.php @@ -0,0 +1,56 @@ +reportUrlProvider = $objectManager->get(ReportUrlProvider::class); + $this->flagManager = $objectManager->get(FlagManager::class); + } + + /** + * @magentoDbIsolation enabled + */ + public function testGetUrlWhenSubscriptionUpdateIsRunning() + { + $this->flagManager + ->saveFlag( + SubscriptionUpdateHandler::PREVIOUS_BASE_URL_FLAG_CODE, + 'https://previous.example.com/' + ); + $this->setExpectedException( + SubscriptionUpdateException::class, + 'Your Base URL has been changed and your reports are being updated. ' + . 'Advanced Reporting will be available once this change has been processed. Please try again later.' + ); + $this->reportUrlProvider->getUrl(); + } +}