From 1e734581d75239b4ffbf5cdca12097c32e5ae2ed Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Tue, 24 May 2022 11:36:03 +0900 Subject: [PATCH] Session class update, cli check add, tests updates, edit table update Update edit_access_data table and set unique check for edit_access_id + name so we do not have two identical keys for one edit access set Update config host and add more test domains for various access tests Update Session and move cli check to Get\System class. Some other minor session info updates New method \Get\System::checkCLI() returns true if the sapi name has cli inside, else false --- 4dev/database/table/edit_access_data.sql | 4 ++ 4dev/tests/CoreLibsConvertJsonTest.php | 1 - 4dev/tests/CoreLibsCreateSessionTest.php | 84 +++++++++++++++++++----- www/configs/config.host.php | 5 +- www/lib/CoreLibs/Create/Session.php | 7 +- www/lib/CoreLibs/Get/System.php | 10 +++ 6 files changed, 92 insertions(+), 19 deletions(-) diff --git a/4dev/database/table/edit_access_data.sql b/4dev/database/table/edit_access_data.sql index 5d08fa65..db77a900 100644 --- a/4dev/database/table/edit_access_data.sql +++ b/4dev/database/table/edit_access_data.sql @@ -14,3 +14,7 @@ CREATE TABLE edit_access_data ( name VARCHAR, value VARCHAR ) INHERITS (edit_generic) WITHOUT OIDS; + +-- create a unique index for each attached data block for each edit access can +-- only have ONE value; +CREATE UNIQUE INDEX edit_access_data_edit_access_id_name_ukey ON edit_access_data (edit_access_id, name); diff --git a/4dev/tests/CoreLibsConvertJsonTest.php b/4dev/tests/CoreLibsConvertJsonTest.php index 1ae8d5bf..af6e7e35 100644 --- a/4dev/tests/CoreLibsConvertJsonTest.php +++ b/4dev/tests/CoreLibsConvertJsonTest.php @@ -13,7 +13,6 @@ use PHPUnit\Framework\TestCase; */ final class CoreLibsConvertJsonTest extends TestCase { - /** * test list for json convert tests * diff --git a/4dev/tests/CoreLibsCreateSessionTest.php b/4dev/tests/CoreLibsCreateSessionTest.php index 5d9c2402..066e470e 100644 --- a/4dev/tests/CoreLibsCreateSessionTest.php +++ b/4dev/tests/CoreLibsCreateSessionTest.php @@ -20,6 +20,10 @@ final class CoreLibsCreateSessionTest extends TestCase */ public function sessionProvider(): array { + // 0: session name as parameter + // 1: type p (param), g: global, c: constant + // 2: exepcted name (session) + // 3: regex check return [ 'session parameter' => [ 'sessionNameParameter', @@ -42,23 +46,11 @@ final class CoreLibsCreateSessionTest extends TestCase ]; } - /** - * Undocumented function - * - * @return void - */ - protected function setUp(): void - { - if (session_id()) { - session_destroy(); - } - } - /** * Undocumented function * * @dataProvider sessionProvider - * @testdox startSession $input name for $type will be $expected_n with $expected_i [$_dataName] + * @testdox startSession $input name for $type will be $expected_n with $expected_i [$_dataName] * * @param string $input * @param string $type @@ -66,8 +58,12 @@ final class CoreLibsCreateSessionTest extends TestCase * @param string|bool $expected_i * @return void */ - public function testStartSession(string $input, string $type, $expected_n, $expected_i): void - { + public function testStartSession( + string $input, + string $type, + $expected_n, + $expected_i + ): void { // NEEDS MOCKING /* $session_id = ''; switch ($type) { @@ -101,6 +97,64 @@ final class CoreLibsCreateSessionTest extends TestCase $this->markTestSkipped('[CoreLibsCreateSessionTest] No implementation ' . 'for Create\Session. Cannot run session_start in CLI'); } + + /** + * provider for session name check + * + * @return array + */ + public function sessionNameProvider(): array + { + // 0: string for session + // 1: expected return + return [ + 'valid name' => [ + 'abc', + true + ], + 'valid name longer' => [ + 'something-abc-123', + true + ], + 'invalid name' => [ + 'abc#abc', + false + ], + 'only numbers' => [ + '123', + false + ], + 'longer than 128 chars' => [ + 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' + . 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' + . 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz', + false + ], + 'too short' => [ + '', + false + ], + ]; + } + + /** + * Undocumented function + * + * @covers ::checkValidSessionName + * @dataProvider sessionNameProvider + * @testdox checkValidSessionName $input seessionn name is $expected [$_dataName] + * + * @param string $input + * @param bool $expected + * @return void + */ + public function testCheckValidSessionName(string $input, bool $expected): void + { + $this->assertEquals( + $expected, + \CoreLibs\Create\Session::checkValidSessionName($input) + ); + } } // __END__ diff --git a/www/configs/config.host.php b/www/configs/config.host.php index 2b57ed2e..63ebde03 100644 --- a/www/configs/config.host.php +++ b/www/configs/config.host.php @@ -53,7 +53,10 @@ $SITE_CONFIG = [ 'login_enabled' => true ], // 'other.host.com' => $__LOCAL_CONFIG - 'soba-dev.tequila.jp' => $__LOCAL_CONFIG + 'soba-dev.tequila.jp' => $__LOCAL_CONFIG, + 'soba.tequila.jp' => $__LOCAL_CONFIG, + 'soba.teq.jp' => $__LOCAL_CONFIG, + 'soba-local.tokyo.tequila.jp' => $__LOCAL_CONFIG, ]; // __END__ diff --git a/www/lib/CoreLibs/Create/Session.php b/www/lib/CoreLibs/Create/Session.php index db771b80..2c6cdd10 100644 --- a/www/lib/CoreLibs/Create/Session.php +++ b/www/lib/CoreLibs/Create/Session.php @@ -61,7 +61,10 @@ class Session } /** - * Undocumented function + * start session with given session name if set + * aborts on command line or if sessions are not enabled + * also aborts if session cannot be started + * On sucess returns the session id * * @param string|null $session_name * @return string|bool @@ -69,7 +72,7 @@ class Session public static function startSession(?string $session_name = null) { // we can't start sessions on command line - if (php_sapi_name() === 'cli') { + if (\CoreLibs\Get\System::checkCLI()) { self::$error_str = '[SESSION] No sessions in php cli'; return false; } diff --git a/www/lib/CoreLibs/Get/System.php b/www/lib/CoreLibs/Get/System.php index 878cbbba..17865293 100644 --- a/www/lib/CoreLibs/Get/System.php +++ b/www/lib/CoreLibs/Get/System.php @@ -94,6 +94,16 @@ class System { return pathinfo($_SERVER['PHP_SELF']); } + + /** + * Check if the php sapi interface has cli inside + * + * @return bool True for CLI type PHP, else false + */ + public static function checkCLI(): bool + { + return substr(php_sapi_name(), 0, 3) === 'cli' ? true : false; + } } // __END__