1
0

ConfigurationTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. use PrivateBin\Configuration;
  3. class ConfigurationTest extends PHPUnit_Framework_TestCase
  4. {
  5. private $_options;
  6. private $_minimalConfig;
  7. public function setUp()
  8. {
  9. /* Setup Routine */
  10. Helper::confBackup();
  11. $this->_options = Configuration::getDefaults();
  12. $this->_options['model_options']['dir'] = PATH . $this->_options['model_options']['dir'];
  13. $this->_options['traffic']['dir'] = PATH . $this->_options['traffic']['dir'];
  14. $this->_options['purge']['dir'] = PATH . $this->_options['purge']['dir'];
  15. $this->_minimalConfig = '[main]' . PHP_EOL . '[model]' . PHP_EOL . '[model_options]';
  16. }
  17. public function tearDown()
  18. {
  19. /* Tear Down Routine */
  20. if (is_file(CONF)) {
  21. unlink(CONF);
  22. }
  23. Helper::confRestore();
  24. }
  25. public function testDefaultConfigFile()
  26. {
  27. $conf = new Configuration;
  28. $this->assertEquals($this->_options, $conf->get(), 'default configuration is correct');
  29. }
  30. public function testHandleFreshConfigFile()
  31. {
  32. Helper::createIniFile(CONF, $this->_options);
  33. $conf = new Configuration;
  34. $this->assertEquals($this->_options, $conf->get(), 'newly generated configuration is correct');
  35. }
  36. public function testHandleMissingConfigFile()
  37. {
  38. if (is_file(CONF)) {
  39. unlink(CONF);
  40. }
  41. $conf = new Configuration;
  42. $this->assertEquals($this->_options, $conf->get(), 'returns correct defaults on missing file');
  43. }
  44. /**
  45. * @expectedException Exception
  46. * @expectedExceptionCode 2
  47. */
  48. public function testHandleBlankConfigFile()
  49. {
  50. file_put_contents(CONF, '');
  51. new Configuration;
  52. }
  53. public function testHandleMinimalConfigFile()
  54. {
  55. file_put_contents(CONF, $this->_minimalConfig);
  56. $conf = new Configuration;
  57. $this->assertEquals($this->_options, $conf->get(), 'returns correct defaults on empty file');
  58. }
  59. /**
  60. * @expectedException Exception
  61. * @expectedExceptionCode 3
  62. */
  63. public function testHandleInvalidSection()
  64. {
  65. file_put_contents(CONF, $this->_minimalConfig);
  66. $conf = new Configuration;
  67. $conf->getKey('foo', 'bar');
  68. }
  69. /**
  70. * @expectedException Exception
  71. * @expectedExceptionCode 4
  72. */
  73. public function testHandleInvalidKey()
  74. {
  75. file_put_contents(CONF, $this->_minimalConfig);
  76. $conf = new Configuration;
  77. $conf->getKey('foo');
  78. }
  79. public function testHandleGetKey()
  80. {
  81. file_put_contents(CONF, $this->_minimalConfig);
  82. $conf = new Configuration;
  83. $this->assertEquals($this->_options['main']['sizelimit'], $conf->getKey('sizelimit'), 'get default size');
  84. }
  85. public function testHandleWrongTypes()
  86. {
  87. $original_options = $this->_options;
  88. $original_options['main']['syntaxhighlightingtheme'] = 'foo';
  89. $options = $original_options;
  90. $options['main']['discussion'] = 'true';
  91. $options['main']['opendiscussion'] = 0;
  92. $options['main']['password'] = -1; // evaluates to TRUE
  93. $options['main']['fileupload'] = 'false';
  94. $options['expire_options']['foo'] = 'bar';
  95. $options['formatter_options'][] = 'foo';
  96. Helper::createIniFile(CONF, $options);
  97. $conf = new Configuration;
  98. $original_options['expire_options']['foo'] = intval('bar');
  99. $original_options['formatter_options'][0] = 'foo';
  100. $this->assertEquals($original_options, $conf->get(), 'incorrect types are corrected');
  101. }
  102. public function testHandleMissingSubKeys()
  103. {
  104. $options = $this->_options;
  105. unset($options['expire_options']['1week']);
  106. unset($options['expire_options']['1year']);
  107. unset($options['expire_options']['never']);
  108. Helper::createIniFile(CONF, $options);
  109. $conf = new Configuration;
  110. $options['expire']['default'] = '5min';
  111. $this->assertEquals($options, $conf->get(), 'not overriding "missing" subkeys');
  112. }
  113. public function testHandlePreRenameConfig()
  114. {
  115. $options = $this->_options;
  116. $options['model']['class'] = 'zerobin_data';
  117. Helper::createIniFile(CONF, $options);
  118. $conf = new Configuration;
  119. $this->assertEquals('Filesystem', $conf->getKey('class', 'model'), 'old data class gets renamed');
  120. $options['model']['class'] = 'zerobin_db';
  121. Helper::createIniFile(CONF, $options);
  122. $conf = new Configuration;
  123. $this->assertEquals('Database', $conf->getKey('class', 'model'), 'old db class gets renamed');
  124. }
  125. public function testHandleConfigFileRename()
  126. {
  127. $options = $this->_options;
  128. Helper::createIniFile(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini.sample', $options);
  129. $options['main']['opendiscussion'] = true;
  130. $options['main']['fileupload'] = true;
  131. $options['main']['template'] = 'darkstrap';
  132. Helper::createIniFile(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', $options);
  133. $conf = new Configuration;
  134. $this->assertFileExists(CONF, 'old configuration file gets converted');
  135. $this->assertFileNotExists(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', 'old configuration file gets removed');
  136. $this->assertFileNotExists(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini.sample', 'old configuration sample file gets removed');
  137. $this->assertTrue(
  138. $conf->getKey('opendiscussion') &&
  139. $conf->getKey('fileupload') &&
  140. $conf->getKey('template') === 'darkstrap',
  141. 'configuration values get converted'
  142. );
  143. }
  144. public function testRenameIniSample()
  145. {
  146. $iniSample = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini.sample';
  147. Helper::createIniFile(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', $this->_options);
  148. if (is_file(CONF)) {
  149. unlink(CONF);
  150. }
  151. rename(CONF_SAMPLE, $iniSample);
  152. new Configuration;
  153. $this->assertFileNotExists($iniSample, 'old sample file gets removed');
  154. $this->assertFileExists(CONF_SAMPLE, 'new sample file gets created');
  155. $this->assertFileExists(CONF, 'old configuration file gets converted');
  156. $this->assertFileNotExists(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', 'old configuration file gets removed');
  157. }
  158. }