ConfigTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /**
  3. * Config' tests
  4. */
  5. require_once 'application/Config.php';
  6. /**
  7. * Unitary tests for Shaarli config related functions
  8. */
  9. class ConfigTest extends PHPUnit_Framework_TestCase
  10. {
  11. // Configuration input set.
  12. private static $configFields;
  13. /**
  14. * Executed before each test.
  15. */
  16. public function setUp()
  17. {
  18. self::$configFields = array(
  19. 'login' => 'login',
  20. 'hash' => 'hash',
  21. 'salt' => 'salt',
  22. 'timezone' => 'Europe/Paris',
  23. 'title' => 'title',
  24. 'titleLink' => 'titleLink',
  25. 'redirector' => '',
  26. 'disablesessionprotection' => false,
  27. 'privateLinkByDefault' => false,
  28. 'config' => array(
  29. 'CONFIG_FILE' => 'tests/config.php',
  30. 'DATADIR' => 'tests',
  31. 'config1' => 'config1data',
  32. 'config2' => 'config2data',
  33. )
  34. );
  35. }
  36. /**
  37. * Executed after each test.
  38. *
  39. * @return void
  40. */
  41. public function tearDown()
  42. {
  43. if (is_file(self::$configFields['config']['CONFIG_FILE'])) {
  44. unlink(self::$configFields['config']['CONFIG_FILE']);
  45. }
  46. }
  47. /**
  48. * Test writeConfig function, valid use case, while being logged in.
  49. */
  50. public function testWriteConfig()
  51. {
  52. writeConfig(self::$configFields, true);
  53. include self::$configFields['config']['CONFIG_FILE'];
  54. $this->assertEquals(self::$configFields['login'], $GLOBALS['login']);
  55. $this->assertEquals(self::$configFields['hash'], $GLOBALS['hash']);
  56. $this->assertEquals(self::$configFields['salt'], $GLOBALS['salt']);
  57. $this->assertEquals(self::$configFields['timezone'], $GLOBALS['timezone']);
  58. $this->assertEquals(self::$configFields['title'], $GLOBALS['title']);
  59. $this->assertEquals(self::$configFields['titleLink'], $GLOBALS['titleLink']);
  60. $this->assertEquals(self::$configFields['redirector'], $GLOBALS['redirector']);
  61. $this->assertEquals(self::$configFields['disablesessionprotection'], $GLOBALS['disablesessionprotection']);
  62. $this->assertEquals(self::$configFields['privateLinkByDefault'], $GLOBALS['privateLinkByDefault']);
  63. $this->assertEquals(self::$configFields['config']['config1'], $GLOBALS['config']['config1']);
  64. $this->assertEquals(self::$configFields['config']['config2'], $GLOBALS['config']['config2']);
  65. }
  66. /**
  67. * Test writeConfig option while logged in:
  68. * 1. init fields.
  69. * 2. update fields, add new sub config, add new root config.
  70. * 3. rewrite config.
  71. * 4. check result.
  72. */
  73. public function testWriteConfigFieldUpdate()
  74. {
  75. writeConfig(self::$configFields, true);
  76. self::$configFields['title'] = 'ok';
  77. self::$configFields['config']['config1'] = 'ok';
  78. self::$configFields['config']['config_new'] = 'ok';
  79. self::$configFields['new'] = 'should not be saved';
  80. writeConfig(self::$configFields, true);
  81. include self::$configFields['config']['CONFIG_FILE'];
  82. $this->assertEquals('ok', $GLOBALS['title']);
  83. $this->assertEquals('ok', $GLOBALS['config']['config1']);
  84. $this->assertEquals('ok', $GLOBALS['config']['config_new']);
  85. $this->assertFalse(isset($GLOBALS['new']));
  86. }
  87. /**
  88. * Test writeConfig function with an empty array.
  89. *
  90. * @expectedException MissingFieldConfigException
  91. */
  92. public function testWriteConfigEmpty()
  93. {
  94. writeConfig(array(), true);
  95. }
  96. /**
  97. * Test writeConfig function with a missing mandatory field.
  98. *
  99. * @expectedException MissingFieldConfigException
  100. */
  101. public function testWriteConfigMissingField()
  102. {
  103. unset(self::$configFields['login']);
  104. writeConfig(self::$configFields, true);
  105. }
  106. /**
  107. * Test writeConfig function while being logged out, and there is no config file existing.
  108. */
  109. public function testWriteConfigLoggedOutNoFile()
  110. {
  111. writeConfig(self::$configFields, false);
  112. }
  113. /**
  114. * Test writeConfig function while being logged out, and a config file already exists.
  115. *
  116. * @expectedException UnauthorizedConfigException
  117. */
  118. public function testWriteConfigLoggedOutWithFile()
  119. {
  120. file_put_contents(self::$configFields['config']['CONFIG_FILE'], '');
  121. writeConfig(self::$configFields, false);
  122. }
  123. /**
  124. * Test mergeDeprecatedConfig while being logged in:
  125. * 1. init a config file.
  126. * 2. init a options.php file with update value.
  127. * 3. merge.
  128. * 4. check updated value in config file.
  129. */
  130. public function testMergeDeprecatedConfig()
  131. {
  132. // init
  133. writeConfig(self::$configFields, true);
  134. $configCopy = self::$configFields;
  135. $invert = !$configCopy['privateLinkByDefault'];
  136. $configCopy['privateLinkByDefault'] = $invert;
  137. // Use writeConfig to create a options.php
  138. $configCopy['config']['CONFIG_FILE'] = 'tests/options.php';
  139. writeConfig($configCopy, true);
  140. $this->assertTrue(is_file($configCopy['config']['CONFIG_FILE']));
  141. // merge configs
  142. mergeDeprecatedConfig(self::$configFields, true);
  143. // make sure updated field is changed
  144. include self::$configFields['config']['CONFIG_FILE'];
  145. $this->assertEquals($invert, $GLOBALS['privateLinkByDefault']);
  146. $this->assertFalse(is_file($configCopy['config']['CONFIG_FILE']));
  147. }
  148. /**
  149. * Test mergeDeprecatedConfig while being logged in without options file.
  150. */
  151. public function testMergeDeprecatedConfigNoFile()
  152. {
  153. writeConfig(self::$configFields, true);
  154. mergeDeprecatedConfig(self::$configFields, true);
  155. include self::$configFields['config']['CONFIG_FILE'];
  156. $this->assertEquals(self::$configFields['login'], $GLOBALS['login']);
  157. }
  158. /**
  159. * Test save_plugin_config with valid data.
  160. *
  161. * @throws PluginConfigOrderException
  162. */
  163. public function testSavePluginConfigValid()
  164. {
  165. $data = array(
  166. 'order_plugin1' => 2, // no plugin related
  167. 'plugin2' => 0, // new - at the end
  168. 'plugin3' => 0, // 2nd
  169. 'order_plugin3' => 8,
  170. 'plugin4' => 0, // 1st
  171. 'order_plugin4' => 5,
  172. );
  173. $expected = array(
  174. 'plugin3',
  175. 'plugin4',
  176. 'plugin2',
  177. );
  178. $out = save_plugin_config($data);
  179. $this->assertEquals($expected, $out);
  180. }
  181. /**
  182. * Test save_plugin_config with invalid data.
  183. *
  184. * @expectedException PluginConfigOrderException
  185. */
  186. public function testSavePluginConfigInvalid()
  187. {
  188. $data = array(
  189. 'plugin2' => 0,
  190. 'plugin3' => 0,
  191. 'order_plugin3' => 0,
  192. 'plugin4' => 0,
  193. 'order_plugin4' => 0,
  194. );
  195. save_plugin_config($data);
  196. }
  197. /**
  198. * Test save_plugin_config without data.
  199. */
  200. public function testSavePluginConfigEmpty()
  201. {
  202. $this->assertEquals(array(), save_plugin_config(array()));
  203. }
  204. /**
  205. * Test validate_plugin_order with valid data.
  206. */
  207. public function testValidatePluginOrderValid()
  208. {
  209. $data = array(
  210. 'order_plugin1' => 2,
  211. 'plugin2' => 0,
  212. 'plugin3' => 0,
  213. 'order_plugin3' => 1,
  214. 'plugin4' => 0,
  215. 'order_plugin4' => 5,
  216. );
  217. $this->assertTrue(validate_plugin_order($data));
  218. }
  219. /**
  220. * Test validate_plugin_order with invalid data.
  221. */
  222. public function testValidatePluginOrderInvalid()
  223. {
  224. $data = array(
  225. 'order_plugin1' => 2,
  226. 'order_plugin3' => 1,
  227. 'order_plugin4' => 1,
  228. );
  229. $this->assertFalse(validate_plugin_order($data));
  230. }
  231. /**
  232. * Test load_plugin_parameter_values.
  233. */
  234. public function testLoadPluginParameterValues()
  235. {
  236. $plugins = array(
  237. 'plugin_name' => array(
  238. 'parameters' => array(
  239. 'param1' => true,
  240. 'param2' => false,
  241. 'param3' => '',
  242. )
  243. )
  244. );
  245. $parameters = array(
  246. 'param1' => 'value1',
  247. 'param2' => 'value2',
  248. );
  249. $result = load_plugin_parameter_values($plugins, $parameters);
  250. $this->assertEquals('value1', $result['plugin_name']['parameters']['param1']);
  251. $this->assertEquals('value2', $result['plugin_name']['parameters']['param2']);
  252. $this->assertEquals('', $result['plugin_name']['parameters']['param3']);
  253. }
  254. }