2012-08-27 06:42:25 +02:00
|
|
|
<?php
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
namespace SilverStripe\Core\Tests\Manifest;
|
|
|
|
|
2017-02-22 04:15:08 +01:00
|
|
|
use SilverStripe\Config\Collections\MemoryConfigCollection;
|
2017-02-26 04:52:33 +01:00
|
|
|
use SilverStripe\Core\Config\CoreConfigFactory;
|
2017-10-16 05:43:12 +02:00
|
|
|
use SilverStripe\Core\Environment;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Core\Kernel;
|
2017-02-27 02:38:06 +01:00
|
|
|
use SilverStripe\Core\Manifest\ModuleLoader;
|
|
|
|
use SilverStripe\Core\Manifest\ModuleManifest;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
class ConfigManifestTest extends SapphireTest
|
|
|
|
{
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
2017-02-27 02:38:06 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$moduleManifest = new ModuleManifest(dirname(__FILE__) . '/fixtures/configmanifest');
|
2017-06-22 12:50:45 +02:00
|
|
|
$moduleManifest->init();
|
2017-05-19 04:38:06 +02:00
|
|
|
ModuleLoader::inst()->pushManifest($moduleManifest);
|
2017-02-27 02:38:06 +01:00
|
|
|
}
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function tearDown(): void
|
2017-02-27 02:38:06 +01:00
|
|
|
{
|
2017-05-19 04:38:06 +02:00
|
|
|
ModuleLoader::inst()->popManifest();
|
2017-02-27 02:38:06 +01:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* This is a helper method for getting a new manifest
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function getConfigFixtureValue($name)
|
|
|
|
{
|
2017-02-22 04:15:08 +01:00
|
|
|
return $this->getTestConfig()->get(__CLASS__, $name);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-22 04:15:08 +01:00
|
|
|
* Build a new config based on YMl manifest
|
2016-12-16 05:34:21 +01:00
|
|
|
*
|
2017-02-22 04:15:08 +01:00
|
|
|
* @return MemoryConfigCollection
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
2017-02-22 04:15:08 +01:00
|
|
|
public function getTestConfig()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2017-02-22 04:15:08 +01:00
|
|
|
$config = new MemoryConfigCollection();
|
2017-06-22 12:50:45 +02:00
|
|
|
$factory = new CoreConfigFactory();
|
|
|
|
$transformer = $factory->buildYamlTransformerForPath(dirname(__FILE__) . '/fixtures/configmanifest');
|
2017-02-22 04:15:08 +01:00
|
|
|
$config->transform([$transformer]);
|
|
|
|
return $config;
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2017-01-17 13:23:41 +01:00
|
|
|
/**
|
2017-02-22 04:15:08 +01:00
|
|
|
* This is a helper method for displaying a relevant message about a parsing failure
|
2017-01-17 13:23:41 +01:00
|
|
|
*
|
2017-02-22 04:15:08 +01:00
|
|
|
* @param string $path
|
|
|
|
* @return string
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
2017-02-22 04:15:08 +01:00
|
|
|
protected function getParsedAsMessage($path)
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2017-02-22 04:15:08 +01:00
|
|
|
return sprintf('Reference path "%s" failed to parse correctly', $path);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testClassRules()
|
|
|
|
{
|
|
|
|
$config = $this->getConfigFixtureValue('Class');
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'Yes',
|
|
|
|
@$config['DirectorExists'],
|
|
|
|
'Only rule correctly detects existing class'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'No',
|
|
|
|
@$config['NoSuchClassExists'],
|
|
|
|
'Except rule correctly detects missing class'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testModuleRules()
|
|
|
|
{
|
|
|
|
$config = $this->getConfigFixtureValue('Module');
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'Yes',
|
|
|
|
@$config['MysiteExists'],
|
|
|
|
'Only rule correctly detects existing module'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'No',
|
|
|
|
@$config['NoSuchModuleExists'],
|
|
|
|
'Except rule correctly detects missing module'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnvVarSetRules()
|
|
|
|
{
|
2017-10-16 05:43:12 +02:00
|
|
|
Environment::setEnv('ENVVARSET_FOO', '1');
|
2016-12-16 05:34:21 +01:00
|
|
|
$config = $this->getConfigFixtureValue('EnvVarSet');
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'Yes',
|
|
|
|
@$config['FooSet'],
|
|
|
|
'Only rule correctly detects set environment variable'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'No',
|
|
|
|
@$config['BarSet'],
|
|
|
|
'Except rule correctly detects unset environment variable'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConstantDefinedRules()
|
|
|
|
{
|
|
|
|
define('CONSTANTDEFINED_FOO', 1);
|
|
|
|
$config = $this->getConfigFixtureValue('ConstantDefined');
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'Yes',
|
|
|
|
@$config['FooDefined'],
|
|
|
|
'Only rule correctly detects defined constant'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'No',
|
|
|
|
@$config['BarDefined'],
|
|
|
|
'Except rule correctly detects undefined constant'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnvOrConstantMatchesValueRules()
|
|
|
|
{
|
2017-10-16 05:43:12 +02:00
|
|
|
Environment::setEnv('CONSTANTMATCHESVALUE_FOO', 'Foo');
|
2017-02-22 04:15:08 +01:00
|
|
|
define('CONSTANTMATCHESVALUE_BAR', 'Bar');
|
2016-12-16 05:34:21 +01:00
|
|
|
$config = $this->getConfigFixtureValue('EnvOrConstantMatchesValue');
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'Yes',
|
|
|
|
@$config['FooIsFoo'],
|
|
|
|
'Only rule correctly detects environment variable matches specified value'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'Yes',
|
|
|
|
@$config['BarIsBar'],
|
|
|
|
'Only rule correctly detects constant matches specified value'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'No',
|
|
|
|
@$config['FooIsQux'],
|
|
|
|
'Except rule correctly detects environment variable that doesn\'t match specified value'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'No',
|
|
|
|
@$config['BarIsQux'],
|
|
|
|
'Except rule correctly detects environment variable that doesn\'t match specified value'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'No',
|
|
|
|
@$config['BazIsBaz'],
|
|
|
|
'Except rule correctly detects undefined variable'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnvironmentRules()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
/** @var Kernel $kernel */
|
|
|
|
$kernel = Injector::inst()->get(Kernel::class);
|
2020-04-20 19:58:09 +02:00
|
|
|
foreach (['dev', 'test', 'live'] as $env) {
|
2017-06-22 12:50:45 +02:00
|
|
|
$kernel->setEnvironment($env);
|
2016-12-16 05:34:21 +01:00
|
|
|
$config = $this->getConfigFixtureValue('Environment');
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
foreach (['dev', 'test', 'live'] as $check) {
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2018-01-16 19:39:30 +01:00
|
|
|
$env == $check ? $check : 'not' . $check,
|
|
|
|
@$config[ucfirst($check) . 'Environment'],
|
2017-02-22 04:15:08 +01:00
|
|
|
'Only & except rules correctly detect environment in env ' . $env
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMultipleRules()
|
|
|
|
{
|
2017-10-16 05:43:12 +02:00
|
|
|
Environment::setEnv('MULTIPLERULES_ENVVARIABLESET', '1');
|
2016-12-16 05:34:21 +01:00
|
|
|
define('MULTIPLERULES_DEFINEDCONSTANT', 'defined');
|
|
|
|
$config = $this->getConfigFixtureValue('MultipleRules');
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
isset($config['TwoOnlyFail']),
|
|
|
|
'Fragment is not included if one of the Only rules fails.'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
isset($config['TwoOnlySucceed']),
|
|
|
|
'Fragment is included if both Only rules succeed.'
|
|
|
|
);
|
|
|
|
|
2017-07-21 05:02:27 +02:00
|
|
|
$this->assertFalse(
|
|
|
|
isset($config['OneExceptFail']),
|
|
|
|
'Fragment is not included if one of the Except rules fails.'
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
isset($config['TwoExceptFail']),
|
|
|
|
'Fragment is not included if both of the Except rules fail.'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertFalse(
|
|
|
|
isset($config['TwoBlocksFail']),
|
|
|
|
'Fragment is not included if one block fails.'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
isset($config['TwoBlocksSucceed']),
|
|
|
|
'Fragment is included if both blocks succeed.'
|
|
|
|
);
|
|
|
|
}
|
2021-10-27 04:39:47 +02:00
|
|
|
|
2019-07-03 15:32:41 +02:00
|
|
|
public function testExtensionLoaded()
|
|
|
|
{
|
|
|
|
$config = $this->getConfigFixtureValue('ExtensionLoaded');
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'Yes',
|
|
|
|
@$config['SessionExtLoaded'],
|
|
|
|
'Only rule correctly detects loaded PHP extension'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'No',
|
|
|
|
@$config['DummyExtLoaded'],
|
|
|
|
'Except rule correctly detects not-loaded PHP extension'
|
|
|
|
);
|
|
|
|
}
|
2013-07-02 05:51:53 +02:00
|
|
|
}
|