Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/site/app/tests/test_helper_web.php
blob: 68cdb340cef4626a5bbf3274b1276fad47f165dc (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
<?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.
     */
    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>$title</a>.
     */
    function assertLinkLocation($href, $title) {
        $pattern = '#<a [^>]*href="[^"]*'.$href.'"[^>]*>'.$title.'</a>#';
        $this->assertWantedPattern($pattern, htmlentities($pattern));
    }
}

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;
    }
}
?>