RoundCube Webmail
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.2 KiB

  1. <?php
  2. namespace Roundcube\Tests;
  3. /*
  4. +-----------------------------------------------------------------------+
  5. | This file is part of the Roundcube Webmail client |
  6. | |
  7. | Copyright (C) The Roundcube Dev Team |
  8. | |
  9. | Licensed under the GNU General Public License version 3 or |
  10. | any later version with exceptions for skins & plugins. |
  11. | See the README file for a full license statement. |
  12. | |
  13. | PURPOSE: |
  14. | A class for easier testing of code that uses rcmail_output classes |
  15. +-----------------------------------------------------------------------+
  16. | Author: Aleksander Machniak <alec@alec.pl> |
  17. +-----------------------------------------------------------------------+
  18. */
  19. /**
  20. * A class for easier testing of code that uses rcmail_output classes
  21. */
  22. class OutputHtmlMock extends \rcmail_output_html
  23. {
  24. public const E_EXIT = 101;
  25. public const E_REDIRECT = 102;
  26. public $output;
  27. public $headers = [];
  28. public $errorCode;
  29. public $errorMessage;
  30. public $template = '';
  31. /**
  32. * Redirect to a certain url
  33. *
  34. * @param mixed $p Either a string with the action or url parameters as key-value pairs
  35. * @param int $delay Delay in seconds
  36. * @param bool $secure Redirect to secure location (see rcmail::url())
  37. */
  38. #[\Override]
  39. public function redirect($p = [], $delay = 1, $secure = false)
  40. {
  41. if (!empty($this->env['extwin'])) {
  42. $p['extwin'] = 1;
  43. }
  44. $location = $this->app->url($p, false, false, $secure);
  45. // header('Location: ' . $location);
  46. throw new ExitException("Location: {$location}", self::E_REDIRECT);
  47. }
  48. /**
  49. * Send the request output to the client.
  50. * This will either parse a skin template.
  51. *
  52. * @param string $templ Template name
  53. * @param bool $exit True if script should terminate (default)
  54. */
  55. #[\Override]
  56. public function send($templ = null, $exit = true)
  57. {
  58. $this->template = $templ;
  59. parent::send($templ, false);
  60. if ($exit) {
  61. throw new ExitException('Output sent', self::E_EXIT);
  62. }
  63. }
  64. /**
  65. * A helper to send output to the browser and exit
  66. *
  67. * @param string $body The output body
  68. * @param array $headers Headers
  69. */
  70. #[\Override]
  71. public function sendExit($body = '', $headers = [])
  72. {
  73. foreach ($headers as $header) {
  74. $this->header($header);
  75. }
  76. $this->output .= $body;
  77. throw new ExitException('Output sent', self::E_EXIT);
  78. }
  79. /**
  80. * A helper to send HTTP error code and message to the browser, and exit.
  81. *
  82. * @param int $code The HTTP error code
  83. * @param string $message The HTTP error message
  84. */
  85. #[\Override]
  86. public function sendExitError($code, $message = '')
  87. {
  88. $this->errorCode = $code;
  89. $this->errorMessage = $message;
  90. throw new ExitException('Output sent (error)', self::E_EXIT);
  91. }
  92. /**
  93. * Process template and write to stdOut
  94. *
  95. * @param string $template HTML template content
  96. */
  97. #[\Override]
  98. public function write($template = '')
  99. {
  100. ob_start();
  101. parent::write($template);
  102. $this->output = ob_get_contents();
  103. ob_end_clean();
  104. }
  105. /**
  106. * Parse a specific skin template and deliver to stdout (or return)
  107. *
  108. * @param string $name Template name
  109. * @param bool $exit Exit script
  110. * @param bool $write Don't write to stdout, return parsed content instead
  111. *
  112. * @see http://php.net/manual/en/function.exit.php
  113. */
  114. #[\Override]
  115. public function parse($name = 'main', $exit = true, $write = true)
  116. {
  117. // ob_start();
  118. parent::parse($name, false, $write);
  119. // $this->output = ob_get_contents();
  120. // ob_end_clean();
  121. if ($exit) {
  122. throw new ExitException('Output sent', self::E_EXIT);
  123. }
  124. }
  125. /**
  126. * Delete all stored env variables and commands
  127. */
  128. #[\Override]
  129. public function reset($all = false)
  130. {
  131. parent::reset($all);
  132. $this->headers = [];
  133. $this->output = null;
  134. $this->template = null;
  135. $this->errorCode = null;
  136. $this->errorMessage = null;
  137. }
  138. /**
  139. * A wrapper for header() function, so it can be replaced for automated tests
  140. *
  141. * @param string $header The header string
  142. * @param bool $replace Replace previously set header?
  143. */
  144. #[\Override]
  145. public function header($header, $replace = true)
  146. {
  147. $this->headers[] = $header;
  148. }
  149. /**
  150. * Return the output
  151. */
  152. public function getOutput()
  153. {
  154. return $this->output;
  155. }
  156. /**
  157. * Return private/protected property
  158. */
  159. public function getProperty($name)
  160. {
  161. return $this->{$name};
  162. }
  163. }