Skip to content

Commit 7865bf0

Browse files
authored
Merge pull request #6642 from magento-trigger/MC-39697
[Trigger] Gainsight Stories
2 parents caa85f3 + 32bda1f commit 7865bf0

File tree

13 files changed

+319
-16
lines changed

13 files changed

+319
-16
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
11+
<test name="AdminCheckAnalyticsTrackingTest">
12+
<annotations>
13+
<stories value="AdminAnalytics Check Tracking."/>
14+
<title value="AdminAnalytics Check Tracking."/>
15+
<description value="AdminAnalytics Check Tracking."/>
16+
<severity value="MINOR"/>
17+
<testCaseId value="MC-36869"/>
18+
</annotations>
19+
<before>
20+
<actionGroup ref="LoginAsAdmin" stepKey="loginAsAdmin"/>
21+
<magentoCLI command="config:set admin/usage/enabled 1" stepKey="enableAdminUsageTracking"/>
22+
<actionGroup ref="CliCacheCleanActionGroup" stepKey="cleanInvalidatedCaches">
23+
<argument name="tags" value="config full_page"/>
24+
</actionGroup>
25+
<reloadPage stepKey="pageReload"/>
26+
</before>
27+
<after>
28+
<magentoCLI command="config:set admin/usage/enabled 0" stepKey="disableAdminUsageTracking"/>
29+
<amOnPage url="{{AdminLogoutPage.url}}" stepKey="amOnLogoutPage"/>
30+
</after>
31+
32+
<waitForPageLoad stepKey="waitForPageReloaded"/>
33+
<grabPageSource stepKey="pageSource"/>
34+
<assertRegExp message="adminAnalyticsMetadata object is invalid" stepKey="validateadminAnalyticsMetadata">
35+
<expectedResult type="string">#var\s+adminAnalyticsMetadata\s+=\s+{\s+("[\w_]+":\s+"[^"]*?",\s+)*?("[\w_]+":\s+"[^"]*?"\s+)};#s</expectedResult>
36+
<actualResult type="variable">$pageSource</actualResult>
37+
</assertRegExp>
38+
<assertRegExp message="adminAnalyticsMetadata object contains incorrect user ID" stepKey="validateUserId">
39+
<expectedResult type="string">#var\s+adminAnalyticsMetadata\s+=\s+{\s+("[\w_]+":\s+"[^"]*?",\s+)*?"user":\s+"[\w\d]{64}"#s</expectedResult>
40+
<actualResult type="variable">$pageSource</actualResult>
41+
</assertRegExp>
42+
<assertRegExp message="adminAnalyticsMetadata object contains incorrect secure base URL" stepKey="validateSecureBaseURL">
43+
<expectedResult type="string">#var\s+adminAnalyticsMetadata\s+=\s+{\s+("[\w_]+":\s+"[^"]*?",\s+)*?"secure_base_url":\s+"http(s)?\\\\u003A\\\\u002F\\\\u002F.+?\\\\u002F"#s</expectedResult>
44+
<actualResult type="variable">$pageSource</actualResult>
45+
</assertRegExp>
46+
<assertRegExp message="adminAnalyticsMetadata object contains incorrect product version" stepKey="validateProductVersion">
47+
<expectedResult type="string">#var\s+adminAnalyticsMetadata\s+=\s+{\s+("[\w_]+":\s+"[^"]*?",\s+)*?"version":\s+"[^\s]+"#s</expectedResult>
48+
<actualResult type="variable">$pageSource</actualResult>
49+
</assertRegExp>
50+
<assertRegExp message="adminAnalyticsMetadata object contains incorrect product edition" stepKey="validateProductEdition">
51+
<expectedResult type="string">#var\s+adminAnalyticsMetadata\s+=\s+{\s+("[\w_]+":\s+"[^"]*?",\s+)*?"product_edition":\s+"(Community|Enterprise|B2B)"#s</expectedResult>
52+
<actualResult type="variable">$pageSource</actualResult>
53+
</assertRegExp>
54+
<assertRegExp message="adminAnalyticsMetadata object contains incorrect application mode" stepKey="validateApplicationMode">
55+
<expectedResult type="string">#var\s+adminAnalyticsMetadata\s+=\s+{\s+("[\w_]+":\s+"[^"]*?",\s+)*?"mode":\s+"default|developer|production"#s</expectedResult>
56+
<actualResult type="variable">$pageSource</actualResult>
57+
</assertRegExp>
58+
<assertRegExp message="adminAnalyticsMetadata object contains incorrect store name" stepKey="validateStoreName">
59+
<expectedResult type="string">#var\s+adminAnalyticsMetadata\s+=\s+{\s+("[\w_]+":\s+"[^"]*?",\s+)*?"store_name_default":\s+".*?"#s</expectedResult>
60+
<actualResult type="variable">$pageSource</actualResult>
61+
</assertRegExp>
62+
<assertRegExp message="adminAnalyticsMetadata object contains incorrect admin user created date" stepKey="validateAdminUserCreatedDate">
63+
<expectedResult type="string">#var\s+adminAnalyticsMetadata\s+=\s+{\s+("[\w_]+":\s+"[^"]*?",\s+)*?"admin_user_created":\s+".+?"#s</expectedResult>
64+
<actualResult type="variable">$pageSource</actualResult>
65+
</assertRegExp>
66+
<assertRegExp message="adminAnalyticsMetadata object contains incorrect admin user log date" stepKey="validateAdminUserLogDate">
67+
<expectedResult type="string">#var\s+adminAnalyticsMetadata\s+=\s+{\s+("[\w_]+":\s+"[^"]*?",\s+)*?"admin_user_logdate":\s+".+?"#s</expectedResult>
68+
<actualResult type="variable">$pageSource</actualResult>
69+
</assertRegExp>
70+
<assertRegExp message="adminAnalyticsMetadata object contains incorrect admin user role name" stepKey="validateAdminUserRoleName">
71+
<expectedResult type="string">#var\s+adminAnalyticsMetadata\s+=\s+{\s+("[\w_]+":\s+"[^"]*?",\s+)*?"admin_user_role_name":\s+".+?"#s</expectedResult>
72+
<actualResult type="variable">$pageSource</actualResult>
73+
</assertRegExp>
74+
</test>
75+
</tests>

