Skip to content

Commit

Permalink
FIX Ensure environment is checked before enabling deprecations (#11055)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Nov 21, 2023
1 parent bbc6167 commit 7eab49f
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/Dev/Deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,23 @@ public static function get_enabled()

public static function isEnabled(): bool
{
if (Environment::hasEnv('SS_DEPRECATION_ENABLED')) {
$envVar = Environment::getEnv('SS_DEPRECATION_ENABLED');
return self::varAsBoolean($envVar);
$hasEnv = Environment::hasEnv('SS_DEPRECATION_ENABLED');

// Return early if disabled
if ($hasEnv && !Environment::getEnv('SS_DEPRECATION_ENABLED')) {
return false;
}
if (!$hasEnv && !static::$currentlyEnabled) {
// Static property is ignored if SS_DEPRECATION_ENABLED was set
return false;
}

// If it's enabled, explicitly don't allow for non-dev environments
if (!Director::isDev()) {
return false;
}
return static::$currentlyEnabled;

return true;
}

/**
Expand Down
102 changes: 102 additions & 0 deletions tests/php/Dev/DeprecationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

use PHPUnit\Framework\Error\Deprecated;
use ReflectionMethod;
use ReflectionProperty;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Environment;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\Tests\DeprecationTest\DeprecationTestObject;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Kernel;

class DeprecationTest extends SapphireTest
{
Expand Down Expand Up @@ -445,4 +447,104 @@ public function testVarAsBoolean($rawValue, bool $expected)

$this->assertSame($expected, $reflectionVarAsBoolean->invoke(null, $rawValue));
}

public function provideIsEnabled()
{
return [
'dev, explicitly enabled' => [
'envMode' => 'dev',
'envEnabled' => true,
'staticEnabled' => true,
'expected' => true,
],
'dev, explicitly enabled override static' => [
'envMode' => 'dev',
'envEnabled' => true,
'staticEnabled' => false,
'expected' => true,
],
'dev, explicitly disabled override static' => [
'envMode' => 'dev',
'envEnabled' => false,
'staticEnabled' => true,
'expected' => false,
],
'dev, explicitly disabled' => [
'envMode' => 'dev',
'envEnabled' => false,
'staticEnabled' => false,
'expected' => false,
],
'dev, statically disabled' => [
'envMode' => 'dev',
'envEnabled' => null,
'staticEnabled' => true,
'expected' => true,
],
'dev, statically disabled' => [
'envMode' => 'dev',
'envEnabled' => null,
'staticEnabled' => false,
'expected' => false,
],
'live, explicitly enabled' => [
'envMode' => 'live',
'envEnabled' => true,
'staticEnabled' => true,
'expected' => false,
],
'live, explicitly disabled' => [
'envMode' => 'live',
'envEnabled' => false,
'staticEnabled' => false,
'expected' => false,
],
'live, statically disabled' => [
'envMode' => 'live',
'envEnabled' => null,
'staticEnabled' => true,
'expected' => false,
],
'live, statically disabled' => [
'envMode' => 'live',
'envEnabled' => null,
'staticEnabled' => false,
'expected' => false,
],
];
}

/**
* @dataProvider provideIsEnabled
*/
public function testIsEnabled(string $envMode, ?bool $envEnabled, bool $staticEnabled, bool $expected)
{
/** @var Kernel $kernel */
$kernel = Injector::inst()->get(Kernel::class);
$origMode = $kernel->getEnvironment();
$origEnvEnabled = Environment::getEnv('SS_DEPRECATION_ENABLED');
$reflectionEnabled = new ReflectionProperty(Deprecation::class, 'currentlyEnabled');
$reflectionEnabled->setAccessible(true);
$origStaticEnabled = $reflectionEnabled->getValue();

try {
$kernel->setEnvironment($envMode);
Environment::setEnv('SS_DEPRECATION_ENABLED', $envEnabled);
$this->setEnabledViaStatic($staticEnabled);
$this->assertSame($expected, Deprecation::isEnabled());
} finally {
$kernel->setEnvironment($origMode);
Environment::setEnv('SS_DEPRECATION_ENABLED', $origEnvEnabled);
$this->setEnabledViaStatic($origStaticEnabled);
}
}

private function setEnabledViaStatic(bool $enabled): void
{
if ($enabled) {
Deprecation::enable();
} else {
Deprecation::disable();
}
}
}

0 comments on commit 7eab49f

Please sign in to comment.