6 require_once 'application/Config.php';
9 * Unitary tests for Shaarli config related functions
11 class ConfigTest
extends PHPUnit_Framework_TestCase
13 // Configuration input set.
14 private static $_configFields;
17 * Executed before each test.
19 public function setUp()
21 self
::$_configFields = array(
25 'timezone' => 'Europe/Paris',
27 'titleLink' => 'titleLink',
29 'disablesessionprotection' => false,
30 'privateLinkByDefault' => false,
32 'CONFIG_FILE' => 'tests/config.php',
34 'config1' => 'config1data',
35 'config2' => 'config2data',
41 * Executed after each test.
45 public function tearDown()
47 if (is_file(self
::$_configFields['config']['CONFIG_FILE'])) {
48 unlink(self
::$_configFields['config']['CONFIG_FILE']);
53 * Test writeConfig function, valid use case, while being logged in.
55 public function testWriteConfig()
57 writeConfig(self
::$_configFields, true);
59 include self
::$_configFields['config']['CONFIG_FILE'];
60 $this->assertEquals(self
::$_configFields['login'], $GLOBALS['login']);
61 $this->assertEquals(self
::$_configFields['hash'], $GLOBALS['hash']);
62 $this->assertEquals(self
::$_configFields['salt'], $GLOBALS['salt']);
63 $this->assertEquals(self
::$_configFields['timezone'], $GLOBALS['timezone']);
64 $this->assertEquals(self
::$_configFields['title'], $GLOBALS['title']);
65 $this->assertEquals(self
::$_configFields['titleLink'], $GLOBALS['titleLink']);
66 $this->assertEquals(self
::$_configFields['redirector'], $GLOBALS['redirector']);
67 $this->assertEquals(self
::$_configFields['disablesessionprotection'], $GLOBALS['disablesessionprotection']);
68 $this->assertEquals(self
::$_configFields['privateLinkByDefault'], $GLOBALS['privateLinkByDefault']);
69 $this->assertEquals(self
::$_configFields['config']['config1'], $GLOBALS['config']['config1']);
70 $this->assertEquals(self
::$_configFields['config']['config2'], $GLOBALS['config']['config2']);
74 * Test writeConfig option while logged in:
76 * 2. update fields, add new sub config, add new root config.
80 public function testWriteConfigFieldUpdate()
82 writeConfig(self
::$_configFields, true);
83 self
::$_configFields['title'] = 'ok';
84 self
::$_configFields['config']['config1'] = 'ok';
85 self
::$_configFields['config']['config_new'] = 'ok';
86 self
::$_configFields['new'] = 'should not be saved';
87 writeConfig(self
::$_configFields, true);
89 include self
::$_configFields['config']['CONFIG_FILE'];
90 $this->assertEquals('ok', $GLOBALS['title']);
91 $this->assertEquals('ok', $GLOBALS['config']['config1']);
92 $this->assertEquals('ok', $GLOBALS['config']['config_new']);
93 $this->assertFalse(isset($GLOBALS['new']));
97 * Test writeConfig function with an empty array.
99 * @expectedException MissingFieldConfigException
101 public function testWriteConfigEmpty()
103 writeConfig(array(), true);
107 * Test writeConfig function with a missing mandatory field.
109 * @expectedException MissingFieldConfigException
111 public function testWriteConfigMissingField()
113 unset(self
::$_configFields['login']);
114 writeConfig(self
::$_configFields, true);
118 * Test writeConfig function while being logged out, and there is no config file existing.
120 public function testWriteConfigLoggedOutNoFile()
122 writeConfig(self
::$_configFields, false);
126 * Test writeConfig function while being logged out, and a config file already exists.
128 * @expectedException UnauthorizedConfigException
130 public function testWriteConfigLoggedOutWithFile()
132 file_put_contents(self
::$_configFields['config']['CONFIG_FILE'], '');
133 writeConfig(self
::$_configFields, false);
137 * Test mergeDeprecatedConfig while being logged in:
138 * 1. init a config file.
139 * 2. init a options.php file with update value.
141 * 4. check updated value in config file.
143 public function testMergeDeprecatedConfig()
146 writeConfig(self
::$_configFields, true);
147 $configCopy = self
::$_configFields;
148 $invert = !$configCopy['privateLinkByDefault'];
149 $configCopy['privateLinkByDefault'] = $invert;
151 // Use writeConfig to create a options.php
152 $configCopy['config']['CONFIG_FILE'] = 'tests/options.php';
153 writeConfig($configCopy, true);
155 $this->assertTrue(is_file($configCopy['config']['CONFIG_FILE']));
158 mergeDeprecatedConfig(self
::$_configFields, true);
160 // make sure updated field is changed
161 include self
::$_configFields['config']['CONFIG_FILE'];
162 $this->assertEquals($invert, $GLOBALS['privateLinkByDefault']);
163 $this->assertFalse(is_file($configCopy['config']['CONFIG_FILE']));
167 * Test mergeDeprecatedConfig while being logged in without options file.
169 public function testMergeDeprecatedConfigNoFile()
171 writeConfig(self
::$_configFields, true);
172 mergeDeprecatedConfig(self
::$_configFields, true);
174 include self
::$_configFields['config']['CONFIG_FILE'];
175 $this->assertEquals(self
::$_configFields['login'], $GLOBALS['login']);