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.

169 lines
4.7 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 OutputJsonMock extends \rcmail_output_json
  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. /**
  31. * Redirect to a certain url
  32. *
  33. * @param mixed $p Either a string with the action or url parameters as key-value pairs
  34. * @param int $delay Delay in seconds
  35. *
  36. * @see rcmail::url()
  37. */
  38. #[\Override]
  39. public function redirect($p = [], $delay = 1)
  40. {
  41. $location = $this->app->url($p);
  42. ob_start();
  43. $this->remote_response(sprintf("window.setTimeout(function(){ %s.redirect('%s',true); }, %d);",
  44. self::JS_OBJECT_NAME, $location, $delay));
  45. $this->output = ob_get_contents();
  46. ob_end_clean();
  47. throw new ExitException("Location: {$location}", self::E_REDIRECT);
  48. }
  49. /**
  50. * Send an AJAX response to the client.
  51. */
  52. #[\Override]
  53. public function send()
  54. {
  55. ob_start();
  56. $this->remote_response();
  57. $this->output = ob_get_contents();
  58. ob_end_clean();
  59. throw new ExitException('Output sent', self::E_EXIT);
  60. }
  61. /**
  62. * A helper to send output to the browser and exit
  63. *
  64. * @param string $body The output body
  65. * @param array $headers Headers
  66. */
  67. #[\Override]
  68. public function sendExit($body = '', $headers = [])
  69. {
  70. foreach ($headers as $header) {
  71. $this->header($header);
  72. }
  73. $this->output = $body;
  74. throw new ExitException('Output sent', self::E_EXIT);
  75. }
  76. /**
  77. * A helper to send HTTP error code and message to the browser, and exit.
  78. *
  79. * @param int $code The HTTP error code
  80. * @param string $message The HTTP error message
  81. */
  82. #[\Override]
  83. public function sendExitError($code, $message = '')
  84. {
  85. $this->errorCode = $code;
  86. $this->errorMessage = $message;
  87. throw new ExitException('Output sent (error)', self::E_EXIT);
  88. }
  89. /**
  90. * Show error page and terminate script execution
  91. *
  92. * @param int $code Error code
  93. * @param string $message Error message
  94. */
  95. #[\Override]
  96. public function raise_error($code, $message)
  97. {
  98. if ($code == 403) {
  99. throw new ExitException('403 Forbidden', self::E_EXIT);
  100. }
  101. $this->show_message("Application Error ({$code}): {$message}", 'error');
  102. ob_start();
  103. $this->remote_response();
  104. $this->output = ob_get_contents();
  105. ob_end_clean();
  106. throw new ExitException("Error {$code} raised", self::E_EXIT);
  107. }
  108. /**
  109. * Delete all stored env variables and commands
  110. */
  111. #[\Override]
  112. public function reset()
  113. {
  114. parent::reset();
  115. $this->headers = [];
  116. $this->output = null;
  117. $this->header_sent = false;
  118. $this->errorCode = null;
  119. $this->errorMessage = null;
  120. }
  121. /**
  122. * A wrapper for header() function, so it can be replaced for automated tests
  123. *
  124. * @param string $header The header string
  125. * @param bool $replace Replace previously set header?
  126. */
  127. #[\Override]
  128. public function header($header, $replace = true)
  129. {
  130. $this->headers[] = $header;
  131. }
  132. /**
  133. * Return the JSON output as an array
  134. */
  135. public function getOutput()
  136. {
  137. return $this->output ? json_decode($this->output, true) : null;
  138. }
  139. /**
  140. * Return private/protected property
  141. */
  142. public function getProperty($name)
  143. {
  144. return $this->{$name};
  145. }
  146. }