ConfigurationTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. use PrivateBin\Configuration;
  4. class ConfigurationTest extends TestCase
  5. {
  6. private $_minimalConfig;
  7. private $_options;
  8. private $_path;
  9. public function setUp()
  10. {
  11. /* Setup Routine */
  12. Helper::confBackup();
  13. $this->_minimalConfig = '[main]' . PHP_EOL . '[model]' . PHP_EOL . '[model_options]';
  14. $this->_options = Configuration::getDefaults();
  15. $this->_options['model_options']['dir'] = PATH . $this->_options['model_options']['dir'];
  16. $this->_options['traffic']['dir'] = PATH . $this->_options['traffic']['dir'];
  17. $this->_options['purge']['dir'] = PATH . $this->_options['purge']['dir'];
  18. $this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_cfg';
  19. if (!is_dir($this->_path)) {
  20. mkdir($this->_path);
  21. }
  22. }
  23. public function tearDown()
  24. {
  25. /* Tear Down Routine */
  26. Helper::rmDir($this->_path);
  27. if (is_file(CONF)) {
  28. unlink(CONF);
  29. }
  30. Helper::confRestore();
  31. }
  32. public function testDefaultConfigFile()
  33. {
  34. $conf = new Configuration;
  35. $this->assertEquals($this->_options, $conf->get(), 'default configuration is correct');
  36. }
  37. public function testHandleFreshConfigFile()
  38. {
  39. Helper::createIniFile(CONF, $this->_options);
  40. $conf = new Configuration;
  41. $this->assertEquals($this->_options, $conf->get(), 'newly generated configuration is correct');
  42. }
  43. public function testHandleMissingConfigFile()
  44. {
  45. if (is_file(CONF)) {
  46. unlink(CONF);
  47. }
  48. $conf = new Configuration;
  49. $this->assertEquals($this->_options, $conf->get(), 'returns correct defaults on missing file');
  50. }
  51. /**
  52. * @expectedException Exception
  53. * @expectedExceptionCode 2
  54. */
  55. public function testHandleBlankConfigFile()
  56. {
  57. file_put_contents(CONF, '');
  58. new Configuration;
  59. }
  60. public function testHandleMinimalConfigFile()
  61. {
  62. file_put_contents(CONF, $this->_minimalConfig);
  63. $conf = new Configuration;
  64. $this->assertEquals($this->_options, $conf->get(), 'returns correct defaults on empty file');
  65. }
  66. /**
  67. * @expectedException Exception
  68. * @expectedExceptionCode 3
  69. */
  70. public function testHandleInvalidSection()
  71. {
  72. file_put_contents(CONF, $this->_minimalConfig);
  73. $conf = new Configuration;
  74. $conf->getKey('foo', 'bar');
  75. }
  76. /**
  77. * @expectedException Exception
  78. * @expectedExceptionCode 4
  79. */
  80. public function testHandleInvalidKey()
  81. {
  82. file_put_contents(CONF, $this->_minimalConfig);
  83. $conf = new Configuration;
  84. $conf->getKey('foo');
  85. }
  86. public function testHandleGetKey()
  87. {
  88. file_put_contents(CONF, $this->_minimalConfig);
  89. $conf = new Configuration;
  90. $this->assertEquals($this->_options['main']['sizelimit'], $conf->getKey('sizelimit'), 'get default size');
  91. }
  92. public function testHandleWrongTypes()
  93. {
  94. $original_options = $this->_options;
  95. $original_options['main']['syntaxhighlightingtheme'] = 'foo';
  96. $options = $original_options;
  97. $options['main']['discussion'] = 'true';
  98. $options['main']['opendiscussion'] = 0;
  99. $options['main']['password'] = -1; // evaluates to TRUE
  100. $options['main']['fileupload'] = 'false';
  101. $options['expire_options']['foo'] = 'bar';
  102. $options['formatter_options'][] = 'foo';
  103. Helper::createIniFile(CONF, $options);
  104. $conf = new Configuration;
  105. $original_options['expire_options']['foo'] = intval('bar');
  106. $original_options['formatter_options'][0] = 'foo';
  107. $this->assertEquals($original_options, $conf->get(), 'incorrect types are corrected');
  108. }
  109. public function testHandleMissingSubKeys()
  110. {
  111. $options = $this->_options;
  112. unset($options['expire_options']['1week']);
  113. unset($options['expire_options']['1year']);
  114. unset($options['expire_options']['never']);
  115. Helper::createIniFile(CONF, $options);
  116. $conf = new Configuration;
  117. $options['expire']['default'] = '5min';
  118. $this->assertEquals($options, $conf->get(), 'not overriding "missing" subkeys');
  119. }
  120. public function testHandlePreRenameConfig()
  121. {
  122. $options = $this->_options;
  123. $options['model']['class'] = 'zerobin_data';
  124. Helper::createIniFile(CONF, $options);
  125. $conf = new Configuration;
  126. $this->assertEquals('Filesystem', $conf->getKey('class', 'model'), 'old data class gets renamed');
  127. $options['model']['class'] = 'zerobin_db';
  128. Helper::createIniFile(CONF, $options);
  129. $conf = new Configuration;
  130. $this->assertEquals('Database', $conf->getKey('class', 'model'), 'old db class gets renamed');
  131. }
  132. public function testHandleConfigFileRename()
  133. {
  134. $options = $this->_options;
  135. Helper::createIniFile(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini.sample', $options);
  136. $options['main']['opendiscussion'] = true;
  137. $options['main']['fileupload'] = true;
  138. $options['main']['template'] = 'darkstrap';
  139. Helper::createIniFile(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', $options);
  140. $conf = new Configuration;
  141. $this->assertFileExists(CONF, 'old configuration file gets converted');
  142. $this->assertFileNotExists(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', 'old configuration file gets removed');
  143. $this->assertFileNotExists(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini.sample', 'old configuration sample file gets removed');
  144. $this->assertTrue(
  145. $conf->getKey('opendiscussion') &&
  146. $conf->getKey('fileupload') &&
  147. $conf->getKey('template') === 'darkstrap',
  148. 'configuration values get converted'
  149. );
  150. }
  151. public function testRenameIniSample()
  152. {
  153. $iniSample = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini.sample';
  154. Helper::createIniFile(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', $this->_options);
  155. if (is_file(CONF)) {
  156. unlink(CONF);
  157. }
  158. rename(CONF_SAMPLE, $iniSample);
  159. new Configuration;
  160. $this->assertFileNotExists($iniSample, 'old sample file gets removed');
  161. $this->assertFileExists(CONF_SAMPLE, 'new sample file gets created');
  162. $this->assertFileExists(CONF, 'old configuration file gets converted');
  163. $this->assertFileNotExists(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', 'old configuration file gets removed');
  164. }
  165. public function testConfigPath()
  166. {
  167. // setup
  168. $configFile = $this->_path . DIRECTORY_SEPARATOR . 'conf.php';
  169. $options = $this->_options;
  170. $options['main']['name'] = 'OtherBin';
  171. Helper::createIniFile($configFile, $options);
  172. // test
  173. putenv('CONFIG_PATH=' . $this->_path);
  174. $conf = new Configuration;
  175. $this->assertEquals('OtherBin', $conf->getKey('name'), 'changing config path is supported');
  176. // cleanup environment
  177. if (is_file($configFile)) {
  178. unlink($configFile);
  179. }
  180. putenv('CONFIG_PATH');
  181. }
  182. public function testConfigPathIni()
  183. {
  184. // setup
  185. $configFile = $this->_path . DIRECTORY_SEPARATOR . 'conf.ini';
  186. $configMigrated = $this->_path . DIRECTORY_SEPARATOR . 'conf.php';
  187. $options = $this->_options;
  188. $options['main']['name'] = 'OtherBin';
  189. Helper::createIniFile($configFile, $options);
  190. $this->assertFileNotExists(CONF, 'configuration in the default location is non existing');
  191. // test
  192. putenv('CONFIG_PATH=' . $this->_path);
  193. $conf = new Configuration;
  194. $this->assertEquals('OtherBin', $conf->getKey('name'), 'changing config path is supported for ini files as well');
  195. $this->assertFileExists($configMigrated, 'old configuration file gets converted');
  196. $this->assertFileNotExists($configFile, 'old configuration file gets removed');
  197. $this->assertFileNotExists(CONF, 'configuration is not created in the default location');
  198. // cleanup environment
  199. if (is_file($configFile)) {
  200. unlink($configFile);
  201. }
  202. putenv('CONFIG_PATH');
  203. }
  204. }