1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Integration;
use Piwik\CronArchive\SharedSiteIds;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
/**
* @group Core
* @group SharedSiteIdsTest
*/
class SharedSiteIdsTest extends IntegrationTestCase
{
/**
* @var SharedSiteIds
*/
private $sharedSiteIds;
public function setUp(): void
{
parent::setUp();
if (!SharedSiteIds::isSupported()) {
$this->markTestSkipped('Not supported on this platform');
return;
}
$this->sharedSiteIds = $this->makeSharedSiteIds(array(1,2,5,9));
}
public function tearDown(): void
{
if (!SharedSiteIds::isSupported()) {
return;
}
$siteIdsToCleanup = $this->makeSharedSiteIds(array());
$siteIdsToCleanup->setSiteIdsToArchive(array());
parent::tearDown();
}
private function makeSharedSiteIds($siteIds, $optionalKey = SharedSiteIds::OPTION_DEFAULT)
{
return new SharedSiteIds($siteIds, $optionalKey);
}
public function test_construct_withEmptyValue()
{
$this->sharedSiteIds->setSiteIdsToArchive(array());
$siteIds = new SharedSiteIds(null);
$this->assertEquals(0, $siteIds->getNumSites());
$this->assertNull($siteIds->getNextSiteId());
}
public function test_construct_withCustomOptionName()
{
$first = new SharedSiteIds(array(1, 2), 'SharedSiteIdsToArchive_Test');
$second = new SharedSiteIds(array(), 'SharedSiteIdsToArchive_Test');
$this->assertEquals(array(1, 2), $first->getAllSiteIdsToArchive());
$this->assertEquals(array(1, 2), $second->getAllSiteIdsToArchive());
}
public function test_getNumSites()
{
$this->assertEquals(4, $this->sharedSiteIds->getNumSites());
}
public function test_getAllSiteIdsToArchive()
{
$this->assertEquals(array(1,2,5,9), $this->sharedSiteIds->getAllSiteIdsToArchive());
}
public function test_getNumProcessedWebsites_getNextSiteId()
{
$this->assertEquals(0, $this->sharedSiteIds->getNumProcessedWebsites());
$this->assertEquals(1, $this->sharedSiteIds->getNextSiteId());
$this->assertEquals(1, $this->sharedSiteIds->getNumProcessedWebsites());
$this->assertEquals(2, $this->sharedSiteIds->getNextSiteId());
$this->assertEquals(2, $this->sharedSiteIds->getNumProcessedWebsites());
$this->assertEquals(5, $this->sharedSiteIds->getNextSiteId());
$this->assertEquals(3, $this->sharedSiteIds->getNumProcessedWebsites());
$this->assertEquals(9, $this->sharedSiteIds->getNextSiteId());
$this->assertEquals(4, $this->sharedSiteIds->getNumProcessedWebsites());
$this->assertNull($this->sharedSiteIds->getNextSiteId());
$this->assertEquals(4, $this->sharedSiteIds->getNumProcessedWebsites());
}
public function test_getNextSiteId_shouldDetectWhenTheQueueHasBeenResetMeanwhile()
{
$this->assertEquals(1, $this->sharedSiteIds->getNextSiteId());
$this->assertEquals(2, $this->sharedSiteIds->getNextSiteId());
// we fake to reset the sharedSiteIds by another process
$this->sharedSiteIds->setSiteIdsToArchive(array(1,2,5,9));
// it detects that sites must have been processed by now
$this->assertNull($this->sharedSiteIds->getNextSiteId());
}
public function test_getNextSiteId_queueWithOnlyOneSite()
{
$sharedSiteIds = $this->makeSharedSiteIds(array(1), 'mytest');
$this->assertEquals(1, $sharedSiteIds->getNextSiteId());
$this->assertNull($sharedSiteIds->getNextSiteId());
// still returns null even when calling it again
$this->assertNull($sharedSiteIds->getNextSiteId());
}
public function test_usingMultipleSharedSiteIds()
{
$second = new SharedSiteIds(array(7,9,11,6,1,2));
// should ignore his queue and help processing the existing queue
$this->assertEquals(4, $second->getNumSites());
$this->assertEquals(4, $this->sharedSiteIds->getNumSites());
$this->assertEquals(array(1,2,5,9), $second->getAllSiteIdsToArchive());
$this->assertEquals(1, $second->getNextSiteId());
$this->assertEquals(1, $second->getNumProcessedWebsites());
$this->assertEquals(array(2,5,9), $this->sharedSiteIds->getAllSiteIdsToArchive());
$this->assertEquals(2, $this->sharedSiteIds->getNextSiteId());
$this->assertEquals(2, $this->sharedSiteIds->getNumProcessedWebsites());
$this->assertEquals(array(5,9), $second->getAllSiteIdsToArchive());
$this->assertEquals(5, $second->getNextSiteId());
$this->assertEquals(3, $second->getNumProcessedWebsites());
$this->assertEquals(array(9), $this->sharedSiteIds->getAllSiteIdsToArchive());
$this->assertEquals(9, $this->sharedSiteIds->getNextSiteId());
$this->assertEquals(4, $this->sharedSiteIds->getNumProcessedWebsites());
$this->assertNull($second->getNextSiteId());
$this->assertEquals(4, $second->getNumProcessedWebsites());
$this->assertEquals(array(), $second->getAllSiteIdsToArchive());
$this->assertNull($this->sharedSiteIds->getNextSiteId());
$this->assertEquals(4, $this->sharedSiteIds->getNumProcessedWebsites());
$this->assertEquals(array(), $this->sharedSiteIds->getAllSiteIdsToArchive());
}
}
|