File size: 5,913 Bytes
bf8c0ce | 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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | <?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Integration\Segment;
use Piwik\ArchiveProcessor\Rules;
use Piwik\Cache;
use Piwik\Date;
use Piwik\Option;
use Piwik\Plugins\Live\SystemSettings;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Piwik\Plugins\SegmentEditor\API;
use Piwik\Plugins\SitesManager\API as APISitesManager;
/**
* @group Segment
* @group SegmentUnavailableTest
*/
class SegmentUnavailableTest extends IntegrationTestCase
{
protected $idSite = 1;
public function setUp(): void
{
parent::setUp();
// Setup test site
Date::$now = strtotime('2020-03-01 00:00:00');
FakeAccess::setIdSitesView([1, 2]);
FakeAccess::setIdSitesAdmin([3, 4]);
FakeAccess::$superUser = true;
FakeAccess::$superUserLogin = 'superusertest';
APISitesManager::getInstance()->addSite('test', 'http://example.org');
Option::set(Rules::OPTION_BROWSER_TRIGGER_ARCHIVING, 0);
}
public function tearDown(): void
{
parent::tearDown();
Option::set(Rules::OPTION_BROWSER_TRIGGER_ARCHIVING, 1);
}
public function testvisitorIdSegmentIsDisabledWhenVisitorProfileUnavailable()
{
// Create a new segment that uses the visitorId property
$definition = 'visitorId==4b3d9389bae51466';
$name = 'visitorId';
$visitorSegmentId = API::getInstance()->add($name, $definition, $this->idSite, $autoArchive = 1, $enabledAllUsers = 1);
// Check it is returned by the API get all segment call
$this->checkSegmentAvailable($definition, $name, true);
// Disable the visitor progile
$this->disableVisitorProfile(true);
// Check that the new segment is no longer returned by the API
$this->checkSegmentAvailable($definition, $name, false);
// Clean up
API::getInstance()->delete($visitorSegmentId);
$this->disableVisitorProfile(false);
}
public function testUserIdSegmentIsDisabledWhenVisitorProfileUnavailable()
{
// Create a new segment that uses the userId property
$definition = 'userId==4b3d9389bae51466';
$name = 'userId';
$userSegmentId = API::getInstance()->add($name, $definition, $this->idSite, $autoArchive = 1, $enabledAllUsers = 1);
// Check it is returned by the API get all segment call
$this->checkSegmentAvailable($definition, $name, true);
// Disable the visitor progile
$this->disableVisitorProfile(true);
// Check that the new segment is no longer returned by the API
$this->checkSegmentAvailable($definition, $name, false);
// Clean up
API::getInstance()->delete($userSegmentId);
$this->disableVisitorProfile(false);
}
public function testPluginSegmentIsDisabledWhenPluginUnavailable()
{
// Create a new segment that uses the userId property
$definition = 'browserName==Chrome';
$name = 'browserName';
$browserNameSegmentId = API::getInstance()->add($name, $definition, $this->idSite, $autoArchive = 1, $enabledAllUsers = 1);
// Check it is returned by the API get all segment call
$this->checkSegmentAvailable($definition, $name, true);
// Disable the devices detection plugin so the browser name metric will be unavailable
\Piwik\Plugin\Manager::getInstance()->deactivatePlugin('DevicesDetection');
\Piwik\Plugin\Manager::getInstance()->unloadPlugin('DevicesDetection');
$this->flushCaches();
// Check that the new segment is no longer returned by the API
$this->checkSegmentAvailable($definition, $name, false);
// Clean up
\Piwik\Plugin\Manager::getInstance()->loadPlugin('DevicesDetection');
\Piwik\Plugin\Manager::getInstance()->installLoadedPlugins();
\Piwik\Plugin\Manager::getInstance()->activatePlugin('DevicesDetection');
API::getInstance()->delete($browserNameSegmentId);
$this->flushCaches();
}
/**
* Disable the visitorlog feature
*
* @throws \Exception
*/
private function disableVisitorProfile(bool $status): void
{
$settings = new SystemSettings();
$settings->disableVisitorLog->setValue($status);
$settings->disableVisitorProfile->setValue($status);
$settings->save();
$this->flushCaches();
}
/**
* Flush the caches that contain cached segment data
*/
private function flushCaches(): void
{
Cache::getLazyCache()->flushAll();
Cache::getEagerCache()->flushAll();
Cache::getTransientCache()->flushAll();
}
/**
* Check if a segment is available
*/
private function checkSegmentAvailable(string $definition, string $name, bool $shouldBeEnabled): void
{
$expected = [];
if ($shouldBeEnabled) {
$expected = [0 =>
[
'idsegment' => 1,
'name' => $name,
'definition' => $definition,
'hash' => md5($definition),
'login' => 'super user was set',
'enable_all_users' => 1,
'enable_only_idsite' => 1,
'auto_archive' => 1,
'ts_last_edit' => null,
'deleted' => 0,
'starred' => 0,
'starred_by' => null,
],
];
}
$segments = API::getInstance()->getAll($this->idSite);
if (isset($segments[0]['ts_created'])) {
unset($segments[0]['ts_created']);
}
$this->assertEquals($expected, $segments);
}
}
|