From e4e274976eef796207744d897213631910b07ed8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sun, 25 Oct 2020 22:45:56 -0300 Subject: [PATCH] Extract cache methods from Util to SessionCache class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- libraries/classes/CheckUserPrivileges.php | 39 +++++----- .../Controllers/NavigationController.php | 4 +- libraries/classes/DatabaseInterface.php | 35 ++++----- libraries/classes/Menu.php | 13 ++-- .../Plugins/Auth/AuthenticationCookie.php | 19 ++--- libraries/classes/Profiling.php | 9 ++- libraries/classes/StorageEngine.php | 3 +- libraries/classes/Util.php | 75 +------------------ libraries/classes/Utils/SessionCache.php | 58 ++++++++++++++ psalm-baseline.xml | 15 +++- .../Server/BinlogControllerTest.php | 4 +- .../Server/Status/MonitorControllerTest.php | 4 +- test/classes/DatabaseInterfaceTest.php | 6 +- test/classes/ProfilingTest.php | 6 +- test/classes/UtilTest.php | 74 +----------------- test/classes/Utils/SessionCacheTest.php | 73 ++++++++++++++++++ 16 files changed, 225 insertions(+), 212 deletions(-) create mode 100644 libraries/classes/Utils/SessionCache.php create mode 100644 test/classes/Utils/SessionCacheTest.php diff --git a/libraries/classes/CheckUserPrivileges.php b/libraries/classes/CheckUserPrivileges.php index a0928fafb2..189c7c0253 100644 --- a/libraries/classes/CheckUserPrivileges.php +++ b/libraries/classes/CheckUserPrivileges.php @@ -8,6 +8,7 @@ declare(strict_types=1); namespace PhpMyAdmin; use PhpMyAdmin\Query\Utilities; +use PhpMyAdmin\Utils\SessionCache; use function mb_strpos; use function mb_substr; use function preg_match; @@ -169,33 +170,33 @@ class CheckUserPrivileges */ private function analyseShowGrant(): void { - if (Util::cacheExists('is_create_db_priv')) { - $GLOBALS['is_create_db_priv'] = Util::cacheGet( + if (SessionCache::has('is_create_db_priv')) { + $GLOBALS['is_create_db_priv'] = SessionCache::get( 'is_create_db_priv' ); - $GLOBALS['is_reload_priv'] = Util::cacheGet( + $GLOBALS['is_reload_priv'] = SessionCache::get( 'is_reload_priv' ); - $GLOBALS['db_to_create'] = Util::cacheGet( + $GLOBALS['db_to_create'] = SessionCache::get( 'db_to_create' ); - $GLOBALS['dbs_where_create_table_allowed'] = Util::cacheGet( + $GLOBALS['dbs_where_create_table_allowed'] = SessionCache::get( 'dbs_where_create_table_allowed' ); - $GLOBALS['dbs_to_test'] = Util::cacheGet( + $GLOBALS['dbs_to_test'] = SessionCache::get( 'dbs_to_test' ); - $GLOBALS['db_priv'] = Util::cacheGet( + $GLOBALS['db_priv'] = SessionCache::get( 'db_priv' ); - $GLOBALS['col_priv'] = Util::cacheGet( + $GLOBALS['col_priv'] = SessionCache::get( 'col_priv' ); - $GLOBALS['table_priv'] = Util::cacheGet( + $GLOBALS['table_priv'] = SessionCache::get( 'table_priv' ); - $GLOBALS['proc_priv'] = Util::cacheGet( + $GLOBALS['proc_priv'] = SessionCache::get( 'proc_priv' ); @@ -324,19 +325,19 @@ class CheckUserPrivileges // must also cacheUnset() them in // PhpMyAdmin\Plugins\Auth\AuthenticationCookie - Util::cacheSet('is_create_db_priv', $GLOBALS['is_create_db_priv']); - Util::cacheSet('is_reload_priv', $GLOBALS['is_reload_priv']); - Util::cacheSet('db_to_create', $GLOBALS['db_to_create']); - Util::cacheSet( + SessionCache::set('is_create_db_priv', $GLOBALS['is_create_db_priv']); + SessionCache::set('is_reload_priv', $GLOBALS['is_reload_priv']); + SessionCache::set('db_to_create', $GLOBALS['db_to_create']); + SessionCache::set( 'dbs_where_create_table_allowed', $GLOBALS['dbs_where_create_table_allowed'] ); - Util::cacheSet('dbs_to_test', $GLOBALS['dbs_to_test']); + SessionCache::set('dbs_to_test', $GLOBALS['dbs_to_test']); - Util::cacheSet('proc_priv', $GLOBALS['proc_priv']); - Util::cacheSet('table_priv', $GLOBALS['table_priv']); - Util::cacheSet('col_priv', $GLOBALS['col_priv']); - Util::cacheSet('db_priv', $GLOBALS['db_priv']); + SessionCache::set('proc_priv', $GLOBALS['proc_priv']); + SessionCache::set('table_priv', $GLOBALS['table_priv']); + SessionCache::set('col_priv', $GLOBALS['col_priv']); + SessionCache::set('db_priv', $GLOBALS['db_priv']); } /** diff --git a/libraries/classes/Controllers/NavigationController.php b/libraries/classes/Controllers/NavigationController.php index c4a26290d9..8a5722ff24 100644 --- a/libraries/classes/Controllers/NavigationController.php +++ b/libraries/classes/Controllers/NavigationController.php @@ -10,7 +10,7 @@ use PhpMyAdmin\Navigation\Navigation; use PhpMyAdmin\Relation; use PhpMyAdmin\Response; use PhpMyAdmin\Template; -use PhpMyAdmin\Util; +use PhpMyAdmin\Utils\SessionCache; /** * The navigation panel @@ -60,7 +60,7 @@ class NavigationController extends AbstractController } if (isset($_POST['reload'])) { - Util::cacheSet('dbs_to_test', false);// Empty database list cache, see #14252 + SessionCache::set('dbs_to_test', false);// Empty database list cache, see #14252 } $cfgRelation = $this->relation->getRelationsParam(); diff --git a/libraries/classes/DatabaseInterface.php b/libraries/classes/DatabaseInterface.php index 3d70bef65a..2e4241d023 100644 --- a/libraries/classes/DatabaseInterface.php +++ b/libraries/classes/DatabaseInterface.php @@ -18,6 +18,7 @@ use PhpMyAdmin\Query\Compatibility; use PhpMyAdmin\Query\Generator as QueryGenerator; use PhpMyAdmin\Query\Utilities; use PhpMyAdmin\SqlParser\Context; +use PhpMyAdmin\Utils\SessionCache; use const E_USER_WARNING; use const LOG_INFO; use const LOG_NDELAY; @@ -1735,12 +1736,12 @@ class DatabaseInterface implements DbalInterface */ public function getCurrentUser(): string { - if (Util::cacheExists('mysql_cur_user')) { - return Util::cacheGet('mysql_cur_user'); + if (SessionCache::has('mysql_cur_user')) { + return SessionCache::get('mysql_cur_user'); } $user = $this->fetchValue('SELECT CURRENT_USER();'); if ($user !== false) { - Util::cacheSet('mysql_cur_user', $user); + SessionCache::set('mysql_cur_user', $user); return $user; } @@ -1750,8 +1751,8 @@ class DatabaseInterface implements DbalInterface public function isSuperUser(): bool { - if (Util::cacheExists('is_superuser')) { - return Util::cacheGet('is_superuser'); + if (SessionCache::has('is_superuser')) { + return SessionCache::get('is_superuser'); } if (! $this->isConnected()) { @@ -1770,7 +1771,7 @@ class DatabaseInterface implements DbalInterface } $this->freeResult($result); - Util::cacheSet('is_superuser', $isSuperUser); + SessionCache::set('is_superuser', $isSuperUser); return $isSuperUser; } @@ -1779,8 +1780,8 @@ class DatabaseInterface implements DbalInterface { global $cfg; - if (Util::cacheExists('is_grantuser')) { - return Util::cacheGet('is_grantuser'); + if (SessionCache::has('is_grantuser')) { + return SessionCache::get('is_grantuser'); } if (! $this->isConnected()) { @@ -1799,7 +1800,7 @@ class DatabaseInterface implements DbalInterface } } - Util::cacheSet('is_grantuser', $hasGrantPrivilege); + SessionCache::set('is_grantuser', $hasGrantPrivilege); return $hasGrantPrivilege; } @@ -1813,7 +1814,7 @@ class DatabaseInterface implements DbalInterface } $this->freeResult($result); - Util::cacheSet('is_grantuser', $hasGrantPrivilege); + SessionCache::set('is_grantuser', $hasGrantPrivilege); return $hasGrantPrivilege; } @@ -1822,8 +1823,8 @@ class DatabaseInterface implements DbalInterface { global $cfg; - if (Util::cacheExists('is_createuser')) { - return Util::cacheGet('is_createuser'); + if (SessionCache::has('is_createuser')) { + return SessionCache::get('is_createuser'); } if (! $this->isConnected()) { @@ -1844,7 +1845,7 @@ class DatabaseInterface implements DbalInterface } } - Util::cacheSet('is_createuser', $hasCreatePrivilege); + SessionCache::set('is_createuser', $hasCreatePrivilege); return $hasCreatePrivilege; } @@ -1858,7 +1859,7 @@ class DatabaseInterface implements DbalInterface } $this->freeResult($result); - Util::cacheSet('is_createuser', $hasCreatePrivilege); + SessionCache::set('is_createuser', $hasCreatePrivilege); return $hasCreatePrivilege; } @@ -2319,13 +2320,13 @@ class DatabaseInterface implements DbalInterface */ public function isAmazonRds(): bool { - if (Util::cacheExists('is_amazon_rds')) { - return Util::cacheGet('is_amazon_rds'); + if (SessionCache::has('is_amazon_rds')) { + return SessionCache::get('is_amazon_rds'); } $sql = 'SELECT @@basedir'; $result = $this->fetchValue($sql); $rds = (substr($result, 0, 10) === '/rdsdbbin/'); - Util::cacheSet('is_amazon_rds', $rds); + SessionCache::set('is_amazon_rds', $rds); return $rds; } diff --git a/libraries/classes/Menu.php b/libraries/classes/Menu.php index fc054dd02d..462ebf8cfb 100644 --- a/libraries/classes/Menu.php +++ b/libraries/classes/Menu.php @@ -8,6 +8,7 @@ declare(strict_types=1); namespace PhpMyAdmin; use PhpMyAdmin\Query\Utilities; +use PhpMyAdmin\Utils\SessionCache; use function array_key_exists; use function count; use function in_array; @@ -139,8 +140,8 @@ class Menu global $dbi; $cache_key = 'menu-levels-' . $level; - if (Util::cacheExists($cache_key)) { - return Util::cacheGet($cache_key); + if (SessionCache::has($cache_key)) { + return SessionCache::get($cache_key); } $allowedTabs = Util::getMenuTabList($level); $cfgRelation = $this->relation->getRelationsParam(); @@ -169,7 +170,7 @@ class Menu } } } - Util::cacheSet($cache_key, $allowedTabs); + SessionCache::set($cache_key, $allowedTabs); return $allowedTabs; } @@ -506,8 +507,8 @@ class Menu $is_superuser = $dbi->isSuperUser(); $isCreateOrGrantUser = $dbi->isGrantUser() || $dbi->isCreateUser(); - if (Util::cacheExists('binary_logs')) { - $binary_logs = Util::cacheGet('binary_logs'); + if (SessionCache::has('binary_logs')) { + $binary_logs = SessionCache::get('binary_logs'); } else { $binary_logs = $dbi->fetchResult( 'SHOW MASTER LOGS', @@ -516,7 +517,7 @@ class Menu DatabaseInterface::CONNECT_USER, DatabaseInterface::QUERY_STORE ); - Util::cacheSet('binary_logs', $binary_logs); + SessionCache::set('binary_logs', $binary_logs); } $tabs = []; diff --git a/libraries/classes/Plugins/Auth/AuthenticationCookie.php b/libraries/classes/Plugins/Auth/AuthenticationCookie.php index a59ee909ac..96e723c878 100644 --- a/libraries/classes/Plugins/Auth/AuthenticationCookie.php +++ b/libraries/classes/Plugins/Auth/AuthenticationCookie.php @@ -18,6 +18,7 @@ use PhpMyAdmin\Session; use PhpMyAdmin\Template; use PhpMyAdmin\Url; use PhpMyAdmin\Util; +use PhpMyAdmin\Utils\SessionCache; use phpseclib\Crypt; use phpseclib\Crypt\Random; use ReCaptcha; @@ -405,15 +406,15 @@ class AuthenticationCookie extends AuthenticationPlugin } // All sessions expired if (empty($_SESSION['browser_access_time'])) { - Util::cacheUnset('is_create_db_priv'); - Util::cacheUnset('is_reload_priv'); - Util::cacheUnset('db_to_create'); - Util::cacheUnset('dbs_where_create_table_allowed'); - Util::cacheUnset('dbs_to_test'); - Util::cacheUnset('db_priv'); - Util::cacheUnset('col_priv'); - Util::cacheUnset('table_priv'); - Util::cacheUnset('proc_priv'); + SessionCache::remove('is_create_db_priv'); + SessionCache::remove('is_reload_priv'); + SessionCache::remove('db_to_create'); + SessionCache::remove('dbs_where_create_table_allowed'); + SessionCache::remove('dbs_to_test'); + SessionCache::remove('db_priv'); + SessionCache::remove('col_priv'); + SessionCache::remove('table_priv'); + SessionCache::remove('proc_priv'); $this->showFailure('no-activity'); if (! defined('TESTSUITE')) { diff --git a/libraries/classes/Profiling.php b/libraries/classes/Profiling.php index 29d11846a3..5258c4a46d 100644 --- a/libraries/classes/Profiling.php +++ b/libraries/classes/Profiling.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace PhpMyAdmin; +use PhpMyAdmin\Utils\SessionCache; use function is_array; /** @@ -13,8 +14,8 @@ final class Profiling { public static function isSupported(DatabaseInterface $dbi): bool { - if (Util::cacheExists('profiling_supported')) { - return Util::cacheGet('profiling_supported'); + if (SessionCache::has('profiling_supported')) { + return SessionCache::get('profiling_supported'); } /** @@ -23,12 +24,12 @@ final class Profiling * and do not set a constant as we might be switching servers */ if ($dbi->fetchValue('SELECT @@have_profiling')) { - Util::cacheSet('profiling_supported', true); + SessionCache::set('profiling_supported', true); return true; } - Util::cacheSet('profiling_supported', false); + SessionCache::set('profiling_supported', false); return false; } diff --git a/libraries/classes/StorageEngine.php b/libraries/classes/StorageEngine.php index 44dd0facf8..f5e5f9fd4c 100644 --- a/libraries/classes/StorageEngine.php +++ b/libraries/classes/StorageEngine.php @@ -20,6 +20,7 @@ use PhpMyAdmin\Engines\Ndbcluster; use PhpMyAdmin\Engines\Pbxt; use PhpMyAdmin\Engines\PerformanceSchema; use PhpMyAdmin\Html\Generator; +use PhpMyAdmin\Utils\SessionCache; use function array_key_exists; use function define; use function explode; @@ -106,7 +107,7 @@ class StorageEngine if ($storage_engines == null) { $storage_engines = $dbi->fetchResult('SHOW STORAGE ENGINES', 'Engine'); if ($dbi->getVersion() >= 50708) { - $disabled = (string) Util::cacheGet( + $disabled = (string) SessionCache::get( 'disabled_storage_engines', static function () use ($dbi) { return $dbi->fetchValue( diff --git a/libraries/classes/Util.php b/libraries/classes/Util.php index 8ac3b37115..117288f07e 100644 --- a/libraries/classes/Util.php +++ b/libraries/classes/Util.php @@ -4,13 +4,13 @@ declare(strict_types=1); namespace PhpMyAdmin; -use Closure; use PhpMyAdmin\Html\Generator; use PhpMyAdmin\Html\MySQLDocumentation; use PhpMyAdmin\Query\Utilities; use PhpMyAdmin\SqlParser\Components\Expression; use PhpMyAdmin\SqlParser\Context; use PhpMyAdmin\SqlParser\Token; +use PhpMyAdmin\Utils\SessionCache; use phpseclib\Crypt\Random; use stdClass; use const ENT_COMPAT; @@ -1348,76 +1348,9 @@ class Util */ public static function clearUserCache(): void { - self::cacheUnset('is_superuser'); - self::cacheUnset('is_createuser'); - self::cacheUnset('is_grantuser'); - } - - /** - * Calculates session cache key - */ - public static function cacheKey(): string - { - if (isset($GLOBALS['cfg']['Server']['user'])) { - return 'server_' . $GLOBALS['server'] . '_' . $GLOBALS['cfg']['Server']['user']; - } - - return 'server_' . $GLOBALS['server']; - } - - /** - * Verifies if something is cached in the session - * - * @param string $var variable name - */ - public static function cacheExists($var): bool - { - return isset($_SESSION['cache'][self::cacheKey()][$var]); - } - - /** - * Gets cached information from the session - * - * @param string $var variable name - * @param Closure $callback callback to fetch the value - * - * @return mixed - */ - public static function cacheGet($var, $callback = null) - { - if (self::cacheExists($var)) { - return $_SESSION['cache'][self::cacheKey()][$var]; - } - - if ($callback) { - $val = $callback(); - self::cacheSet($var, $val); - - return $val; - } - - return null; - } - - /** - * Caches information in the session - * - * @param string $var variable name - * @param mixed $val value - */ - public static function cacheSet($var, $val = null): void - { - $_SESSION['cache'][self::cacheKey()][$var] = $val; - } - - /** - * Removes cached information from the session - * - * @param string $var variable name - */ - public static function cacheUnset($var): void - { - unset($_SESSION['cache'][self::cacheKey()][$var]); + SessionCache::remove('is_superuser'); + SessionCache::remove('is_createuser'); + SessionCache::remove('is_grantuser'); } /** diff --git a/libraries/classes/Utils/SessionCache.php b/libraries/classes/Utils/SessionCache.php new file mode 100644 index 0000000000..99333e952b --- /dev/null +++ b/libraries/classes/Utils/SessionCache.php @@ -0,0 +1,58 @@ +$a $b + + SessionCache::get('is_amazon_rds') + SessionCache::get('is_createuser') + SessionCache::get('is_grantuser') + SessionCache::get('is_superuser') + SessionCache::get('mysql_cur_user') + $table @@ -1127,8 +1134,9 @@ - + $allowedTabs + SessionCache::get($cache_key) mb_strpos($row['tab'], '_') @@ -2104,6 +2112,11 @@ bool + + + SessionCache::get('profiling_supported') + + bool|string diff --git a/test/classes/Controllers/Server/BinlogControllerTest.php b/test/classes/Controllers/Server/BinlogControllerTest.php index 240505e889..d463334e6a 100644 --- a/test/classes/Controllers/Server/BinlogControllerTest.php +++ b/test/classes/Controllers/Server/BinlogControllerTest.php @@ -10,7 +10,7 @@ use PhpMyAdmin\Template; use PhpMyAdmin\Tests\AbstractTestCase; use PhpMyAdmin\Tests\Stubs\Response; use PhpMyAdmin\Url; -use PhpMyAdmin\Util; +use PhpMyAdmin\Utils\SessionCache; class BinlogControllerTest extends AbstractTestCase { @@ -34,7 +34,7 @@ class BinlogControllerTest extends AbstractTestCase $GLOBALS['table'] = 'table'; $GLOBALS['PMA_PHP_SELF'] = 'index.php'; - Util::cacheSet('profiling_supported', true); + SessionCache::set('profiling_supported', true); } public function testIndex(): void diff --git a/test/classes/Controllers/Server/Status/MonitorControllerTest.php b/test/classes/Controllers/Server/Status/MonitorControllerTest.php index b976cdd33e..42d257ad84 100644 --- a/test/classes/Controllers/Server/Status/MonitorControllerTest.php +++ b/test/classes/Controllers/Server/Status/MonitorControllerTest.php @@ -10,7 +10,7 @@ use PhpMyAdmin\Server\Status\Monitor; use PhpMyAdmin\Template; use PhpMyAdmin\Tests\AbstractTestCase; use PhpMyAdmin\Tests\Stubs\Response; -use PhpMyAdmin\Util; +use PhpMyAdmin\Utils\SessionCache; class MonitorControllerTest extends AbstractTestCase { @@ -273,7 +273,7 @@ class MonitorControllerTest extends AbstractTestCase global $cached_affected_rows; $cached_affected_rows = 'cached_affected_rows'; - Util::cacheSet('profiling_supported', true); + SessionCache::set('profiling_supported', true); $value = [ 'sql_text' => 'insert sql_text', diff --git a/test/classes/DatabaseInterfaceTest.php b/test/classes/DatabaseInterfaceTest.php index 0407088b04..016d87ec24 100644 --- a/test/classes/DatabaseInterfaceTest.php +++ b/test/classes/DatabaseInterfaceTest.php @@ -12,7 +12,7 @@ use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\Query\Utilities; use PhpMyAdmin\SystemDatabase; use PhpMyAdmin\Tests\Stubs\DbiDummy; -use PhpMyAdmin\Util; +use PhpMyAdmin\Utils\SessionCache; use stdClass; /** @@ -48,7 +48,7 @@ class DatabaseInterfaceTest extends AbstractTestCase */ public function testGetCurrentUser($value, string $string, array $expected): void { - Util::cacheUnset('mysql_cur_user'); + SessionCache::remove('mysql_cur_user'); $extension = new DbiDummy(); /** @var array $value */ @@ -290,7 +290,7 @@ class DatabaseInterfaceTest extends AbstractTestCase */ public function atestIsAmazonRdsData(array $value, bool $expected): void { - Util::cacheUnset('is_amazon_rds'); + SessionCache::remove('is_amazon_rds'); $extension = new DbiDummy(); $extension->setResult('SELECT @@basedir', $value); diff --git a/test/classes/ProfilingTest.php b/test/classes/ProfilingTest.php index ef476f4e95..16b2b3a1b1 100644 --- a/test/classes/ProfilingTest.php +++ b/test/classes/ProfilingTest.php @@ -5,7 +5,7 @@ declare(strict_types=1); namespace PhpMyAdmin\Tests; use PhpMyAdmin\Profiling; -use PhpMyAdmin\Util; +use PhpMyAdmin\Utils\SessionCache; class ProfilingTest extends AbstractTestCase { @@ -15,11 +15,11 @@ class ProfilingTest extends AbstractTestCase $server = 1; - Util::cacheSet('profiling_supported', true); + SessionCache::set('profiling_supported', true); $condition = Profiling::isSupported($dbi); $this->assertTrue($condition); - Util::cacheSet('profiling_supported', false); + SessionCache::set('profiling_supported', false); $condition = Profiling::isSupported($dbi); $this->assertFalse($condition); } diff --git a/test/classes/UtilTest.php b/test/classes/UtilTest.php index 0582cd66c4..ccf20cd146 100644 --- a/test/classes/UtilTest.php +++ b/test/classes/UtilTest.php @@ -10,6 +10,7 @@ use PhpMyAdmin\MoTranslator\Loader; use PhpMyAdmin\SqlParser\Context; use PhpMyAdmin\SqlParser\Token; use PhpMyAdmin\Util; +use PhpMyAdmin\Utils\SessionCache; use const LC_ALL; use function date_default_timezone_get; use function date_default_timezone_set; @@ -445,77 +446,6 @@ class UtilTest extends AbstractTestCase $this->assertEquals(16, strlen(Util::generateRandom(16))); } - /** - * Test if cached data is available after set - * - * @covers \PhpMyAdmin\Util::cacheExists - */ - public function testCacheExists(): void - { - $GLOBALS['server'] = 'server'; - Util::cacheSet('test_data', 5); - Util::cacheSet('test_data_2', 5); - - $this->assertTrue(Util::cacheExists('test_data')); - $this->assertTrue(Util::cacheExists('test_data_2')); - $this->assertFalse(Util::cacheExists('fake_data_2')); - } - - /** - * Test if PhpMyAdmin\Util::cacheGet does not return data for non existing cache entries - * - * @covers \PhpMyAdmin\Util::cacheGet - */ - public function testCacheGet(): void - { - $GLOBALS['server'] = 'server'; - Util::cacheSet('test_data', 5); - Util::cacheSet('test_data_2', 5); - - $this->assertNotNull(Util::cacheGet('test_data')); - $this->assertNotNull(Util::cacheGet('test_data_2')); - $this->assertNull(Util::cacheGet('fake_data_2')); - } - - /** - * Test retrieval of cached data - * - * @covers \PhpMyAdmin\Util::cacheSet - */ - public function testCacheSetGet(): void - { - $GLOBALS['server'] = 'server'; - Util::cacheSet('test_data', 25); - - Util::cacheSet('test_data', 5); - $this->assertEquals(5, $_SESSION['cache']['server_server']['test_data']); - Util::cacheSet('test_data_3', 3); - $this->assertEquals(3, $_SESSION['cache']['server_server']['test_data_3']); - } - - /** - * Test clearing cached values - * - * @covers \PhpMyAdmin\Util::cacheUnset - */ - public function testCacheUnSet(): void - { - $GLOBALS['server'] = 'server'; - Util::cacheSet('test_data', 25); - Util::cacheSet('test_data_2', 25); - - Util::cacheUnset('test_data'); - $this->assertArrayNotHasKey( - 'test_data', - $_SESSION['cache']['server_server'] - ); - Util::cacheUnset('test_data_2'); - $this->assertArrayNotHasKey( - 'test_data_2', - $_SESSION['cache']['server_server'] - ); - } - /** * Test clearing user cache * @@ -524,7 +454,7 @@ class UtilTest extends AbstractTestCase public function testClearUserCache(): void { $GLOBALS['server'] = 'server'; - Util::cacheSet('is_superuser', 'yes'); + SessionCache::set('is_superuser', 'yes'); $this->assertEquals( 'yes', $_SESSION['cache']['server_server']['is_superuser'] diff --git a/test/classes/Utils/SessionCacheTest.php b/test/classes/Utils/SessionCacheTest.php new file mode 100644 index 0000000000..f994db0dfa --- /dev/null +++ b/test/classes/Utils/SessionCacheTest.php @@ -0,0 +1,73 @@ +assertNotNull(SessionCache::get('test_data')); + $this->assertNotNull(SessionCache::get('test_data_2')); + $this->assertNull(SessionCache::get('fake_data_2')); + } + + public function testRemove(): void + { + global $server; + + $server = 'server'; + + SessionCache::set('test_data', 25); + SessionCache::set('test_data_2', 25); + + SessionCache::remove('test_data'); + $this->assertArrayNotHasKey( + 'test_data', + $_SESSION['cache']['server_server'] + ); + SessionCache::remove('test_data_2'); + $this->assertArrayNotHasKey( + 'test_data_2', + $_SESSION['cache']['server_server'] + ); + } + + public function testSet(): void + { + global $server; + + $server = 'server'; + + SessionCache::set('test_data', 25); + SessionCache::set('test_data', 5); + $this->assertEquals(5, $_SESSION['cache']['server_server']['test_data']); + SessionCache::set('test_data_3', 3); + $this->assertEquals(3, $_SESSION['cache']['server_server']['test_data_3']); + } + + public function testHas(): void + { + global $server; + + $server = 'server'; + + SessionCache::set('test_data', 5); + SessionCache::set('test_data_2', 5); + + $this->assertTrue(SessionCache::has('test_data')); + $this->assertTrue(SessionCache::has('test_data_2')); + $this->assertFalse(SessionCache::has('fake_data_2')); + } +}