Add basic tests

This commit is contained in:
Bartek Fabiszewski 2017-09-05 09:36:10 +02:00
parent 33e77d9962
commit d67d342dfb
15 changed files with 2974 additions and 0 deletions

View File

@ -0,0 +1,17 @@
<?xml version="1.0"?>
<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<database name="ulogger">
<table_data name="positions">
</table_data>
<table_data name="tracks">
</table_data>
<table_data name="users">
<!-- admin:admin -->
<row>
<field name="id">1</field>
<field name="login">admin</field>
<field name="password">$2y$10$7OvZrKgonVZM9lkzrTbiou.CVhO3HjPk5y0W9L68fVwPs/osBRIMq</field>
</row>
</table_data>
</database>
</mysqldump>

View File

@ -0,0 +1,11 @@
<?xml version="1.0"?>
<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<database name="ulogger">
<table_data name="positions">
</table_data>
<table_data name="tracks">
</table_data>
<table_data name="users">
</table_data>
</database>
</mysqldump>

View File

@ -0,0 +1,171 @@
<?php
use PHPUnit\Framework\TestCase;
abstract class BaseDatabaseTestCase extends PHPUnit_Extensions_Database_TestCase {
static private $pdo = null;
private $conn = null;
protected $testUser = "testUser";
protected $testUser2 = "testUser2";
protected $testAdminUser = "admin";
protected $testAdminPass = "admin";
protected $testPass = "testPass1234567890-;";
protected $testUserId = 1;
protected $testUserId2 = 2;
protected $testUserId3 = 3;
protected $testTrackId = 1;
protected $testTrackId2 = 2;
protected $testTrackName = "test track";
protected $testTrackComment = "test track comment";
protected $testTimestamp = 1502974402;
protected $testLat = 0;
protected $testLon = 10.01;
protected $testAltitude = 10.01;
protected $testSpeed = 10.01;
protected $testBearing = 10.01;
protected $testAccuracy = 10;
protected $testProvider = "gps";
protected $testComment = "test comment";
protected $testImageId = 1;
public static function setUpBeforeClass() {
if (file_exists(__DIR__ . '/../.env')) {
$dotenv = new Dotenv\Dotenv(__DIR__ . '/..');
$dotenv->load();
$dotenv->required(['DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASS']);
}
$db_host = getenv('DB_HOST');
$db_name = getenv('DB_NAME');
$db_user = getenv('DB_USER');
$db_pass = getenv('DB_PASS');
$db_port = getenv('DB_PORT') ?: NULL;
$db_dsn = "mysql:dbname={$db_name};host={$db_host}";
if (!empty($db_port)) {
$db_dsn .= ";port={$db_port}";
}
// pdo connection
if (self::$pdo == null) {
self::$pdo = new PDO($db_dsn, $db_user, $db_pass);;
}
}
public static function tearDownAfterClass() {
self::$pdo = null;
}
/**
* Set up database connection
* This will also override uDb class connection
*
* @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
*/
public function getConnection() {
if ($this->conn === null) {
$this->conn = $this->createDefaultDBConnection(self::$pdo, getenv('DB_NAME'));
}
return $this->conn;
}
/**
* Create data set from xml fixture
*
* @return PHPUnit_Extensions_Database_DataSet_IDataSet
*/
protected function getDataSet() {
return $this->createMySQLXMLDataSet(__DIR__ . '/../fixtures/fixture_empty.xml');
}
/**
* Insert to database from array
*
* @param string $table Table name
* @param array $rowsArr Array of rows
* @return int|null Last insert id if available, NULL otherwise
*/
private function pdoInsert($table, $rowsArr = []) {
$ret = NULL;
if (!empty($rowsArr)) {
$values = ':' . implode(', :', array_keys($rowsArr));
$columns = implode(', ', array_keys($rowsArr));
$query = "INSERT INTO $table ($columns) VALUES ($values)";
$stmt = self::$pdo->prepare($query);
if ($stmt !== false) {
$stmt->execute(array_combine(explode(', ', $values), array_values($rowsArr)));
}
$ret = self::$pdo->lastInsertId();
}
return $ret;
}
/**
* Get single column from first row of query result
*
* @param string $query SQL query
* @param int $columnNumber Optional column number (default is first column)
* @return string|bool Column or false if no data
*/
protected function pdoGetColumn($query, $columnNumber = 0) {
$column = false;
$stmt = self::$pdo->query($query);
if ($stmt !== false) {
$column = $stmt->fetchColumn($columnNumber);
$stmt->closeCursor();
}
return $column;
}
/**
* Insert user data to database
* If parameters are omitted they default test values are used
*
* @param string $user User login
* @param string $pass User password
* @return int|bool User id or false on error
*/
protected function addTestUser($user = NULL, $pass = NULL) {
if (is_null($user)) { $user = $this->testUser; }
if (is_null($pass)) { $pass = $this->testPass; }
return $this->pdoInsert('users', [ 'login' => $user, 'password' => $pass ]);
}
/**
* Insert track data to database.
* If parameters are omitted they default test values are used
*
* @param int $userId Optional track id
* @param string $trackName Optional track name
* @param string $comment Optional comment
* @return int|bool Track id or false on error
*/
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; }
return $this->pdoInsert('tracks', [ 'user_id' => $userId, 'name' => $trackName, 'comment' => $comment ]);
}
/**
* Insert position data to database
* If parameters are omitted they default test values are used
*
* @param int $userId
* @param int $trackId
* @param int $timeStamp
* @param double $latitude
* @param double $longitude
* @return int|bool Position id or false on error
*/
protected function addTestPosition($userId = NULL, $trackId = NULL, $timeStamp = NULL, $latitude = NULL, $longitude = NULL) {
if (is_null($userId)) { $userId = $this->testUserId; }
if (is_null($trackId)) { $trackId = $this->testTrackId; }
if (is_null($timeStamp)) { $timeStamp = $this->testTimestamp; }
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 ]);
}
}
?>

