5e4f1d084c15f683d42225e5b37122c8e4b6ff5e
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Site / SettingsTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Site;
4
5 use Drupal\Core\Site\Settings;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\Core\Site\Settings
10  * @group Site
11  */
12 class SettingsTest extends UnitTestCase {
13
14   /**
15    * Simple settings array to test against.
16    *
17    * @var array
18    */
19   protected $config = [];
20
21   /**
22    * The class under test.
23    *
24    * @var \Drupal\Core\Site\Settings
25    */
26   protected $settings;
27
28   /**
29    * @covers ::__construct
30    */
31   protected function setUp() {
32     $this->config = [
33       'one' => '1',
34       'two' => '2',
35       'hash_salt' => $this->randomMachineName(),
36     ];
37     $this->settings = new Settings($this->config);
38   }
39
40   /**
41    * @covers ::get
42    */
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.');
47
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.');
51   }
52
53   /**
54    * @covers ::getAll
55    */
56   public function testGetAll() {
57     $this->assertEquals($this->config, Settings::getAll());
58   }
59
60   /**
61    * @covers ::getInstance
62    */
63   public function testGetInstance() {
64     $singleton = $this->settings->getInstance();
65     $this->assertEquals($singleton, $this->settings);
66   }
67
68   /**
69    * Tests Settings::getHashSalt();
70    *
71    * @covers ::getHashSalt
72    */
73   public function testGetHashSalt() {
74     $this->assertSame($this->config['hash_salt'], $this->settings->getHashSalt());
75   }
76
77   /**
78    * Tests Settings::getHashSalt() with no hash salt value.
79    *
80    * @covers ::getHashSalt
81    *
82    * @dataProvider providerTestGetHashSaltEmpty
83    */
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();
89   }
90
91   /**
92    * Data provider for testGetHashSaltEmpty.
93    *
94    * @return array
95    */
96   public function providerTestGetHashSaltEmpty() {
97     return [
98       [[]],
99       [['hash_salt' => '']],
100       [['hash_salt' => NULL]],
101     ];
102   }
103
104   /**
105    * Ensures settings cannot be serialized.
106    *
107    * @covers ::__sleep
108    */
109   public function testSerialize() {
110     $this->setExpectedException(\LogicException::class);
111     serialize(new Settings([]));
112   }
113
114   /**
115    * Tests Settings::getApcuPrefix().
116    *
117    * @covers ::getApcuPrefix
118    */
119   public function testGetApcuPrefix() {
120     $settings = new Settings(['hash_salt' => 123]);
121     $this->assertNotEquals($settings::getApcuPrefix('cache_test', '/test/a'), $settings::getApcuPrefix('cache_test', '/test/b'));
122
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'));
125   }
126
127   /**
128    * Tests that an exception is thrown when settings are not initialized yet.
129    *
130    * @covers ::getInstance
131    */
132   public function testGetInstanceReflection() {
133     $settings = new Settings([]);
134
135     $class = new \ReflectionClass(Settings::class);
136     $instace_property = $class->getProperty("instance");
137     $instace_property->setAccessible(TRUE);
138     $instace_property->setValue(NULL);
139
140     $this->setExpectedException(\BadMethodCallException::class);
141     $settings->getInstance();
142   }
143
144 }