ViewTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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('BASEPATH', '');
  28. $page->assign('ERROR', self::$error);
  29. $page->assign('STATUS', self::$status);
  30. $page->assign('VERSION', self::$version);
  31. $page->assign('DISCUSSION', true);
  32. $page->assign('OPENDISCUSSION', true);
  33. $page->assign('MARKDOWN', true);
  34. $page->assign('SYNTAXHIGHLIGHTING', true);
  35. $page->assign('SYNTAXHIGHLIGHTINGTHEME', 'sons-of-obsidian');
  36. $page->assign('FORMATTER', self::$formatters);
  37. $page->assign('FORMATTERDEFAULT', self::$formatter_default);
  38. $page->assign('BURNAFTERREADINGSELECTED', false);
  39. $page->assign('PASSWORD', true);
  40. $page->assign('FILEUPLOAD', false);
  41. $page->assign('ZEROBINCOMPATIBILITY', false);
  42. $page->assign('INFO', 'example');
  43. $page->assign('NOTICE', 'example');
  44. $page->assign('LANGUAGESELECTION', '');
  45. $page->assign('LANGUAGES', I18n::getLanguageLabels(I18n::getAvailableLanguages()));
  46. $page->assign('EXPIRE', self::$expire);
  47. $page->assign('EXPIREDEFAULT', self::$expire_default);
  48. $page->assign('URLSHORTENER', '');
  49. $page->assign('QRCODE', true);
  50. $page->assign('HTTPWARNING', true);
  51. $page->assign('HTTPSLINK', 'https://example.com/');
  52. $page->assign('COMPRESSION', 'zlib');
  53. $page->assign('CSPHEADER', 'default-src \'none\'');
  54. $dir = dir(PATH . 'tpl');
  55. while (false !== ($file = $dir->read())) {
  56. if (substr($file, -4) === '.php') {
  57. $template = substr($file, 0, -4);
  58. ob_start();
  59. $page->draw($template);
  60. $this->_content[$template] = ob_get_contents();
  61. ob_end_clean();
  62. }
  63. }
  64. // check bootstrap variants
  65. $template = 'bootstrap-page';
  66. ob_start();
  67. $page->draw($template);
  68. $this->_content[$template] = ob_get_contents();
  69. ob_end_clean();
  70. foreach (array('-dark', '-compact') as $suffix) {
  71. $template = 'bootstrap' . $suffix;
  72. ob_start();
  73. $page->draw($template);
  74. $this->_content[$template] = ob_get_contents();
  75. ob_end_clean();
  76. $template .= '-page';
  77. ob_start();
  78. $page->draw($template);
  79. $this->_content[$template] = ob_get_contents();
  80. ob_end_clean();
  81. }
  82. }
  83. public function tearDown()
  84. {
  85. /* Tear Down Routine */
  86. }
  87. public function testTemplateRendersCorrectly()
  88. {
  89. foreach ($this->_content as $template => $content) {
  90. $this->assertRegExp(
  91. '#<div[^>]+id="errormessage"[^>]*>.*' . self::$error . '#s',
  92. $content,
  93. $template . ': outputs error correctly'
  94. );
  95. if ($template === 'yourlsproxy') {
  96. // yourlsproxy template only displays error message
  97. continue;
  98. }
  99. $this->assertRegExp(
  100. '#<[^>]+id="password"[^>]*>#',
  101. $content,
  102. $template . ': password available if configured'
  103. );
  104. $this->assertRegExp(
  105. '#<input[^>]+id="opendiscussion"[^>]*checked="checked"[^>]*>#',
  106. $content,
  107. $template . ': checked discussion if configured'
  108. );
  109. $this->assertRegExp(
  110. '#<[^>]+id="opendiscussionoption"[^>]*>#',
  111. $content,
  112. $template . ': discussions available if configured'
  113. );
  114. // testing version number in JS address, since other instances may not be present in different templates
  115. $this->assertRegExp(
  116. '#<script[^>]+src="js/privatebin.js\\?' . rawurlencode(self::$version) . '"[^>]*>#',
  117. $content,
  118. $template . ': outputs version correctly'
  119. );
  120. }
  121. }
  122. /**
  123. * @expectedException Exception
  124. * @expectedExceptionCode 80
  125. */
  126. public function testMissingTemplate()
  127. {
  128. $test = new View;
  129. $test->draw('123456789 does not exist!');
  130. }
  131. }