~ubuntu-branches/ubuntu/maverick/gallery2/maverick

« back to all changes in this revision

Viewing changes to modules/webdav/test/phpunit/WebDavModuleTest.class

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2007-09-10 20:22:19 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070910202219-0jsuntvqge4ade6b
Tags: 2.2.3-2
Add Slovak translation of Debconf templates.  (Thanks to 
Ivan Masá.  Closes: #441671)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Gallery - a web based photo album viewer and editor
4
 
 * Copyright (C) 2000-2007 Bharat Mediratta
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or (at
9
 
 * your option) any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful, but
12
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
 * General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
19
 
 */
20
 
 
21
 
/**
22
 
 * WebDAV module tests.
23
 
 * @package WebDav
24
 
 * @subpackage PHPUnit
25
 
 * @author Jack Bates <ms419@freezone.co.uk>
26
 
 * @version $Revision: 15723 $
27
 
 */
28
 
class WebDavModuleTest extends GalleryTestCase {
29
 
 
30
 
    function WebDavModuleTest($methodName) {
31
 
        $this->GalleryTestCase($methodName);
32
 
    }
33
 
 
34
 
    function setUp() {
35
 
        parent::setUp();
36
 
        global $gallery;
37
 
 
38
 
        /* Initialize test URL generator */
39
 
        $urlGenerator = new GalleryUrlGenerator();
40
 
        $ret = $urlGenerator->init('http://example.com/gallery2/main.php');
41
 
        if ($ret) {
42
 
            print $ret->getAsHtml();
43
 
            return $this->failWithStatus($ret);
44
 
        }
45
 
        $gallery->setUrlGenerator($urlGenerator);
46
 
 
47
 
        /* Create test items */
48
 
        list ($ret, $this->_album) = $this->_createRandomAlbum($this->_getRootId());
49
 
        if ($ret) {
50
 
            print $ret->getAsHtml();
51
 
            return $this->failWithStatus($ret);
52
 
        }
53
 
        $this->_markForCleanup($this->_album);
54
 
 
55
 
        list ($ret, $this->_item) = $this->_createRandomDataItem($this->_album->getId(),
56
 
            'test/file');
57
 
        if ($ret) {
58
 
            print $ret->getAsHtml();
59
 
            return $this->failWithStatus($ret);
60
 
        }
61
 
 
62
 
        list ($ret, $this->_subAlbum) = $this->_createRandomAlbum($this->_album->getId());
63
 
        if ($ret) {
64
 
            print $ret->getAsHtml();
65
 
            return $this->failWithStatus($ret);
66
 
        }
67
 
 
68
 
        /* Load WebDAV module */
69
 
        list ($ret, $this->_module) = GalleryCoreApi::loadPlugin('module', 'webdav');
70
 
        if ($ret) {
71
 
            print $ret->getAsHtml();
72
 
            return $this->failWithStatus($ret);
73
 
        }
74
 
 
75
 
        /* Mock disable WebDAV rewrite rules */
76
 
        $this->_activeRules =& WebDavModuleTestRewriteApi::_fetchActiveRulesForModule('webdav');
77
 
        $this->_activeRules = array();
78
 
        $this->_activateCalls =& WebDavModuleTestRewriteApi::_getActivateCalls();
79
 
        $this->_markFactoryForCleanup();
80
 
        $this->_registerFactoryImplementationForTest(
81
 
            'RewriteApi', 'WebDavModuleTestRewriteApi', 'WebDavModuleTestRewriteApi',
82
 
            'modules/webdav/test/phpunit/WebDavModuleTest.class', 'test');
83
 
        if ($ret) {
84
 
            print $ret->getAsHtml();
85
 
            return $this->failWithStatus($ret);
86
 
        }
87
 
 
88
 
        $this->_registerFactoryImplementationForTest(
89
 
            'HttpAuthInterface_1_0', 'WebDavModuleTestHttpAuthInterface',
90
 
            'WebDavModuleTestHttpAuthInterface',
91
 
            'modules/webdav/test/phpunit/WebDavModuleTest.class', 'test');
92
 
        WebDavModuleTestHttpAuthInterface::_requestAuthenticationCalls('reset');
93
 
 
94
 
        /* Define OPTIONS request */
95
 
        $this->_optionsRequest = implode("\r\n", array(
96
 
            'OPTIONS ' . $urlGenerator->generateUrl(
97
 
                array('controller' => 'webdav.WebDav'),
98
 
                array('forceServerRelativeUrl' => true,
99
 
                      'htmlEntities' => false)) . ' HTTP/1.0',
100
 
            'Host: ' . $urlGenerator->getHostName(),
101
 
            '',
102
 
            ''));
103
 
 
104
 
        /* Define success and failure responses */
105
 
        $this->_includesDavHeadersResponse = implode("\r\n", array(
106
 
            'HTTP/1.1 200 OK',
107
 
            'Date: Mon, 24 Nov 2003 05:40:03 GMT',
108
 
            'Server: Apache/1.3.28 (Unix)',
109
 
            'Allow: OPTIONS,PROPFIND,PROPPATCH,MKCOL,GET,HEAD,DELETE,PUT,MOVE,LOCK,UNLOCK',
110
 
            'DAV: 1,2',
111
 
            'MS-Author-Via: DAV',
112
 
            'Content-Length: 0',
113
 
            'Connection: close',
114
 
            'Content-Type: text/html',
115
 
            '',
116
 
            ''));
117
 
        $this->_missingDavHeadersResponse = implode("\r\n", array(
118
 
            'HTTP/1.1 200 OK',
119
 
            'Date: Mon, 24 Nov 2003 05:40:03 GMT',
120
 
            'Server: Apache/1.3.28 (Unix)',
121
 
            'Allow: OPTIONS,GET,HEAD,POST',
122
 
            'Content-Length: 0',
123
 
            'Connection: close',
124
 
            'Content-Type: text/html',
125
 
            '',
126
 
            ''));
127
 
 
128
 
        /* Use WebDavModuleTestPlatform */
129
 
        $this->_platform = new WebDavModuleTestPlatform();
130
 
        $this->_platform->_messages[$this->_optionsRequest] =
131
 
            $this->_includesDavHeadersResponse;
132
 
        $gallery->setPlatform($this->_platform);
133
 
 
134
 
        $this->_registerTestEventListener('Gallery::ActivatePlugin', $this->_module);
135
 
    }
136
 
 
137
 
    function tearDown() {
138
 
        GalleryDataCache::reset();
139
 
        parent::tearDown();
140
 
    }
141
 
 
142
 
    function testGetItemLinks() {
143
 
        global $gallery;
144
 
 
145
 
        $items = array($this->_item, $this->_subAlbum);
146
 
        $wantsDetailedLinks =
147
 
            array($this->_item->getId() => true, $this->_subAlbum->getId() => true);
148
 
        list ($ret, $permissions) = GalleryCoreApi::fetchPermissionsForItems(
149
 
            array($this->_item->getId(), $this->_subAlbum->getId()));
150
 
        if ($ret) {
151
 
            return $this->failWithStatus($ret);
152
 
        }
153
 
 
154
 
        /* Any user agent */
155
 
        $_SERVER['HTTP_USER_AGENT'] = 'Foo Bar';
156
 
 
157
 
        list ($ret, $links) = $this->_module->getItemLinks(
158
 
            $items, $wantsDetailedLinks, $permissions, $gallery->getActiveUserId());
159
 
        if ($ret) {
160
 
            return $this->failWithStatus($ret);
161
 
        }
162
 
 
163
 
        $expected = array();
164
 
        $expected[$this->_subAlbum->getId()][0]['text'] =
165
 
            $this->_module->translate(array('text' => 'Mount with WebDAV'));
166
 
        $expected[$this->_subAlbum->getId()][0]['params'] =
167
 
            array('view' => 'webdav.WebDavMount', 'itemId' => $this->_subAlbum->getId());
168
 
        $this->assertEquals($expected, $links, 'item links for summary');
169
 
 
170
 
        /* Verify that item links are only for detailed mode */
171
 
        $wantsDetailedLinks[$this->_subAlbum->getId()] = false;
172
 
        list ($ret, $links) = $this->_module->getItemLinks(
173
 
            $items, $wantsDetailedLinks, $permissions, $gallery->getActiveUserId());
174
 
        if ($ret) {
175
 
            return $this->failWithStatus($ret);
176
 
        }
177
 
 
178
 
        $this->assertEquals(array(), $links, 'must be a request for detailed links');
179
 
    }
180
 
 
181
 
    function testGetRewriteRules() {
182
 
        $rules = $this->_module->getRewriteRules();
183
 
        $this->assertEquals(array('connect', 'davmount'), array_keys($rules));
184
 
    }
185
 
 
186
 
    function testGetRewriteRulesOptionsRuleActive() {
187
 
        $this->_activeRules = array('options');
188
 
 
189
 
        $rules = $this->_module->getRewriteRules();
190
 
        $this->assertEquals(array('connect', 'davmount', 'options'), array_keys($rules));
191
 
    }
192
 
 
193
 
    function testGetRewriteRulesMissingDavHeaders() {
194
 
        $this->_platform->_messages[$this->_optionsRequest] =
195
 
            $this->_missingDavHeadersResponse;
196
 
 
197
 
        $rules = $this->_module->getRewriteRules();
198
 
        $this->assertEquals(array('connect', 'davmount', 'options'), array_keys($rules));
199
 
    }
200
 
 
201
 
    function testHandleEventActivatePlugin() {
202
 
        $event = GalleryCoreApi::newEvent('Gallery::ActivatePlugin');
203
 
        $event->setData(array('pluginType' => 'module',
204
 
                              'pluginId' => 'rewrite'));
205
 
        list ($ret, $results) = GalleryCoreApi::postEvent($event);
206
 
        if ($ret) {
207
 
            return $this->failWithStatus($ret);
208
 
        }
209
 
 
210
 
        $this->assertEquals(array(array('webdav')), $this->_activateCalls);
211
 
    }
212
 
 
213
 
    function testHandleEventError() {
214
 
        GalleryUtilities::removeRequestVariable('controller');
215
 
        GalleryUtilities::putRequestVariable('view', 'webdav.WebDav');
216
 
        $this->_registerTestEventListener('Gallery::Error', $this->_module);
217
 
 
218
 
        $error = GalleryCoreApi::error(GALLERY_ERROR);
219
 
        $event = GalleryCoreApi::newEvent('Gallery::Error');
220
 
        $event->setData(array('error' => $error));
221
 
        list ($ret, $results) = GalleryCoreApi::postEvent($event);
222
 
        if ($ret) {
223
 
            return $this->failWithStatus($ret);
224
 
        }
225
 
 
226
 
        $this->assertEquals(array(array('suppressBody' => 1)), $results);
227
 
        $this->assertEquals(0,
228
 
                WebDavModuleTestHttpAuthInterface::_requestAuthenticationCalls('read'),
229
 
                'httpauth should only be called for ERROR_PERMISSION_DENIED');
230
 
    }
231
 
 
232
 
    function testHandleEventErrorPermissionDenied() {
233
 
        GalleryUtilities::removeRequestVariable('controller');
234
 
        GalleryUtilities::putRequestVariable('view', 'webdav.WebDav');
235
 
        $this->_registerTestEventListener('Gallery::Error', $this->_module);
236
 
 
237
 
        $error = GalleryCoreApi::error(ERROR_PERMISSION_DENIED);
238
 
        $event = GalleryCoreApi::newEvent('Gallery::Error');
239
 
        $event->setData(array('error' => $error));
240
 
        list ($ret, $results) = GalleryCoreApi::postEvent($event);
241
 
        if ($ret) {
242
 
            return $this->failWithStatus($ret);
243
 
        }
244
 
 
245
 
        $this->assertEquals(array(array('suppressBody' => 1)), $results);
246
 
        $this->assertEquals(1,
247
 
                WebDavModuleTestHttpAuthInterface::_requestAuthenticationCalls('read'),
248
 
                'httpauth has not been called');
249
 
    }
250
 
}
251
 
 
252
 
class WebDavModuleTestRewriteApi {
253
 
    var $foo;
254
 
 
255
 
    function &_fetchActiveRulesForModule($moduleId) {
256
 
        static $activeRules;
257
 
        return $activeRules;
258
 
    }
259
 
 
260
 
    function &_getActivateCalls() {
261
 
        static $activateCalls;
262
 
        return $activateCalls;
263
 
    }
264
 
 
265
 
    function isCompatibleWithApi($version) {
266
 
        return array(null, true);
267
 
    }
268
 
 
269
 
    function fetchActiveRulesForModule($moduleId) {
270
 
        return array(null, WebDavModuleTestRewriteApi::_fetchActiveRulesForModule($moduleId));
271
 
    }
272
 
 
273
 
    function activateRewriteRulesForModule($moduleId) {
274
 
        $activateCalls =& WebDavModuleTestRewriteApi::_getActivateCalls();
275
 
        $activateCalls[] = array($moduleId);
276
 
    }
277
 
}
278
 
 
279
 
class WebDavModuleTestPlatform extends GalleryPlatform {
280
 
    var $_messages;
281
 
    var $_buffer;
282
 
 
283
 
    function fsockopen($target, $port, &$errno, &$errstr, $timeout) {
284
 
        return 'test';
285
 
    }
286
 
 
287
 
    function feof($handle) {
288
 
        return empty($this->_buffer);
289
 
    }
290
 
 
291
 
    function fgets($handle, $length) {
292
 
        if (empty($this->_buffer)) {
293
 
            return null;
294
 
        }
295
 
 
296
 
        if (strpos($this->_buffer, "\n") < $length) {
297
 
            $length = strpos($this->_buffer, "\n") + 1;
298
 
        }
299
 
 
300
 
        return $this->fread($handle, $length);
301
 
    }
302
 
 
303
 
    function fread($handle, $length) {
304
 
        if (empty($this->_buffer)) {
305
 
            return null;
306
 
        }
307
 
 
308
 
        $buffer = substr($this->_buffer, 0, $length);
309
 
        $this->_buffer = substr($this->_buffer, $length);
310
 
        return $buffer;
311
 
    }
312
 
 
313
 
    function fwrite($handle, $string, $length=0) {
314
 
        if (isset($this->_messages[$string])) {
315
 
            $this->_buffer = $this->_messages[$string];
316
 
            return strlen($string);
317
 
        }
318
 
    }
319
 
 
320
 
    function fflush($handle) {
321
 
        return true;
322
 
    }
323
 
 
324
 
    function fclose($handle) {
325
 
        return true;
326
 
    }
327
 
}
328
 
 
329
 
class WebDavModuleTestHttpAuthInterface /* extends HttpAuthInterface_1_0 */ {
330
 
    function _requestAuthenticationCalls($mode=false) {
331
 
        static $calls = 0;
332
 
        if ($mode == 'reset') {
333
 
            $calls = 0;
334
 
        } else if ($mode == 'read') {
335
 
            return $calls;
336
 
        } else {
337
 
            $calls++;
338
 
        }
339
 
    }
340
 
 
341
 
    function requestAuthentication() {
342
 
        WebDavModuleTestHttpAuthInterface::_requestAuthenticationCalls();
343
 
        return null;
344
 
    }
345
 
}
346
 
?>