. */ define("SKIP_RUN", true); require_once(dirname(__DIR__) . "/../scripts/migrate_to_1_x.php"); require_once(dirname(__DIR__) . "/lib/UloggerDatabaseTestCase.php"); class MigrateTest extends UloggerDatabaseTestCase { protected function tearDown() { if ($this->getName() === "testUpdateSchemas") { self::runSqlScript(dirname(__DIR__) . "/../scripts/ulogger." . $this->getDbDriverName()); } parent::tearDown(); } public function testUpdateSchemas() { self::runSqlScript(dirname(__DIR__) . "/fixtures/ulogger_0_6." . $this->getDbDriverName()); $this->loadDataSet("fixture_0_6.xml"); $this->assertEquals(1, $this->getConnection()->getRowCount("users"), "Wrong row count"); $this->assertNotContains("admin", $this->getConnection()->getMetaData()->getTableColumns("users")); $this->assertContains("image_id", $this->getConnection()->getMetaData()->getTableColumns("positions")); $this->assertNotContains("ol_layers", $this->getConnection()->getMetaData()->getTableNames()); $this->assertNotContains("config", $this->getConnection()->getMetaData()->getTableNames()); $this->setOutputCallback(static function() {}); $ret = updateSchemas(); $this->resetConnection(); $this->assertEquals(true, $ret, "Function updateSchemas() failed"); $this->assertEquals(1, $this->getConnection()->getRowCount("users"), "Wrong row count"); $this->assertEquals(1, $this->getConnection()->getRowCount("tracks"), "Wrong row count"); $this->assertEquals(1, $this->getConnection()->getRowCount("positions"), "Wrong row count"); $this->assertContains("admin", $this->getConnection()->getMetaData()->getTableColumns("users"), "Missing table column"); $this->assertContains("image", $this->getConnection()->getMetaData()->getTableColumns("positions"), "Missing table column"); $this->assertContains("ol_layers", $this->getConnection()->getMetaData()->getTableNames(), "Missing table"); $this->assertContains("config", $this->getConnection()->getMetaData()->getTableNames(), "Missing table"); } public function testUpdateConfig() { $this->loadDataSet("fixture_non_admin.xml"); $this->setOutputCallback(static function() {}); $ret = updateConfig(dirname(__DIR__) . "/fixtures/config_0_6.php"); $this->assertEquals(true, $ret, "Function updateConfig() failed"); // admin user imported from config file $expected = [ "admin" => 1 ]; $actual = $this->getConnection()->createQueryTable( "admin", "SELECT admin FROM users WHERE login = 'admin'" ); $this->assertTableContains($expected, $actual, "Wrong actual table data"); // settings imported from config file $expected = [ "config" => [ ["name" => "color_extra", "value" => "s:7:\"#cccccc\";"], // default ["name" => "color_hilite", "value" => "s:7:\"#feff6a\";"], // default ["name" => "color_normal", "value" => "s:7:\"#ffffff\";"], // default ["name" => "color_start", "value" => "s:7:\"#55b500\";"], // default ["name" => "color_stop", "value" => "s:7:\"#ff6a00\";"], // default ["name" => "google_key", "value" => "s:13:\"testGoogleKey\";"], ["name" => "interval_seconds", "value" => "i:1234;"], ["name" => "lang", "value" => "s:2:\"pl\";"], ["name" => "latitude", "value" => "d:12.34;"], ["name" => "longitude", "value" => "d:12.34;"], ["name" => "map_api", "value" => "s:7:\"testApi\";"], ["name" => "pass_lenmin", "value" => "i:12;"], ["name" => "pass_strength", "value" => "i:2;"], ["name" => "public_tracks", "value" => "b:0;"], ["name" => "require_auth", "value" => "b:1;"], ["name" => "stroke_color", "value" => "s:7:\"#abcdef\";"], ["name" => "stroke_opacity", "value" => "i:1;"], ["name" => "stroke_weight", "value" => "i:22;"], ["name" => "units", "value" => "s:8:\"imperial\";"] ]]; $actual = $this->getConnection()->createQueryTable( "config", "SELECT name, " . uDb::getInstance()->from_lob("value") . " FROM config ORDER BY name" ); $expected = $this->createArrayDataSet($expected)->getTable("config"); self::assertTablesEqual($expected, $actual); // layers imported from config file $this->assertEquals(1, $this->getConnection()->getRowCount("ol_layers"), "Wrong row count"); $expected = [ "id" => 1, "name" => "TestLayer", "url" => "https://test_tile.png", "priority" => 0 ]; $actual = $this->getConnection()->createQueryTable( "ol_layers", "SELECT * FROM ol_layers" ); $this->assertTableContains($expected, $actual, "Wrong actual table data"); } public function testWaitForUser() { $this->setOutputCallback(static function() {}); $yes = tmpfile(); fwrite($yes, "yes"); $ret = waitForUser(stream_get_meta_data($yes)['uri']); fclose($yes); $this->assertEquals(true, $ret, "Wrong return status"); $no = tmpfile(); fwrite($no, "no"); $ret = waitForUser(stream_get_meta_data($no)['uri']); fclose($no); $this->assertEquals(false, $ret, "Wrong return status"); } /** * Run SQL commands from file. * Basic subset only. Multiple commands must not be on the same line. * @param string $path Script path * @throws PDOException */ private static function runSqlScript($path) { $script = file_get_contents($path); $count = preg_match_all('/^(?:(?:DROP|CREATE) (?:TABLE|INDEX)|INSERT|PRAGMA|SET) .*?;\s*$/smi', $script, $queries); if ($count) { try { uDb::getInstance()->beginTransaction(); foreach ($queries[0] as $query) { uDb::getInstance()->exec($query); } uDb::getInstance()->commit(); } catch (PDOException $e) { uDb::getInstance()->rollBack(); throw $e; } } } private function getDbDriverName() { return uDb::getInstance()->getAttribute(PDO::ATTR_DRIVER_NAME); } private function loadDataSet($name) { $this->resetAutoincrement(); $dataSet = $this->createFlatXMLDataSet(dirname(__DIR__) . '/fixtures/' . $name); $this->getDatabaseTester()->setDataSet($dataSet); $this->getDatabaseTester()->onSetUp(); } } ?>