Skip to content

Commit 6a99c35

Browse files
committed
MC-31767: [AR] Data sync is not enabled by default
1 parent 84a808e commit 6a99c35

File tree

5 files changed

+280
-31
lines changed

5 files changed

+280
-31
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Analytics\Setup\Patch\Data;
8+
9+
use Magento\Analytics\Model\Config\Backend\CollectionTime;
10+
use Magento\Analytics\Model\SubscriptionStatusProvider;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\Setup\Patch\DataPatchInterface;
13+
14+
/**
15+
* Activate data collection mechanism
16+
*/
17+
class ActivateDataCollection implements DataPatchInterface
18+
{
19+
/**
20+
* @var ScopeConfigInterface
21+
*/
22+
private $scopeConfig;
23+
24+
/**
25+
* @var SubscriptionStatusProvider
26+
*/
27+
private $subscriptionStatusProvider;
28+
29+
/**
30+
* @var string
31+
*/
32+
private $analyticsCollectionTimeConfigPath = 'analytics/general/collection_time';
33+
34+
/**
35+
* @var CollectionTime
36+
*/
37+
private $collectionTimeBackendModel;
38+
39+
/**
40+
* @param ScopeConfigInterface $scopeConfig
41+
* @param SubscriptionStatusProvider $subscriptionStatusProvider
42+
* @param CollectionTime $collectionTimeBackandModel
43+
*/
44+
public function __construct(
45+
ScopeConfigInterface $scopeConfig,
46+
SubscriptionStatusProvider $subscriptionStatusProvider,
47+
CollectionTime $collectionTimeBackandModel
48+
) {
49+
$this->scopeConfig = $scopeConfig;
50+
$this->subscriptionStatusProvider = $subscriptionStatusProvider;
51+
$this->collectionTimeBackendModel = $collectionTimeBackandModel;
52+
}
53+
54+
/**
55+
* @inheritDoc
56+
* @throws \Exception
57+
*/
58+
public function apply()
59+
{
60+
$subscriptionStatus = $this->subscriptionStatusProvider->getStatus();
61+
if ($subscriptionStatus !== $this->subscriptionStatusProvider->getStatusForDisabledSubscription()) {
62+
$isCollectionProcessActivated = $this->scopeConfig->getValue(CollectionTime::CRON_SCHEDULE_PATH);
63+
if (!$isCollectionProcessActivated) {
64+
$this->collectionTimeBackendModel
65+
->setValue($this->scopeConfig->getValue($this->analyticsCollectionTimeConfigPath));
66+
$this->collectionTimeBackendModel->setPath($this->analyticsCollectionTimeConfigPath);
67+
$this->collectionTimeBackendModel->afterSave();
68+
}
69+
}
70+
}
71+
72+
/**
73+
* @inheritDoc
74+
*/
75+
public function getAliases()
76+
{
77+
return [];
78+
}
79+
80+
/**
81+
* @inheritDoc
82+
*/
83+
public static function getDependencies()
84+
{
85+
return [
86+
PrepareInitialConfig::class,
87+
];
88+
}
89+
}

app/code/Magento/Analytics/Setup/Patch/Data/PrepareInitialConfig.php

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
namespace Magento\Analytics\Setup\Patch\Data;
88

99
use Magento\Analytics\Model\Config\Backend\Enabled\SubscriptionHandler;
10+
use Magento\Config\Model\Config\Source\Enabledisable;
1011
use Magento\Framework\Setup\ModuleDataSetupInterface;
1112
use Magento\Framework\Setup\Patch\DataPatchInterface;
1213
use Magento\Framework\Setup\Patch\PatchVersionInterface;
1314

