ViewTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. use PrivateBin\I18n;
  3. use PrivateBin\View;
  4. class ViewTest extends PHPUnit_Framework_TestCase
  5. {
  6. private static $error = 'foo bar';
  7. private static $status = '!*#@?$+';
  8. private static $formatters = array(
  9. 'plaintext' => 'Plain Text',
  10. 'syntaxhighlighting' => 'Source Code',
  11. 'markdown' => 'Markdown',
  12. );
  13. private static $formatter_default = 'plaintext';
  14. private static $expire = array(
  15. '5min' => '5 minutes',
  16. '1hour' => '1 hour',
  17. 'never' => 'Never',
  18. );
  19. private static $expire_default = '1hour';
  20. private static $version = 'Version 1.2.3';
  21. private $_content = array();
  22. public function setUp()
  23. {
  24. /* Setup Routine */
  25. $page = new View;
  26. $page->assign('NAME', 'PrivateBinTest');
  27. $page->assign('ERROR', self::$error);
  28. $page->assign('STATUS', self::$status);
  29. $page->assign('VERSION', self::$version);
  30. $page->assign('DISCUSSION', true);
  31. $page->assign('OPENDISCUSSION', true);
  32. $page->assign('MARKDOWN', true);
  33. $page->assign('SYNTAXHIGHLIGHTING', true);
  34. $page->assign('SYNTAXHIGHLIGHTINGTHEME', 'sons-of-obsidian');
  35. $page->assign('FORMATTER', self::$formatters);
  36. $page->assign('FORMATTERDEFAULT', self::$formatter_default);
  37. $page->assign('BURNAFTERREADINGSELECTED', false);
  38. $page->assign('PASSWORD', true);
  39. $page->assign('FILEUPLOAD', false);
  40. $page->assign('ZEROBINCOMPATIBILITY', false);
  41. $page->assign('NOTICE', 'example');
  42. $page->assign('LANGUAGESELECTION', '');
  43. $page->assign('LANGUAGES', I18n::getLanguageLabels(I18n::getAvailableLanguages()));
  44. $page->assign('EXPIRE', self::$expire);
  45. $page->assign('EXPIREDEFAULT', self::$expire_default);
  46. $page->assign('URLSHORTENER', '');
  47. $page->assign('QRCODE', true);
  48. $page->assign('HTTPWARNING', true);
  49. $page->assign('COMPRESSION', 'zlib');
  50. $dir = dir(PATH . 'tpl');
  51. while (false !== ($file = $dir->read())) {
  52. if (substr($file, -4) === '.php') {
  53. $template = substr($file, 0, -4);
  54. ob_start();
  55. $page->draw($template);
  56. $this->_content[$template] = ob_get_contents();
  57. ob_end_clean();
  58. }
  59. }
  60. // check bootstrap variants
  61. $template = 'bootstrap-page';
  62. ob_start();
  63. $page->draw($template);
  64. $this->_content[$template] = ob_get_contents();
  65. ob_end_clean();
  66. foreach (array('-dark', '-compact') as $suffix) {
  67. $template = 'bootstrap' . $suffix;
  68. ob_start();
  69. $page->draw($template);
  70. $this->_content[$template] = ob_get_contents();
  71. ob_end_clean();
  72. $template .= '-page';
  73. ob_start();
  74. $page->draw($template);
  75. $this->_content[$template] = ob_get_contents();
  76. ob_end_clean();
  77. }
  78. }
  79. public function tearDown()
  80. {
  81. /* Tear Down Routine */
  82. }
  83. public function testTemplateRendersCorrectly()
  84. {
  85. foreach ($this->_content as $template => $content) {
  86. $this->assertRegExp(
  87. '#<div[^>]+id="errormessage"[^>]*>.*' . self::$error . '#s',
  88. $content,
  89. $template . ': outputs error correctly'
  90. );
  91. $this->assertRegExp(
  92. '#<[^>]+id="password"[^>]*>#',
  93. $content,
  94. $template . ': password available if configured'
  95. );
  96. $this->assertRegExp(
  97. '#<input[^>]+id="opendiscussion"[^>]*checked="checked"[^>]*>#',
  98. $content,
  99. $template . ': checked discussion if configured'
  100. );
  101. $this->assertRegExp(
  102. '#<[^>]+id="opendiscussionoption"[^>]*>#',
  103. $content,
  104. $template . ': discussions available if configured'
  105. );
  106. // testing version number in JS address, since other instances may not be present in different templates
  107. $this->assertRegExp(
  108. '#<script[^>]+src="js/privatebin.js\\?' . rawurlencode(self::$version) . '"[^>]*>#',
  109. $content,
  110. $template . ': outputs version correctly'
  111. );
  112. }
  113. }
  114. /**
  115. * @expectedException Exception
  116. * @expectedExceptionCode 80
  117. */
  118. public function testMissingTemplate()
  119. {
  120. $test = new View;
  121. $test->draw('123456789 does not exist!');
  122. }
  123. }