3 namespace Drupal\Tests\Core\Site;
5 use Drupal\Core\Site\Settings;
6 use Drupal\Tests\UnitTestCase;
9 * @coversDefaultClass \Drupal\Core\Site\Settings
12 class SettingsTest extends UnitTestCase {
15 * Simple settings array to test against.
19 protected $config = [];
22 * The class under test.
24 * @var \Drupal\Core\Site\Settings
29 * @covers ::__construct
31 protected function setUp() {
35 'hash_salt' => $this->randomMachineName(),
37 $this->settings = new Settings($this->config);
43 public function testGet() {
44 // Test stored settings.
45 $this->assertEquals($this->config['one'], Settings::get('one'), 'The correct setting was not returned.');
46 $this->assertEquals($this->config['two'], Settings::get('two'), 'The correct setting was not returned.');
48 // Test setting that isn't stored with default.
49 $this->assertEquals('3', Settings::get('three', '3'), 'Default value for a setting not properly returned.');
50 $this->assertNull(Settings::get('four'), 'Non-null value returned for a setting that should not exist.');
56 public function testGetAll() {
57 $this->assertEquals($this->config, Settings::getAll());
61 * @covers ::getInstance
63 public function testGetInstance() {
64 $singleton = $this->settings->getInstance();
65 $this->assertEquals($singleton, $this->settings);
69 * Tests Settings::getHashSalt();
71 * @covers ::getHashSalt
73 public function testGetHashSalt() {
74 $this->assertSame($this->config['hash_salt'], $this->settings->getHashSalt());
78 * Tests Settings::getHashSalt() with no hash salt value.
80 * @covers ::getHashSalt
82 * @dataProvider providerTestGetHashSaltEmpty
84 public function testGetHashSaltEmpty(array $config) {
85 // Re-create settings with no 'hash_salt' key.
86 $settings = new Settings($config);
87 $this->setExpectedException(\RuntimeException::class);
88 $settings->getHashSalt();
92 * Data provider for testGetHashSaltEmpty.
96 public function providerTestGetHashSaltEmpty() {
99 [['hash_salt' => '']],
100 [['hash_salt' => NULL]],
105 * Ensures settings cannot be serialized.
109 public function testSerialize() {
110 $this->setExpectedException(\LogicException::class);
111 serialize(new Settings([]));
115 * Tests Settings::getApcuPrefix().
117 * @covers ::getApcuPrefix
119 public function testGetApcuPrefix() {
120 $settings = new Settings(['hash_salt' => 123, 'apcu_ensure_unique_prefix' => TRUE]);
121 $this->assertNotEquals($settings::getApcuPrefix('cache_test', '/test/a'), $settings::getApcuPrefix('cache_test', '/test/b'));
123 $settings = new Settings(['hash_salt' => 123, 'apcu_ensure_unique_prefix' => FALSE]);
124 $this->assertNotEquals($settings::getApcuPrefix('cache_test', '/test/a'), $settings::getApcuPrefix('cache_test', '/test/b'));
128 * Tests that an exception is thrown when settings are not initialized yet.
130 * @covers ::getInstance
132 public function testGetInstanceReflection() {
133 $settings = new Settings([]);
135 $class = new \ReflectionClass(Settings::class);
136 $instace_property = $class->getProperty("instance");
137 $instace_property->setAccessible(TRUE);
138 $instace_property->setValue(NULL);
140 $this->setExpectedException(\BadMethodCallException::class);
141 $settings->getInstance();