Skip to content

Commit

Permalink
Make Tracking::enabled not use statics as it does mess up with tests
Browse files Browse the repository at this point in the history
Signed-off-by: William Desportes <williamdes@wdes.fr>
  • Loading branch information
williamdes committed Aug 24, 2021
1 parent 2c28a71 commit 36e24d1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
29 changes: 11 additions & 18 deletions libraries/classes/Tracker.php
Expand Up @@ -41,12 +41,7 @@
*/
class Tracker
{
/**
* Whether tracking is ready.
*
* @var bool
*/
protected static $enabled = false;
public const TRACKER_ENABLED_CACHE_KEY = 'phpmyadmin.tracker.enabled';

/**
* Cache to avoid quering tracking status multiple times.
Expand All @@ -58,14 +53,10 @@ class Tracker
/**
* Actually enables tracking. This needs to be done after all
* underlaying code is initialized.
*
* @return void
*
* @static
*/
public static function enable()
public static function enable(): void
{
self::$enabled = true;
Cache::set(self::TRACKER_ENABLED_CACHE_KEY, true);
}

/**
Expand All @@ -79,19 +70,20 @@ public static function isActive()
{
global $dbi;

if (! self::$enabled) {
$trackingEnabled = Cache::get(self::TRACKER_ENABLED_CACHE_KEY, false);
if (! $trackingEnabled) {
return false;
}

/**
* We need to avoid attempt to track any queries
* from Relation::getRelationsParam
*/
self::$enabled = false;
Cache::set(self::TRACKER_ENABLED_CACHE_KEY, false);
$relation = new Relation($dbi);
$cfgRelation = $relation->getRelationsParam();
/* Restore original state */
self::$enabled = true;
Cache::set(self::TRACKER_ENABLED_CACHE_KEY, true);
if (! $cfgRelation['trackingwork']) {
return false;
}
Expand Down Expand Up @@ -142,7 +134,8 @@ public static function isTracked($dbName, $tableName)
{
global $dbi;

if (! self::$enabled) {
$trackingEnabled = Cache::get(self::TRACKER_ENABLED_CACHE_KEY, false);
if (! $trackingEnabled) {
return false;
}

Expand All @@ -154,11 +147,11 @@ public static function isTracked($dbName, $tableName)
* We need to avoid attempt to track any queries
* from Relation::getRelationsParam
*/
self::$enabled = false;
Cache::set(self::TRACKER_ENABLED_CACHE_KEY, false);
$relation = new Relation($dbi);
$cfgRelation = $relation->getRelationsParam();
/* Restore original state */
self::$enabled = true;
Cache::set(self::TRACKER_ENABLED_CACHE_KEY, true);
if (! $cfgRelation['trackingwork']) {
return false;
}
Expand Down
22 changes: 11 additions & 11 deletions test/classes/TrackerTest.php
Expand Up @@ -4,12 +4,12 @@

namespace PhpMyAdmin\Tests;

use PhpMyAdmin\Cache;
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Tracker;
use PhpMyAdmin\Util;
use PhpMyAdmin\Version;
use ReflectionMethod;
use ReflectionProperty;

/**
* @covers \PhpMyAdmin\Tracker
Expand Down Expand Up @@ -53,12 +53,12 @@ protected function setUp(): void
*/
public function testEnabled(): void
{
$reflection = new ReflectionProperty(Tracker::class, 'enabled');
$reflection->setAccessible(true);

$this->assertFalse(
Cache::has(Tracker::TRACKER_ENABLED_CACHE_KEY)
);
Tracker::enable();
$this->assertTrue(
$reflection->getValue()
Cache::get(Tracker::TRACKER_ENABLED_CACHE_KEY)
);
}

Expand All @@ -67,9 +67,9 @@ public function testEnabled(): void
*/
public function testIsActive(): void
{
$attr = new ReflectionProperty(Tracker::class, 'enabled');
$attr->setAccessible(true);
$attr->setValue(false);
$this->assertFalse(
Cache::has(Tracker::TRACKER_ENABLED_CACHE_KEY)
);

$this->assertFalse(
Tracker::isActive()
Expand Down Expand Up @@ -142,9 +142,9 @@ public function getTableNameData(): array
*/
public function testIsTracked(): void
{
$attr = new ReflectionProperty(Tracker::class, 'enabled');
$attr->setAccessible(true);
$attr->setValue(false);
$this->assertFalse(
Cache::has(Tracker::TRACKER_ENABLED_CACHE_KEY)
);

$this->assertFalse(
Tracker::isTracked('', '')
Expand Down

0 comments on commit 36e24d1

Please sign in to comment.