File size: 6,541 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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | <?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\Fixtures;
use Piwik\Config;
use Piwik\Filesystem;
use Piwik\Http;
use Piwik\Plugins\CoreUpdater\ReleaseChannel\LatestStable;
use Piwik\Tests\Framework\Fixture;
use Piwik\Unzip;
use Piwik\Version;
class LatestStableInstall extends Fixture
{
public const DOWNLOAD_TIMEOUT = 900;
/**
* @var string
*/
private $subdirToInstall;
public function __construct($subdirToInstall = 'latestStableInstall')
{
$this->subdirToInstall = $subdirToInstall;
}
public function setUp(): void
{
$this->removeLatestStableInstall();
// create new package from git contents
$this->generateMatomoPackageFromGit();
// install latest stable
$this->downloadAndUnzipLatestStable();
$tokenAuth = $this->installSubdirectoryInstall();
$this->placeAndActivateIncompatibleExamplePlugin();
$this->verifyInstall($tokenAuth);
}
public function tearDown(): void
{
$this->removeLatestStableInstall();
}
private function removeLatestStableInstall()
{
$installSubdirectory = $this->getInstallSubdirectoryPath();
Filesystem::mkdir($installSubdirectory);
if (file_exists($installSubdirectory)) {
Filesystem::unlinkRecursive($installSubdirectory, true);
}
if (file_exists($this->getBuildArchivePath())) {
Filesystem::unlinkRecursive($this->getBuildArchivePath(), true);
}
$latestStableZip = $this->getArchiveDestPath();
if (file_exists($latestStableZip)) {
unlink($latestStableZip);
}
}
protected function downloadAndUnzipLatestStable()
{
$url = $this->getDownloadUrl();
$archiveFile = $this->getArchiveDestPath();
Http::fetchRemoteFile($url, $archiveFile, 0, self::DOWNLOAD_TIMEOUT);
$installSubdirectory = $this->getInstallSubdirectoryPath();
Filesystem::mkdir($installSubdirectory);
$archive = Unzip::factory('PclZip', $archiveFile);
$archiveFiles = $archive->extract($installSubdirectory);
if (
0 == $archiveFiles
|| 0 == count($archiveFiles)
) {
throw new \Exception("Failed to extract matomo build ZIP archive.");
}
shell_exec('mv "' . $installSubdirectory . '"/matomo/* "' . $installSubdirectory . '"');
}
protected function getDownloadUrl()
{
$latestStableChannel = new LatestStable();
return 'http' . $latestStableChannel->getDownloadUrlWithoutScheme(null);
}
private function installSubdirectoryInstall()
{
$installScript = PIWIK_INCLUDE_PATH . '/tests/resources/install-matomo.php';
$host = parse_url(Fixture::getRootUrl(), PHP_URL_HOST);
$port = parse_url(Fixture::getRootUrl(), PHP_URL_PORT);
if (!empty($port)) {
$host .= ':' . $port;
}
$command = "php " . $installScript . " " . $this->subdirToInstall . ' "' . addslashes($this->getDbConfigJson()) . '" ' . $host;
$output = shell_exec($command);
$lines = explode("\n", $output);
$tokenAuth = trim(end($lines));
if (strlen($tokenAuth) != 32) {
throw new \Exception("Failed to install new matomo, output: $output");
}
return $tokenAuth;
}
private function placeAndActivateIncompatibleExamplePlugin()
{
$source = PIWIK_DOCUMENT_ROOT . '/plugins/ExampleTracker/';
$target = $this->getInstallSubdirectoryPath() . '/plugins/ExampleTracker/';
Filesystem::mkdir($target);
Filesystem::copyRecursive($source, $target);
// remove columns to avoid adding them to the database
Filesystem::unlinkRecursive($target . '/Columns/', true);
$pluginJson = json_decode(file_get_contents($target . 'plugin.json'), true);
// mark plugin as incompatible with version we will be updating to
$pluginJson['require']['matomo'] = '>=4.0.0-b1,<' . Version::VERSION;
file_put_contents($target . 'plugin.json', json_encode($pluginJson));
// activate ExampleTracker, having it incompatible to next version
// deactivating the plugin during update will cause CustomJsTracker plugin to update the tracker file
chmod($this->getInstallSubdirectoryPath() . '/console', 0775);
passthru($this->getInstallSubdirectoryPath() . '/console plugin:activate ExampleTracker');
passthru($this->getInstallSubdirectoryPath() . '/console core:version');
passthru($this->getInstallSubdirectoryPath() . '/console plugin:list');
}
private function verifyInstall($tokenAuth)
{
$url = Fixture::getRootUrl() . '/' . $this->subdirToInstall
. '/index.php?module=API&method=API.get&idSite=1&date=yesterday&period=day&format=json&token_auth=' . $tokenAuth;
$response = Http::sendHttpRequest($url, 30);
$response = json_decode($response, true);
$this->assertEquals(0, $response['nb_visits']);
}
private function getArchiveDestPath()
{
return PIWIK_INCLUDE_PATH . DIRECTORY_SEPARATOR . 'test_latest_stable.zip';
}
protected function getInstallSubdirectoryPath()
{
return PIWIK_INCLUDE_PATH . DIRECTORY_SEPARATOR . $this->subdirToInstall;
}
private function getBuildArchivePath()
{
return PIWIK_INCLUDE_PATH . DIRECTORY_SEPARATOR . 'archives';
}
private function getDbConfigJson()
{
$dbConfig = Config::getInstance()->database;
$dbConfig = json_encode($dbConfig);
return $dbConfig;
}
private function generateMatomoPackageFromGit()
{
$matomoBuildPath = PIWIK_INCLUDE_PATH . '/matomo-build.zip';
if (file_exists($matomoBuildPath)) {
unlink($matomoBuildPath);
}
$command = 'cd ' . PIWIK_INCLUDE_PATH . ' && ';
$command .= 'chmod 755 ./.github/scripts/*.sh && ';
$command .= './.github/scripts/build-package.sh build matomo';
exec($command, $output, $returnCode);
echo implode("\n", $output);
if ($returnCode != 0) {
throw new \Exception("matomo-package failed: " . implode("\n", $output));
}
$path = $this->getBuildArchivePath() . '/matomo-build.zip';
rename($path, $matomoBuildPath);
}
}
|