ViewTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 = array(
  11. 'plaintext' => 'Plain Text',
  12. 'syntaxhighlighting' => 'Source Code',
  13. 'markdown' => 'Markdown',
  14. );
  15. private static $formatter_default = 'plaintext';
  16. private static $expire = array(
  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 = array();
  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', array());
  58. $dir = dir(PATH . 'tpl');
  59. while (false !== ($file = $dir->read())) {
  60. if (substr($file, -4) === '.php') {
  61. $template = substr($file, 0, -4);
  62. ob_start();
  63. $page->draw($template);
  64. $this->_content[$template] = ob_get_contents();
  65. ob_end_clean();
  66. }
  67. }
  68. // check bootstrap variants
  69. $template = 'bootstrap-page';
  70. ob_start();
  71. $page->draw($template);
  72. $this->_content[$template] = ob_get_contents();
  73. ob_end_clean();
  74. foreach (array('-dark', '-compact') as $suffix) {
  75. $template = 'bootstrap' . $suffix;
  76. ob_start();
  77. $page->draw($template);
  78. $this->_content[$template] = ob_get_contents();
  79. ob_end_clean();
  80. $template .= '-page';
  81. ob_start();
  82. $page->draw($template);
  83. $this->_content[$template] = ob_get_contents();
  84. ob_end_clean();
  85. }
  86. }
  87. public function testTemplateRendersCorrectly()
  88. {
  89. foreach ($this->_content as $template => $content) {
  90. $this->assertMatchesRegularExpression(
  91. '#<div[^>]+id="errormessage"[^>]*>.*' . self::$error . '#s',
  92. $content,
  93. $template . ': outputs error correctly'
  94. );
  95. if ($template === 'shortenerproxy') {
  96. // shortenerproxy template only displays error message
  97. continue;
  98. }
  99. $this->assertMatchesRegularExpression(
  100. '#<[^>]+id="password"[^>]*>#',
  101. $content,
  102. $template . ': password available if configured'
  103. );
  104. $this->assertMatchesRegularExpression(
  105. '#<input[^>]+id="opendiscussion"[^>]*checked="checked"[^>]*>#',
  106. $content,
  107. $template . ': checked discussion if configured'
  108. );
  109. $this->assertMatchesRegularExpression(
  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->assertMatchesRegularExpression(
  116. '#<script[^>]+src="js/privatebin.js\\?' . rawurlencode(self::$version) . '"[^>]*>#',
  117. $content,
  118. $template . ': outputs version correctly'
  119. );
  120. }
  121. }
  122. public function testMissingTemplate()
  123. {
  124. $test = new View;
  125. $this->expectException(Exception::class);
  126. $this->expectExceptionCode(80);
  127. $test->draw('123456789 does not exist!');
  128. }
  129. public function testTemplateFilePath()
  130. {
  131. $template = 'bootstrap';
  132. $templatePath = PATH . 'tpl' . DIRECTORY_SEPARATOR . $template . '.php';
  133. $path = View::getTemplateFilePath($template);
  134. $this->assertEquals($templatePath, $path, 'Template file path');
  135. }
  136. public function testIsBootstrapTemplate()
  137. {
  138. $bootstrapTemplate = 'bootstrap-dark';
  139. $nonBootstrapTemplate = 'bootstrap5';
  140. $this->assertTrue(View::isBootstrapTemplate($bootstrapTemplate), 'Is bootstrap template');
  141. $this->assertFalse(View::isBootstrapTemplate($nonBootstrapTemplate), 'Is not bootstrap template');
  142. }
  143. }