configuration.php 4.4 KB

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