Welcome to mirror list, hosted at ThFree Co, Russian Federation.

OutputJsonMock.php « tests - github.com/roundcube/roundcubemail.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ce19420f3da2be68a2e78f5512c8823fa7ca212e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php

/**
 +-----------------------------------------------------------------------+
 | This file is part of the Roundcube Webmail client                     |
 |                                                                       |
 | Copyright (C) The Roundcube Dev Team                                  |
 |                                                                       |
 | Licensed under the GNU General Public License version 3 or            |
 | any later version with exceptions for skins & plugins.                |
 | See the README file for a full license statement.                     |
 |                                                                       |
 | PURPOSE:                                                              |
 |   A class for easier testing of code that uses rcmail_output classes  |
 +-----------------------------------------------------------------------+
 | Author: Aleksander Machniak <alec@alec.pl>                            |
 +-----------------------------------------------------------------------+
*/

/**
 * A class for easier testing of code that uses rcmail_output classes
 *
 * @package Tests
 */
class OutputJsonMock extends rcmail_output_json
{
    const E_EXIT     = 101;
    const E_REDIRECT = 102;

    public $output;
    public $headers = [];

    /**
     * Redirect to a certain url
     *
     * @param mixed $p     Either a string with the action or url parameters as key-value pairs
     * @param int   $delay Delay in seconds
     *
     * @see rcmail::url()
     */
    public function redirect($p = [], $delay = 1)
    {
        $location = $this->app->url($p);
        ob_start();
        $this->remote_response(sprintf("window.setTimeout(function(){ %s.redirect('%s',true); }, %d);",
            self::JS_OBJECT_NAME, $location, $delay));
        $this->output = ob_get_contents();
        ob_end_clean();

        throw new ExitException("Location: $location", self::E_REDIRECT);
    }

    /**
     * Send an AJAX response to the client.
     */
    public function send()
    {
        ob_start();
        $this->remote_response();
        $this->output = ob_get_contents();
        ob_end_clean();

        throw new ExitException("Output sent", self::E_EXIT);
    }

    /**
     * A helper to send output to the browser and exit
     *
     * @param string $body    The output body
     * @param array  $headers Headers
     */
    public function sendExit($body = '', $headers = [])
    {
        foreach ($headers as $header) {
            $this->header($header);
        }

        $this->output = $body;

        throw new ExitException("Output sent", self::E_EXIT);
    }

    /**
     * Show error page and terminate script execution
     *
     * @param int    $code    Error code
     * @param string $message Error message
     */
    public function raise_error($code, $message)
    {
        if ($code == 403) {
            throw new ExitException("403 Forbidden", self::E_EXIT);
        }

        $this->show_message("Application Error ($code): $message", 'error');

        ob_start();
        $this->remote_response();
        $this->output = ob_get_contents();
        ob_end_clean();

        throw new ExitException("Error $code raised", self::E_EXIT);
    }

    /**
     * Delete all stored env variables and commands
     */
    public function reset()
    {
        parent::reset();

        $this->headers     = [];
        $this->output      = null;
        $this->header_sent = false;
    }

    /**
     * A wrapper for header() function, so it can be replaced for automated tests
     *
     * @param string $header  The header string
     * @param bool   $replace Replace previously set header?
     */
    public function header($header, $replace = true)
    {
        $this->headers[] = $header;
    }

    /**
     * Return the JSON output as an array
     */
    public function getOutput()
    {
        return $this->output ? json_decode($this->output, true) : null;
    }

    /**
     * Return private/protected property
     */
    public function getProperty($name)
    {
        return $this->{$name};
    }
}