configuration.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. class configurationTest extends PHPUnit_Framework_TestCase
  3. {
  4. private $_options;
  5. private $_minimalConfig;
  6. public function setUp()
  7. {
  8. /* Setup Routine */
  9. helper::confBackup();
  10. $this->_options = configuration::getDefaults();
  11. $this->_options['model_options']['dir'] = PATH . $this->_options['model_options']['dir'];
  12. $this->_options['traffic']['dir'] = PATH . $this->_options['traffic']['dir'];
  13. $this->_options['purge']['dir'] = PATH . $this->_options['purge']['dir'];
  14. $this->_minimalConfig = '[main]' . PHP_EOL . '[model]' . PHP_EOL . '[model_options]';
  15. }
  16. public function tearDown()
  17. {
  18. /* Tear Down Routine */
  19. helper::confRestore();
  20. }
  21. public function testDefaultConfigFile()
  22. {
  23. $this->assertTrue(copy(CONF . '.bak', CONF), 'copy default configuration file');
  24. $conf = new configuration;
  25. $this->assertEquals($this->_options, $conf->get(), 'default configuration is correct');
  26. }
  27. public function testHandleFreshConfigFile()
  28. {
  29. helper::createIniFile(CONF, $this->_options);
  30. $conf = new configuration;
  31. $this->assertEquals($this->_options, $conf->get(), 'newly generated configuration is correct');
  32. }
  33. public function testHandleMissingConfigFile()
  34. {
  35. @unlink(CONF);
  36. $conf = new configuration;
  37. $this->assertEquals($this->_options, $conf->get(), 'returns correct defaults on missing file');
  38. }
  39. /**
  40. * @expectedException Exception
  41. * @expectedExceptionCode 2
  42. */
  43. public function testHandleBlankConfigFile()
  44. {
  45. file_put_contents(CONF, '');
  46. new configuration;
  47. }
  48. public function testHandleMinimalConfigFile()
  49. {
  50. file_put_contents(CONF, $this->_minimalConfig);
  51. $conf = new configuration;
  52. $this->assertEquals($this->_options, $conf->get(), 'returns correct defaults on empty file');
  53. }
  54. /**
  55. * @expectedException Exception
  56. * @expectedExceptionCode 3
  57. */
  58. public function testHandleInvalidSection()
  59. {
  60. file_put_contents(CONF, $this->_minimalConfig);
  61. $conf = new configuration;
  62. $conf->getKey('foo', 'bar');
  63. }
  64. /**
  65. * @expectedException Exception
  66. * @expectedExceptionCode 4
  67. */
  68. public function testHandleInvalidKey()
  69. {
  70. file_put_contents(CONF, $this->_minimalConfig);
  71. $conf = new configuration;
  72. $conf->getKey('foo');
  73. }
  74. public function testHandleGetKey()
  75. {
  76. file_put_contents(CONF, $this->_minimalConfig);
  77. $conf = new configuration;
  78. $this->assertEquals($this->_options['main']['sizelimit'], $conf->getKey('sizelimit'), 'get default size');
  79. }
  80. public function testHandleWrongTypes()
  81. {
  82. $original_options = $this->_options;
  83. $original_options['main']['syntaxhighlightingtheme'] = 'foo';
  84. $options = $original_options;
  85. $options['main']['discussion'] = 'true';
  86. $options['main']['opendiscussion'] = 0;
  87. $options['main']['password'] = -1; // evaluates to TRUE
  88. $options['main']['fileupload'] = 'false';
  89. $options['expire_options']['foo'] = 'bar';
  90. $options['formatter_options'][] = 'foo';
  91. helper::createIniFile(CONF, $options);
  92. $conf = new configuration;
  93. $original_options['expire_options']['foo'] = intval('bar');
  94. $original_options['formatter_options'][0] = 'foo';
  95. $this->assertEquals($original_options, $conf->get(), 'incorrect types are corrected');
  96. }
  97. public function testHandleMissingSubKeys()
  98. {
  99. $options = $this->_options;
  100. unset($options['expire_options']['1week']);
  101. unset($options['expire_options']['1year']);
  102. unset($options['expire_options']['never']);
  103. helper::createIniFile(CONF, $options);
  104. $conf = new configuration;
  105. $options['expire']['default'] = '5min';
  106. $this->assertEquals($options, $conf->get(), 'not overriding "missing" subkeys');
  107. }
  108. public function testHandlePreRenameConfig()
  109. {
  110. $options = $this->_options;
  111. $options['model']['class'] = 'zerobin_data';
  112. helper::createIniFile(CONF, $options);
  113. $conf = new configuration;
  114. $this->assertEquals('privatebin_data', $conf->getKey('class', 'model'), 'old data class gets renamed');
  115. $options['model']['class'] = 'zerobin_db';
  116. helper::createIniFile(CONF, $options);
  117. $conf = new configuration;
  118. $this->assertEquals('privatebin_db', $conf->getKey('class', 'model'), 'old db class gets renamed');
  119. }
  120. }