ViewTest.php 4.7 KB

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