aboutsummaryrefslogtreecommitdiff
path: root/tests/TestBaseApi.php
blob: 50a2413361569702942ef2fe44f9be6924b5a484 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/**
 * SemanticScuttle - your social bookmark manager.
 *
 * PHP version 5.
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */

require_once 'HTTP/Request2.php';

/**
 * Base unittest class for web API tests.
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author Christian Weiske <cweiske@cweiske.de>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */
class TestBaseApi extends TestBase
{
    /**
     * Created from the configured host and the $urlPart.
     * Should be used as base for all generated URLs
     *
     * @var string
     */
    protected $url;

    /**
     * Part of the URL behind the configured host.
     * Needs to be overwritten in each derived test case class.
     *
     * @var string
     */
    protected $urlPart = null;

    /**
     * @var SemanticScuttle_Service_User
     */
    protected $us;

    /**
     * @var SemanticScuttle_Service_Bookmark
     */
    protected $bs;



    protected function setUp()
    {
        if ($GLOBALS['unittestUrl'] === null) {
            $this->assertTrue(false, 'Unittest URL not set in config');
        }
        if ($this->urlPart === null) {
            $this->assertTrue(false, 'Set the urlPart variable');
        }
        $this->url = $GLOBALS['unittestUrl'] . $this->urlPart;

        //clean up before test
        $configFile = $GLOBALS['datadir'] . '/config.testing-tmp.php';
        if (file_exists($configFile)) {
            unlink($configFile);
        }

        $this->us = SemanticScuttle_Service_Factory::get('User');
        $this->us->deleteAll();
        $this->bs = SemanticScuttle_Service_Factory::get('Bookmark');
        $this->bs->deleteAll();
        $this->b2t = SemanticScuttle_Service_Factory::get('Bookmark2Tag');
        $this->b2t->deleteAll();
    }



    /**
     * Creates and returns a HTTP GET request object.
     * Uses $this->url plus $urlSuffix as request URL.
     *
     * @param string $urlSuffix Suffix for the URL
     *
     * @return HTTP_Request2 HTTP request object
     */
    protected function getRequest($urlSuffix = null)
    {
        $url = $this->getTestUrl($urlSuffix);
        $req = new HTTP_Request2($url, HTTP_Request2::METHOD_GET);

        return $req;
    }

    /**
     * Creates an URL from $this->url plus $urlSuffix and an appended
     * unittestMode=1 parameter.
     *
     * @param string $urlSuffix Suffix for the URL
     *
     * @return string URL
     *
     * @uses $url
     */
    protected function getTestUrl($urlSuffix = null)
    {
        $url = $this->url . $urlSuffix;
        if (strpos($urlSuffix, '?') !== false) {
            $url .= '&unittestMode=1';
        } else {
            $url .= '?unittestMode=1';
        }
        return $url;
    }


    /**
     * Completes an URL that's missing the protocol.
     * Useful when re-using URLs extracted from HTML
     *
     * @param string $url Potentially partial URL
     *
     * @return string Full URL
     */
    protected function completeUrl($url)
    {
        if (substr($url, 0, 2) == '//') {
            $url = 'http:' . $url;
        }
        return $url;
    }



    /**
     * Creates a user and a HTTP GET request object and prepares
     * the request object with authentication details, so that
     * the user is logged in.
     *
     * Useful for HTTP API methods only, cannot be used with
     * "normal" HTML pages since they do not support HTTP auth.
     *
     * @param string $urlSuffix Suffix for the URL
     * @param mixed  $auth      If user authentication is needed (true/false)
     *                          or array with username and password
     *
     * @return array(HTTP_Request2, integer) HTTP request object and user id
     *
     * @uses getRequest()
     * @see getLoggedInRequest()
     */
    protected function getAuthRequest($urlSuffix = null, $auth = true)
    {
        $req = $this->getRequest($urlSuffix);
        if (is_array($auth)) {
            list($username, $password) = $auth;
        } else {
            $username = 'testuser';
            $password = 'testpassword';
        }
        $uid = $this->addUser($username, $password);
        $req->setAuth(
            $username, $password,
            HTTP_Request2::AUTH_BASIC
        );
        return array($req, $uid);
    }



    /**
     * Creates a user and a HTTP_Request2 object, does a normal login
     * and prepares the cookies for the HTTP GET request object so that
     * the user is seen as logged in when requesting any HTML page.
     *
     * Useful for testing HTML pages or ajax URLs.
     *
     * @param string  $urlSuffix  Suffix for the URL
     * @param mixed   $auth       If user authentication is needed (true/false)
     *                            or array with username and password
     * @param boolean $privateKey True if to add user with private key
     *
     * @return array(HTTP_Request2, integer) HTTP request object and user id
     *
     * @uses getRequest()
     */
    protected function getLoggedInRequest(
        $urlSuffix = null, $auth = true, $privateKey = false
    ) {
        if (is_array($auth)) {
            list($username, $password) = $auth;
        } else {
            $username = 'testuser';
            $password = 'testpassword';
        }
        //include privatekey if requested
        if ($privateKey) {
            $pKey = $this->us->getNewPrivateKey();
        } else {
            $pKey = null;
        }
        $uid = $this->addUser($username, $password, $pKey);

        $req = new HTTP_Request2(
            $GLOBALS['unittestUrl'] . '/login.php?unittestMode=1',
            HTTP_Request2::METHOD_POST
        );
        $cookies = $req->setCookieJar()->getCookieJar();
        $req->addPostParameter('username', $username);
        $req->addPostParameter('password', $password);
        $req->addPostParameter('submitted', 'Log In');
        $res = $req->send();

        //after login, we normally get redirected
        $this->assertEquals(302, $res->getStatus(), 'Login failure');

        $req = $this->getRequest($urlSuffix);
        $req->setCookieJar($cookies);

        return array($req, $uid);
    }



    /**
     * Verifies that the HTTP response has status code 200 and
     * content-type application/json; charset=utf-8
     *
     * @param HTTP_Request2_Response $res HTTP Response object
     *
     * @return void
     */
    protected function assertResponseJson200(HTTP_Request2_Response $res)
    {
        $this->assertEquals(200, $res->getStatus());
        $this->assertEquals(
            'application/json; charset=utf-8',
            $res->getHeader('content-type')
        );
    }



    /**
     * Writes a special unittest configuration file.
     * The unittest config file is read when a GET request with unittestMode=1
     * is sent, and the user allowed unittestmode in config.php.
     *
     * @param array $arConfig Array with config names as key and their value as
     *                        value
     *
     * @return void
     */
    protected function setUnittestConfig($arConfig)
    {
        $str = '<' . "?php\n";
        foreach ($arConfig as $name => $value) {
            $str .= '$' . $name . ' = '
                . var_export($value, true) . ";\n";
        }

        if (!is_dir($GLOBALS['datadir'])) {
            $this->fail(
                'datadir not set or not a directory: ' . $GLOBALS['datadir']
            );
        }

        $this->assertInternalType(
            'integer',
            file_put_contents($GLOBALS['datadir'] . '/config.testing-tmp.php', $str),
            'Writing config.unittest.php failed'
        );
    }
}
?>