View File

@ -0,0 +1,52 @@
<?php
use PHPUnit\Framework\TestCase;
require_once("BaseDatabaseTestCase.php");
class UloggerAPITestCase extends BaseDatabaseTestCase {
protected $http = null;
public function setUp() {
parent::setUp();
if (file_exists(__DIR__ . '/../.env')) {
$dotenv = new Dotenv\Dotenv(__DIR__ . '/..');
$dotenv->load();
$dotenv->required(['ULOGGER_URL']);
}
$url = getenv('ULOGGER_URL');
$this->http = new GuzzleHttp\Client([ 'base_uri' => $url, 'cookies' => true ]);
}
public function tearDown() {
parent::tearDown();
$this->http = null;
}
protected function getDataSet() {
return $this->createMySQLXMLDataSet(__DIR__ . '/../fixtures/fixture_admin.xml');
}
/**
* Authenticate on server
* @param string $user Login
*
* @return bool true on success, false otherwise
*/
public function authenticate($user = NULL, $pass = NULL) {
if (is_null($user)) { $user = $this->testAdminUser; }
if (is_null($pass)) { $pass = $this->testAdminPass; }
$options = [
'http_errors' => false,
'form_params' => [ 'action' => 'auth', 'user' => $user, 'pass' => $pass ],
];
$response = $this->http->post('/client/index.php', $options);
return ($response->getStatusCode() == 200);
}
}
?>

View File

@ -0,0 +1,40 @@
<?php
use PHPUnit\Framework\TestCase;
require_once("BaseDatabaseTestCase.php");
require_once(__DIR__ . "/../../helpers/db.php");
class UloggerDatabaseTestCase extends BaseDatabaseTestCase {
static private $udb = null;
public static function setUpBeforeClass() {
parent::setUpBeforeClass();
if (file_exists(__DIR__ . '/../.env')) {
$dotenv = new Dotenv\Dotenv(__DIR__ . '/..');
$dotenv->load();
$dotenv->required(['DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASS']);
}
$db_host = getenv('DB_HOST');
$db_name = getenv('DB_NAME');
$db_user = getenv('DB_USER');
$db_pass = getenv('DB_PASS');
$db_port = getenv('DB_PORT') ?: NULL;
// uDb connection
if (self::$udb == null) {
self::$udb = new ReflectionClass("uDb");
$dbInstance = self::$udb->getProperty('instance');
$dbInstance->setAccessible(true);
$dbInstance->setValue(new uDb($db_host, $db_user, $db_pass, $db_name, $db_port));
}
}
public static function tearDownAfterClass() {
parent::tearDownAfterClass();
self::$udb = null;
}
}
?>

20
.tests/phpunit.xml Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="../vendor/autoload.php">
<testsuites>
<testsuite name="µlogger test suite">
<directory suffix=".php">tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">..</directory>
<exclude>
<directory suffix=".php">../.tests</directory>
<directory suffix=".php">../vendor</directory>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
</logging>
</phpunit>

158
.tests/tests/AuthTest.php Normal file
View File

@ -0,0 +1,158 @@
<?php
use PHPUnit\Framework\TestCase;
require_once(__DIR__ . "/../../helpers/auth.php");
require_once(__DIR__ . "/../lib/UloggerDatabaseTestCase.php");
require_once(__DIR__ . "/../../helpers/config.php");
class AuthTest extends UloggerDatabaseTestCase {
public function setUp() {
$_REQUEST = [];
$_SESSION = [];
parent::setUp();
}
private function request($user, $pass) {
$request = [];
$request["action"] = "auth";
$request["user"] = $user;
$request["pass"] = $pass;
return $request;
}
/**
* @runInSeparateProcess
*/
public function testLogin() {
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
$_REQUEST = $this->request($this->testUser, $this->testPass);
$auth = new uAuth();
$this->assertTrue($auth->isAuthenticated(), "Not authenticated");
$this->assertTrue($auth->isLoginAttempt(), "Not login attempt");
$this->assertTrue($auth->user instanceof uUser, "User variable not set");
$this->assertEquals($this->testUser, $auth->user->login, "Wrong login");
$this->assertEquals($_SESSION["user"]->login, $auth->user->login, "Wrong login");
$this->assertTrue($_SESSION["user"] instanceof uUser, "User not set in session");
}
/**
* @runInSeparateProcess
*/
public function testLoginBadPass() {
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
$_REQUEST = $this->request($this->testUser, "badPass");
$auth = new uAuth();
$this->assertFalse($auth->isAuthenticated(), "Should not be authenticated");
$this->assertTrue($auth->isLoginAttempt(), "Not login attempt");
$this->assertTrue(is_null($auth->user), "User not null");
}
/**
* @runInSeparateProcess
*/
public function testLoginEmptyLogin() {
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
$_REQUEST = $this->request("", $this->testPass);
$auth = new uAuth();
$this->assertFalse($auth->isAuthenticated(), "Should not be authenticated");
$this->assertTrue($auth->isLoginAttempt(), "Not login attempt");
$this->assertTrue(is_null($auth->user), "User not null");
}
/**
* @runInSeparateProcess
*/
public function testLoginNoFormData() {
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
$auth = new uAuth();
$this->assertFalse($auth->isAuthenticated(), "Should not be authenticated");
$this->assertFalse($auth->isLoginAttempt(), "Should not be login attempt");
$this->assertTrue(is_null($auth->user), "User not null");
}
/**
* @runInSeparateProcess
*/
public function testSessionAuth() {
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
$user = new uUser($this->testUser);
$this->assertTrue($user->isValid, "User not valid");
session_name("ulogger");
session_start();
$_SESSION["user"] = $user;
unset($user);
@$auth = new uAuth();
$this->assertTrue($auth->isAuthenticated(), "Should be authenticated");
$this->assertFalse($auth->isLoginAttempt(), "Should not be login attempt");
$this->assertEquals($this->testUser, $auth->user->login, "Wrong login");
}
/**
* @runInSeparateProcess
*/
public function testSessionAndRequest() {
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
$_REQUEST = $this->request($this->testUser, $this->testPass);
$user = new uUser($this->testUser);
$this->assertTrue($user->isValid, "User not valid");
session_name("ulogger");
session_start();
$_SESSION["user"] = $user;
unset($user);
@$auth = new uAuth();
$this->assertTrue($auth->isAuthenticated(), "Should be authenticated");
$this->assertFalse($auth->isLoginAttempt(), "Should not be login attempt");
$this->assertEquals($this->testUser, $auth->user->login, "Wrong login");
}
/**
* @runInSeparateProcess
*/
public function testNotIsAdmin() {
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
$_REQUEST = $this->request($this->testUser, $this->testPass);
@$auth = new uAuth();
$this->assertTrue($auth->isAuthenticated(), "Should be authenticated");
$this->assertFalse($auth->isAdmin(), "Should not be admin");
}
/**
* @runInSeparateProcess
*/
public function testIsAdmin() {
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
uConfig::$admin_user = $this->testUser;
$_REQUEST = $this->request($this->testUser, $this->testPass);
@$auth = new uAuth();
$this->assertTrue($auth->isAuthenticated(), "Should be authenticated");
$this->assertTrue($auth->isAdmin(), "Should not be admin");
}
}
?>

