ViewTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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('ZEROBINCOMPATIBILITY', false);
  45. $page->assign('INFO', 'example');
  46. $page->assign('NOTICE', 'example');
  47. $page->assign('LANGUAGESELECTION', '');
  48. $page->assign('LANGUAGES', I18n::getLanguageLabels(I18n::getAvailableLanguages()));
  49. $page->assign('EXPIRE', self::$expire);
  50. $page->assign('EXPIREDEFAULT', self::$expire_default);
  51. $page->assign('URLSHORTENER', '');
  52. $page->assign('QRCODE', true);
  53. $page->assign('EMAIL', true);
  54. $page->assign('HTTPWARNING', true);
  55. $page->assign('HTTPSLINK', 'https://example.com/');
  56. $page->assign('COMPRESSION', 'zlib');
  57. $page->assign('CSPHEADER', 'default-src \'none\'');
  58. $page->assign('SRI', array());
  59. $dir = dir(PATH . 'tpl');
  60. while (false !== ($file = $dir->read())) {
  61. if (substr($file, -4) === '.php') {
  62. $template = substr($file, 0, -4);
  63. ob_start();
  64. $page->draw($template);
  65. $this->_content[$template] = ob_get_contents();
  66. ob_end_clean();
  67. }
  68. }
  69. // check bootstrap variants
  70. $template = 'bootstrap-page';
  71. ob_start();
  72. $page->draw($template);
  73. $this->_content[$template] = ob_get_contents();
  74. ob_end_clean();
  75. foreach (array('-dark', '-compact') as $suffix) {
  76. $template = 'bootstrap' . $suffix;
  77. ob_start();
  78. $page->draw($template);
  79. $this->_content[$template] = ob_get_contents();
  80. ob_end_clean();
  81. $template .= '-page';
  82. ob_start();
  83. $page->draw($template);
  84. $this->_content[$template] = ob_get_contents();
  85. ob_end_clean();
  86. }
  87. }
  88. public function testTemplateRendersCorrectly()
  89. {
  90. foreach ($this->_content as $template => $content) {
  91. $this->assertMatchesRegularExpression(
  92. '#<div[^>]+id="errormessage"[^>]*>.*' . self::$error . '#s',
  93. $content,
  94. $template . ': outputs error correctly'
  95. );
  96. if ($template === 'yourlsproxy') {
  97. // yourlsproxy template only displays error message
  98. continue;
  99. }
  100. $this->assertMatchesRegularExpression(
  101. '#<[^>]+id="password"[^>]*>#',
  102. $content,
  103. $template . ': password available if configured'
  104. );
  105. $this->assertMatchesRegularExpression(
  106. '#<input[^>]+id="opendiscussion"[^>]*checked="checked"[^>]*>#',
  107. $content,
  108. $template . ': checked discussion if configured'
  109. );
  110. $this->assertMatchesRegularExpression(
  111. '#<[^>]+id="opendiscussionoption"[^>]*>#',
  112. $content,
  113. $template . ': discussions available if configured'
  114. );
  115. // testing version number in JS address, since other instances may not be present in different templates
  116. $this->assertMatchesRegularExpression(
  117. '#<script[^>]+src="js/privatebin.js\\?' . rawurlencode(self::$version) . '"[^>]*>#',
  118. $content,
  119. $template . ': outputs version correctly'
  120. );
  121. }
  122. }
  123. public function testMissingTemplate()
  124. {
  125. $test = new View;
  126. $this->expectException(Exception::class);
  127. $this->expectExceptionCode(80);
  128. $test->draw('123456789 does not exist!');
  129. }
  130. public function testTemplateFilePath()
  131. {
  132. $template = 'bootstrap';
  133. $templatePath = PATH . 'tpl' . DIRECTORY_SEPARATOR . $template . '.php';
  134. $path = View::getTemplateFilePath($template);
  135. $this->assertEquals($templatePath, $path, 'Template file path');
  136. }
  137. public function testIsBootstrapTemplate()
  138. {
  139. $bootstrapTemplate = 'bootstrap-dark';
  140. $nonBootstrapTemplate = 'bootstrap5';
  141. $this->assertTrue(View::isBootstrapTemplate($bootstrapTemplate), 'Is bootstrap template');
  142. $this->assertFalse(View::isBootstrapTemplate($nonBootstrapTemplate), 'Is not bootstrap template');
  143. }
  144. }