1
0

ViewTest.php 4.9 KB

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