ViewTest.php 4.6 KB

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