app/code/Magento/AdminAnalytics/ViewModel/Metadata.php

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\AdminAnalytics\ViewModel;
710

11+
use Magento\Config\Model\Config\Backend\Admin\Custom;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
813
use Magento\Framework\App\ProductMetadataInterface;
914
use Magento\Backend\Model\Auth\Session;
1015
use Magento\Framework\App\State;
1116
use Magento\Framework\View\Element\Block\ArgumentInterface;
17+
use Magento\Store\Model\Information;
1218

1319
/**
1420
* Gets user version and mode
@@ -30,19 +36,27 @@ class Metadata implements ArgumentInterface
3036
*/
3137
private $productMetadata;
3238

39+
/**
40+
* @var ScopeConfigInterface
41+
*/
42+
private $config;
43+
3344
/**
3445
* @param ProductMetadataInterface $productMetadata
3546
* @param Session $authSession
3647
* @param State $appState
48+
* @param ScopeConfigInterface $config
3749
*/
3850
public function __construct(
3951
ProductMetadataInterface $productMetadata,
4052
Session $authSession,
41-
State $appState
53+
State $appState,
54+
ScopeConfigInterface $config
4255
) {
4356
$this->productMetadata = $productMetadata;
4457
$this->authSession = $authSession;
4558
$this->appState = $appState;
59+
$this->config = $config;
4660
}
4761

4862
/**
@@ -55,15 +69,26 @@ public function getMagentoVersion() :string
5569
return $this->productMetadata->getVersion();
5670
}
5771

72+
/**
73+
* Get product edition
74+
*
75+
* @return string
76+
*/
77+
public function getProductEdition(): string
78+
{
79+
return $this->productMetadata->getEdition();
80+
}
81+
5882
/**
5983
* Get current user id (hash generated from email)
6084
*
6185
* @return string
6286
*/
6387
public function getCurrentUser() :string
6488
{
65-
return hash('sha512', 'ADMIN_USER' . $this->authSession->getUser()->getEmail());
89+
return hash('sha256', 'ADMIN_USER' . $this->authSession->getUser()->getEmail());
6690
}
91+
6792
/**
6893
* Get Magento mode that the user is using
6994
*
@@ -73,4 +98,62 @@ public function getMode() :string
7398
{
7499
return $this->appState->getMode();
75100
}
101+
102+
/**
103+
* Get created date for current user
104+
*
105+
* @return string
106+
*/
107+
public function getCurrentUserCreatedDate(): string
108+
{
109+
return $this->authSession->getUser()->getCreated();
110+
}
111+
112+
/**
113+
* Get log date for current user
114+
*
115+
* @return string|null
116+
*/
117+
public function getCurrentUserLogDate(): ?string
118+
{
119+
return $this->authSession->getUser()->getLogdate();
120+
}
121+
122+
/**
123+
* Get secure base URL
124+
*
125+
* @param string $scope
126+
* @param string|null $scopeCode
127+
* @return string|null
128+
*/
129+
public function getSecureBaseUrlForScope(
130+
string $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
131+
?string $scopeCode = null
132+
): ?string {
133+
return $this->config->getValue(Custom::XML_PATH_SECURE_BASE_URL, $scope, $scopeCode);
134+
}
135+
136+
/**
137+
* Get store name
138+
*
139+
* @param string $scope
140+
* @param string|null $scopeCode
141+
* @return string|null
142+
*/
143+
public function getStoreNameForScope(
144+
string $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
145+
?string $scopeCode = null
146+
): ?string {
147+
return $this->config->getValue(Information::XML_PATH_STORE_INFO_NAME, $scope, $scopeCode);
148+
}
149+
150+
/**
151+
* Get current user role name
152+
*
153+
* @return string
154+
*/
155+
public function getCurrentUserRoleName(): string
156+
{
157+
return $this->authSession->getUser()->getRole()->getRoleName();
158+
}
76159
}