1415
/**
15-
* Initial patch.
16-
*
17-
* @package Magento\Analytics\Setup\Patch
16+
* Initial patch
1817
*/
1918
class PrepareInitialConfig implements DataPatchInterface, PatchVersionInterface
2019
{
@@ -24,66 +23,61 @@ class PrepareInitialConfig implements DataPatchInterface, PatchVersionInterface
2423
private $moduleDataSetup;
2524

2625
/**
27-
* PrepareInitialConfig constructor.
26+
* @var SubscriptionHandler
27+
*/
28+
private $subscriptionHandler;
29+
30+
/**
31+
* @var string
32+
*/
33+
private $subscriptionEnabledConfigPath = 'analytics/subscription/enabled';
34+
35+
/**
2836
* @param ModuleDataSetupInterface $moduleDataSetup
37+
* @param SubscriptionHandler $subscriptionHandler
2938
*/
3039
public function __construct(
31-
ModuleDataSetupInterface $moduleDataSetup
40+
ModuleDataSetupInterface $moduleDataSetup,
41+
SubscriptionHandler $subscriptionHandler
3242
) {
3343
$this->moduleDataSetup = $moduleDataSetup;
44+
$this->subscriptionHandler = $subscriptionHandler;
3445
}
3546

3647
/**
37-
* {@inheritdoc}
48+
* @inheritDoc
3849
*/
3950
public function apply()
4051
{
41-
$this->moduleDataSetup->getConnection()->insertMultiple(
52+
$this->moduleDataSetup->getConnection()->insert(
4253
$this->moduleDataSetup->getTable('core_config_data'),
4354
[
44-
[
45-
'scope' => 'default',
46-
'scope_id' => 0,
47-
'path' => 'analytics/subscription/enabled',
48-
'value' => 1
49-
],
50-
[
51-
'scope' => 'default',
52-
'scope_id' => 0,
53-
'path' => SubscriptionHandler::CRON_STRING_PATH,
54-
'value' => join(' ', SubscriptionHandler::CRON_EXPR_ARRAY)
55-
]
55+
'path' => $this->subscriptionEnabledConfigPath,
56+
'value' => Enabledisable::ENABLE_VALUE,
5657
]
5758
);
5859

59-
$this->moduleDataSetup->getConnection()->insert(
60-
$this->moduleDataSetup->getTable('flag'),
61-
[
62-
'flag_code' => SubscriptionHandler::ATTEMPTS_REVERSE_COUNTER_FLAG_CODE,
63-
'state' => 0,
64-
'flag_data' => 24,
65-
]
66-
);
60+
$this->subscriptionHandler->processEnabled();
6761
}
6862

6963
/**
70-
* {@inheritdoc}
64+
* @inheritDoc
7165
*/
7266
public static function getDependencies()
7367
{
7468
return [];
7569
}
7670

7771
/**
78-
* {@inheritdoc}
72+
* @inheritDoc
7973
*/
8074
public static function getVersion()
8175
{
8276
return '2.0.0';
8377
}
8478

8579
/**
86-
* {@inheritdoc}
80+
* @inheritDoc
8781
*/
8882
public function getAliases()
8983
{

app/code/Magento/Analytics/etc/adminhtml/system.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
<label>Time of day to send data</label>
2929
<frontend_model>Magento\Analytics\Block\Adminhtml\System\Config\CollectionTimeLabel</frontend_model>
3030
<backend_model>Magento\Analytics\Model\Config\Backend\CollectionTime</backend_model>
31+
<depends>
32+
<field id="analytics/general/enabled">1</field>
33+
</depends>
3134
</field>
3235
<field id="vertical" translate="label comment" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="0">
3336
<hint>Industry Data</hint>

app/code/Magento/Analytics/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9-
<preference for="Magento\Analytics\ReportXML\ConfigInterface" type="Magento\Analytics\ReportXML\Config" />
9+
<preference for="Magento\Analytics\ReportXml\ConfigInterface" type="Magento\Analytics\ReportXml\Config" />
1010
<preference for="Magento\Analytics\Model\ConfigInterface" type="Magento\Analytics\Model\Config" />
1111
<preference for="Magento\Analytics\Model\ReportWriterInterface" type="Magento\Analytics\Model\ReportWriter" />
1212
<preference for="Magento\Analytics\Api\LinkProviderInterface" type="Magento\Analytics\Model\LinkProvider" />
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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\Model\Config\Backend;
10+
11+
use Magento\Analytics\Model\SubscriptionStatusProvider;
12+
use Magento\Config\Model\Config\Source\Enabledisable;
13+
use Magento\Config\Model\PreparedValueFactory;
14+
use Magento\Config\Model\ResourceModel\Config\Data as ConfigDataResource;
15+
use Magento\Framework\App\Config\ReinitableConfigInterface;
16+
use Magento\Framework\App\Config\ScopeConfigInterface;
17+
use Magento\TestFramework\Helper\Bootstrap;
18+
19+
/**
20+
* @magentoAppArea adminhtml
21+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
22+
*/
23+
class EnabledTest extends \PHPUnit\Framework\TestCase
24+
{
25+
/**
26+
* @var \Magento\Framework\ObjectManagerInterface
27+
*/
28+
private $objectManager;
29+
30+
/**
31+
* @var ScopeConfigInterface
32+
*/
33+
private $scopeConfig;
34+
35+
/**
36+
* @var SubscriptionStatusProvider
37+
*/
38+
private $subscriptionStatusProvider;
39+
40+
/**
41+
* @var PreparedValueFactory
42+
*/
43+
private $preparedValueFactory;
44+
45+
/**
46+
* @var ConfigDataResource
47+
*/
48+
private $configValueResourceModel;
49+
50+
/**
51+
* @var ReinitableConfigInterface
52+
*/
53+
private $reinitableConfig;
54+
55+
/**
56+
* @inheritDoc
57+
*/
58+
protected function setUp()
59+
{
60+
$this->objectManager = Bootstrap::getObjectManager();
61+
$this->scopeConfig = $this->objectManager->get(ScopeConfigInterface::class);
62+
$this->subscriptionStatusProvider = $this->objectManager->get(SubscriptionStatusProvider::class);
63+
$this->preparedValueFactory = $this->objectManager->get(PreparedValueFactory::class);
64+
$this->configValueResourceModel = $this->objectManager->get(ConfigDataResource::class);
65+
$this->reinitableConfig = $this->objectManager->get(ReinitableConfigInterface::class);
66+
}
67+
68+
/**
69+
* @magentoDbIsolation enabled
70+
*/
71+
public function testDisable()
72+
{
73+
$this->checkInitialStatus();
74+
$this->saveConfigValue(Enabled::XML_ENABLED_CONFIG_STRUCTURE_PATH, (string)Enabledisable::DISABLE_VALUE);
75+
$this->reinitableConfig->reinit();
76+
77+
$this->checkDisabledStatus();
78+
}
79+
80+
/**
81+
* @depends testDisable
82+
* @magentoDbIsolation enabled
83+
*/
84+
public function testReEnable()
85+
{
86+
$this->checkDisabledStatus();
87+
$this->saveConfigValue(Enabled::XML_ENABLED_CONFIG_STRUCTURE_PATH, (string)Enabledisable::ENABLE_VALUE);
88+
$this->checkReEnabledStatus();
89+
}
90+
91+
/**
92+
* Get configuration value
93+
*
94+
* @param string $path
95+
* @param string $scopeType
96+
* @return mixed
97+
*/
98+
private function getConfigValue(
99+
string $path,
100+
string $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT
101+
) {
102+
return $this->scopeConfig->getValue(
103+
$path,
104+
$scopeType
105+
);
106+
}
107+
108+
/**
109+
* Save configuration value
110+
*
111+
* @param string $path The configuration path in format section/group/field_name
112+
* @param string $value The configuration value
113+
* @param string $scope The configuration scope (default, website, or store)
114+
* @return void
115+
* @throws \Magento\Framework\Exception\RuntimeException
116+
* @throws \Magento\Framework\Exception\AlreadyExistsException
117+
*/
118+
private function saveConfigValue(
119+
string $path,
120+
string $value,
121+
string $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT
122+
) {
123+
$configValue = $this->preparedValueFactory->create(
124+
$path,
125+
$value,
126+
$scope
127+
);
128+
$this->configValueResourceModel->save($configValue);
129+
}
130+
131+
/**
132+
* Check the instance status after installation
133+
*/
134+
private function checkInitialStatus()
135+
{
136+
$this->assertNotSame(SubscriptionStatusProvider::DISABLED, $this->subscriptionStatusProvider->getStatus());
137+
$this->assertNotEmpty($this->getConfigValue(CollectionTime::CRON_SCHEDULE_PATH));
138+
}
139+
140+
/**
141+
* Check the instance status after disabling AR synchronisation
142+
*/
143+
private function checkDisabledStatus()
144+
{
145+
$this->assertSame(SubscriptionStatusProvider::DISABLED, $this->subscriptionStatusProvider->getStatus());
146+
$this->assertEmpty($this->getConfigValue(CollectionTime::CRON_SCHEDULE_PATH));
147+
}
148+
149+
/**
150+
* Check the instance status after re-activation AR synchronisation
151+
*/
152+
private function checkReEnabledStatus()
153+
{
154+
$this->assertContains(
155+
$this->subscriptionStatusProvider->getStatus(),
156+
[
157+
SubscriptionStatusProvider::ENABLED,
158+
SubscriptionStatusProvider::PENDING,
159+
]
160+
);
161+
$this->assertNotEmpty($this->getConfigValue(CollectionTime::CRON_SCHEDULE_PATH));
162+
}
163+
}

0 commit comments

Comments
 (0)