142 lines
7.3 KiB
PHP
Raw Permalink Normal View History

2017-09-05 09:36:10 +02:00
<?php
require_once(__DIR__ . "/../lib/UloggerDatabaseTestCase.php");
require_once(__DIR__ . "/../../helpers/track.php");
class TrackTest extends UloggerDatabaseTestCase {
2021-04-22 19:47:52 +02:00
public function testAddTrack(): void {
2019-01-24 19:07:41 +01:00
$this->addTestUser();
2017-09-05 09:36:10 +02:00
$trackId = uTrack::add($this->testUserId, $this->testTrackName, $this->testTrackComment);
2021-04-22 19:47:52 +02:00
self::assertNotFalse($trackId, "Track id should not be false");
self::assertEquals(1, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
self::assertEquals(1, $trackId, "Wrong track id returned");
2017-09-05 09:36:10 +02:00
$expected = [ "id" => $trackId, "user_id" => $this->testUserId, "name" => $this->testTrackName, "comment" => $this->testTrackComment ];
$actual = $this->getConnection()->createQueryTable("tracks", "SELECT id, user_id, name, comment FROM tracks");
$this->assertTableContains($expected, $actual, "Wrong actual table data");
2021-04-22 19:47:52 +02:00
self::assertFalse(uTrack::add("", $this->testTrackName), "Adding track with empty user id should fail");
self::assertFalse(uTrack::add($this->testUserId, ""), "Adding track with empty name should fail");
2017-09-05 09:36:10 +02:00
}
2021-04-22 19:47:52 +02:00
public function testDeleteTrack(): void {
2019-01-24 19:07:41 +01:00
$userId = $this->addTestUser();
$trackId = $this->addTestTrack($userId);
$this->addTestPosition($userId, $trackId);
2021-04-22 19:47:52 +02:00
self::assertEquals(1, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
self::assertEquals(1, $this->getConnection()->getRowCount('positions'), "Wrong row count");
2017-09-05 09:36:10 +02:00
$track = new uTrack($trackId);
$track->delete();
2021-04-22 19:47:52 +02:00
self::assertEquals(0, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
self::assertEquals(0, $this->getConnection()->getRowCount('positions'), "Wrong row count");
self::assertFalse($track->isValid, "Deleted track should not be valid");
2017-09-05 09:36:10 +02:00
}
2021-04-22 19:47:52 +02:00
public function testAddPosition(): void {
2019-01-24 19:07:41 +01:00
$userId = $this->addTestUser();
$userId2 = $this->addTestUser($this->testUser2);
$trackId = $this->addTestTrack($userId);
2021-04-22 19:47:52 +02:00
self::assertEquals(1, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
2017-09-05 09:36:10 +02:00
$track = new uTrack($trackId + 1);
2019-07-12 21:50:21 +02:00
$posId = $track->addPosition($userId, $this->testTimestamp, $this->testLat, $this->testLon, $this->testAltitude, $this->testSpeed, $this->testBearing, $this->testAccuracy, $this->testProvider, $this->testComment, $this->testImage);
2021-04-22 19:47:52 +02:00
self::assertEquals(0, $this->getConnection()->getRowCount('positions'), "Wrong row count");
self::assertFalse($posId, "Adding position with nonexistant track should fail");
2017-09-05 09:36:10 +02:00
$track = new uTrack($trackId);
2019-07-12 21:50:21 +02:00
$posId = $track->addPosition($userId2, $this->testTimestamp, $this->testLat, $this->testLon, $this->testAltitude, $this->testSpeed, $this->testBearing, $this->testAccuracy, $this->testProvider, $this->testComment, $this->testImage);
2021-04-22 19:47:52 +02:00
self::assertEquals(0, $this->getConnection()->getRowCount('positions'), "Wrong row count");
self::assertFalse($posId, "Adding position with wrong user should fail");
2017-09-05 09:36:10 +02:00
2019-07-12 21:50:21 +02:00
$posId = $track->addPosition($userId, $this->testTimestamp, $this->testLat, $this->testLon, $this->testAltitude, $this->testSpeed, $this->testBearing, $this->testAccuracy, $this->testProvider, $this->testComment, $this->testImage);
2021-04-22 19:47:52 +02:00
self::assertEquals(1, $this->getConnection()->getRowCount('positions'), "Wrong row count");
2017-09-05 09:36:10 +02:00
$expected = [
"id" => $posId,
"user_id" => $this->testUserId,
"track_id" => $trackId,
"time" => $this->testTimestamp,
"latitude" => $this->testLat,
"longitude" => $this->testLon,
"altitude" => $this->testAltitude,
"speed" => $this->testSpeed,
"bearing" => $this->testBearing,
"accuracy" => $this->testAccuracy,
"provider" => $this->testProvider,
"comment" => $this->testComment,
2019-07-12 21:50:21 +02:00
"image" => $this->testImage
2017-09-05 09:36:10 +02:00
];
$actual = $this->getConnection()->createQueryTable(
"positions",
2019-07-12 21:50:21 +02:00
"SELECT id, user_id, track_id, " . $this->unix_timestamp('time') . " AS time, latitude, longitude, altitude, speed, bearing, accuracy, provider, comment, image FROM positions"
2017-09-05 09:36:10 +02:00
);
$this->assertTableContains($expected, $actual, "Wrong actual table data");
2022-03-19 17:48:44 +01:00
$posId = $track->addPosition($userId, null, $this->testLat, $this->testLon);
2021-04-22 19:47:52 +02:00
self::assertFalse($posId, "Adding position with null time stamp should fail");
2022-03-19 17:48:44 +01:00
$posId = $track->addPosition($userId, $this->testTimestamp, null, $this->testLon);
2021-04-22 19:47:52 +02:00
self::assertFalse($posId, "Adding position with null latitude should fail");
2022-03-19 17:48:44 +01:00
$posId = $track->addPosition($userId, $this->testTimestamp, $this->testLat, null);
2021-04-22 19:47:52 +02:00
self::assertFalse($posId, "Adding position with null longitude should fail");
2017-09-05 09:36:10 +02:00
2019-01-24 19:07:41 +01:00
$posId = $track->addPosition($userId, "", $this->testLat, $this->testLon);
2021-04-22 19:47:52 +02:00
self::assertFalse($posId, "Adding position with empty time stamp should fail");
2019-01-24 19:07:41 +01:00
$posId = $track->addPosition($userId, $this->testTimestamp, "", $this->testLon);
2021-04-22 19:47:52 +02:00
self::assertFalse($posId, "Adding position with empty latitude should fail");
2019-01-24 19:07:41 +01:00
$posId = $track->addPosition($userId, $this->testTimestamp, $this->testLat, "");
2021-04-22 19:47:52 +02:00
self::assertFalse($posId, "Adding position with empty longitude should fail");
2017-09-05 09:36:10 +02:00
}
2021-04-22 19:47:52 +02:00
public function testGetAll(): void {
2019-01-24 19:07:41 +01:00
$this->addTestTrack($this->addTestUser());
$this->addTestTrack($this->addTestUser($this->testUser2));
2021-04-22 19:47:52 +02:00
self::assertEquals(2, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
2017-09-05 09:36:10 +02:00
$trackArr = uTrack::getAll();
2021-04-22 19:47:52 +02:00
self::assertCount(2, $trackArr, "Wrong array size");
self::assertInstanceOf(uTrack::class, $trackArr[0], "Wrong array member");
2017-09-05 09:36:10 +02:00
}
2021-04-22 19:47:52 +02:00
public function testDeleteAll(): void {
2019-01-24 19:07:41 +01:00
$userId = $this->addTestUser();
$trackId = $this->addTestTrack($userId);
$this->addTestTrack($userId);
$this->addTestPosition($userId, $trackId);
2017-09-05 09:36:10 +02:00
2019-01-24 19:07:41 +01:00
$userId2 = $this->addTestUser($this->testUser2);
$trackId2 = $this->addTestTrack($userId2);
$this->addTestPosition($userId2, $trackId2);
2017-09-05 09:36:10 +02:00
2021-04-22 19:47:52 +02:00
self::assertEquals(3, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
self::assertEquals(2, $this->getConnection()->getRowCount('positions'), "Wrong row count");
2017-09-05 09:36:10 +02:00
2019-01-24 19:07:41 +01:00
uTrack::deleteAll($userId);
2021-04-22 19:47:52 +02:00
self::assertEquals(1, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
self::assertEquals(1, $this->getConnection()->getRowCount('positions'), "Wrong row count");
2022-03-19 17:48:44 +01:00
self::assertFalse(uTrack::deleteAll(null), "User id should not be empty");
2017-09-05 09:36:10 +02:00
}
2021-04-22 19:47:52 +02:00
public function testUpdate(): void {
2019-01-24 19:07:41 +01:00
$userId = $this->addTestUser();
$trackId = $this->addTestTrack($userId);
2017-09-05 09:36:10 +02:00
$track = new uTrack($trackId);
$track->update("newName", "newComment");
$expected = [ "id" => $trackId, "user_id" => $this->testUserId, "name" => "newName", "comment" => "newComment" ];
$actual = $this->getConnection()->createQueryTable("tracks", "SELECT id, user_id, name, comment FROM tracks");
$this->assertTableContains($expected, $actual, "Wrong actual table data");
$trackInvalid = new uTrack($trackId + 1);
2021-04-22 19:47:52 +02:00
self::assertFalse($trackInvalid->update("newName", "newComment"), "Updating nonexistant track should fail");
2017-09-05 09:36:10 +02:00
}
2021-04-22 19:47:52 +02:00
public function testIsValid(): void {
2019-01-24 19:07:41 +01:00
$userId = $this->addTestUser();
$trackId = $this->addTestTrack($userId);
2017-09-05 09:36:10 +02:00
$trackValid = new uTrack($trackId);
2021-04-22 19:47:52 +02:00
self::assertTrue($trackValid->isValid, "Track should be valid");
2017-09-05 09:36:10 +02:00
$trackInvalid = new uTrack($trackId + 1);
2021-04-22 19:47:52 +02:00
self::assertFalse($trackInvalid->isValid, "Track should not be valid");
2017-09-05 09:36:10 +02:00
}
}
?>