1
0

ViewTest.php 5.2 KB

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