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
|
<?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\Plugins\CoreUpdater\tests\Integration\Commands;
use Piwik\DataAccess\ArchiveTableCreator;
use Piwik\Date;
use Piwik\Db;
use Piwik\DbHelper;
use Piwik\Option;
use Piwik\Tests\Framework\TestCase\ConsoleCommandTestCase;
use Piwik\Updates\Updates_2_10_0_b5;
use Piwik\Version;
require_once PIWIK_INCLUDE_PATH . '/core/Updates/2.10.0-b5.php';
/**
* @group CoreUpdater
*/
class UpdateTest extends ConsoleCommandTestCase
{
public const VERSION_TO_UPDATE_FROM = '2.9.0';
public const EXPECTED_SQL_FROM_2_10 = "UPDATE report SET reports = REPLACE(reports, 'UserSettings_getBrowserVersion', 'DevicesDetection_getBrowserVersions');";
private $oldScriptName = null;
public function setUp(): void
{
parent::setUp();
Option::set('version_core', self::VERSION_TO_UPDATE_FROM);
$this->oldScriptName = $_SERVER['SCRIPT_NAME'];
$_SERVER['SCRIPT_NAME'] = $_SERVER['SCRIPT_NAME'] . " console"; // update won't execute w/o this, see Common::isRunningConsoleCommand()
ArchiveTableCreator::clear();
DbHelper::getTablesInstalled($forceReload = true); // force reload so internal cache in Mysql.php is refreshed
Updates_2_10_0_b5::$archiveBlobTables = null;
}
public function tearDown(): void
{
$_SERVER['SCRIPT_NAME'] = $this->oldScriptName;
parent::tearDown();
}
public function testUpdateCommandSuccessfullyExecutesUpdate()
{
$result = $this->applicationTester->run(array(
'command' => 'core:update',
'--yes' => true,
));
$this->assertEquals(0, $result, $this->getCommandDisplayOutputErrorMessage());
$this->assertDryRunExecuted($this->applicationTester->getDisplay());
// make sure update went through
$this->assertEquals(Version::VERSION, Option::get('version_core'));
}
public function testUpdateCommandDoesntExecuteSqlWhenUserSaysNo()
{
$this->applicationTester->setInputs(['N']);
$result = $this->applicationTester->run(array(
'command' => 'core:update',
));
$this->assertEquals(0, $result, $this->getCommandDisplayOutputErrorMessage());
$this->assertDryRunExecuted($this->applicationTester->getDisplay());
// make sure update did not go through
$this->assertEquals(self::VERSION_TO_UPDATE_FROM, Option::get('version_core'));
}
public function testUpdateCommandDoesNotExecuteUpdateIfPiwikUpToDate()
{
Option::set('version_core', Version::VERSION);
$result = $this->applicationTester->run(array(
'command' => 'core:update',
'--yes' => true,
));
$this->assertEquals(0, $result, $this->getCommandDisplayOutputErrorMessage());
// check no update occurred
self::assertStringContainsString("Everything is already up to date.", $this->applicationTester->getDisplay());
$this->assertEquals(Version::VERSION, Option::get('version_core'));
}
public function testUpdateCommandReturnsCorrectExitCodeWhenErrorOccurs()
{
// create a blob table, then drop it manually so update 2.10.0-b10 will fail
$tableName = ArchiveTableCreator::getBlobTable(Date::factory('2015-01-01'));
Db::exec("DROP TABLE $tableName");
$result = $this->applicationTester->run(array(
'command' => 'core:update',
'--yes' => true,
));
$this->assertEquals(1, $result, $this->getCommandDisplayOutputErrorMessage());
self::assertStringContainsString("Matomo could not be updated! See above for more information.", $this->applicationTester->getDisplay());
}
private function assertDryRunExecuted($output)
{
self::assertStringContainsString("Note: this is a Dry Run", $output);
self::assertStringContainsString(self::EXPECTED_SQL_FROM_2_10, $output);
}
}
|