Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/site/app/tests/test_helper_web.php
blob: 81ec63f3d0df33d8648f5be646672d87848de95f (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php

class WebTestHelper extends WebTestCase {

    function before($method) {
        // The test browser is created in parent::before.
        parent::before($method);
        $this->addHeader('X-Amo-Test: damn right');
    }

    /* Compute protocol and hostname prefix, no trailing slash. */
    function hostPrefix() {
        $http = (!empty($_SERVER["HTTP_MOZ_REQ_METHOD"]) && $_SERVER["HTTP_MOZ_REQ_METHOD"] == 'HTTPS') ? 'https://' : 'http://';
        $uriBase = $http . $_SERVER['HTTP_HOST'];
        return $uriBase;
    }

    /**
     * The default SimpleBrowser tries to parse all responses, even when
     * they're not HTML.  That fails.  We need a better browser.
     *
     *    Creates a new default web browser object.
     *    Will be cleared at the end of the test method.
     */
    function &createBrowser() {
        $browser =& new BetterBrowser();
        return $browser;
    }

    /* Compute the URI for the given action, accounting for us possibly not
     * being at the root of the web space.
     */
    function actionURI($action) {
        /**
            If HTTP_MOZ_REQ_METHOD indicates this was requested via https://,
            use that, otherwise default to http:// 
        */
        return $this->hostPrefix() . $this->actionPath($action);
    }

    /* As above, but just the local path and not a complete URI. */
    function actionPath($action) {
        return preg_replace('/\/tests.*/', $action, setUri());
    }
    
    /* Return a URI computed from the installation base, without locale or app. */
    
    function rawURI($path) {
        return $this->hostPrefix() . $this->rawPath($path);
    }
    
    /* As above, but just the local path and not a complete URI. */
    function rawPath($path) {
        return preg_replace('/\/' . LANG . '\/' . APP_SHORTNAME . '\/tests.*/', $path, setUri());
    }

    /* Make a GET for the given action, accounting for us possibly not being at
     * the root of the web space.
     */
    function getAction($action) {
        $this->get($this->actionURI($action));
    }
    
    /* GET a fully-specified local URI path (needs to include site prefix if any). */
    function getPath($path) {
        $this->get($this->hostPrefix() . $path);
    }
    
   /**
    * Logs in with test account info.
    */
    function login() {
        $username = 'nobody@mozilla.org';
        $password = 'test';
        
        $path = $this->actionURI('/users/login');
        $data = array(
                    'data[Login][email]' => $username,
                    'data[Login][password]' => $password
                );
        
        $this->post($path, $data);
        $this->assertNoUnwantedText(_('error_username_or_pw_wrong'), 'Logged in with test account');        
    }

    /**
     * Check if the retrieved XML document is well-formed/trivially parsable
     * (no DTD validity for now)
     */
    function checkXML() {
        $browser = $this->getBrowser();
        $data = $browser->getContent();
        $xmlparser = xml_parser_create();
        return (xml_parse($xmlparser, $data, true) == 1);;
    }

    /**
     * Check that there is a link like <a href=$href>$text</a>.
     */
    function assertLinkLocation($href, $text) {
        phpQuery::newDocument($this->_browser->getContent());
        $q = pq("a[href$={$href}");
        $msg = htmlentities("<a href='{$href}'>{$text}</a> exists");
        $this->assertEqual($q->text(), $text, $msg);
    }

    /**
     * Check that the element $q has the attributes in the $attrs array.
     * $q can be a phpquery object or a selector string.
     */
    function assertAttrs($q, $attrs, $msg) {
        if (is_string($q)) {
            phpQuery::newDocument($this->_browser->getContent());
            $q = pq($q);
        }
        foreach ($attrs as $attr => $val) {
            $this->assertEqual($q->attr($attr), $val, $msg);
        }
    }

    /* Helper to normalize whitespace in a string. */
    function _norm($x) {
        return implode(' ', preg_split('/\s+/', trim($x)));
    }

    /* Assert that the strings have the same content, ignoring whitespace. */
    function assertEquiv($a, $b, $msg=null) {
        $this->assertEqual($this->_norm($a), $this->_norm($b));
    }

    function delete($url, $parameters = false) {
        return $this->_failOnError($this->_browser->delete($url, $parameters));
    }
    
    function put($url, $parameters = false) {
        return $this->_failOnError($this->_browser->put($url, $parameters));
    }
    
    function postMultipart($url, $parameters = false, $files = false) {
        return $this->_failOnError($this->_browser->postMultipart($url, $parameters, $files));
    }
}

class BetterBrowser extends SimpleBrowser {

    /**
     * Overrides _buildPage to only parse responses when the Content Type
     * looks like HTML.
     */
    function &_buildPage($response) {
        $headers = $response->getHeaders()->getRaw();
        if (preg_match('#^Content-Type: (text/html|application/xhtml+xml)#m', $headers)) {
            $page =& parent::_buildPage($response);
        } else {
            $page =& new SimplePage($response);
        }
        return $page;
    }

    /* Define $this->_browser->skipParse to avoid the HTML parsing overhead. */
    function &_fetch($url, $encoding, $depth=0) {
        if (isset($this->skipParse)) {
            $response = &$this->_user_agent->fetchResponse($url, $encoding);
            $page = new SimplePage($response);
            return $page;
        } else {
            return parent::_fetch($url, $encoding, $depth);
        }
    }

    function delete($url, $parameters = false) {
        if (! is_object($url)) {
            $url = new SimpleUrl($url);
        }
        if ($this->getUrl()) {
            $url = $url->makeAbsolute($this->getUrl());
        }
        return $this->_load($url, new SimpleDeleteEncoding($parameters));
    }

    function put($url, $parameters = false) {
        if (! is_object($url)) {
            $url = new SimpleUrl($url);
        }
        if ($this->getUrl()) {
            $url = $url->makeAbsolute($this->getUrl());
        }
        return $this->_load($url, new SimplePutEncoding($parameters));
    }

    /**
     * Multipart POST request. May contain file attachments.
     * @param string $url target URL
     * @param array $parameters array of POST parameters
     * @param array $files further POST parameters of type key => path to file to attach
     */
    function postMultipart($url, $parameters = false, $files = false) {
        if (! is_object($url)) {
            $url = new SimpleUrl($url);
        }
        if ($this->getUrl()) {
            $url = $url->makeAbsolute($this->getUrl());
        }
        // prepare multipart encoding
        $encoding = new SimpleMultipartEncoding($parameters);
        // attach files if needed
        if (!empty($files) && is_array($files)) {
            foreach($files as $k => &$f) {
                if (! file_exists($f)) continue;
                $encoding->attach(
                    $k,
                    implode('', file($f)),
                    basename($f));
            }
        }

        return $this->_load($url, $encoding);
    }
}

/**
 * Bundle of URL parameters for a DELETE request.
 */
class SimpleDeleteEncoding extends SimpleGetEncoding {
    function SimpleDeleteEncoding($query = false) {
        $this->SimpleGetEncoding($query);
    }
    function getMethod() {
        return 'DELETE';
    }
}

/**
 * Bundle of URL parameters for a PUT request.
 */
class SimplePutEncoding extends SimplePostEncoding {
    function SimplePutEncoding($query = false) {
        $this->SimplePostEncoding($query);
    }
    function getMethod() {
        return 'PUT';
    }
}
?>