Add tests

This commit is contained in:
Bartek Fabiszewski 2017-09-19 22:04:40 +02:00
parent 42475a5851
commit 65a8252db7
3 changed files with 1744 additions and 3 deletions

View File

@ -20,7 +20,7 @@ abstract class BaseDatabaseTestCase extends PHPUnit_Extensions_Database_TestCase
protected $testTrackComment = "test track comment";
protected $testTimestamp = 1502974402;
protected $testLat = 0;
protected $testLon = 10.01;
protected $testLon = 10.604001083;
protected $testAltitude = 10.01;
protected $testSpeed = 10.01;
protected $testBearing = 10.01;
@ -100,6 +100,20 @@ abstract class BaseDatabaseTestCase extends PHPUnit_Extensions_Database_TestCase
return $ret;
}
/**
* Execute raw insert query on database
*
* @param string $query Insert query
* @return int|null Last insert id if available, NULL otherwise
*/
private function pdoInsertRaw($query) {
$ret = NULL;
if (self::$pdo->exec($query) !== false) {
$ret = self::$pdo->lastInsertId();
}
return $ret;
}
/**
* Get single column from first row of query result
*
@ -143,7 +157,7 @@ abstract class BaseDatabaseTestCase extends PHPUnit_Extensions_Database_TestCase
protected function addTestTrack($userId = NULL, $trackName = NULL, $comment = NULL) {
if (is_null($userId)) { $userId = $this->testUserId; }
if (is_null($trackName)) { $trackName = $this->testTrackName; }
if (is_null($comment)) { $comment = $this->testComment; }
if (is_null($comment)) { $comment = $this->testTrackComment; }
return $this->pdoInsert('tracks', [ 'user_id' => $userId, 'name' => $trackName, 'comment' => $comment ]);
}
@ -165,7 +179,9 @@ abstract class BaseDatabaseTestCase extends PHPUnit_Extensions_Database_TestCase
if (is_null($latitude)) { $latitude = $this->testLat; }
if (is_null($longitude)) { $longitude = $this->testLon; }
return $this->pdoInsert('positions', [ "user_id" => $userId, "track_id" => $trackId, "time" => date("Y-m-d H:m:s", $timeStamp), "latitude" => $latitude, "longitude" => $longitude ]);
$query = "INSERT INTO positions (user_id, track_id, time, latitude, longitude)
VALUES ('$userId', '$trackId', FROM_UNIXTIME($timeStamp), '$latitude', '$longitude')";
return $this->pdoInsertRaw($query);
}
}
?>

810
.tests/tests/ImportTest.php Normal file
View File

@ -0,0 +1,810 @@
<?php
require_once(__DIR__ . "/../lib/UloggerAPITestCase.php");
if (!defined("ROOT_DIR")) { define("ROOT_DIR", __DIR__ . "/../.."); }
require_once(ROOT_DIR . "/helpers/config.php");
require_once(ROOT_DIR . "/lang.php");
class ImportTest extends UloggerAPITestCase {
public function testImportGPX10() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(0, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$gpx10 = '<?xml version="1.0"?>
<gpx version="1.0" creator="test software"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/0"
xsi:schemaLocation="http://www.topografix.com/GPX/1/0
http://www.topografix.com/GPX/1/0/gpx.xsd">
<time>2017-09-19T11:00:08Z</time>
<trk>
<name>' . $this->testTrackName . '</name>
<trkseg>
<trkpt lat="' . $this->testLat . '" lon="' . $this->testLon . '">
<ele>' . $this->testAltitude . '</ele>
<time>' . gmdate("Y-m-d\TH:i:s\Z", $this->testTimestamp) . '</time>
</trkpt>
<trkpt lat="' . $this->testLat . '" lon="' . -1 * $this->testLon . '">
<ele>' . $this->testAltitude . '</ele>
<time>' . gmdate("Y-m-d\TH:i:s\Z", $this->testTimestamp + 1) . '</time>
</trkpt>
</trkseg>
</trk>
</gpx>';
$options = [
"http_errors" => false,
"multipart" => [
[
"name" => "gpx",
"contents" => $this->getStream($gpx10)
],
[
"name" => "MAX_FILE_SIZE",
"contents" => 300000
]
],
];
$response = $this->http->post("/utils/import.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals(0, (int) $xml->error, "Wrong error status");
$this->assertEquals(1, (int) $xml->trackid, "Wrong error message");
$this->assertEquals(1, (int) $xml->trackcnt, "Wrong error message");
$this->assertEquals(1, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(2, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$expected = [
"id" => 1,
"user_id" => 1,
"name" => $this->testTrackName,
"comment" => null
];
$actual = $this->getConnection()->createQueryTable(
"tracks",
"SELECT * FROM tracks"
);
$this->assertTableContains($expected, $actual, "Wrong actual table data");
$expected = [
"id" => 1,
"time" => $this->testTimestamp,
"user_id" => 1,
"track_id" => 1,
"latitude" => $this->testLat,
"longitude" => $this->testLon,
"altitude" => $this->testAltitude,
"speed" => null,
"bearing" => null,
"accuracy" => null,
"provider" => "gps",
"comment" => null,
"image_id" => null
];
$actual = $this->getConnection()->createQueryTable(
"positions",
"SELECT id, UNIX_TIMESTAMP(time) AS time, user_id, track_id, latitude, longitude,
altitude, speed, bearing, accuracy, provider, comment, image_id FROM positions"
);
$this->assertTableContains($expected, $actual, "Wrong actual table data");
$expected = [
"id" => 2,
"time" => $this->testTimestamp + 1,
"user_id" => 1,
"track_id" => 1,
"latitude" => $this->testLat,
"longitude" => $this->testLon * -1,
"altitude" => $this->testAltitude,
"speed" => null,
"bearing" => null,
"accuracy" => null,
"provider" => "gps",
"comment" => null,
"image_id" => null
];
$this->assertTableContains($expected, $actual, "Wrong actual table data");
}
public function testImportGPX11() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(0, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$gpx11 = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<gpx version="1.1"
creator="test creator"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/1"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
<metadata>
<name>' . $this->testTrackComment . '</name>
<desc>Track for testing ulogger</desc>
<author>
<name>Bartek Fabiszewski</name>
<link href="http://www.fabiszewski.net"><text>fabiszewski.net</text></link>
</author>
<time>2017-09-19T09:22:06Z</time>
<keywords>Test, ulogger</keywords>
<bounds minlat="44.64365345" maxlat="45.341" minlon="14.452354" maxlon="24.54234"/>
</metadata>
<trk>
<src>Crafted by Bartek Fabiszewski</src>
<link href="http://www.fabiszewski.net"><text>fabiszewski.net</text></link>
<trkseg>
<trkpt lat="' . $this->testLat . '" lon="' . $this->testLon . '">
<ele>' . $this->testAltitude . '</ele>
<time>' . gmdate("Y-m-d\TH:i:s\Z", $this->testTimestamp) . '</time>
<fix>3d</fix>
<hdop>300</hdop><vdop>300</vdop><pdop>300</pdop>
</trkpt>
</trkseg>
</trk>
</gpx>';
$options = [
"http_errors" => false,
"multipart" => [
[
"name" => "gpx",
"contents" => $this->getStream($gpx11),
"filename" => $this->testTrackName
],
[
"name" => "MAX_FILE_SIZE",
"contents" => 300000
]
],
];
$response = $this->http->post("/utils/import.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals(0, (int) $xml->error, "Wrong error status");
$this->assertEquals(1, (int) $xml->trackid, "Wrong error message");
$this->assertEquals(1, (int) $xml->trackcnt, "Wrong error message");
$this->assertEquals(1, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(1, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$expected = [
"id" => 1,
"user_id" => 1,
"name" => $this->testTrackName,
"comment" => $this->testTrackComment
];
$actual = $this->getConnection()->createQueryTable(
"tracks",
"SELECT * FROM tracks"
);
$this->assertTableContains($expected, $actual, "Wrong actual table data");
$expected = [
"id" => 1,
"time" => $this->testTimestamp,
"user_id" => 1,
"track_id" => 1,
"latitude" => $this->testLat,
"longitude" => $this->testLon,
"altitude" => $this->testAltitude,
"speed" => null,
"bearing" => null,
"accuracy" => null,
"provider" => "gps",
"comment" => null,
"image_id" => null
];
$actual = $this->getConnection()->createQueryTable(
"positions",
"SELECT id, UNIX_TIMESTAMP(time) AS time, user_id, track_id, latitude, longitude,
altitude, speed, bearing, accuracy, provider, comment, image_id FROM positions"
);
$this->assertTableContains($expected, $actual, "Wrong actual table data");
}
public function testImportExtensions() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(0, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$gpx = '<?xml version="1.0" encoding="UTF-8"?>
<gpx xsi:schemaLocation="http://www.topografix.com/GPX/1/1
http://www.topografix.com/GPX/1/1/gpx.xsd
https://github.com/bfabiszewski/ulogger-android/1
https://github.com/bfabiszewski/ulogger-android/gpx_extensions1.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/1"
xmlns:ulogger="https://github.com/bfabiszewski/ulogger-android/1"
creator="μlogger" version="1.1">
<metadata>
<name>' . $this->testTrackComment . '</name>
<time>2017-06-24T22:50:45Z</time>
</metadata>
<trk>
<name>' . $this->testTrackName . '</name>
<trkseg>
<trkpt lat="' . $this->testLat . '" lon="' . $this->testLon . '">
<ele>' . $this->testAltitude . '</ele>
<time>' . gmdate("Y-m-d\TH:i:s\Z", $this->testTimestamp) . '</time>
<name>1</name>
<desc><![CDATA[User: demo Track: client_test2 Time: 2017-06-25 00:50:45 (Europe/Warsaw) Speed: 0 km/h Altitude: 15 m Total time: 00:00:00 Average speed: 0 km/h Total distance: 0 km Point 1 of 18]]></desc>
<extensions>
<ulogger:speed>' . $this->testSpeed . '</ulogger:speed>
<ulogger:bearing>' . $this->testBearing . '</ulogger:bearing>
<ulogger:accuracy>' . $this->testAccuracy . '</ulogger:accuracy>
<ulogger:provider>' . $this->testProvider . '</ulogger:provider>
</extensions>
</trkpt>
</trkseg>
</trk>
</gpx>';
$options = [
"http_errors" => false,
"multipart" => [
[
"name" => "gpx",
"contents" => $this->getStream($gpx),
"filename" => $this->testTrackName
],
[
"name" => "MAX_FILE_SIZE",
"contents" => 300000
]
],
];
$response = $this->http->post("/utils/import.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals(0, (int) $xml->error, "Wrong error status");
$this->assertEquals(1, (int) $xml->trackid, "Wrong error message");
$this->assertEquals(1, (int) $xml->trackcnt, "Wrong error message");
$this->assertEquals(1, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(1, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$expected = [
"id" => 1,
"user_id" => 1,
"name" => $this->testTrackName,
"comment" => $this->testTrackComment
];
$actual = $this->getConnection()->createQueryTable(
"tracks",
"SELECT * FROM tracks"
);
$this->assertTableContains($expected, $actual, "Wrong actual table data");
$expected = [
"id" => 1,
"time" => $this->testTimestamp,
"user_id" => 1,
"track_id" => 1,
"latitude" => $this->testLat,
"longitude" => $this->testLon,
"altitude" => $this->testAltitude,
"speed" => $this->testSpeed,
"bearing" => $this->testBearing,
"accuracy" => $this->testAccuracy,
"provider" => $this->testProvider,
"comment" => null,
"image_id" => null
];
$actual = $this->getConnection()->createQueryTable(
"positions",
"SELECT id, UNIX_TIMESTAMP(time) AS time, user_id, track_id, latitude, longitude,
altitude, speed, bearing, accuracy, provider, comment, image_id FROM positions"
);
$this->assertTableContains($expected, $actual, "Wrong actual table data");
}
public function testImportNoTime() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(0, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$gpx = '<?xml version="1.0" encoding="UTF-8"?>
<gpx xsi:schemaLocation="http://www.topografix.com/GPX/1/1
http://www.topografix.com/GPX/1/1/gpx.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/1"
creator="μlogger" version="1.1">
<trk>
<trkseg>
<trkpt lat="' . $this->testLat . '" lon="' . $this->testLon . '"></trkpt>
</trkseg>
</trk>
</gpx>';
$options = [
"http_errors" => false,
"multipart" => [
[
"name" => "gpx",
"contents" => $this->getStream($gpx),
"filename" => $this->testTrackName
],
[
"name" => "MAX_FILE_SIZE",
"contents" => 300000
]
],
];
$response = $this->http->post("/utils/import.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals(0, (int) $xml->error, "Wrong error status");
$this->assertEquals(1, (int) $xml->trackid, "Wrong error message");
$this->assertEquals(1, (int) $xml->trackcnt, "Wrong error message");
$this->assertEquals(1, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(1, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$expected = [
"id" => 1,
"user_id" => 1,
"name" => $this->testTrackName,
"comment" => null
];
$actual = $this->getConnection()->createQueryTable(
"tracks",
"SELECT * FROM tracks"
);
$this->assertTableContains($expected, $actual, "Wrong actual table data");
$expected = [
"id" => 1,
"time" => 0,
"user_id" => 1,
"track_id" => 1,
"latitude" => $this->testLat,
"longitude" => $this->testLon,
"altitude" => null,
"speed" => null,
"bearing" => null,
"accuracy" => null,
"provider" => "gps",
"comment" => null,
"image_id" => null
];
$actual = $this->getConnection()->createQueryTable(
"positions",
"SELECT id, UNIX_TIMESTAMP(time) AS time, user_id, track_id, latitude, longitude,
altitude, speed, bearing, accuracy, provider, comment, image_id FROM positions"
);
$this->assertTableContains($expected, $actual, "Wrong actual table data");
}
public function testImportMultipleSegments() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(0, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$gpx = '<?xml version="1.0" encoding="UTF-8"?>
<gpx xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/1"
creator="μlogger" version="1.1">
<trk>
<trkseg>
<trkpt lat="' . $this->testLat . '" lon="' . $this->testLon . '">
<time>' . gmdate("Y-m-d\TH:i:s\Z", $this->testTimestamp) . '</time>
</trkpt>
</trkseg>
<trkseg>
<trkpt lat="' . ($this->testLat + 1) . '" lon="' . ($this->testLon + 1) . '">
<time>' . gmdate("Y-m-d\TH:i:s\Z", $this->testTimestamp + 1) . '</time>
</trkpt>
</trkseg>
</trk>
</gpx>';
$options = [
"http_errors" => false,
"multipart" => [
[
"name" => "gpx",
"contents" => $this->getStream($gpx),
"filename" => $this->testTrackName
],
[
"name" => "MAX_FILE_SIZE",
"contents" => 300000
]
],
];
$response = $this->http->post("/utils/import.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals(0, (int) $xml->error, "Wrong error status: $xml->message");
$this->assertEquals(1, (int) $xml->trackid, "Wrong error message");
$this->assertEquals(1, (int) $xml->trackcnt, "Wrong error message");
$this->assertEquals(1, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(2, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$expected = [
"id" => 1,
"user_id" => 1,
"name" => $this->testTrackName,
"comment" => null
];
$actual = $this->getConnection()->createQueryTable(
"tracks",
"SELECT * FROM tracks"
);
$this->assertTableContains($expected, $actual, "Wrong actual table data");
$expected = [
"id" => 1,
"time" => $this->testTimestamp,
"user_id" => 1,
"track_id" => 1,
"latitude" => $this->testLat,
"longitude" => $this->testLon,
"altitude" => null,
"speed" => null,
"bearing" => null,
"accuracy" => null,
"provider" => "gps",
"comment" => null,
"image_id" => null
];
$actual = $this->getConnection()->createQueryTable(
"positions",
"SELECT id, UNIX_TIMESTAMP(time) AS time, user_id, track_id, latitude, longitude,
altitude, speed, bearing, accuracy, provider, comment, image_id FROM positions"
);
$this->assertTableContains($expected, $actual, "Wrong actual table data");
$expected = [
"id" => 2,
"time" => $this->testTimestamp + 1,
"user_id" => 1,
"track_id" => 1,
"latitude" => $this->testLat + 1,
"longitude" => $this->testLon + 1,
"altitude" => null,
"speed" => null,
"bearing" => null,
"accuracy" => null,
"provider" => "gps",
"comment" => null,
"image_id" => null
];
$this->assertTableContains($expected, $actual, "Wrong actual table data");
}
public function testImportMultipleTracks() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(0, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$gpx = '<?xml version="1.0" encoding="UTF-8"?>
<gpx xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/1"
creator="μlogger" version="1.1">
<trk>
<trkseg>
<trkpt lat="' . $this->testLat . '" lon="' . $this->testLon . '">
<time>' . gmdate("Y-m-d\TH:i:s\Z", $this->testTimestamp) . '</time>
</trkpt>
</trkseg>
</trk>
<trk>
<trkseg>
<trkpt lat="' . ($this->testLat + 1) . '" lon="' . ($this->testLon + 1) . '">
<time>' . gmdate("Y-m-d\TH:i:s\Z", $this->testTimestamp + 1) . '</time>
</trkpt>
</trkseg>
</trk>
</gpx>';
$options = [
"http_errors" => false,
"multipart" => [
[
"name" => "gpx",
"contents" => $this->getStream($gpx),
"filename" => $this->testTrackName
],
[
"name" => "MAX_FILE_SIZE",
"contents" => 300000
]
],
];
$response = $this->http->post("/utils/import.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals(0, (int) $xml->error, "Wrong error status: $xml->message");
$this->assertEquals(2, (int) $xml->trackid, "Wrong error message");
$this->assertEquals(2, (int) $xml->trackcnt, "Wrong error message");
$this->assertEquals(2, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(2, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$expected = [
"id" => 1,
"user_id" => 1,
"name" => $this->testTrackName,
"comment" => null
];
$actual = $this->getConnection()->createQueryTable(
"tracks",
"SELECT * FROM tracks"
);
$this->assertTableContains($expected, $actual, "Wrong actual table data");
$expected = [
"id" => 2,
"user_id" => 1,
"name" => $this->testTrackName,
"comment" => null
];
$this->assertTableContains($expected, $actual, "Wrong actual table data");
$expected = [
"id" => 1,
"time" => $this->testTimestamp,
"user_id" => 1,
"track_id" => 1,
"latitude" => $this->testLat,
"longitude" => $this->testLon,
"altitude" => null,
"speed" => null,
"bearing" => null,
"accuracy" => null,
"provider" => "gps",
"comment" => null,
"image_id" => null
];
$actual = $this->getConnection()->createQueryTable(
"positions",
"SELECT id, UNIX_TIMESTAMP(time) AS time, user_id, track_id, latitude, longitude,
altitude, speed, bearing, accuracy, provider, comment, image_id FROM positions"
);
$this->assertTableContains($expected, $actual, "Wrong actual table data");
$expected = [
"id" => 2,
"time" => $this->testTimestamp + 1,
"user_id" => 1,
"track_id" => 2,
"latitude" => $this->testLat + 1,
"longitude" => $this->testLon + 1,
"altitude" => null,
"speed" => null,
"bearing" => null,
"accuracy" => null,
"provider" => "gps",
"comment" => null,
"image_id" => null
];
$this->assertTableContains($expected, $actual, "Wrong actual table data");
}
public function testImportNoLongitude() {
global $lang;
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(0, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$gpx = '<?xml version="1.0" encoding="UTF-8"?>
<gpx xsi:schemaLocation="http://www.topografix.com/GPX/1/1
http://www.topografix.com/GPX/1/1/gpx.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/1"
creator="μlogger" version="1.1">
<trk>
<trkseg>
<trkpt lat="' . $this->testLat . '"></trkpt>
</trkseg>
</trk>
</gpx>';
$options = [
"http_errors" => false,
"multipart" => [
[
"name" => "gpx",
"contents" => $this->getStream($gpx),
"filename" => $this->testTrackName
],
[
"name" => "MAX_FILE_SIZE",
"contents" => 300000
]
],
];
$response = $this->http->post("/utils/import.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals(1, (int) $xml->error, "Wrong error status");
$this->assertEquals($lang["iparsefailure"], (string) $xml->message, "Wrong error status");
$this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(0, $this->getConnection()->getRowCount("positions"), "Wrong row count");
}
public function testImportNoLatitude() {
global $lang;
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(0, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$gpx = '<?xml version="1.0" encoding="UTF-8"?>
<gpx xsi:schemaLocation="http://www.topografix.com/GPX/1/1
http://www.topografix.com/GPX/1/1/gpx.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/1"
creator="μlogger" version="1.1">
<trk>
<trkseg>
<trkpt lon="' . $this->testLon . '"></trkpt>
</trkseg>
</trk>
</gpx>';
$options = [
"http_errors" => false,
"multipart" => [
[
"name" => "gpx",
"contents" => $this->getStream($gpx),
"filename" => $this->testTrackName
],
[
"name" => "MAX_FILE_SIZE",
"contents" => 300000
]
],
];
$response = $this->http->post("/utils/import.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals(1, (int) $xml->error, "Wrong error status");
$this->assertEquals($lang["iparsefailure"], (string) $xml->message, "Wrong error status");
$this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(0, $this->getConnection()->getRowCount("positions"), "Wrong row count");
}
public function testImportNoGPX() {
global $lang;
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(0, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$gpx = '<?xml version="1.0" encoding="UTF-8"?>
<trk>
<trkseg>
<trkpt lat="' . $this->testLat . '" lon="' . $this->testLon . '"></trkpt>
</trkseg>
</trk>';
$options = [
"http_errors" => false,
"multipart" => [
[
"name" => "gpx",
"contents" => $this->getStream($gpx),
"filename" => $this->testTrackName
],
[
"name" => "MAX_FILE_SIZE",
"contents" => 300000
]
],
];
$response = $this->http->post("/utils/import.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals(1, (int) $xml->error, "Wrong error status");
$this->assertEquals($lang["iparsefailure"], (string) $xml->message, "Wrong error status");
$this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(0, $this->getConnection()->getRowCount("positions"), "Wrong row count");
}
public function testImportCorrupt() {
global $lang;
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(0, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$gpx = '<?xml version="1.0" encoding="UTF-8"?>
<gpx xsi:schemaLocation="http://www.topografix.com/GPX/1/1
http://www.topografix.com/GPX/1/1/gpx.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.topografix.com/GPX/1/1"
creator="μlogger" version="1.1">
<trk>
<trkseg>
<trkpt lon="' . $this->testLon . '"></trkpt>
</trkseg>';
$options = [
"http_errors" => false,
"multipart" => [
[
"name" => "gpx",
"contents" => $this->getStream($gpx),
"filename" => $this->testTrackName
],
[
"name" => "MAX_FILE_SIZE",
"contents" => 300000
]
],
];
$response = $this->http->post("/utils/import.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals(1, (int) $xml->error, "Wrong error status");
$this->assertEquals(0, strpos((string) $xml->message, $lang["iparsefailure"]), "Wrong error status");
$this->assertEquals(0, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(0, $this->getConnection()->getRowCount("positions"), "Wrong row count");
}
private function getXMLfromResponse($response) {
$xml = false;
libxml_use_internal_errors(true);
try {
$xml = new SimpleXMLElement((string) $response->getBody());
} catch (Exception $e) { /* ignore */ }
return $xml;
}
private function getStream($string) {
$stream = tmpfile();
fwrite($stream, $string);
fseek($stream, 0);
return $stream;
}
}
?>