app/code/Magento/AdminAnalytics/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"magento/framework": "*",
1010
"magento/module-backend": "*",
1111
"magento/module-config": "*",
12+
"magento/module-store": "*",
1213
"magento/module-ui": "*",
1314
"magento/module-release-notification": "*"
1415
},
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<csp_whitelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Csp:etc/csp_whitelist.xsd">
10+
<policies>
11+
<policy id="script-src">
12+
<values>
13+
<value id="aptrinsic" type="host">*.aptrinsic.com</value>
14+
<value id="adobe" type="host">*.adobe.com</value>
15+
</values>
16+
</policy>
17+
<policy id="style-src">
18+
<values>
19+
<value id="aptrinsic" type="host">*.aptrinsic.com</value>
20+
<value id="fonts_googleapis" type="host">fonts.googleapis.com</value>
21+
<value id="adobe" type="host">*.adobe.com</value>
22+
</values>
23+
</policy>
24+
<policy id="img-src">
25+
<values>
26+
<value id="aptrinsic" type="host">*.aptrinsic.com</value>
27+
<value id="storage_googleapis" type="host">storage.googleapis.com</value>
28+
<value id="adobe" type="host">*.adobe.com</value>
29+
</values>
30+
</policy>
31+
<policy id="media-src">
32+
<values>
33+
<value id="adobe" type="host">*.adobe.com</value>
34+
</values>
35+
</policy>
36+
<policy id="frame-src">
37+
<values>
38+
<value id="adobe" type="host">*.adobe.com</value>
39+
</values>
40+
</policy>
41+
<policy id="connect-src">
42+
<values>
43+
<value id="aptrinsic" type="host">*.aptrinsic.com</value>
44+
</values>
45+
</policy>
46+
<policy id="font-src">
47+
<values>
48+
<value id="fonts_gstatic" type="host">fonts.gstatic.com</value>
49+
</values>
50+
</policy>
51+
</policies>
52+
</csp_whitelist>

app/code/Magento/AdminAnalytics/view/adminhtml/layout/default.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<referenceContainer name="header">
1111
<block name="tracking" as="tracking" template="Magento_AdminAnalytics::tracking.phtml" ifconfig="admin/usage/enabled">
1212
<arguments>
13-
<argument name="tracking_url" xsi:type="string">//assets.adobedtm.com/launch-EN30eb7ffa064444f1b8b0368ef38fd3a9.min.js</argument>
13+
<argument name="tracking_url" xsi:type="string">//assets.adobedtm.com/a7d65461e54e/37baabec1b6e/launch-177bc126c8e6.min.js</argument>
1414
<argument name="metadata" xsi:type="object">Magento\AdminAnalytics\ViewModel\Metadata</argument>
1515
</arguments>
1616
</block>

app/code/Magento/AdminAnalytics/view/adminhtml/templates/tracking.phtml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@
55
*/
66
?>
77

