Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Ensure environment is checked before enabling deprecations #11055

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
michalkleiner marked this conversation as resolved.
Show resolved Hide resolved
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();
}
}
}