View File

@ -0,0 +1,915 @@
<?php
require_once(__DIR__ . "/../lib/UloggerAPITestCase.php");
if (!defined("ROOT_DIR")) { define("ROOT_DIR", __DIR__ . "/../.."); }
require_once(ROOT_DIR . "/helpers/config.php");
require_once(ROOT_DIR . "/lang.php");
class InternalAPITest extends UloggerAPITestCase {
/* getpositions */
public function testGetPositionsAdmin() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$trackId = $this->addTestTrack($this->testUserId);
$this->addTestPosition($this->testUserId, $trackId, $this->testTimestamp);
$this->addTestPosition($this->testUserId, $trackId, $this->testTimestamp + 1);
$this->assertEquals(1, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(2, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "userid" => $this->testUserId, "trackid" => $trackId ],
];
$response = $this->http->post("/utils/getpositions.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals($xml->position->count(), 2, "Wrong count of positions");
$position = $xml->position[0];
$this->assertEquals((int) $position["id"], 1, "Wrong position id");
$this->assertEquals((float) $position->latitude, $this->testLat, "Wrong latitude");
$this->assertEquals((float) $position->longitude, $this->testLon, "Wrong longitude");
$this->assertEquals((int) $position->timestamp, $this->testTimestamp, "Wrong timestamp");
$this->assertEquals((string) $position->username, $this->testAdminUser, "Wrong username");
$this->assertEquals((string) $position->trackname, $this->testTrackName, "Wrong trackname");
$position = $xml->position[1];
$this->assertEquals((int) $position["id"], 2, "Wrong position id");
$this->assertEquals((float) $position->latitude, $this->testLat, "Wrong latitude");
$this->assertEquals((float) $position->longitude, $this->testLon, "Wrong longitude");
$this->assertEquals((int) $position->timestamp, $this->testTimestamp + 1, "Wrong timestamp");
$this->assertEquals((string) $position->username, $this->testAdminUser, "Wrong username");
$this->assertEquals((string) $position->trackname, $this->testTrackName, "Wrong trackname");
}
public function testGetPositionsUser() {
$userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$this->assertTrue($this->authenticate($this->testUser, $this->testPass), "Authentication failed");
$trackId = $this->addTestTrack($userId);
$this->addTestPosition($userId, $trackId, $this->testTimestamp);
$this->addTestPosition($userId, $trackId, $this->testTimestamp + 1);
$this->assertEquals(1, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(2, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "userid" => $userId, "trackid" => $trackId ],
];
$response = $this->http->post("/utils/getpositions.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals($xml->position->count(), 2, "Wrong count of positions");
$position = $xml->position[0];
$this->assertEquals((int) $position["id"], 1, "Wrong position id");
$this->assertEquals((float) $position->latitude, $this->testLat, "Wrong latitude");
$this->assertEquals((float) $position->longitude, $this->testLon, "Wrong longitude");
$this->assertEquals((int) $position->timestamp, $this->testTimestamp, "Wrong timestamp");
$this->assertEquals((string) $position->username, $this->testUser, "Wrong username");
$this->assertEquals((string) $position->trackname, $this->testTrackName, "Wrong trackname");
$position = $xml->position[1];
$this->assertEquals((int) $position["id"], 2, "Wrong position id");
$this->assertEquals((float) $position->latitude, $this->testLat, "Wrong latitude");
$this->assertEquals((float) $position->longitude, $this->testLon, "Wrong longitude");
$this->assertEquals((int) $position->timestamp, $this->testTimestamp + 1, "Wrong timestamp");
$this->assertEquals((string) $position->username, $this->testUser, "Wrong username");
$this->assertEquals((string) $position->trackname, $this->testTrackName, "Wrong trackname");
}
public function testGetPositionsOtherUser() {
$userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $userId);
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$this->assertTrue($this->authenticate($this->testUser, $this->testPass), "Authentication failed");
$trackId = $this->addTestTrack($userId);
$this->addTestPosition($userId, $trackId, $this->testTimestamp);
$this->addTestPosition($userId, $trackId, $this->testTimestamp + 1);
$this->assertEquals(1, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(2, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "userid" => $this->testUserId, "trackid" => $trackId ],
];
$response = $this->http->post("/utils/getpositions.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals($xml->position->count(), 0, "Wrong count of positions");
}
public function testGetPositionsOtherUserByAdmin() {
$userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $userId);
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$this->assertTrue($this->authenticate(), "Authentication failed");
$trackId = $this->addTestTrack($userId);
$this->addTestPosition($userId, $trackId, $this->testTimestamp);
$this->addTestPosition($userId, $trackId, $this->testTimestamp + 1);
$this->assertEquals(1, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(2, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "userid" => $userId, "trackid" => $trackId ],
];
$response = $this->http->post("/utils/getpositions.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals($xml->position->count(), 2, "Wrong count of positions");
$position = $xml->position[0];
$this->assertEquals((int) $position["id"], 1, "Wrong position id");
$this->assertEquals((float) $position->latitude, $this->testLat, "Wrong latitude");
$this->assertEquals((float) $position->longitude, $this->testLon, "Wrong longitude");
$this->assertEquals((int) $position->timestamp, $this->testTimestamp, "Wrong timestamp");
$this->assertEquals((string) $position->username, $this->testUser, "Wrong username");
$this->assertEquals((string) $position->trackname, $this->testTrackName, "Wrong trackname");
$position = $xml->position[1];
$this->assertEquals((int) $position["id"], 2, "Wrong position id");
$this->assertEquals((float) $position->latitude, $this->testLat, "Wrong latitude");
$this->assertEquals((float) $position->longitude, $this->testLon, "Wrong longitude");
$this->assertEquals((int) $position->timestamp, $this->testTimestamp + 1, "Wrong timestamp");
$this->assertEquals((string) $position->username, $this->testUser, "Wrong username");
$this->assertEquals((string) $position->trackname, $this->testTrackName, "Wrong trackname");
}
public function testGetPositionsNoTrackId() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$trackId = $this->addTestTrack($this->testUserId);
$this->addTestPosition($this->testUserId, $trackId, $this->testTimestamp);
$this->addTestPosition($this->testUserId, $trackId, $this->testTimestamp + 1);
$this->assertEquals(1, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(2, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "userid" => $this->testUserId ],
];
$response = $this->http->post("/utils/getpositions.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals($xml->position->count(), 1, "Wrong count of positions");
$position = $xml->position[0];
$this->assertEquals((int) $position["id"], 2, "Wrong position id");
$this->assertEquals((float) $position->latitude, $this->testLat, "Wrong latitude");
$this->assertEquals((float) $position->longitude, $this->testLon, "Wrong longitude");
$this->assertEquals((int) $position->timestamp, $this->testTimestamp + 1, "Wrong timestamp");
$this->assertEquals((string) $position->username, $this->testAdminUser, "Wrong username");
$this->assertEquals((string) $position->trackname, $this->testTrackName, "Wrong trackname");
}
public function testGetPositionsNoUserId() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$trackId = $this->addTestTrack($this->testUserId);
$this->addTestPosition($this->testUserId, $trackId, $this->testTimestamp);
$this->addTestPosition($this->testUserId, $trackId, $this->testTimestamp + 1);
$this->assertEquals(1, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals(2, $this->getConnection()->getRowCount("positions"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "trackid" => $trackId ],
];
$response = $this->http->post("/utils/getpositions.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is not false");
$this->assertEquals($xml->position->count(), 0, "Wrong count of positions");
}
public function testGetPositionsNoAuth() {
$trackId = $this->addTestTrack($this->testUserId);
$this->addTestPosition($this->testUserId, $trackId, $this->testTimestamp);
$this->assertEquals(1, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "userid" => $this->testUserId, "trackid" => $trackId ],
];
$response = $this->http->post("/utils/getpositions.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is not false");
$this->assertEquals($xml->position->count(), 0, "Wrong count of positions");
}
/* gettracks.php */
public function testGetTracksAdmin() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->addTestTrack($this->testUserId);
$this->addTestTrack($this->testUserId, $this->testTrackName . "2");
$this->assertEquals(2, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "userid" => $this->testUserId ],
];
$response = $this->http->post("/utils/gettracks.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals($xml->track->count(), 2, "Wrong count of tracks");
$track = $xml->track[0];
$this->assertEquals((int) $track->trackid, $this->testTrackId2, "Wrong track id");
$this->assertEquals((string) $track->trackname, $this->testTrackName . "2", "Wrong track name");
$track = $xml->track[1];
$this->assertEquals((int) $track->trackid, $this->testTrackId, "Wrong track id");
$this->assertEquals((string) $track->trackname, $this->testTrackName, "Wrong track name");
}
public function testGetTracksUser() {
$userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$this->assertTrue($this->authenticate($this->testUser, $this->testPass), "Authentication failed");
$this->addTestTrack($userId);
$this->addTestTrack($userId, $this->testTrackName . "2");
$this->assertEquals(2, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "userid" => $userId ],
];
$response = $this->http->post("/utils/gettracks.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals($xml->track->count(), 2, "Wrong count of tracks");
$track = $xml->track[0];
$this->assertEquals((int) $track->trackid, $this->testTrackId2, "Wrong track id");
$this->assertEquals((string) $track->trackname, $this->testTrackName . "2", "Wrong track name");
$track = $xml->track[1];
$this->assertEquals((int) $track->trackid, $this->testTrackId, "Wrong track id");
$this->assertEquals((string) $track->trackname, $this->testTrackName, "Wrong track name");
}
public function testGetTracksOtherUser() {
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$this->assertTrue($this->authenticate($this->testUser, $this->testPass), "Authentication failed");
$this->addTestTrack($this->testUserId);
$this->addTestTrack($this->testUserId, $this->testTrackName . "2");
$this->assertEquals(2, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "userid" => $this->testUserId ],
];
$response = $this->http->post("/utils/gettracks.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals($xml->track->count(), 0, "Wrong count of tracks");
}
public function testGetTracksNoUserId() {
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$this->assertTrue($this->authenticate($this->testUser, $this->testPass), "Authentication failed");
$this->addTestTrack($this->testUserId);
$this->addTestTrack($this->testUserId, $this->testTrackName . "2");
$this->assertEquals(2, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$options = [
"http_errors" => false,
];
$response = $this->http->post("/utils/gettracks.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is not false");
$this->assertEquals($xml->track->count(), 0, "Wrong count of tracks");
}
public function testGetTracksNoAuth() {
$this->addTestTrack($this->testUserId);
$this->addTestTrack($this->testUserId, $this->testTrackName . "2");
$this->assertEquals(2, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "userid" => $this->testUserId ],
];
$response = $this->http->post("/utils/gettracks.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is not false");
$this->assertEquals($xml->track->count(), 0, "Wrong count of tracks");
}
/* changepass.php */
public function testChangePassNoAuth() {
$options = [
"http_errors" => false,
"form_params" => [ "userid" => $this->testUserId ],
];
$response = $this->http->post("/utils/changepass.php", $options);
$this->assertEquals(401, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is not false");
$this->assertEquals((int) $xml->error, 1, "Wrong error status");
$this->assertEquals((string) $xml->message, "Unauthorized", "Wrong error message");
}
public function testChangePassEmpty() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$options = [
"http_errors" => false,
"form_params" => [ "login" => $this->testAdminUser ],
];
$response = $this->http->post("/utils/changepass.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is not false");
$this->assertEquals((int) $xml->error, 1, "Wrong error status");
$this->assertEquals((string) $xml->message, "Empty password", "Wrong error message");
}
public function testChangePassNoUser() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$options = [
"http_errors" => false,
"form_params" => [
"login" => $this->testUser,
"pass" => $this->testPass,
],
];
$response = $this->http->post("/utils/changepass.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is not false");
$this->assertEquals((int) $xml->error, 1, "Wrong error status");
$this->assertEquals((string) $xml->message, "User unknown", "Wrong error message");
}
public function testChangePassWrongOldpass() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$options = [
"http_errors" => false,
"form_params" => [
"oldpass" => "badpass",
"pass" => "newpass",
],
];
$response = $this->http->post("/utils/changepass.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is not false");
$this->assertEquals((int) $xml->error, 1, "Wrong error status");
$this->assertEquals((string) $xml->message, "Wrong old password", "Wrong error message");
}
public function testChangePassNoOldpass() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$options = [
"http_errors" => false,
"form_params" => [
"pass" => "newpass",
],
];
$response = $this->http->post("/utils/changepass.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is not false");
$this->assertEquals((int) $xml->error, 1, "Wrong error status");
$this->assertEquals((string) $xml->message, "Wrong old password", "Wrong error message");
}
public function testChangePassSelfAdmin() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$newPass = "newpass";
$options = [
"http_errors" => false,
"form_params" => [
"oldpass" => $this->testAdminPass,
"pass" => $newPass,
],
];
$response = $this->http->post("/utils/changepass.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is not false");
$this->assertEquals((int) $xml->error, 0, "Wrong error status");
$this->assertTrue(password_verify($newPass, $this->pdoGetColumn("SELECT password FROM users")), "Wrong actual password hash");
}
public function testChangePassSelfUser() {
$userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertTrue($this->authenticate($this->testUser, $this->testPass), "Authentication failed");
$newPass = "newpass";
$options = [
"http_errors" => false,
"form_params" => [
"oldpass" => $this->testPass,
"pass" => $newPass,
],
];
$response = $this->http->post("/utils/changepass.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is not false");
$this->assertEquals((int) $xml->error, 0, "Wrong error status");
$this->assertTrue(password_verify($newPass, $this->pdoGetColumn("SELECT password FROM users WHERE id = $userId")), "Wrong actual password hash");
}
public function testChangePassOtherAdmin() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$newPass = "newpass";
$options = [
"http_errors" => false,
"form_params" => [
"login" => $this->testUser,
"pass" => $newPass,
],
];
$response = $this->http->post("/utils/changepass.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is not false");
$this->assertEquals((int) $xml->error, 0, "Wrong error status");
$this->assertTrue(password_verify($newPass, $this->pdoGetColumn("SELECT password FROM users WHERE id = $userId")), "Wrong actual password hash");
}
public function testChangePassOtherUser() {
$userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$userId2 = $this->addTestUser($this->testUser2, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertTrue($this->authenticate($this->testUser, $this->testPass), "Authentication failed");
$newPass = "newpass";
$options = [
"http_errors" => false,
"form_params" => [
"login" => $this->testUser2,
"pass" => $newPass,
],
];
$response = $this->http->post("/utils/changepass.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is not false");
$this->assertEquals((int) $xml->error, 1, "Wrong error status");
$this->assertEquals((string) $xml->message, "Unauthorized", "Wrong error message");
}
/* handletrack.php */
public function testHandleTrackDeleteAdmin() {
global $lang;
$this->assertTrue($this->authenticate(), "Authentication failed");
$userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$trackId = $this->addTestTrack($userId);
$trackId2 = $this->addTestTrack($userId);
$this->assertEquals(2, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "trackid" => $trackId, "action" => "delete" ],
];
$response = $this->http->post("/utils/handletrack.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals((int) $xml->error, 0, "Wrong error status");
$this->assertEquals(1, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals($trackId2, $this->pdoGetColumn("SELECT id FROM tracks WHERE id = $trackId2"), "Wrong actual track id");
}
public function testHandleTrackDeleteSelf() {
global $lang;
$userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$this->assertTrue($this->authenticate($this->testUser, $this->testPass), "Authentication failed");
$trackId = $this->addTestTrack($userId);
$trackId2 = $this->addTestTrack($userId);
$this->assertEquals(2, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "trackid" => $trackId, "action" => "delete" ],
];
$response = $this->http->post("/utils/handletrack.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals((int) $xml->error, 0, "Wrong error status");
$this->assertEquals(1, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertEquals($trackId2, $this->pdoGetColumn("SELECT id FROM tracks WHERE id = $trackId2"), "Wrong actual track id");
}
public function testHandleTrackDeleteOtherUser() {
global $lang;
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$this->assertTrue($this->authenticate($this->testUser, $this->testPass), "Authentication failed");
$trackId = $this->addTestTrack($this->testUserId);
$this->assertEquals(1, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "trackid" => $trackId, "action" => "delete" ],
];
$response = $this->http->post("/utils/handletrack.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals((int) $xml->error, 1, "Wrong error status");
$this->assertEquals((string) $xml->message, $lang["servererror"], "Wrong error message");
}
public function testHandleTrackUpdate() {
global $lang;
$newName = "New name";
$this->assertTrue($this->authenticate(), "Authentication failed");
$userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$trackId = $this->addTestTrack($userId);
$trackId2 = $this->addTestTrack($userId);
$this->assertEquals(2, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "trackid" => $trackId, "action" => "update", "trackname" => $newName ],
];
$response = $this->http->post("/utils/handletrack.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals((int) $xml->error, 0, "Wrong error status");
$this->assertEquals(2, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$row1 = [
"id" => $trackId2,
"user_id" => $userId,
"name" => $this->testTrackName,
"comment" => $this->testTrackComment
];
$row2 = [
"id" => $trackId,
"user_id" => $userId,
"name" => $newName,
"comment" => $this->testTrackComment
];
$actual = $this->getConnection()->createQueryTable(
"tracks",
"SELECT * FROM tracks"
);
$this->assertTableContains($row1, $actual, "Wrong actual table data");
$this->assertTableContains($row2, $actual, "Wrong actual table data");
}
public function testHandleTrackUpdateEmptyName() {
global $lang;
$this->assertTrue($this->authenticate(), "Authentication failed");
$userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$trackId = $this->addTestTrack($userId);
$trackId2 = $this->addTestTrack($userId);
$this->assertEquals(2, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "trackid" => $trackId, "action" => "update", "trackname" => "" ],
];
$response = $this->http->post("/utils/handletrack.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals((int) $xml->error, 1, "Wrong error status");
$this->assertEquals((string) $xml->message, $lang["servererror"], "Wrong error message");
$this->assertEquals(2, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
}
public function testHandleTrackUpdateNonexistantTrack() {
global $lang;
$newName = "New name";
$this->assertTrue($this->authenticate(), "Authentication failed");
$userId = $this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$trackId = $this->addTestTrack($userId);
$nonexistantTrackId = $trackId + 1;
$this->assertEquals(1, $this->getConnection()->getRowCount("tracks"), "Wrong row count");
$this->assertFalse($this->pdoGetColumn("SELECT id FROM tracks WHERE id = $nonexistantTrackId"), "Nonexistant track exists");
$options = [
"http_errors" => false,
"form_params" => [ "trackid" => $nonexistantTrackId, "action" => "update", "trackname" => $newName ],
];
$response = $this->http->post("/utils/handletrack.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals((int) $xml->error, 1, "Wrong error status");
$this->assertEquals((string) $xml->message, $lang["servererror"], "Wrong error message");
}
public function testHandleTrackMissingAction() {
global $lang;
$this->assertTrue($this->authenticate(), "Authentication failed");
$options = [
"http_errors" => false,
];
$response = $this->http->post("/utils/handletrack.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals((int) $xml->error, 1, "Wrong error status");
$this->assertEquals((string) $xml->message, $lang["servererror"], "Wrong error message");
}
/* handleuser.php */
public function testHandleUserMissingAction() {
global $lang;
$this->assertTrue($this->authenticate(), "Authentication failed");
$options = [
"http_errors" => false,
];
$response = $this->http->post("/utils/handleuser.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals((int) $xml->error, 1, "Wrong error status");
$this->assertEquals((string) $xml->message, $lang["servererror"], "Wrong error message");
}
public function testHandleUserNonAdmin() {
global $lang;
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$this->assertTrue($this->authenticate($this->testUser, $this->testPass), "Authentication failed");
$options = [
"http_errors" => false,
"form_params" => [ "action" => "delete", "login" => "test" ],
];
$response = $this->http->post("/utils/handleuser.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals((int) $xml->error, 1, "Wrong error status");
$this->assertEquals((string) $xml->message, $lang["servererror"], "Wrong error message");
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
}
public function testHandleUserSelf() {
global $lang;
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(1, $this->getConnection()->getRowCount("users"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "action" => "delete", "login" => $this->testAdminUser ],
];
$response = $this->http->post("/utils/handleuser.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals((int) $xml->error, 1, "Wrong error status");
$this->assertEquals((string) $xml->message, $lang["servererror"], "Wrong error message");
$this->assertEquals(1, $this->getConnection()->getRowCount("users"), "Wrong row count");
}
public function testHandleUserEmptyLogin() {
global $lang;
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(1, $this->getConnection()->getRowCount("users"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "action" => "delete", "login" => "" ],
];
$response = $this->http->post("/utils/handleuser.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals((int) $xml->error, 1, "Wrong error status");
$this->assertEquals((string) $xml->message, $lang["servererror"], "Wrong error message");
$this->assertEquals(1, $this->getConnection()->getRowCount("users"), "Wrong row count");
}
public function testHandleUserNoAuth() {
global $lang;
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "action" => "delete", "login" => $this->testUser ],
];
$response = $this->http->post("/utils/handleuser.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals((int) $xml->error, 1, "Wrong error status");
$this->assertEquals((string) $xml->message, $lang["servererror"], "Wrong error message");
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
}
public function testHandleUserAdd() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(1, $this->getConnection()->getRowCount("users"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "action" => "add", "login" => $this->testUser, "pass" => $this->testPass ],
];
$response = $this->http->post("/utils/handleuser.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals(0, (int) $xml->error, "Wrong error status");
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$expected = [
"login" => $this->testUser,
];
$actual = $this->getConnection()->createQueryTable(
"users",
"SELECT login FROM users"
);
$this->assertTableContains($expected, $actual, "Wrong actual table data");
$this->assertTrue(password_verify($this->testPass, $this->pdoGetColumn("SELECT password FROM users WHERE login = '$this->testUser'")), "Wrong actual password hash");
}
public function testHandleUserAddSameLogin() {
global $lang;
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "action" => "add", "login" => $this->testUser, "pass" => $this->testPass ],
];
$response = $this->http->post("/utils/handleuser.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals(1, (int) $xml->error, "Wrong error status");
$this->assertEquals((string) $xml->message, $lang["userexists"], "Wrong error message");
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
}
public function testHandleUserUpdate() {
$newPass = $this->testPass . "new";
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "action" => "update", "login" => $this->testUser, "pass" => $newPass ],
];
$response = $this->http->post("/utils/handleuser.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals(0, (int) $xml->error, "Wrong error status");
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$this->assertTrue(password_verify($newPass, $this->pdoGetColumn("SELECT password FROM users WHERE login = '$this->testUser'")), "Wrong actual password hash");
}
public function testHandleUserUpdateEmptyPass() {
global $lang;
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "action" => "update", "login" => $this->testUser, "pass" => "" ],
];
$response = $this->http->post("/utils/handleuser.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals(1, (int) $xml->error, "Wrong error status");
$this->assertEquals((string) $xml->message, $lang["servererror"], "Wrong error message");
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$this->assertTrue(password_verify($this->testPass, $this->pdoGetColumn("SELECT password FROM users WHERE login = '$this->testUser'")), "Wrong actual password hash");
}
public function testHandleUserDelete() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(2, $this->getConnection()->getRowCount("users"), "Wrong row count");
$options = [
"http_errors" => false,
"form_params" => [ "action" => "delete", "login" => $this->testUser ],
];
$response = $this->http->post("/utils/handleuser.php", $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$xml = $this->getXMLfromResponse($response);
$this->assertTrue($xml !== false, "XML object is false");
$this->assertEquals(0, (int) $xml->error, "Wrong error status");
$this->assertEquals(1, $this->getConnection()->getRowCount("users"), "Wrong row count");
}
private function getXMLfromResponse($response) {
$xml = false;
libxml_use_internal_errors(true);
try {
$xml = new SimpleXMLElement((string) $response->getBody());
} catch (Exception $e) { /* ignore */ }
return $xml;
}
}
?>