configuration.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. class configurationTest extends PHPUnit_Framework_TestCase
  3. {
  4. private $_options = array(
  5. 'main' => array(
  6. 'discussion' => true,
  7. 'opendiscussion' => false,
  8. 'password' => true,
  9. 'fileupload' => false,
  10. 'burnafterreadingselected' => false,
  11. 'defaultformatter' => 'plaintext',
  12. 'syntaxhighlightingtheme' => null,
  13. 'sizelimit' => 2097152,
  14. 'template' => 'bootstrap',
  15. 'notice' => '',
  16. 'languageselection' => false,
  17. 'languagedefault' => '',
  18. 'urlshortener' => '',
  19. 'zerobincompatibility' => false,
  20. ),
  21. 'expire' => array(
  22. 'default' => '1week',
  23. 'clone' => true,
  24. ),
  25. 'expire_options' => array(
  26. '5min' => 300,
  27. '10min' => 600,
  28. '1hour' => 3600,
  29. '1day' => 86400,
  30. '1week' => 604800,
  31. '1month' => 2592000,
  32. '1year' => 31536000,
  33. 'never' => 0,
  34. ),
  35. 'formatter_options' => array(
  36. 'plaintext' => 'Plain Text',
  37. 'syntaxhighlighting' => 'Source Code',
  38. 'markdown' => 'Markdown',
  39. ),
  40. 'traffic' => array(
  41. 'limit' => 10,
  42. 'header' => null,
  43. 'dir' => '../data',
  44. ),
  45. 'model' => array(
  46. 'class' => 'zerobin_data',
  47. ),
  48. 'model_options' => array(
  49. 'dir' => '../data',
  50. ),
  51. );
  52. public function setUp()
  53. {
  54. /* Setup Routine */
  55. helper::confBackup();
  56. }
  57. public function tearDown()
  58. {
  59. /* Tear Down Routine */
  60. helper::confRestore();
  61. }
  62. public function testDefaultConfigFile()
  63. {
  64. $this->assertTrue(copy(CONF . '.bak', CONF), 'copy default configuration file');
  65. $conf = new configuration;
  66. $this->assertEquals($this->_options, $conf->get(), 'default configuration is correct');
  67. }
  68. public function testHandleFreshConfigFile()
  69. {
  70. helper::createIniFile(CONF, $this->_options);
  71. $conf = new configuration;
  72. $this->assertEquals($this->_options, $conf->get(), 'newly generated configuration is correct');
  73. }
  74. public function testHandleMissingConfigFile()
  75. {
  76. @unlink(CONF);
  77. $conf = new configuration;
  78. $this->assertEquals($this->_options, $conf->get(), 'returns correct defaults on missing file');
  79. }
  80. /**
  81. * @expectedException Exception
  82. * @expectedExceptionCode 2
  83. */
  84. public function testHandleBlankConfigFile()
  85. {
  86. file_put_contents(CONF, '');
  87. new configuration;
  88. }
  89. public function testHandleMinimalConfigFile()
  90. {
  91. file_put_contents(CONF, '[main]' . PHP_EOL . '[model]');
  92. $conf = new configuration;
  93. $this->assertEquals($this->_options, $conf->get(), 'returns correct defaults on empty file');
  94. }
  95. /**
  96. * @expectedException Exception
  97. * @expectedExceptionCode 3
  98. */
  99. public function testHandleInvalidSection()
  100. {
  101. file_put_contents(CONF, '[main]' . PHP_EOL . '[model]');
  102. $conf = new configuration;
  103. $conf->getKey('foo', 'bar');
  104. }
  105. /**
  106. * @expectedException Exception
  107. * @expectedExceptionCode 4
  108. */
  109. public function testHandleInvalidKey()
  110. {
  111. file_put_contents(CONF, '[main]' . PHP_EOL . '[model]');
  112. $conf = new configuration;
  113. $conf->getKey('foo');
  114. }
  115. public function testHandleGetKey()
  116. {
  117. file_put_contents(CONF, '[main]' . PHP_EOL . '[model]');
  118. $conf = new configuration;
  119. $this->assertEquals($this->_options['main']['sizelimit'], $conf->getKey('sizelimit'), 'get default size');
  120. }
  121. public function testHandleWrongTypes()
  122. {
  123. $original_options = $this->_options;
  124. $original_options['main']['syntaxhighlightingtheme'] = 'foo';
  125. $options = $original_options;
  126. $options['main']['discussion'] = 'true';
  127. $options['main']['opendiscussion'] = 0;
  128. $options['main']['password'] = -1; // evaluates to TRUE
  129. $options['main']['fileupload'] = 'false';
  130. $options['expire_options']['foo'] = 'bar';
  131. $options['formatter_options'][] = 'foo';
  132. helper::createIniFile(CONF, $options);
  133. $conf = new configuration;
  134. $original_options['expire_options']['foo'] = intval('bar');
  135. $original_options['formatter_options'][0] = 'foo';
  136. $this->assertEquals($original_options, $conf->get(), 'incorrect types are corrected');
  137. }
  138. public function testHandleMissingSubKeys()
  139. {
  140. $options = $this->_options;
  141. unset($options['expire_options']['1week']);
  142. unset($options['expire_options']['1year']);
  143. unset($options['expire_options']['never']);
  144. helper::createIniFile(CONF, $options);
  145. $conf = new configuration;
  146. $options['expire']['default'] = '5min';
  147. $this->assertEquals($options, $conf->get(), 'not overriding "missing" subkeys');
  148. }
  149. }