Skip to content

Commit 7f3acca

Browse files
committed
Create back-end Fixture for #23885 (#24161)
* Create back-end Fixture * Feedback from @sgiehl * Update expected screenshots
1 parent 063e49d commit 7f3acca

3 files changed

Lines changed: 109 additions & 95 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/**
4+
* Matomo - free/libre analytics platform
5+
*
6+
* @link https://matomo.org
7+
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace Piwik\Plugins\SegmentEditor\tests\Fixtures;
13+
14+
use Piwik\ArchiveProcessor\Rules;
15+
use Piwik\Config;
16+
use Piwik\Option;
17+
use Piwik\Plugins\SegmentEditor\API as SegmentEditorAPI;
18+
use Piwik\Tests\Fixtures\OneVisitorTwoVisits;
19+
20+
class SegmentManagementPageFixture extends OneVisitorTwoVisits
21+
{
22+
public const GLOBAL_SEGMENT_NAME = 'UI Test Global Segment';
23+
public const GLOBAL_SEGMENT_DEFINITION = 'countryCode==fr';
24+
25+
public const SITE_SEGMENT_NAME = 'UI Test Site Segment';
26+
public const SITE_SEGMENT_DEFINITION = 'visitCount>=1';
27+
28+
public const XSS_SEGMENT_NAME = '<script>alert("testsegment");</script>';
29+
public const XSS_SEGMENT_DEFINITION = 'browserCode==FF';
30+
31+
public const REALTIME_SEGMENT_NAME = 'UI Test Realtime Segment';
32+
public const REALTIME_SEGMENT_DEFINITION = 'browserCode==FF';
33+
34+
public const COMPLEX_DASHBOARD_SEGMENT_NAME = 'UI Test Complex Dashboard Segment';
35+
public const COMPLEX_DASHBOARD_SEGMENT_DEFINITION = 'browserName!=s%2525232%252526%252523--_*%25252B%25253F%252523%252520%252520%2525235%252522%2527%252526%25253C%25253E.22%25252C3,browserName==s%2525232%252526%252523--_*%25252B%25253F%252523%252520%252520%2525235%252522%2527%252526%25253C%25253E.22%25252C3;browserName!=s%2525232%252526%252523--_*%25252B%25253F%252523%252520%252520%2525235%252522%2527%252526%25253C%25253E.22%25252C3';
36+
37+
public function setUp(): void
38+
{
39+
parent::setUp();
40+
41+
$this->setUpSegments();
42+
}
43+
44+
private function setUpSegments(): void
45+
{
46+
$originalEnableBrowserArchivingTriggering = Config::getInstance()->General['enable_browser_archiving_triggering'];
47+
$originalBrowserArchivingDisabledEnforce = Config::getInstance()->General['browser_archiving_disabled_enforce'];
48+
$originalBrowserTriggerOption = Option::get(Rules::OPTION_BROWSER_TRIGGER_ARCHIVING);
49+
50+
try {
51+
Config::getInstance()->General['browser_archiving_disabled_enforce'] = 1;
52+
Config::getInstance()->General['enable_browser_archiving_triggering'] = 0;
53+
Rules::setBrowserTriggerArchiving(false);
54+
55+
$api = SegmentEditorAPI::getInstance();
56+
$api->add(self::GLOBAL_SEGMENT_NAME, self::GLOBAL_SEGMENT_DEFINITION, null, true, true);
57+
$api->add(self::SITE_SEGMENT_NAME, self::SITE_SEGMENT_DEFINITION, $this->idSite, true, true);
58+
$api->add(self::XSS_SEGMENT_NAME, self::XSS_SEGMENT_DEFINITION, $this->idSite, true, true);
59+
$api->add(self::REALTIME_SEGMENT_NAME, self::REALTIME_SEGMENT_DEFINITION, $this->idSite, false, true);
60+
$api->add(self::COMPLEX_DASHBOARD_SEGMENT_NAME, self::COMPLEX_DASHBOARD_SEGMENT_DEFINITION, $this->idSite, true, true);
61+
} finally {
62+
Config::getInstance()->General['browser_archiving_disabled_enforce'] = $originalBrowserArchivingDisabledEnforce;
63+
Config::getInstance()->General['enable_browser_archiving_triggering'] = $originalEnableBrowserArchivingTriggering;
64+
65+
if ($originalBrowserTriggerOption === false) {
66+
Option::delete(Rules::OPTION_BROWSER_TRIGGER_ARCHIVING);
67+
} else {
68+
Rules::setBrowserTriggerArchiving((bool) $originalBrowserTriggerOption);
69+
}
70+
}
71+
}
72+
}

plugins/SegmentEditor/tests/UI/SegmentManagementPage_spec.js

Lines changed: 35 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
describe("SegmentManagementPageTest", function () {
11-
this.fixture = 'Piwik\\Tests\\Fixtures\\OneVisitorTwoVisits';
11+
this.fixture = 'Piwik\\Plugins\\SegmentEditor\\tests\\Fixtures\\SegmentManagementPageFixture';
1212

1313
var generalParams = 'idSite=1&period=range&date=2010-03-06,2010-03-08';
1414
var url = '?module=CoreHome&action=index&' + generalParams + '#?' + generalParams + '&category=General_Visitors&subcategory=CoreHome_Segments';
@@ -39,95 +39,9 @@ describe("SegmentManagementPageTest", function () {
3939
};
4040

4141
before(async function () {
42-
testEnvironment.configOverride.General = {
43-
browser_archiving_disabled_enforce: '1',
44-
enable_browser_archiving_triggering: '0',
45-
};
46-
testEnvironment.optionsOverride = {
47-
enableBrowserTriggerArchiving: '0',
48-
};
49-
await testEnvironment.save();
50-
51-
const globalSegmentResult = await testEnvironment.callApi('SegmentEditor.add', {
52-
name: globalSegment.name,
53-
definition: globalSegment.definition,
54-
idSite: 0,
55-
autoArchive: 1,
56-
enabledAllUsers: 1,
57-
});
58-
59-
const siteSegmentResult = await testEnvironment.callApi('SegmentEditor.add', {
60-
name: siteSegment.name,
61-
definition: siteSegment.definition,
62-
idSite: 1,
63-
autoArchive: 1,
64-
enabledAllUsers: 1,
65-
});
66-
67-
const xssSegmentResult = await testEnvironment.callApi('SegmentEditor.add', {
68-
name: xssSegment.name,
69-
definition: xssSegment.definition,
70-
idSite: 1,
71-
autoArchive: 1,
72-
enabledAllUsers: 1,
73-
});
74-
75-
const realtimeSegmentResult = await testEnvironment.callApi('SegmentEditor.add', {
76-
name: realtimeSegment.name,
77-
definition: realtimeSegment.definition,
78-
idSite: 1,
79-
autoArchive: 0,
80-
enabledAllUsers: 1,
81-
});
82-
83-
const complexDashboardSegmentResult = await testEnvironment.callApi('SegmentEditor.add', {
84-
name: complexDashboardSegment.name,
85-
definition: complexDashboardSegment.definition,
86-
idSite: 1,
87-
autoArchive: 1,
88-
enabledAllUsers: 1,
89-
});
90-
91-
globalSegment.id = extractSegmentId(globalSegmentResult);
92-
siteSegment.id = extractSegmentId(siteSegmentResult);
93-
xssSegment.id = extractSegmentId(xssSegmentResult);
94-
realtimeSegment.id = extractSegmentId(realtimeSegmentResult);
95-
complexDashboardSegment.id = extractSegmentId(complexDashboardSegmentResult);
96-
97-
testEnvironment.configOverride.General = {
98-
browser_archiving_disabled_enforce: '0',
99-
enable_browser_archiving_triggering: '1',
100-
};
101-
testEnvironment.optionsOverride = {
102-
enableBrowserTriggerArchiving: '1',
103-
};
104-
await testEnvironment.save();
105-
106-
await testEnvironment.callApi('VisitsSummary.get', {
107-
idSite: 1,
108-
period: 'range',
109-
date: '2010-03-06,2010-03-08',
110-
segment: siteSegment.definition,
111-
});
112-
});
113-
114-
after(async function () {
115-
if (globalSegment.id) {
116-
await testEnvironment.callApi('SegmentEditor.delete', { idSegment: globalSegment.id });
117-
}
118-
if (siteSegment.id) {
119-
await testEnvironment.callApi('SegmentEditor.delete', { idSegment: siteSegment.id });
120-
}
121-
if (xssSegment.id) {
122-
await testEnvironment.callApi('SegmentEditor.delete', { idSegment: xssSegment.id });
123-
}
124-
if (realtimeSegment.id) {
125-
await testEnvironment.callApi('SegmentEditor.delete', { idSegment: realtimeSegment.id });
126-
}
127-
});
128-
129-
afterEach(async function () {
13042
await switchToAdminUser();
43+
const allSegmentsResponse = await testEnvironment.callApi('SegmentEditor.getAll', {});
44+
assignSegmentIdsFromApiResponse(allSegmentsResponse);
13145
});
13246

13347
it("should load correctly", async function() {
@@ -556,11 +470,39 @@ describe("SegmentManagementPageTest", function () {
556470
expect(alertCount).to.equal(0);
557471
});
558472

559-
function extractSegmentId(result) {
560-
if (result && typeof result === 'object' && typeof result.value !== 'undefined') {
561-
return parseInt(result.value, 10) || 0;
473+
function assignSegmentIdsFromApiResponse(response) {
474+
const segments = normalizeSegmentsResponse(response);
475+
476+
const findSegmentId = (target) => {
477+
const expectedSite = target === globalSegment ? 0 : 1;
478+
const match = segments.find((segment) => {
479+
const segmentSite = parseInt(segment && segment.enable_only_idsite, 10) || 0;
480+
return segment
481+
&& segment.definition === target.definition
482+
&& segmentSite === expectedSite;
483+
});
484+
485+
return parseInt(match && (match.idsegment || match.idSegment), 10) || 0;
486+
};
487+
488+
globalSegment.id = findSegmentId(globalSegment);
489+
siteSegment.id = findSegmentId(siteSegment);
490+
xssSegment.id = findSegmentId(xssSegment);
491+
realtimeSegment.id = findSegmentId(realtimeSegment);
492+
complexDashboardSegment.id = findSegmentId(complexDashboardSegment);
493+
}
494+
495+
function normalizeSegmentsResponse(segments) {
496+
if (Array.isArray(segments)) {
497+
return segments;
498+
}
499+
if (segments && Array.isArray(segments.value)) {
500+
return segments.value;
501+
}
502+
if (segments && typeof segments === 'object') {
503+
return Object.values(segments).filter((value) => value && typeof value === 'object');
562504
}
563-
return parseInt(result, 10) || 0;
505+
return [];
564506
}
565507

566508
async function getSegmentRowNumericData(segmentName) {
Lines changed: 2 additions & 2 deletions
Loading

0 commit comments

Comments
 (0)