ViewTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. use PrivateBin\I18n;
  3. use PrivateBin\View;
  4. class ViewTest extends PHPUnit_Framework_TestCase
  5. {
  6. private static $error = 'foo bar';
  7. private static $status = '!*#@?$+';
  8. private static $formatters = array(
  9. 'plaintext' => 'Plain Text',
  10. 'syntaxhighlighting' => 'Source Code',
  11. 'markdown' => 'Markdown',
  12. );
  13. private static $formatter_default = 'plaintext';
  14. private static $expire = array(
  15. '5min' => '5 minutes',
  16. '1hour' => '1 hour',
  17. 'never' => 'Never',
  18. );
  19. private static $expire_default = '1hour';
  20. private static $version = 'Version 1.2.3';
  21. private $_content = array();
  22. public function setUp()
  23. {
  24. /* Setup Routine */
  25. $page = new View;
  26. $page->assign('NAME', 'PrivateBinTest');
  27. $page->assign('CIPHERDATA', Helper::getPaste()['data']);
  28. $page->assign('ERROR', self::$error);
  29. $page->assign('STATUS', self::$status);
  30. $page->assign('VERSION', self::$version);
  31. $page->assign('DISCUSSION', true);
  32. $page->assign('OPENDISCUSSION', true);
  33. $page->assign('MARKDOWN', true);
  34. $page->assign('SYNTAXHIGHLIGHTING', true);
  35. $page->assign('SYNTAXHIGHLIGHTINGTHEME', 'sons-of-obsidian');
  36. $page->assign('FORMATTER', self::$formatters);
  37. $page->assign('FORMATTERDEFAULT', self::$formatter_default);
  38. $page->assign('BURNAFTERREADINGSELECTED', false);
  39. $page->assign('PASSWORD', true);
  40. $page->assign('FILEUPLOAD', false);
  41. $page->assign('ZEROBINCOMPATIBILITY', false);
  42. $page->assign('NOTICE', 'example');
  43. $page->assign('LANGUAGESELECTION', '');
  44. $page->assign('LANGUAGES', I18n::getLanguageLabels(I18n::getAvailableLanguages()));
  45. $page->assign('EXPIRE', self::$expire);
  46. $page->assign('EXPIREDEFAULT', self::$expire_default);
  47. $page->assign('EXPIRECLONE', true);
  48. $page->assign('URLSHORTENER', '');
  49. $dir = dir(PATH . 'tpl');
  50. while (false !== ($file = $dir->read())) {
  51. if (substr($file, -4) === '.php') {
  52. $template = substr($file, 0, -4);
  53. ob_start();
  54. $page->draw($template);
  55. $this->_content[$template] = ob_get_contents();
  56. ob_end_clean();
  57. }
  58. }
  59. // check bootstrap variants
  60. $template = 'bootstrap-page';
  61. ob_start();
  62. $page->draw($template);
  63. $this->_content[$template] = ob_get_contents();
  64. ob_end_clean();
  65. foreach (array('-dark', '-compact') as $suffix) {
  66. $template = 'bootstrap' . $suffix;
  67. ob_start();
  68. $page->draw($template);
  69. $this->_content[$template] = ob_get_contents();
  70. ob_end_clean();
  71. $template .= '-page';
  72. ob_start();
  73. $page->draw($template);
  74. $this->_content[$template] = ob_get_contents();
  75. ob_end_clean();
  76. }
  77. }
  78. public function tearDown()
  79. {
  80. /* Tear Down Routine */
  81. }
  82. public function testTemplateRendersCorrectly()
  83. {
  84. foreach ($this->_content as $template => $content) {
  85. $this->assertRegExp(
  86. '#<div[^>]+id="cipherdata"[^>]*>' .
  87. preg_quote(htmlspecialchars(Helper::getPaste()['data'], ENT_NOQUOTES)) .
  88. '</div>#',
  89. $content,
  90. $template . ': outputs data correctly'
  91. );
  92. $this->assertRegExp(
  93. '#<div[^>]+id="errormessage"[^>]*>.*' . self::$error . '</div>#',
  94. $content,
  95. $template . ': outputs error correctly'
  96. );
  97. $this->assertRegExp(
  98. '#<[^>]+id="password"[^>]*>#',
  99. $content,
  100. $template . ': password available if configured'
  101. );
  102. $this->assertRegExp(
  103. '#<input[^>]+id="opendiscussion"[^>]*checked="checked"[^>]*>#',
  104. $content,
  105. $template . ': checked discussion if configured'
  106. );
  107. $this->assertRegExp(
  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->assertRegExp(
  114. '#<script[^>]+src="js/privatebin.js\\?' . rawurlencode(self::$version) . '"[^>]*>#',
  115. $content,
  116. $template . ': outputs version correctly'
  117. );
  118. }
  119. }
  120. /**
  121. * @expectedException Exception
  122. * @expectedExceptionCode 80
  123. */
  124. public function testMissingTemplate()
  125. {
  126. $test = new View;
  127. $test->draw('123456789 does not exist!');
  128. }
  129. }