2017-09-05 09:36:10 +02:00
|
|
|
<?php
|
|
|
|
|
2021-04-22 19:47:52 +02:00
|
|
|
use PHPUnit\DbUnit\DataSet\IDataSet;
|
|
|
|
|
2020-02-19 18:42:44 +01:00
|
|
|
if (!defined("ROOT_DIR")) { define("ROOT_DIR", __DIR__ . "/../.."); }
|
2017-09-05 09:36:10 +02:00
|
|
|
require_once(__DIR__ . "/../../helpers/config.php");
|
2020-02-19 18:42:44 +01:00
|
|
|
require_once(__DIR__ . "/../lib/UloggerDatabaseTestCase.php");
|
2017-09-05 09:36:10 +02:00
|
|
|
|
2020-02-19 18:42:44 +01:00
|
|
|
class ConfigTest extends UloggerDatabaseTestCase {
|
|
|
|
|
2020-02-20 17:08:47 +01:00
|
|
|
private $config;
|
|
|
|
|
2020-02-19 18:42:44 +01:00
|
|
|
private $mapApi;
|
|
|
|
private $latitude;
|
|
|
|
private $longitude;
|
|
|
|
private $googleKey;
|
|
|
|
private $requireAuth;
|
|
|
|
private $publicTracks;
|
|
|
|
private $passLenMin;
|
|
|
|
private $passStrength;
|
|
|
|
private $interval;
|
|
|
|
private $lang;
|
|
|
|
private $units;
|
|
|
|
private $strokeWeight;
|
|
|
|
private $strokeColor;
|
|
|
|
private $strokeOpacity;
|
|
|
|
private $testLayer;
|
|
|
|
private $testUrl;
|
|
|
|
private $testPriority;
|
|
|
|
|
2021-04-22 19:47:52 +02:00
|
|
|
public function setUp(): void {
|
2020-02-19 18:42:44 +01:00
|
|
|
parent::setUp();
|
2020-02-20 17:08:47 +01:00
|
|
|
$this->config = uConfig::getInstance();
|
2020-02-19 18:42:44 +01:00
|
|
|
$this->initConfigValues();
|
|
|
|
}
|
|
|
|
|
2021-04-22 19:47:52 +02:00
|
|
|
protected function tearDown(): void {
|
2020-05-20 17:12:07 +02:00
|
|
|
parent::tearDown();
|
|
|
|
$configClass = new ReflectionClass("uConfig");
|
|
|
|
$configInstance = $configClass->getProperty('instance');
|
|
|
|
$configInstance->setAccessible(true);
|
|
|
|
$configInstance->setValue(null);
|
|
|
|
}
|
|
|
|
|
2021-04-22 19:47:52 +02:00
|
|
|
protected function getDataSet(): IDataSet {
|
2020-02-19 18:42:44 +01:00
|
|
|
$this->initConfigValues();
|
|
|
|
$this->resetAutoincrement();
|
|
|
|
$dataset = [
|
|
|
|
"config" => [
|
2020-02-23 22:21:17 +01:00
|
|
|
[ "name" => "map_api", "value" => serialize($this->mapApi) ],
|
|
|
|
[ "name" => "latitude", "value" => serialize($this->latitude) ],
|
|
|
|
[ "name" => "longitude", "value" => serialize($this->longitude) ],
|
|
|
|
[ "name" => "google_key", "value" => serialize($this->googleKey) ],
|
|
|
|
[ "name" => "require_auth", "value" => serialize($this->requireAuth) ],
|
|
|
|
[ "name" => "public_tracks", "value" => serialize($this->publicTracks) ],
|
|
|
|
[ "name" => "pass_lenmin", "value" => serialize($this->passLenMin) ],
|
|
|
|
[ "name" => "pass_strength", "value" => serialize($this->passStrength) ],
|
|
|
|
[ "name" => "interval_seconds", "value" => serialize($this->interval) ],
|
|
|
|
[ "name" => "lang", "value" => serialize($this->lang) ],
|
|
|
|
[ "name" => "units", "value" => serialize($this->units) ],
|
|
|
|
[ "name" => "stroke_weight", "value" => serialize($this->strokeWeight) ],
|
|
|
|
[ "name" => "stroke_color", "value" => serialize($this->strokeColor) ],
|
|
|
|
[ "name" => "stroke_opacity", "value" => serialize($this->strokeOpacity) ]
|
2020-02-19 18:42:44 +01:00
|
|
|
],
|
|
|
|
"ol_layers" => [
|
|
|
|
[
|
|
|
|
"id" => 1, "name" => $this->testLayer, "url" => $this->testUrl, "priority" => $this->testPriority
|
|
|
|
]
|
|
|
|
]];
|
|
|
|
return $this->createArrayDataSet($dataset);
|
|
|
|
}
|
|
|
|
|
2021-04-22 19:47:52 +02:00
|
|
|
public function testSetFromDatabase(): void {
|
|
|
|
self::assertEquals($this->mapApi, $this->config->mapApi);
|
|
|
|
self::assertEquals($this->latitude, $this->config->initLatitude);
|
|
|
|
self::assertEquals($this->longitude, $this->config->initLongitude);
|
|
|
|
self::assertEquals($this->googleKey, $this->config->googleKey);
|
|
|
|
self::assertEquals($this->requireAuth, $this->config->requireAuthentication);
|
|
|
|
self::assertEquals($this->publicTracks, $this->config->publicTracks);
|
|
|
|
self::assertEquals($this->passLenMin, $this->config->passLenMin);
|
|
|
|
self::assertEquals($this->passStrength, $this->config->passStrength);
|
|
|
|
self::assertEquals($this->interval, $this->config->interval);
|
|
|
|
self::assertEquals($this->lang, $this->config->lang);
|
|
|
|
self::assertEquals($this->units, $this->config->units);
|
|
|
|
self::assertEquals($this->strokeWeight, $this->config->strokeWeight);
|
|
|
|
self::assertEquals($this->strokeColor, $this->config->strokeColor);
|
|
|
|
self::assertEquals($this->strokeOpacity, $this->config->strokeOpacity);
|
|
|
|
|
|
|
|
self::assertEquals($this->testLayer, $this->config->olLayers[0]->name);
|
|
|
|
self::assertEquals($this->testUrl, $this->config->olLayers[0]->url);
|
|
|
|
self::assertEquals($this->testPriority, $this->config->olLayers[0]->priority);
|
2020-02-20 17:08:47 +01:00
|
|
|
}
|
|
|
|
|
2021-04-22 19:47:52 +02:00
|
|
|
public function testSave(): void {
|
2020-02-20 17:08:47 +01:00
|
|
|
$this->config->mapApi = 'newApi';
|
|
|
|
$this->config->initLatitude = 33.11;
|
|
|
|
$this->config->initLongitude = 22.11;
|
|
|
|
$this->config->googleKey = 'newKey';
|
|
|
|
$this->config->requireAuthentication = false;
|
|
|
|
$this->config->publicTracks = false;
|
|
|
|
$this->config->passLenMin = 31;
|
|
|
|
$this->config->passStrength = 31;
|
|
|
|
$this->config->interval = 661;
|
|
|
|
$this->config->lang = 'newLang';
|
|
|
|
$this->config->units = 'newUnits';
|
|
|
|
$this->config->strokeWeight = 551;
|
|
|
|
$this->config->strokeColor = '#bfbfbf';
|
|
|
|
$this->config->strokeOpacity = 0.11;
|
|
|
|
$this->config->olLayers = [];
|
|
|
|
$this->config->olLayers[0] = new uLayer(11, 'newLayer', 'newUrl', 51);
|
|
|
|
|
|
|
|
$this->config->save();
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
"map_api" => $this->config->mapApi,
|
|
|
|
"latitude" => $this->config->initLatitude,
|
|
|
|
"longitude" => $this->config->initLongitude,
|
|
|
|
"google_key" => $this->config->googleKey,
|
|
|
|
"require_auth" => $this->config->requireAuthentication,
|
|
|
|
"public_tracks" => $this->config->publicTracks,
|
|
|
|
"pass_lenmin" => $this->config->passLenMin,
|
|
|
|
"pass_strength" => $this->config->passStrength,
|
|
|
|
"interval_seconds" => $this->config->interval,
|
|
|
|
"lang" => $this->config->lang,
|
|
|
|
"units" => $this->config->units,
|
|
|
|
"stroke_weight" => $this->config->strokeWeight,
|
2020-02-23 22:21:17 +01:00
|
|
|
"stroke_color" => $this->config->strokeColor,
|
|
|
|
"stroke_opacity" => $this->config->strokeOpacity
|
2020-02-20 17:08:47 +01:00
|
|
|
];
|
2020-02-23 22:21:17 +01:00
|
|
|
$cnt = count($expected);
|
2021-04-22 19:47:52 +02:00
|
|
|
self::assertGreaterThanOrEqual($cnt, $this->getConnection()->getRowCount('config'), "Wrong row count");
|
2020-02-20 17:08:47 +01:00
|
|
|
$actual = $this->getConnection()->createQueryTable("config", "SELECT * FROM config");
|
2020-02-23 22:21:17 +01:00
|
|
|
for ($i = 0; $i < $cnt; $i++) {
|
|
|
|
$row = $actual->getRow($i);
|
|
|
|
$actualValue = $row['value'];
|
2020-06-05 11:41:41 +02:00
|
|
|
if (isset($expected[$row['name']])) {
|
2021-04-22 19:47:52 +02:00
|
|
|
self::assertEquals(serialize($expected[$row['name']]), is_resource($actualValue) ? stream_get_contents($actualValue) : $actualValue);
|
2020-06-05 11:41:41 +02:00
|
|
|
}
|
2020-02-23 22:21:17 +01:00
|
|
|
}
|
2021-04-22 19:47:52 +02:00
|
|
|
self::assertEquals(1, $this->getConnection()->getRowCount('ol_layers'), "Wrong row count");
|
2020-02-20 17:08:47 +01:00
|
|
|
$expected = [
|
|
|
|
"id" => $this->config->olLayers[0]->id,
|
|
|
|
"name" => $this->config->olLayers[0]->name,
|
|
|
|
"url" => $this->config->olLayers[0]->url,
|
|
|
|
"priority" => $this->config->olLayers[0]->priority
|
|
|
|
];
|
|
|
|
$actual = $this->getConnection()->createQueryTable("ol_layers", "SELECT * FROM ol_layers");
|
|
|
|
$this->assertTableContains($expected, $actual, "Wrong actual table data: " . implode(', ', $actual->getRow(0)));
|
2020-02-19 18:42:44 +01:00
|
|
|
}
|
|
|
|
|
2021-04-22 19:47:52 +02:00
|
|
|
private function initConfigValues(): void {
|
2020-02-19 18:42:44 +01:00
|
|
|
$this->mapApi = 'testApi';
|
|
|
|
$this->latitude = 33.33;
|
|
|
|
$this->longitude = 22.22;
|
|
|
|
$this->googleKey = 'testKey';
|
|
|
|
$this->requireAuth = true;
|
|
|
|
$this->publicTracks = true;
|
|
|
|
$this->passLenMin = 3;
|
|
|
|
$this->passStrength = 3;
|
|
|
|
$this->interval = 66;
|
|
|
|
$this->lang = 'pl';
|
|
|
|
$this->units = 'nautical';
|
|
|
|
$this->strokeWeight = 55;
|
|
|
|
$this->strokeColor = '#afafaf';
|
|
|
|
$this->strokeOpacity = 0.44;
|
|
|
|
$this->testLayer = 'testLayer';
|
|
|
|
$this->testUrl = 'testUrl';
|
|
|
|
$this->testPriority = 5;
|
|
|
|
}
|
2017-09-05 09:36:10 +02:00
|
|
|
|
2021-04-22 19:47:52 +02:00
|
|
|
public function testPassRegex(): void {
|
2020-02-20 17:08:47 +01:00
|
|
|
$this->config->passLenMin = 0;
|
|
|
|
$this->config->passStrength = 0;
|
2017-09-05 09:36:10 +02:00
|
|
|
$password0 = "password";
|
|
|
|
$password1 = "PASSword";
|
|
|
|
$password2 = "PASSword1234";
|
|
|
|
$password3 = "PASSword1234-;";
|
|
|
|
|
2020-02-20 17:08:47 +01:00
|
|
|
$regex = $this->config->passRegex();
|
2021-04-22 19:47:52 +02:00
|
|
|
self::assertMatchesRegularExpression($regex, $password0, "Regex: \"$regex\", password: \"$password0\"");
|
|
|
|
self::assertMatchesRegularExpression($regex, $password1, "Regex: \"$regex\", password: \"$password1\"");
|
|
|
|
self::assertMatchesRegularExpression($regex, $password2, "Regex: \"$regex\", password: \"$password2\"");
|
|
|
|
self::assertMatchesRegularExpression($regex, $password3, "Regex: \"$regex\", password: \"$password3\"");
|
2017-09-05 09:36:10 +02:00
|
|
|
|
2020-02-20 17:08:47 +01:00
|
|
|
$this->config->passStrength = 1;
|
|
|
|
$regex = $this->config->passRegex();
|
2021-04-22 19:47:52 +02:00
|
|
|
self::assertDoesNotMatchRegularExpression($regex, $password0, "Regex: \"$regex\", password: \"$password0\"");
|
|
|
|
self::assertMatchesRegularExpression($regex, $password1, "Regex: \"$regex\", password: \"$password1\"");
|
|
|
|
self::assertMatchesRegularExpression($regex, $password2, "Regex: \"$regex\", password: \"$password2\"");
|
|
|
|
self::assertMatchesRegularExpression($regex, $password3, "Regex: \"$regex\", password: \"$password3\"");
|
2017-09-05 09:36:10 +02:00
|
|
|
|
2020-02-20 17:08:47 +01:00
|
|
|
$this->config->passStrength = 2;
|
|
|
|
$regex = $this->config->passRegex();
|
2021-04-22 19:47:52 +02:00
|
|
|
self::assertDoesNotMatchRegularExpression($regex, $password0, "Regex: \"$regex\", password: \"$password0\"");
|
|
|
|
self::assertDoesNotMatchRegularExpression($regex, $password1, "Regex: \"$regex\", password: \"$password1\"");
|
|
|
|
self::assertMatchesRegularExpression($regex, $password2, "Regex: \"$regex\", password: \"$password2\"");
|
|
|
|
self::assertMatchesRegularExpression($regex, $password3, "Regex: \"$regex\", password: \"$password3\"");
|
2017-09-05 09:36:10 +02:00
|
|
|
|
2020-02-20 17:08:47 +01:00
|
|
|
$this->config->passStrength = 3;
|
|
|
|
$regex = $this->config->passRegex();
|
2021-04-22 19:47:52 +02:00
|
|
|
self::assertDoesNotMatchRegularExpression($regex, $password0, "Regex: \"$regex\", password: \"$password0\"");
|
|
|
|
self::assertDoesNotMatchRegularExpression($regex, $password1, "Regex: \"$regex\", password: \"$password1\"");
|
|
|
|
self::assertDoesNotMatchRegularExpression($regex, $password2, "Regex: \"$regex\", password: \"$password2\"");
|
|
|
|
self::assertMatchesRegularExpression($regex, $password3, "Regex: \"$regex\", password: \"$password3\"");
|
2017-09-05 09:36:10 +02:00
|
|
|
|
|
|
|
$password_len5 = "12345";
|
|
|
|
$password_len10 = "1234567890";
|
2020-02-20 17:08:47 +01:00
|
|
|
$this->config->passLenMin = 5;
|
|
|
|
$this->config->passStrength = 0;
|
|
|
|
$regex = $this->config->passRegex();
|
2021-04-22 19:47:52 +02:00
|
|
|
self::assertMatchesRegularExpression($regex, $password_len5, "Regex: \"$regex\", password: \"$password_len5\"");
|
|
|
|
self::assertMatchesRegularExpression($regex, $password_len10, "Regex: \"$regex\", password: \"$password_len10\"");
|
2017-09-05 09:36:10 +02:00
|
|
|
|
2020-02-20 17:08:47 +01:00
|
|
|
$this->config->passLenMin = 7;
|
|
|
|
$regex = $this->config->passRegex();
|
2021-04-22 19:47:52 +02:00
|
|
|
self::assertDoesNotMatchRegularExpression($regex, $password_len5, "Regex: \"$regex\", password: \"$password_len5\"");
|
|
|
|
self::assertMatchesRegularExpression($regex, $password_len10, "Regex: \"$regex\", password: \"$password_len10\"");
|
2017-09-05 09:36:10 +02:00
|
|
|
|
2020-02-20 17:08:47 +01:00
|
|
|
$this->config->passLenMin = 12;
|
|
|
|
$regex = $this->config->passRegex();
|
2021-04-22 19:47:52 +02:00
|
|
|
self::assertDoesNotMatchRegularExpression($regex, $password_len5, "Regex: \"$regex\", password: \"$password_len5\"");
|
|
|
|
self::assertDoesNotMatchRegularExpression($regex, $password_len10, "Regex: \"$regex\", password: \"$password_len10\"");
|
2017-09-05 09:36:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|