diff options
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/ConfigTest.php | 177 | ||||
-rw-r--r-- | tests/LinkDBTest.php | 4 | ||||
-rw-r--r-- | tests/utils/ReferenceLinkDB.php | 20 |
3 files changed, 189 insertions, 12 deletions
diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php new file mode 100755 index 00000000..4279c57e --- /dev/null +++ b/tests/ConfigTest.php | |||
@@ -0,0 +1,177 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * Config' tests | ||
4 | */ | ||
5 | |||
6 | require_once 'application/Config.php'; | ||
7 | |||
8 | /** | ||
9 | * Unitary tests for Shaarli config related functions | ||
10 | */ | ||
11 | class ConfigTest extends PHPUnit_Framework_TestCase | ||
12 | { | ||
13 | // Configuration input set. | ||
14 | private static $_configFields; | ||
15 | |||
16 | /** | ||
17 | * Executed before each test. | ||
18 | */ | ||
19 | public function setUp() | ||
20 | { | ||
21 | self::$_configFields = [ | ||
22 | 'login' => 'login', | ||
23 | 'hash' => 'hash', | ||
24 | 'salt' => 'salt', | ||
25 | 'timezone' => 'Europe/Paris', | ||
26 | 'title' => 'title', | ||
27 | 'titleLink' => 'titleLink', | ||
28 | 'redirector' => '', | ||
29 | 'disablesessionprotection' => false, | ||
30 | 'privateLinkByDefault' => false, | ||
31 | 'config' => [ | ||
32 | 'CONFIG_FILE' => 'tests/config.php', | ||
33 | 'DATADIR' => 'tests', | ||
34 | 'config1' => 'config1data', | ||
35 | 'config2' => 'config2data', | ||
36 | ] | ||
37 | ]; | ||
38 | } | ||
39 | |||
40 | /** | ||
41 | * Executed after each test. | ||
42 | * | ||
43 | * @return void | ||
44 | */ | ||
45 | public function tearDown() | ||
46 | { | ||
47 | if (is_file(self::$_configFields['config']['CONFIG_FILE'])) { | ||
48 | unlink(self::$_configFields['config']['CONFIG_FILE']); | ||
49 | } | ||
50 | } | ||
51 | |||
52 | /** | ||
53 | * Test writeConfig function, valid use case, while being logged in. | ||
54 | */ | ||
55 | public function testWriteConfig() | ||
56 | { | ||
57 | writeConfig(self::$_configFields, true); | ||
58 | |||
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']); | ||
71 | } | ||
72 | |||
73 | /** | ||
74 | * Test writeConfig option while logged in: | ||
75 | * 1. init fields. | ||
76 | * 2. update fields, add new sub config, add new root config. | ||
77 | * 3. rewrite config. | ||
78 | * 4. check result. | ||
79 | */ | ||
80 | public function testWriteConfigFieldUpdate() | ||
81 | { | ||
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); | ||
88 | |||
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'])); | ||
94 | } | ||
95 | |||
96 | /** | ||
97 | * Test writeConfig function with an empty array. | ||
98 | * | ||
99 | * @expectedException MissingFieldConfigException | ||
100 | */ | ||
101 | public function testWriteConfigEmpty() | ||
102 | { | ||
103 | writeConfig(array(), true); | ||
104 | } | ||
105 | |||
106 | /** | ||
107 | * Test writeConfig function with a missing mandatory field. | ||
108 | * | ||
109 | * @expectedException MissingFieldConfigException | ||
110 | */ | ||
111 | public function testWriteConfigMissingField() | ||
112 | { | ||
113 | unset(self::$_configFields['login']); | ||
114 | writeConfig(self::$_configFields, true); | ||
115 | } | ||
116 | |||
117 | /** | ||
118 | * Test writeConfig function while being logged out, and there is no config file existing. | ||
119 | */ | ||
120 | public function testWriteConfigLoggedOutNoFile() | ||
121 | { | ||
122 | writeConfig(self::$_configFields, false); | ||
123 | } | ||
124 | |||
125 | /** | ||
126 | * Test writeConfig function while being logged out, and a config file already exists. | ||
127 | * | ||
128 | * @expectedException UnauthorizedConfigException | ||
129 | */ | ||
130 | public function testWriteConfigLoggedOutWithFile() | ||
131 | { | ||
132 | file_put_contents(self::$_configFields['config']['CONFIG_FILE'], ''); | ||
133 | writeConfig(self::$_configFields, false); | ||
134 | } | ||
135 | |||
136 | /** | ||
137 | * Test mergeDeprecatedConfig while being logged in: | ||
138 | * 1. init a config file. | ||
139 | * 2. init a options.php file with update value. | ||
140 | * 3. merge. | ||
141 | * 4. check updated value in config file. | ||
142 | */ | ||
143 | public function testMergeDeprecatedConfig() | ||
144 | { | ||
145 | // init | ||
146 | writeConfig(self::$_configFields, true); | ||
147 | $configCopy = self::$_configFields; | ||
148 | $invert = !$configCopy['privateLinkByDefault']; | ||
149 | $configCopy['privateLinkByDefault'] = $invert; | ||
150 | |||
151 | // Use writeConfig to create a options.php | ||
152 | $configCopy['config']['CONFIG_FILE'] = 'tests/options.php'; | ||
153 | writeConfig($configCopy, true); | ||
154 | |||
155 | $this->assertTrue(is_file($configCopy['config']['CONFIG_FILE'])); | ||
156 | |||
157 | // merge configs | ||
158 | mergeDeprecatedConfig(self::$_configFields, true); | ||
159 | |||
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'])); | ||
164 | } | ||
165 | |||
166 | /** | ||
167 | * Test mergeDeprecatedConfig while being logged in without options file. | ||
168 | */ | ||
169 | public function testMergeDeprecatedConfigNoFile() | ||
170 | { | ||
171 | writeConfig(self::$_configFields, true); | ||
172 | mergeDeprecatedConfig(self::$_configFields, true); | ||
173 | |||
174 | include self::$_configFields['config']['CONFIG_FILE']; | ||
175 | $this->assertEquals(self::$_configFields['login'], $GLOBALS['login']); | ||
176 | } | ||
177 | } \ No newline at end of file | ||
diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php index 8b0bd23b..d34ea4f5 100644 --- a/tests/LinkDBTest.php +++ b/tests/LinkDBTest.php | |||
@@ -103,7 +103,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase | |||
103 | unlink(self::$testDatastore); | 103 | unlink(self::$testDatastore); |
104 | $this->assertFileNotExists(self::$testDatastore); | 104 | $this->assertFileNotExists(self::$testDatastore); |
105 | 105 | ||
106 | $checkDB = self::getMethod('checkDB'); | 106 | $checkDB = self::getMethod('_checkDB'); |
107 | $checkDB->invokeArgs($linkDB, array()); | 107 | $checkDB->invokeArgs($linkDB, array()); |
108 | $this->assertFileExists(self::$testDatastore); | 108 | $this->assertFileExists(self::$testDatastore); |
109 | 109 | ||
@@ -120,7 +120,7 @@ class LinkDBTest extends PHPUnit_Framework_TestCase | |||
120 | $datastoreSize = filesize(self::$testDatastore); | 120 | $datastoreSize = filesize(self::$testDatastore); |
121 | $this->assertGreaterThan(0, $datastoreSize); | 121 | $this->assertGreaterThan(0, $datastoreSize); |
122 | 122 | ||
123 | $checkDB = self::getMethod('checkDB'); | 123 | $checkDB = self::getMethod('_checkDB'); |
124 | $checkDB->invokeArgs($linkDB, array()); | 124 | $checkDB->invokeArgs($linkDB, array()); |
125 | 125 | ||
126 | // ensure the datastore is left unmodified | 126 | // ensure the datastore is left unmodified |
diff --git a/tests/utils/ReferenceLinkDB.php b/tests/utils/ReferenceLinkDB.php index 59ba671f..0b225720 100644 --- a/tests/utils/ReferenceLinkDB.php +++ b/tests/utils/ReferenceLinkDB.php | |||
@@ -4,9 +4,9 @@ | |||
4 | */ | 4 | */ |
5 | class ReferenceLinkDB | 5 | class ReferenceLinkDB |
6 | { | 6 | { |
7 | private $links = array(); | 7 | private $_links = array(); |
8 | private $publicCount = 0; | 8 | private $_publicCount = 0; |
9 | private $privateCount = 0; | 9 | private $_privateCount = 0; |
10 | 10 | ||
11 | /** | 11 | /** |
12 | * Populates the test DB with reference data | 12 | * Populates the test DB with reference data |
@@ -81,13 +81,13 @@ class ReferenceLinkDB | |||
81 | 'linkdate' => $date, | 81 | 'linkdate' => $date, |
82 | 'tags' => $tags, | 82 | 'tags' => $tags, |
83 | ); | 83 | ); |
84 | $this->links[$date] = $link; | 84 | $this->_links[$date] = $link; |
85 | 85 | ||
86 | if ($private) { | 86 | if ($private) { |
87 | $this->privateCount++; | 87 | $this->_privateCount++; |
88 | return; | 88 | return; |
89 | } | 89 | } |
90 | $this->publicCount++; | 90 | $this->_publicCount++; |
91 | } | 91 | } |
92 | 92 | ||
93 | /** | 93 | /** |
@@ -97,7 +97,7 @@ class ReferenceLinkDB | |||
97 | { | 97 | { |
98 | file_put_contents( | 98 | file_put_contents( |
99 | $filename, | 99 | $filename, |
100 | '<?php /* '.base64_encode(gzdeflate(serialize($this->links))).' */ ?>' | 100 | '<?php /* '.base64_encode(gzdeflate(serialize($this->_links))).' */ ?>' |
101 | ); | 101 | ); |
102 | } | 102 | } |
103 | 103 | ||
@@ -106,7 +106,7 @@ class ReferenceLinkDB | |||
106 | */ | 106 | */ |
107 | public function countLinks() | 107 | public function countLinks() |
108 | { | 108 | { |
109 | return $this->publicCount + $this->privateCount; | 109 | return $this->_publicCount + $this->_privateCount; |
110 | } | 110 | } |
111 | 111 | ||
112 | /** | 112 | /** |
@@ -114,7 +114,7 @@ class ReferenceLinkDB | |||
114 | */ | 114 | */ |
115 | public function countPublicLinks() | 115 | public function countPublicLinks() |
116 | { | 116 | { |
117 | return $this->publicCount; | 117 | return $this->_publicCount; |
118 | } | 118 | } |
119 | 119 | ||
120 | /** | 120 | /** |
@@ -122,7 +122,7 @@ class ReferenceLinkDB | |||
122 | */ | 122 | */ |
123 | public function countPrivateLinks() | 123 | public function countPrivateLinks() |
124 | { | 124 | { |
125 | return $this->privateCount; | 125 | return $this->_privateCount; |
126 | } | 126 | } |
127 | } | 127 | } |
128 | ?> | 128 | ?> |