View File

@ -0,0 +1,379 @@
<?php
require_once(__DIR__ . "/../lib/UloggerAPITestCase.php");
class ClientAPITest extends UloggerAPITestCase {
public function testNoAction() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$options = [
'http_errors' => false
];
$response = $this->http->post('/client/index.php', $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$json = json_decode((string) $response->getBody());
$this->assertTrue($json->{'error'}, "Unexpected success");
}
/* auth */
public function testAuthOk() {
$options = [
'http_errors' => false,
'form_params' => [ 'action' => 'auth', 'user' => $this->testAdminUser, 'pass' => $this->testAdminPass ],
];
$response = $this->http->post('/client/index.php', $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$json = json_decode((string) $response->getBody());
$this->assertFalse($json->{'error'}, "Unexpected error");
}
public function testAuthFail() {
$options = [
'http_errors' => false,
'form_params' => [ 'action' => 'adduser', 'user' => 'noexist', 'pass' => 'noexist' ],
];
$response = $this->http->post('/client/index.php', $options);
$this->assertEquals(401, $response->getStatusCode(), "Unexpected status code");
}
/* adduser */
public function testAddUser() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
$options = [
'http_errors' => false,
'form_params' => [ 'action' => 'adduser', 'login' => $this->testUser, 'password' => $this->testPass ],
];
$response = $this->http->post('/client/index.php', $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$json = json_decode((string) $response->getBody());
$this->assertFalse($json->{'error'}, "Unexpected error");
$this->assertEquals(2, $json->{'userid'}, "Wrong user id");
$this->assertEquals(2, $this->getConnection()->getRowCount('users'), "Wrong row count");
$expected = [ "id" => 2, "login" => $this->testUser ];
$actual = $this->getConnection()->createQueryTable("users", "SELECT id, login FROM users");
$this->assertTableContains($expected, $actual, "Wrong actual table data");
}
public function testAddUserExistingLogin() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
$options = [
'http_errors' => false,
'form_params' => [ 'action' => 'adduser', 'login' => $this->testAdminUser, 'password' => $this->testPass ],
];
$response = $this->http->post('/client/index.php', $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$json = json_decode((string) $response->getBody());
$this->assertTrue($json->{'error'}, "Unexpected success");
$this->assertFalse(isset($json->{'userid'}), "Unexpected user id");
$this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
}
public function testAddUserEmptyLogin() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
$options = [
'http_errors' => false,
'form_params' => [ 'action' => 'adduser', 'login' => '', 'password' => $this->testPass ],
];
$response = $this->http->post('/client/index.php', $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$json = json_decode((string) $response->getBody());
$this->assertTrue($json->{'error'}, "Unexpected success");
$this->assertFalse(isset($json->{'userid'}), "Unexpected user id");
$this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
}
public function testAddUserEmptyPass() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
$options = [
'http_errors' => false,
'form_params' => [ 'action' => 'adduser', 'login' => $this->testUser, 'password' => '' ],
];
$response = $this->http->post('/client/index.php', $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$json = json_decode((string) $response->getBody());
$this->assertTrue($json->{'error'}, "Unexpected success");
$this->assertFalse(isset($json->{'userid'}), "Unexpected user id");
$this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
}
public function testAddUserNoParameters() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
$options = [
'http_errors' => false,
'form_params' => [ 'action' => 'adduser' ],
];
$response = $this->http->post('/client/index.php', $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$json = json_decode((string) $response->getBody());
$this->assertTrue($json->{'error'}, "Unexpected success");
$this->assertFalse(isset($json->{'userid'}), "Unexpected user id");
$this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
}
public function testAddUserByNonAdmin() {
$this->addTestUser($this->testUser, password_hash($this->testPass, PASSWORD_DEFAULT));
$this->assertTrue($this->authenticate($this->testUser, $this->testPass), "Authentication failed");
$this->assertEquals(2, $this->getConnection()->getRowCount('users'), "Wrong row count");
$options = [
'http_errors' => false,
'form_params' => [ 'action' => 'adduser', 'login' => $this->testUser2, 'password' => $this->testPass ],
];
$response = $this->http->post('/client/index.php', $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$json = json_decode((string) $response->getBody());
$this->assertTrue($json->{'error'}, "Unexpected success");
$this->assertFalse(isset($json->{'userid'}), "Unexpected user id");
$this->assertEquals(2, $this->getConnection()->getRowCount('users'), "Wrong row count");
}
/* addtrack */
public function testAddTrack() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(0, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
$options = [
'http_errors' => false,
'form_params' => [ 'action' => 'addtrack', 'track' => $this->testTrackName ],
];
$response = $this->http->post('/client/index.php', $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$json = json_decode((string) $response->getBody());
$this->assertFalse($json->{'error'}, "Unexpected error");
$this->assertEquals(1, $json->{'trackid'}, "Wrong track id");
$this->assertEquals(1, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
$expected = [ "id" => 1, "user_id" => 1, "name" => $this->testTrackName ];
$actual = $this->getConnection()->createQueryTable("users", "SELECT id, user_id, name FROM tracks");
$this->assertTableContains($expected, $actual, "Wrong actual table data");
}
public function testAddTrackEmptyName() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(0, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
$options = [
'http_errors' => false,
'form_params' => [ 'action' => 'addtrack', 'track' => '' ],
];
$response = $this->http->post('/client/index.php', $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$json = json_decode((string) $response->getBody());
$this->assertTrue($json->{'error'}, "Unexpected success");
$this->assertFalse(isset($json->{'trackid'}), "Unexpected track id");
$this->assertEquals(0, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
}
public function testAddTrackNoParameters() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$this->assertEquals(0, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
$options = [
'http_errors' => false,
'form_params' => [ 'action' => 'addtrack' ],
];
$response = $this->http->post('/client/index.php', $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$json = json_decode((string) $response->getBody());
$this->assertTrue($json->{'error'}, "Unexpected success");
$this->assertFalse(isset($json->{'trackid'}), "Unexpected track id");
$this->assertEquals(0, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
}
/* addpos */
public function testAddPosition() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$trackId = $this->addTestTrack($this->testUserId);
$this->assertEquals(1, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
$this->assertEquals(0, $this->getConnection()->getRowCount('positions'), "Wrong row count");
$options = [
'http_errors' => false,
'form_params' => [
'action' => 'addpos',
'trackid' => $trackId,
'time' => $this->testTimestamp,
'lat' => $this->testLat,
'lon' => $this->testLon,
'altitude' => $this->testAltitude,
'speed' => $this->testSpeed,
'bearing' => $this->testBearing,
'accuracy' => $this->testAccuracy,
'provider' => $this->testProvider,
'comment' => $this->testComment,
'imageid' => $this->testImageId
],
];
$response = $this->http->post('/client/index.php', $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$json = json_decode((string) $response->getBody());
$this->assertFalse($json->{'error'}, "Unexpected error");
$this->assertEquals(1, $this->getConnection()->getRowCount('positions'), "Wrong row count");
$expected = [
"id" => 1,
"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,
"image_id" => $this->testImageId
];
$actual = $this->getConnection()->createQueryTable(
"positions",
"SELECT id, user_id, track_id, UNIX_TIMESTAMP(time) AS time, latitude, longitude, altitude, speed, bearing, accuracy, provider, comment, image_id FROM positions"
);
$this->assertTableContains($expected, $actual, "Wrong actual table data");
}
public function testAddPositionNoexistantTrack() {
$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");
$options = [
'http_errors' => false,
'form_params' => [
'action' => 'addpos',
'trackid' => $this->testTrackId,
'time' => $this->testTimestamp,
'lat' => $this->testLat,
'lon' => $this->testLon,
'altitude' => $this->testAltitude,
'speed' => $this->testSpeed,
'bearing' => $this->testBearing,
'accuracy' => $this->testAccuracy,
'provider' => $this->testProvider,
'comment' => $this->testComment,
'imageid' => $this->testImageId
],
];
$response = $this->http->post('/client/index.php', $options);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$json = json_decode((string) $response->getBody());
$this->assertTrue($json->{'error'}, "Unexpected success");
$this->assertEquals(0, $this->getConnection()->getRowCount('positions'), "Wrong row count");
}
public function testAddPositionEmptyParameters() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$trackId = $this->addTestTrack($this->testUserId);
$this->assertEquals(1, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
$this->assertEquals(0, $this->getConnection()->getRowCount('positions'), "Wrong row count");
$options = [
'http_errors' => false,
'form_params' => [
'action' => 'addpos',
'trackid' => $trackId,
'time' => $this->testTimestamp,
'lat' => $this->testLat,
'lon' => $this->testLon,
'altitude' => $this->testAltitude,
'speed' => $this->testSpeed,
'bearing' => $this->testBearing,
'accuracy' => $this->testAccuracy,
'provider' => $this->testProvider,
'comment' => $this->testComment,
'imageid' => $this->testImageId
],
];
// required
foreach ([ 'trackid', 'time', 'lat', 'lon' ] as $parameter) {
$optCopy = $options;
$optCopy['form_params'][$parameter] = '';
$response = $this->http->post('/client/index.php', $optCopy);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$json = json_decode((string) $response->getBody());
$this->assertTrue($json->{'error'}, "Unexpected success ($parameter)");
}
$this->assertEquals(0, $this->getConnection()->getRowCount('positions'), "Wrong row count");
}
public function testAddPositionMissingParameters() {
$this->assertTrue($this->authenticate(), "Authentication failed");
$trackId = $this->addTestTrack($this->testUserId);
$this->assertEquals(1, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
$this->assertEquals(0, $this->getConnection()->getRowCount('positions'), "Wrong row count");
$options = [
'http_errors' => false,
'form_params' => [
'action' => 'addpos',
'trackid' => $trackId,
'time' => $this->testTimestamp,
'lat' => $this->testLat,
'lon' => $this->testLon,
'altitude' => $this->testAltitude,
'speed' => $this->testSpeed,
'bearing' => $this->testBearing,
'accuracy' => $this->testAccuracy,
'provider' => $this->testProvider,
'comment' => $this->testComment,
'imageid' => $this->testImageId
],
];
// required
foreach ([ 'trackid', 'time', 'lat', 'lon' ] as $parameter) {
$optCopy = $options;
unset($optCopy['form_params'][$parameter]);
$response = $this->http->post('/client/index.php', $optCopy);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$json = json_decode((string) $response->getBody());
$this->assertTrue($json->{'error'}, "Unexpected success ($parameter)");
}
$this->assertEquals(0, $this->getConnection()->getRowCount('positions'), "Wrong row count");
// optional
$optional = [ 'altitude', 'speed', 'bearing', 'accuracy', 'provider', 'comment', 'imageid' ];
foreach ($optional as $parameter) {
$optCopy = $options;
unset($optCopy['form_params'][$parameter]);
$response = $this->http->post('/client/index.php', $optCopy);
$this->assertEquals(200, $response->getStatusCode(), "Unexpected status code");
$json = json_decode((string) $response->getBody());
$this->assertFalse($json->{'error'}, "Unexpected error ($parameter)");
}
$this->assertEquals(count($optional), $this->getConnection()->getRowCount('positions'), "Wrong row count");
}
}
?>

View File

@ -0,0 +1,62 @@
<?php
use PHPUnit\Framework\TestCase;
require_once(__DIR__ . "/../../helpers/config.php");
class ConfigTest extends TestCase {
public function testPassRegex() {
uConfig::$pass_lenmin = 0;
uConfig::$pass_strength = 0;
$password0 = "password";
$password1 = "PASSword";
$password2 = "PASSword1234";
$password3 = "PASSword1234-;";
$regex = uConfig::passRegex();
$this->assertRegExp($regex, $password0, "Regex: \"$regex\", password: \"$password0\"");
$this->assertRegExp($regex, $password1, "Regex: \"$regex\", password: \"$password1\"");
$this->assertRegExp($regex, $password2, "Regex: \"$regex\", password: \"$password2\"");
$this->assertRegExp($regex, $password3, "Regex: \"$regex\", password: \"$password3\"");
uConfig::$pass_strength = 1;
$regex = uConfig::passRegex();
$this->assertNotRegExp($regex, $password0, "Regex: \"$regex\", password: \"$password0\"");
$this->assertRegExp($regex, $password1, "Regex: \"$regex\", password: \"$password1\"");
$this->assertRegExp($regex, $password2, "Regex: \"$regex\", password: \"$password2\"");
$this->assertRegExp($regex, $password3, "Regex: \"$regex\", password: \"$password3\"");
uConfig::$pass_strength = 2;
$regex = uConfig::passRegex();
$this->assertNotRegExp($regex, $password0, "Regex: \"$regex\", password: \"$password0\"");
$this->assertNotRegExp($regex, $password1, "Regex: \"$regex\", password: \"$password1\"");
$this->assertRegExp($regex, $password2, "Regex: \"$regex\", password: \"$password2\"");
$this->assertRegExp($regex, $password3, "Regex: \"$regex\", password: \"$password3\"");
uConfig::$pass_strength = 3;
$regex = uConfig::passRegex();
$this->assertNotRegExp($regex, $password0, "Regex: \"$regex\", password: \"$password0\"");
$this->assertNotRegExp($regex, $password1, "Regex: \"$regex\", password: \"$password1\"");
$this->assertNotRegExp($regex, $password2, "Regex: \"$regex\", password: \"$password2\"");
$this->assertRegExp($regex, $password3, "Regex: \"$regex\", password: \"$password3\"");
$password_len5 = "12345";
$password_len10 = "1234567890";
uConfig::$pass_lenmin = 5;
uConfig::$pass_strength = 0;
$regex = uConfig::passRegex();
$this->assertRegExp($regex, $password_len5, "Regex: \"$regex\", password: \"$password_len5\"");
$this->assertRegExp($regex, $password_len10, "Regex: \"$regex\", password: \"$password_len10\"");
uConfig::$pass_lenmin = 7;
$regex = uConfig::passRegex();
$this->assertNotRegExp($regex, $password_len5, "Regex: \"$regex\", password: \"$password_len5\"");
$this->assertRegExp($regex, $password_len10, "Regex: \"$regex\", password: \"$password_len10\"");
uConfig::$pass_lenmin = 12;
$regex = uConfig::passRegex();
$this->assertNotRegExp($regex, $password_len5, "Regex: \"$regex\", password: \"$password_len5\"");
$this->assertNotRegExp($regex, $password_len10, "Regex: \"$regex\", password: \"$password_len10\"");
}
}
?>

View File

@ -0,0 +1,143 @@
<?php
use PHPUnit\Framework\TestCase;
require_once(__DIR__ . "/../lib/UloggerDatabaseTestCase.php");
require_once(__DIR__ . "/../../helpers/track.php");
class PositionTest extends UloggerDatabaseTestCase {
public function testAddPosition() {
$trackId = $this->addTestTrack($this->testUserId);
$this->assertEquals(1, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
$posId = uPosition::add($this->testUserId, $trackId + 1, $this->testTimestamp, $this->testLat, $this->testLon, $this->testAltitude, $this->testSpeed, $this->testBearing, $this->testAccuracy, $this->testProvider, $this->testComment, $this->testImageId);
$this->assertEquals(0, $this->getConnection()->getRowCount('positions'), "Wrong row count");
$this->assertFalse($posId, "Adding position with nonexistant track should fail");
$posId = uPosition::add($this->testUserId2, $trackId, $this->testTimestamp, $this->testLat, $this->testLon, $this->testAltitude, $this->testSpeed, $this->testBearing, $this->testAccuracy, $this->testProvider, $this->testComment, $this->testImageId);
$this->assertEquals(0, $this->getConnection()->getRowCount('positions'), "Wrong row count");
$this->assertFalse($posId, "Adding position with wrong user should fail");
$posId = uPosition::add($this->testUserId, $trackId, $this->testTimestamp, $this->testLat, $this->testLon, $this->testAltitude, $this->testSpeed, $this->testBearing, $this->testAccuracy, $this->testProvider, $this->testComment, $this->testImageId);
$this->assertEquals(1, $this->getConnection()->getRowCount('positions'), "Wrong row count");
$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,
"image_id" => $this->testImageId
];
$actual = $this->getConnection()->createQueryTable(
"positions",
"SELECT id, user_id, track_id, UNIX_TIMESTAMP(time) AS time, latitude, longitude, altitude, speed, bearing, accuracy, provider, comment, image_id FROM positions"
);
$this->assertTableContains($expected, $actual, "Wrong actual table data");
$posId = uPosition::add($this->testUserId, $trackId, NULL, $this->testLat, $this->testLon);
$this->assertFalse($posId, "Adding position with null time stamp should fail");
$posId = uPosition::add($this->testUserId, $trackId, $this->testTimestamp, NULL, $this->testLon);
$this->assertFalse($posId, "Adding position with null latitude should fail");
$posId = uPosition::add($this->testUserId, $trackId, $this->testTimestamp, $this->testLat, NULL);
$this->assertFalse($posId, "Adding position with null longitude should fail");
$posId = uPosition::add($this->testUserId, $trackId, "", $this->testLat, $this->testLon);
$this->assertFalse($posId, "Adding position with empty time stamp should fail");
$posId = uPosition::add($this->testUserId, $trackId, $this->testTimestamp, "", $this->testLon);
$this->assertFalse($posId, "Adding position with empty latitude should fail");
$posId = uPosition::add($this->testUserId, $trackId, $this->testTimestamp, $this->testLat, "");
$this->assertFalse($posId, "Adding position with empty longitude should fail");
}
public function testDeleteAll() {
$trackId = $this->addTestTrack($this->testUserId);
$this->addTestPosition($this->testUserId, $trackId);
$trackId2 = $this->addTestTrack($this->testUserId);
$this->addTestPosition($this->testUserId, $trackId2);
$trackId3 = $this->addTestTrack($this->testUserId2);
$this->addTestPosition($this->testUserId2, $trackId3);
$this->assertEquals(3, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
$this->assertEquals(3, $this->getConnection()->getRowCount('positions'), "Wrong row count");
$this->assertTrue(uPosition::deleteAll($this->testUserId), "Deleting failed");
$this->assertEquals(1, $this->getConnection()->getRowCount('positions'), "Wrong row count");
}
public function testDeleteAllWIthTrackId() {
$trackId = $this->addTestTrack($this->testUserId);
$this->addTestPosition($this->testUserId, $trackId);
$trackId2 = $this->addTestTrack($this->testUserId);
$this->addTestPosition($this->testUserId, $trackId2);
$trackId3 = $this->addTestTrack($this->testUserId2);
$this->addTestPosition($this->testUserId2, $trackId3);
$this->assertEquals(3, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
$this->assertEquals(3, $this->getConnection()->getRowCount('positions'), "Wrong row count");
$this->assertTrue(uPosition::deleteAll($this->testUserId, $trackId), "Deleting failed");
$this->assertEquals(2, $this->getConnection()->getRowCount('positions'), "Wrong row count");
}
public function testGetLast() {
$trackId1 = $this->addTestTrack($this->testUserId);
$trackId2 = $this->addTestTrack($this->testUserId);
$pos1 = $this->addTestPosition($this->testUserId, $trackId1, $this->testTimestamp + 3);
$pos2 = $this->addTestPosition($this->testUserId2, $trackId2, $this->testTimestamp + 1);
$pos3 = $this->addTestPosition($this->testUserId, $trackId1, $this->testTimestamp);
$pos4 = $this->addTestPosition($this->testUserId2, $trackId2, $this->testTimestamp + 2);
$this->assertEquals(2, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
$this->assertEquals(4, $this->getConnection()->getRowCount('positions'), "Wrong row count");
$lastPosition = uPosition::getLast();
$this->assertEquals($lastPosition->id, $pos1, "Wrong last position");
$lastPosition = uPosition::getLast($this->testUserId2);
$this->assertEquals($lastPosition->id, $pos4, "Wrong last position (user)");
}
public function testGetAll() {
$trackId = $this->addTestTrack($this->testUserId);
$this->addTestPosition($this->testUserId, $trackId);
$trackId2 = $this->addTestTrack($this->testUserId);
$this->addTestPosition($this->testUserId, $trackId2);
$trackId3 = $this->addTestTrack($this->testUserId2);
$this->addTestPosition($this->testUserId2, $trackId3);
$this->assertEquals(3, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
$this->assertEquals(3, $this->getConnection()->getRowCount('positions'), "Wrong row count");
$posArr = uPosition::getAll();
$this->assertEquals(3, count($posArr), "Wrong row count");
$posArr = uPosition::getAll($this->testUserId);
$this->assertEquals(2, count($posArr), "Wrong row count");
$posArr = uPosition::getAll($this->testUserId, $trackId);
$this->assertEquals(1, count($posArr), "Wrong row count");
$posArr = uPosition::getAll(NULL, $trackId);
$this->assertEquals(1, count($posArr), "Wrong row count");
$posArr = uPosition::getAll($this->testUserId3);
$this->assertEquals(0, count($posArr), "Wrong row count");
}
public function testDistanceTo() {
$trackId = $this->addTestTrack($this->testUserId);
$pos1 = $this->addTestPosition($this->testUserId, $trackId, $this->testTimestamp, 0, 0);
$pos2 = $this->addTestPosition($this->testUserId, $trackId, $this->testTimestamp, 0, 1);
$posArr = uPosition::getAll();
$this->assertEquals(2, count($posArr), "Wrong row count");
$this->assertEquals(111195, round($posArr[0]->distanceTo($posArr[1])), "Wrong distance");
}
public function testSecondsTo() {
$trackId = $this->addTestTrack($this->testUserId);
$pos1 = $this->addTestPosition($this->testUserId, $trackId, $this->testTimestamp);
$pos2 = $this->addTestPosition($this->testUserId, $trackId, $this->testTimestamp + 1);
$posArr = uPosition::getAll();
$this->assertEquals(2, count($posArr), "Wrong row count");
$this->assertEquals(-1, $posArr[0]->secondsTo($posArr[1]), "Wrong time difference");
}
}
?>

133
.tests/tests/TrackTest.php Normal file
View File

@ -0,0 +1,133 @@
<?php
use PHPUnit\Framework\TestCase;
require_once(__DIR__ . "/../lib/UloggerDatabaseTestCase.php");
require_once(__DIR__ . "/../../helpers/track.php");
class TrackTest extends UloggerDatabaseTestCase {
public function testAddTrack() {
$trackId = uTrack::add($this->testUserId, $this->testTrackName, $this->testTrackComment);
$this->assertEquals(1, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
$this->assertEquals(1, $trackId, "Wrong track id returned");
$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");
$this->assertFalse(uTrack::add("", $this->testTrackName), "Adding track with empty user id should fail");
$this->assertFalse(uTrack::add($this->testUserId, ""), "Adding track with empty name should fail");
}
public function testDeleteTrack() {
$trackId = $this->addTestTrack($this->testUserId);
$this->addTestPosition($this->testUserId, $trackId);
$this->assertEquals(1, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
$this->assertEquals(1, $this->getConnection()->getRowCount('positions'), "Wrong row count");
$track = new uTrack($trackId);
$track->delete();
$this->assertEquals(0, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
$this->assertEquals(0, $this->getConnection()->getRowCount('positions'), "Wrong row count");
$this->assertFalse($track->isValid, "Deleted track should not be valid");
}
public function testAddPosition() {
$trackId = $this->addTestTrack($this->testUserId);
$this->assertEquals(1, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
$track = new uTrack($trackId + 1);
$posId = $track->addPosition($this->testUserId, $this->testTimestamp, $this->testLat, $this->testLon, $this->testAltitude, $this->testSpeed, $this->testBearing, $this->testAccuracy, $this->testProvider, $this->testComment, $this->testImageId);
$this->assertEquals(0, $this->getConnection()->getRowCount('positions'), "Wrong row count");
$this->assertFalse($posId, "Adding position with nonexistant track should fail");
$track = new uTrack($trackId);
$posId = $track->addPosition($this->testUserId2, $this->testTimestamp, $this->testLat, $this->testLon, $this->testAltitude, $this->testSpeed, $this->testBearing, $this->testAccuracy, $this->testProvider, $this->testComment, $this->testImageId);
$this->assertEquals(0, $this->getConnection()->getRowCount('positions'), "Wrong row count");
$this->assertFalse($posId, "Adding position with wrong user should fail");
$posId = $track->addPosition($this->testUserId, $this->testTimestamp, $this->testLat, $this->testLon, $this->testAltitude, $this->testSpeed, $this->testBearing, $this->testAccuracy, $this->testProvider, $this->testComment, $this->testImageId);
$this->assertEquals(1, $this->getConnection()->getRowCount('positions'), "Wrong row count");
$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,
"image_id" => $this->testImageId
];
$actual = $this->getConnection()->createQueryTable(
"positions",
"SELECT id, user_id, track_id, UNIX_TIMESTAMP(time) AS time, latitude, longitude, altitude, speed, bearing, accuracy, provider, comment, image_id FROM positions"
);
$this->assertTableContains($expected, $actual, "Wrong actual table data");
$posId = $track->addPosition($this->testUserId, NULL, $this->testLat, $this->testLon);
$this->assertFalse($posId, "Adding position with null time stamp should fail");
$posId = $track->addPosition($this->testUserId, $this->testTimestamp, NULL, $this->testLon);
$this->assertFalse($posId, "Adding position with null latitude should fail");
$posId = $track->addPosition($this->testUserId, $this->testTimestamp, $this->testLat, NULL);
$this->assertFalse($posId, "Adding position with null longitude should fail");
$posId = $track->addPosition($this->testUserId, "", $this->testLat, $this->testLon);
$this->assertFalse($posId, "Adding position with empty time stamp should fail");
$posId = $track->addPosition($this->testUserId, $this->testTimestamp, "", $this->testLon);
$this->assertFalse($posId, "Adding position with empty latitude should fail");
$posId = $track->addPosition($this->testUserId, $this->testTimestamp, $this->testLat, "");
$this->assertFalse($posId, "Adding position with empty longitude should fail");
}
public function testGetAll() {
$this->addTestTrack();
$this->addTestTrack();
$this->assertEquals(2, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
$trackArr = uTrack::getAll();
$this->assertEquals(2, count($trackArr), "Wrong array size");
$this->assertTrue($trackArr[0] instanceof uTrack, "Wrong array member");
}
public function testDeleteAll() {
$trackId = $this->addTestTrack();
$this->addTestTrack();
$this->addTestPosition($this->testUserId, $trackId);
$trackId2 = $this->addTestTrack($this->testUserId2);
$this->addTestPosition($this->testUserId2, $trackId2);
$this->assertEquals(3, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
$this->assertEquals(2, $this->getConnection()->getRowCount('positions'), "Wrong row count");
uTrack::deleteAll($this->testUserId);
$this->assertEquals(1, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
$this->assertEquals(1, $this->getConnection()->getRowCount('positions'), "Wrong row count");
$this->assertFalse(uTrack::deleteAll(NULL), "User id should not be empty");
}
public function testUpdate() {
$trackId = $this->addTestTrack();
$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);
$this->assertFalse($trackInvalid->update("newName", "newComment"), "Updating nonexistant track should fail");
}
public function testIsValid() {
$trackId = $this->addTestTrack();
$trackValid = new uTrack($trackId);
$this->assertTrue($trackValid->isValid, "Track should be valid");
$trackInvalid = new uTrack($trackId + 1);
$this->assertFalse($trackInvalid->isValid, "Track should not be valid");
}
}
?>

81
.tests/tests/UserTest.php Normal file
View File

@ -0,0 +1,81 @@
<?php
use PHPUnit\Framework\TestCase;
require_once(__DIR__ . "/../lib/UloggerDatabaseTestCase.php");
require_once(__DIR__ . "/../../helpers/user.php");
class UserTest extends UloggerDatabaseTestCase {
public function testAddUser() {
$userId = uUser::add($this->testUser, $this->testPass);
$this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
$this->assertEquals(1, $userId, "Wrong user id returned");
$expected = [ "id" => 1, "login" => $this->testUser ];
$actual = $this->getConnection()->createQueryTable("users", "SELECT id, login FROM users");
$this->assertTableContains($expected, $actual, "Wrong actual table data");
$this->assertTrue(password_verify($this->testPass, $this->pdoGetColumn("SELECT password FROM users")), "Wrong actual password hash");
$this->assertFalse(uUser::add($this->testUser, $this->testPass), "Adding user with same login should fail");
$this->assertFalse(uUser::add($this->testUser, ""), "Adding user with empty password should fail");
$this->assertFalse(uUser::add("", $this->testPass), "Adding user with empty login should fail");
}
public function testDeleteUser() {
$userId = $this->addTestUser($this->testUser);
$trackId = $this->addTestTrack($userId);
$this->addTestPosition($userId, $trackId);
$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");
$user = new uUser($this->testUser);
$user->delete();
$this->assertEquals(0, $this->getConnection()->getRowCount('users'), "Wrong row count");
$this->assertEquals(0, $this->getConnection()->getRowCount('tracks'), "Wrong row count");
$this->assertEquals(0, $this->getConnection()->getRowCount('positions'), "Wrong row count");
$this->assertFalse($user->isValid, "Deleted user should not be valid");
}
public function testSetPass() {
$newPass = $this->testPass . "new";
$this->addTestUser($this->testUser);
$this->assertEquals(1, $this->getConnection()->getRowCount('users'), "Wrong row count");
$user = new uUser($this->testUser);
$user->setPass($newPass);
$this->assertTrue(password_verify($newPass, $this->pdoGetColumn("SELECT password FROM users")), "Wrong actual password hash");
$this->assertFalse($user->setPass(""), "Password should not be empty");
$userInvalid = new uUser($this->testUser . "-noexistant");
$this->assertFalse($userInvalid->setPass($newPass), "Setting pass for nonexistant user should fail");
}
public function testGetAll() {
$this->addTestUser($this->testUser);
$this->addTestUser($this->testUser2);
$this->assertEquals(2, $this->getConnection()->getRowCount('users'), "Wrong row count");
$userArr = uUser::getAll();
$this->assertEquals(2, count($userArr), "Wrong array size");
$this->assertTrue($userArr[0] instanceof uUser, "Wrong array member");
}
public function testIsAdmin() {
$this->addTestUser($this->testUser);
$user = new uUser($this->testUser);
$this->assertFalse($user->isAdmin, "User should not be admin");
uConfig::$admin_user = $this->testUser;
$user = new uUser($this->testUser);
$this->assertTrue($user->isAdmin, "User should be admin");
}
public function testIsValid() {
$this->addTestUser($this->testUser);
$userValid = new uUser($this->testUser);
$this->assertTrue($userValid->isValid, "User should be valid");
$userInvalid = new uUser($this->testUser . "-noexistant");
$this->assertFalse($userInvalid->isValid, "User should not be valid");
}
}
?>

26
.travis.yml Normal file
View File

@ -0,0 +1,26 @@
language: php
sudo: required
php:
- 7.0
env:
- DB_HOST="localhost"
- DB_NAME="ulogger"
- DB_USER="ulogger"
- DB_PASS="secret2"
- DB_PORT=8081
- ULOGGER_URL="http://127.0.0.1:8080"
services:
- docker
before_install:
- docker build -t ulogger .
- docker run -d --name ulogger -p 8080:80 -p 8081:3306 --expose 3306 ulogger
- composer install
- until netstat -atn 2>/dev/null | grep '8080.*LISTEN'; do sleep 1; done
script:
- ./vendor/bin/phpunit -c .tests/phpunit.xml

13
composer.json Normal file
View File

@ -0,0 +1,13 @@
{
"require": {
"phpunit/phpunit": "^5.7",
"vlucas/phpdotenv": "^2.4",
"guzzlehttp/guzzle": "^6.3"
},
"scripts": {
"test": "./vendor/bin/phpunit"
},
"require-dev": {
"phpunit/dbunit": "^2.0"
}
}

1668
composer.lock generated Normal file

File diff suppressed because it is too large Load Diff