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
|
<?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\Fixtures;
use Piwik\Config;
use Piwik\Filesystem;
use Piwik\Http;
use Piwik\Plugins\CoreUpdater\ReleaseChannel\LatestStable;
use Piwik\Tests\Framework\Fixture;
use Piwik\Unzip;
class LatestStableInstall extends Fixture
{
const DOWNLOAD_TIMEOUT = 900;
/**
* @var string
*/
private $subdirToInstall;
public function __construct($subdirToInstall = 'latestStableInstall')
{
$this->subdirToInstall = $subdirToInstall;
}
public function setUp()
{
$this->removeLatestStableInstall();
// create new package from git contents
$this->generateMatomoPackageFromGit();
// install latest stable
$this->downloadAndUnzipLatestStable();
$tokenAuth = $this->installSubdirectoryInstall();
$this->verifyInstall($tokenAuth);
}
private function removeLatestStableInstall()
{
$installSubdirectory = $this->getInstallSubdirectoryPath();
Filesystem::mkdir($installSubdirectory);
if (file_exists($installSubdirectory)) {
Filesystem::unlinkRecursive($installSubdirectory, true);
}
$latestStableZip = $this->getArchiveDestPath();
if (file_exists($latestStableZip)) {
unlink($latestStableZip);
}
}
private function downloadAndUnzipLatestStable()
{
$latestStableChannel = new LatestStable();
$url = 'http' . $latestStableChannel->getDownloadUrlWithoutScheme(null);
$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 . '"/piwik/* "' . $installSubdirectory . '"');
}
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 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';
}
private function getInstallSubdirectoryPath()
{
return PIWIK_INCLUDE_PATH . DIRECTORY_SEPARATOR . $this->subdirToInstall;
}
private function getDbConfigJson()
{
$dbConfig = Config::getInstance()->database;
$dbConfig = json_encode($dbConfig);
return $dbConfig;
}
private function generateMatomoPackageFromGit()
{
$this->cloneMatomoPackageRepo();
$this->runMatomoPackage();
}
private function cloneMatomoPackageRepo()
{
$pathToMatomoPackage = PIWIK_INCLUDE_PATH . '/../matomo-package';
if (file_exists($pathToMatomoPackage)) {
Filesystem::unlinkRecursive($pathToMatomoPackage, true);
}
$command = 'git clone https://github.com/matomo-org/matomo-package.git --depth=1 "' . $pathToMatomoPackage . '"';
exec($command, $output, $returnCode);
if ($returnCode != 0) {
throw new \Exception("Could not clone matomo-package repo: " . implode("\n", $output));
}
}
private function runMatomoPackage()
{
$matomoBuildPath = PIWIK_INCLUDE_PATH . '/matomo-build.zip';
if (file_exists($matomoBuildPath)) {
unlink($matomoBuildPath);
}
$command = 'cd "' . PIWIK_INCLUDE_PATH . '/../matomo-package" && ';
$command .= './scripts/build-package.sh "' . PIWIK_INCLUDE_PATH . '" piwik true';
exec($command, $output, $returnCode);
if ($returnCode != 0) {
throw new \Exception("matomo-package failed: " . implode("\n", $output));
}
$path = PIWIK_INCLUDE_PATH . '/../matomo-package/piwik-build.zip';
rename($path, $matomoBuildPath);
}
}
|