|
8 | 8 | */ |
9 | 9 |
|
10 | 10 | describe("SegmentManagementPageTest", function () { |
11 | | - this.fixture = 'Piwik\\Tests\\Fixtures\\OneVisitorTwoVisits'; |
| 11 | + this.fixture = 'Piwik\\Plugins\\SegmentEditor\\tests\\Fixtures\\SegmentManagementPageFixture'; |
12 | 12 |
|
13 | 13 | var generalParams = 'idSite=1&period=range&date=2010-03-06,2010-03-08'; |
14 | 14 | var url = '?module=CoreHome&action=index&' + generalParams + '#?' + generalParams + '&category=General_Visitors&subcategory=CoreHome_Segments'; |
@@ -39,95 +39,9 @@ describe("SegmentManagementPageTest", function () { |
39 | 39 | }; |
40 | 40 |
|
41 | 41 | 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 () { |
130 | 42 | await switchToAdminUser(); |
| 43 | + const allSegmentsResponse = await testEnvironment.callApi('SegmentEditor.getAll', {}); |
| 44 | + assignSegmentIdsFromApiResponse(allSegmentsResponse); |
131 | 45 | }); |
132 | 46 |
|
133 | 47 | it("should load correctly", async function() { |
@@ -556,11 +470,39 @@ describe("SegmentManagementPageTest", function () { |
556 | 470 | expect(alertCount).to.equal(0); |
557 | 471 | }); |
558 | 472 |
|
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'); |
562 | 504 | } |
563 | | - return parseInt(result, 10) || 0; |
| 505 | + return []; |
564 | 506 | } |
565 | 507 |
|
566 | 508 | async function getSegmentRowNumericData(segmentName) { |
|
0 commit comments