]>
Commit | Line | Data |
---|---|---|
dd484b90 A |
1 | <?php\r |
2 | /**\r | |
3 | * Config' tests\r | |
4 | */\r | |
5 | \r | |
6 | require_once 'application/Config.php';\r | |
7 | \r | |
8 | /**\r | |
9 | * Unitary tests for Shaarli config related functions\r | |
10 | */\r | |
11 | class ConfigTest extends PHPUnit_Framework_TestCase\r | |
12 | {\r | |
13 | // Configuration input set.\r | |
14 | private static $_configFields;\r | |
15 | \r | |
16 | /**\r | |
17 | * Executed before each test.\r | |
18 | */\r | |
19 | public function setUp()\r | |
20 | {\r | |
d1e2f8e5 | 21 | self::$_configFields = array(\r |
dd484b90 A |
22 | 'login' => 'login',\r |
23 | 'hash' => 'hash',\r | |
24 | 'salt' => 'salt',\r | |
25 | 'timezone' => 'Europe/Paris',\r | |
26 | 'title' => 'title',\r | |
27 | 'titleLink' => 'titleLink',\r | |
28 | 'redirector' => '',\r | |
29 | 'disablesessionprotection' => false,\r | |
30 | 'privateLinkByDefault' => false,\r | |
d1e2f8e5 | 31 | 'config' => array(\r |
dd484b90 A |
32 | 'CONFIG_FILE' => 'tests/config.php',\r |
33 | 'DATADIR' => 'tests',\r | |
34 | 'config1' => 'config1data',\r | |
35 | 'config2' => 'config2data',\r | |
d1e2f8e5 V |
36 | )\r |
37 | );\r | |
dd484b90 A |
38 | }\r |
39 | \r | |
40 | /**\r | |
41 | * Executed after each test.\r | |
42 | *\r | |
43 | * @return void\r | |
44 | */\r | |
45 | public function tearDown()\r | |
46 | {\r | |
47 | if (is_file(self::$_configFields['config']['CONFIG_FILE'])) {\r | |
48 | unlink(self::$_configFields['config']['CONFIG_FILE']);\r | |
49 | }\r | |
50 | }\r | |
51 | \r | |
52 | /**\r | |
53 | * Test writeConfig function, valid use case, while being logged in.\r | |
54 | */\r | |
55 | public function testWriteConfig()\r | |
56 | {\r | |
57 | writeConfig(self::$_configFields, true);\r | |
58 | \r | |
59 | include self::$_configFields['config']['CONFIG_FILE'];\r | |
60 | $this->assertEquals(self::$_configFields['login'], $GLOBALS['login']);\r | |
61 | $this->assertEquals(self::$_configFields['hash'], $GLOBALS['hash']);\r | |
62 | $this->assertEquals(self::$_configFields['salt'], $GLOBALS['salt']);\r | |
63 | $this->assertEquals(self::$_configFields['timezone'], $GLOBALS['timezone']);\r | |
64 | $this->assertEquals(self::$_configFields['title'], $GLOBALS['title']);\r | |
65 | $this->assertEquals(self::$_configFields['titleLink'], $GLOBALS['titleLink']);\r | |
66 | $this->assertEquals(self::$_configFields['redirector'], $GLOBALS['redirector']);\r | |
67 | $this->assertEquals(self::$_configFields['disablesessionprotection'], $GLOBALS['disablesessionprotection']);\r | |
68 | $this->assertEquals(self::$_configFields['privateLinkByDefault'], $GLOBALS['privateLinkByDefault']);\r | |
69 | $this->assertEquals(self::$_configFields['config']['config1'], $GLOBALS['config']['config1']);\r | |
70 | $this->assertEquals(self::$_configFields['config']['config2'], $GLOBALS['config']['config2']);\r | |
71 | }\r | |
72 | \r | |
73 | /**\r | |
74 | * Test writeConfig option while logged in:\r | |
75 | * 1. init fields.\r | |
76 | * 2. update fields, add new sub config, add new root config.\r | |
77 | * 3. rewrite config.\r | |
78 | * 4. check result.\r | |
79 | */\r | |
80 | public function testWriteConfigFieldUpdate()\r | |
81 | {\r | |
82 | writeConfig(self::$_configFields, true);\r | |
83 | self::$_configFields['title'] = 'ok';\r | |
84 | self::$_configFields['config']['config1'] = 'ok';\r | |
85 | self::$_configFields['config']['config_new'] = 'ok';\r | |
86 | self::$_configFields['new'] = 'should not be saved';\r | |
87 | writeConfig(self::$_configFields, true);\r | |
88 | \r | |
89 | include self::$_configFields['config']['CONFIG_FILE'];\r | |
90 | $this->assertEquals('ok', $GLOBALS['title']);\r | |
91 | $this->assertEquals('ok', $GLOBALS['config']['config1']);\r | |
92 | $this->assertEquals('ok', $GLOBALS['config']['config_new']);\r | |
93 | $this->assertFalse(isset($GLOBALS['new']));\r | |
94 | }\r | |
95 | \r | |
96 | /**\r | |
97 | * Test writeConfig function with an empty array.\r | |
98 | *\r | |
99 | * @expectedException MissingFieldConfigException\r | |
100 | */\r | |
101 | public function testWriteConfigEmpty()\r | |
102 | {\r | |
103 | writeConfig(array(), true);\r | |
104 | }\r | |
105 | \r | |
106 | /**\r | |
107 | * Test writeConfig function with a missing mandatory field.\r | |
108 | *\r | |
109 | * @expectedException MissingFieldConfigException\r | |
110 | */\r | |
111 | public function testWriteConfigMissingField()\r | |
112 | {\r | |
113 | unset(self::$_configFields['login']);\r | |
114 | writeConfig(self::$_configFields, true);\r | |
115 | }\r | |
116 | \r | |
117 | /**\r | |
118 | * Test writeConfig function while being logged out, and there is no config file existing.\r | |
119 | */\r | |
120 | public function testWriteConfigLoggedOutNoFile()\r | |
121 | {\r | |
122 | writeConfig(self::$_configFields, false);\r | |
123 | }\r | |
124 | \r | |
125 | /**\r | |
126 | * Test writeConfig function while being logged out, and a config file already exists.\r | |
127 | *\r | |
128 | * @expectedException UnauthorizedConfigException\r | |
129 | */\r | |
130 | public function testWriteConfigLoggedOutWithFile()\r | |
131 | {\r | |
132 | file_put_contents(self::$_configFields['config']['CONFIG_FILE'], '');\r | |
133 | writeConfig(self::$_configFields, false);\r | |
134 | }\r | |
135 | \r | |
136 | /**\r | |
137 | * Test mergeDeprecatedConfig while being logged in:\r | |
138 | * 1. init a config file.\r | |
139 | * 2. init a options.php file with update value.\r | |
140 | * 3. merge.\r | |
141 | * 4. check updated value in config file.\r | |
142 | */\r | |
143 | public function testMergeDeprecatedConfig()\r | |
144 | {\r | |
145 | // init\r | |
146 | writeConfig(self::$_configFields, true);\r | |
147 | $configCopy = self::$_configFields;\r | |
148 | $invert = !$configCopy['privateLinkByDefault'];\r | |
149 | $configCopy['privateLinkByDefault'] = $invert;\r | |
150 | \r | |
151 | // Use writeConfig to create a options.php\r | |
152 | $configCopy['config']['CONFIG_FILE'] = 'tests/options.php';\r | |
153 | writeConfig($configCopy, true);\r | |
154 | \r | |
155 | $this->assertTrue(is_file($configCopy['config']['CONFIG_FILE']));\r | |
156 | \r | |
157 | // merge configs\r | |
158 | mergeDeprecatedConfig(self::$_configFields, true);\r | |
159 | \r | |
160 | // make sure updated field is changed\r | |
161 | include self::$_configFields['config']['CONFIG_FILE'];\r | |
162 | $this->assertEquals($invert, $GLOBALS['privateLinkByDefault']);\r | |
163 | $this->assertFalse(is_file($configCopy['config']['CONFIG_FILE']));\r | |
164 | }\r | |
165 | \r | |
166 | /**\r | |
167 | * Test mergeDeprecatedConfig while being logged in without options file.\r | |
168 | */\r | |
169 | public function testMergeDeprecatedConfigNoFile()\r | |
170 | {\r | |
171 | writeConfig(self::$_configFields, true);\r | |
172 | mergeDeprecatedConfig(self::$_configFields, true);\r | |
173 | \r | |
174 | include self::$_configFields['config']['CONFIG_FILE'];\r | |
175 | $this->assertEquals(self::$_configFields['login'], $GLOBALS['login']);\r | |
176 | }\r | |
d1e2f8e5 | 177 | }\r |