8+
<?php
9+
$metadata = $block->getMetadata();
10+
?>
11+
812
<script src="<?= $block->escapeUrl($block->getTrackingUrl()) ?>" async></script>
913
<script>
1014
var adminAnalyticsMetadata = {
11-
"version": "<?= $block->escapeJs($block->getMetadata()->getMagentoVersion()) ?>",
12-
"user": "<?= $block->escapeJs($block->getMetadata()->getCurrentUser()) ?>",
13-
"mode": "<?= $block->escapeJs($block->getMetadata()->getMode()) ?>"
15+
"secure_base_url": "<?= $block->escapeJs($metadata->getSecureBaseUrlForScope()) ?>",
16+
"version": "<?= $block->escapeJs($metadata->getMagentoVersion()) ?>",
17+
"product_edition": "<?= $block->escapeJs($metadata->getProductEdition()) ?>",
18+
"user": "<?= $block->escapeJs($metadata->getCurrentUser()) ?>",
19+
"mode": "<?= $block->escapeJs($metadata->getMode()) ?>",
20+
"store_name_default": "<?= $block->escapeJs($metadata->getStoreNameForScope()) ?>",
21+
"admin_user_created": "<?= $block->escapeJs($metadata->getCurrentUserCreatedDate()) ?>",
22+
"admin_user_logdate": "<?= $block->escapeJs($metadata->getCurrentUserLogDate()) ?>",
23+
"admin_user_role_name": "<?= $block->escapeJs($metadata->getCurrentUserRoleName()) ?>"
1424
};
1525
</script>

app/code/Magento/Csp/Model/Collector/CspWhitelistXml/Converter.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ public function convert($source)
3636
/** @var \DOMElement $value */
3737
foreach ($policy->getElementsByTagName('value') as $value) {
3838
if ($value->attributes->getNamedItem('type')->nodeValue === 'host') {
39-
$policyConfig[$id]['hosts'][] = $value->nodeValue;
39+
$policyConfig[$id]['hosts'][$value->attributes->getNamedItem('id')->nodeValue] = $value->nodeValue;
4040
} else {
4141
$policyConfig[$id]['hashes'][$value->nodeValue]
4242
= $value->attributes->getNamedItem('algorithm')->nodeValue;
4343
}
4444
}
45-
$policyConfig[$id]['hosts'] = array_unique($policyConfig[$id]['hosts']);
4645
}
4746

4847
return $policyConfig;

app/code/Magento/Csp/Model/Collector/FetchPolicyMerger.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public function merge(PolicyInterface $policy1, PolicyInterface $policy2): Polic
2525
return new FetchPolicy(
2626
$policy1->getId(),
2727
$policy1->isNoneAllowed() || $policy2->isNoneAllowed(),
28-
array_unique(array_merge($policy1->getHostSources(), $policy2->getHostSources())),
29-
array_unique(array_merge($policy1->getSchemeSources(), $policy2->getSchemeSources())),
28+
array_merge($policy1->getHostSources(), $policy2->getHostSources()),
29+
array_merge($policy1->getSchemeSources(), $policy2->getSchemeSources()),
3030
$policy1->isSelfAllowed() || $policy2->isSelfAllowed(),
3131
$policy1->isInlineAllowed() || $policy2->isInlineAllowed(),
3232
$policy1->isEvalAllowed() || $policy2->isEvalAllowed(),
33-
array_unique(array_merge($policy1->getNonceValues(), $policy2->getNonceValues())),
33+
array_merge($policy1->getNonceValues(), $policy2->getNonceValues()),
3434
array_merge($policy1->getHashes(), $policy2->getHashes()),
3535
$policy1->isDynamicAllowed() || $policy2->isDynamicAllowed(),
3636
$policy1->areEventHandlersAllowed() || $policy2->areEventHandlersAllowed()

app/code/Magento/Csp/Model/Collector/PluginTypesPolicyMerger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function merge(PolicyInterface $policy1, PolicyInterface $policy2): Polic
2222
{
2323
/** @var PluginTypesPolicy $policy1 */
2424
/** @var PluginTypesPolicy $policy2 */
25-
return new PluginTypesPolicy(array_unique(array_merge($policy1->getTypes(), $policy2->getTypes())));
25+
return new PluginTypesPolicy(array_merge($policy1->getTypes(), $policy2->getTypes()));
2626
}
2727

2828
/**

app/code/Magento/Csp/Model/Policy/FetchPolicy.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ public function __construct(
116116
) {
117117
$this->id = $id;
118118
$this->noneAllowed = $noneAllowed;
119-
$this->hostSources = array_unique($hostSources);
120-
$this->schemeSources = array_unique($schemeSources);
119+
$this->hostSources = array_values(array_unique($hostSources));
120+
$this->schemeSources = array_values(array_unique($schemeSources));
121121
$this->selfAllowed = $selfAllowed;
122122
$this->inlineAllowed = $inlineAllowed;
123123
$this->evalAllowed = $evalAllowed;
124-
$this->nonceValues = array_unique($nonceValues);
124+
$this->nonceValues = array_values(array_unique($nonceValues));
125125
$this->hashes = $hashValues;
126126
$this->dynamicAllowed = $dynamicAllowed;
127127
$this->eventHandlersAllowed = $eventHandlersAllowed;

0 commit comments

Comments
 (0)