configuration.php 4.3 KB

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