ViewTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 $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('EMAIL', true);
  52. $page->assign('HTTPWARNING', true);
  53. $page->assign('HTTPSLINK', 'https://example.com/');
  54. $page->assign('COMPRESSION', 'zlib');
  55. $page->assign('CSPHEADER', 'default-src \'none\'');
  56. $dir = dir(PATH . 'tpl');
  57. while (false !== ($file = $dir->read())) {
  58. if (substr($file, -4) === '.php') {
  59. $template = substr($file, 0, -4);
  60. ob_start();
  61. $page->draw($template);
  62. $this->_content[$template] = ob_get_contents();
  63. ob_end_clean();
  64. }
  65. }
  66. // check bootstrap variants
  67. $template = 'bootstrap-page';
  68. ob_start();
  69. $page->draw($template);
  70. $this->_content[$template] = ob_get_contents();
  71. ob_end_clean();
  72. foreach (array('-dark', '-compact') as $suffix) {
  73. $template = 'bootstrap' . $suffix;
  74. ob_start();
  75. $page->draw($template);
  76. $this->_content[$template] = ob_get_contents();
  77. ob_end_clean();
  78. $template .= '-page';
  79. ob_start();
  80. $page->draw($template);
  81. $this->_content[$template] = ob_get_contents();
  82. ob_end_clean();
  83. }
  84. }
  85. public function testTemplateRendersCorrectly()
  86. {
  87. foreach ($this->_content as $template => $content) {
  88. $this->assertMatchesRegularExpression(
  89. '#<div[^>]+id="errormessage"[^>]*>.*' . self::$error . '#s',
  90. $content,
  91. $template . ': outputs error correctly'
  92. );
  93. if ($template === 'yourlsproxy') {
  94. // yourlsproxy template only displays error message
  95. continue;
  96. }
  97. $this->assertMatchesRegularExpression(
  98. '#<[^>]+id="password"[^>]*>#',
  99. $content,
  100. $template . ': password available if configured'
  101. );
  102. $this->assertMatchesRegularExpression(
  103. '#<input[^>]+id="opendiscussion"[^>]*checked="checked"[^>]*>#',
  104. $content,
  105. $template . ': checked discussion if configured'
  106. );
  107. $this->assertMatchesRegularExpression(
  108. '#<[^>]+id="opendiscussionoption"[^>]*>#',
  109. $content,
  110. $template . ': discussions available if configured'
  111. );
  112. // testing version number in JS address, since other instances may not be present in different templates
  113. $this->assertMatchesRegularExpression(
  114. '#<script[^>]+src="js/privatebin.js\\?' . rawurlencode(self::$version) . '"[^>]*>#',
  115. $content,
  116. $template . ': outputs version correctly'
  117. );
  118. }
  119. }
  120. public function testMissingTemplate()
  121. {
  122. $test = new View;
  123. $this->expectException(Exception::class);
  124. $this->expectExceptionCode(80);
  125. $test->draw('123456789 does not exist!');
  126. }
  127. }