3
* Gallery - a web based photo album viewer and editor
4
* Copyright (C) 2000-2007 Bharat Mediratta
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.
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.
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.
22
* WebDAV module tests.
25
* @author Jack Bates <ms419@freezone.co.uk>
26
* @version $Revision: 15723 $
28
class WebDavModuleTest extends GalleryTestCase {
30
function WebDavModuleTest($methodName) {
31
$this->GalleryTestCase($methodName);
38
/* Initialize test URL generator */
39
$urlGenerator = new GalleryUrlGenerator();
40
$ret = $urlGenerator->init('http://example.com/gallery2/main.php');
42
print $ret->getAsHtml();
43
return $this->failWithStatus($ret);
45
$gallery->setUrlGenerator($urlGenerator);
47
/* Create test items */
48
list ($ret, $this->_album) = $this->_createRandomAlbum($this->_getRootId());
50
print $ret->getAsHtml();
51
return $this->failWithStatus($ret);
53
$this->_markForCleanup($this->_album);
55
list ($ret, $this->_item) = $this->_createRandomDataItem($this->_album->getId(),
58
print $ret->getAsHtml();
59
return $this->failWithStatus($ret);
62
list ($ret, $this->_subAlbum) = $this->_createRandomAlbum($this->_album->getId());
64
print $ret->getAsHtml();
65
return $this->failWithStatus($ret);
68
/* Load WebDAV module */
69
list ($ret, $this->_module) = GalleryCoreApi::loadPlugin('module', 'webdav');
71
print $ret->getAsHtml();
72
return $this->failWithStatus($ret);
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');
84
print $ret->getAsHtml();
85
return $this->failWithStatus($ret);
88
$this->_registerFactoryImplementationForTest(
89
'HttpAuthInterface_1_0', 'WebDavModuleTestHttpAuthInterface',
90
'WebDavModuleTestHttpAuthInterface',
91
'modules/webdav/test/phpunit/WebDavModuleTest.class', 'test');
92
WebDavModuleTestHttpAuthInterface::_requestAuthenticationCalls('reset');
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(),
104
/* Define success and failure responses */
105
$this->_includesDavHeadersResponse = implode("\r\n", array(
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',
111
'MS-Author-Via: DAV',
114
'Content-Type: text/html',
117
$this->_missingDavHeadersResponse = implode("\r\n", array(
119
'Date: Mon, 24 Nov 2003 05:40:03 GMT',
120
'Server: Apache/1.3.28 (Unix)',
121
'Allow: OPTIONS,GET,HEAD,POST',
124
'Content-Type: text/html',
128
/* Use WebDavModuleTestPlatform */
129
$this->_platform = new WebDavModuleTestPlatform();
130
$this->_platform->_messages[$this->_optionsRequest] =
131
$this->_includesDavHeadersResponse;
132
$gallery->setPlatform($this->_platform);
134
$this->_registerTestEventListener('Gallery::ActivatePlugin', $this->_module);
137
function tearDown() {
138
GalleryDataCache::reset();
142
function testGetItemLinks() {
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()));
151
return $this->failWithStatus($ret);
155
$_SERVER['HTTP_USER_AGENT'] = 'Foo Bar';
157
list ($ret, $links) = $this->_module->getItemLinks(
158
$items, $wantsDetailedLinks, $permissions, $gallery->getActiveUserId());
160
return $this->failWithStatus($ret);
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');
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());
175
return $this->failWithStatus($ret);
178
$this->assertEquals(array(), $links, 'must be a request for detailed links');
181
function testGetRewriteRules() {
182
$rules = $this->_module->getRewriteRules();
183
$this->assertEquals(array('connect', 'davmount'), array_keys($rules));
186
function testGetRewriteRulesOptionsRuleActive() {
187
$this->_activeRules = array('options');
189
$rules = $this->_module->getRewriteRules();
190
$this->assertEquals(array('connect', 'davmount', 'options'), array_keys($rules));
193
function testGetRewriteRulesMissingDavHeaders() {
194
$this->_platform->_messages[$this->_optionsRequest] =
195
$this->_missingDavHeadersResponse;
197
$rules = $this->_module->getRewriteRules();
198
$this->assertEquals(array('connect', 'davmount', 'options'), array_keys($rules));
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);
207
return $this->failWithStatus($ret);
210
$this->assertEquals(array(array('webdav')), $this->_activateCalls);
213
function testHandleEventError() {
214
GalleryUtilities::removeRequestVariable('controller');
215
GalleryUtilities::putRequestVariable('view', 'webdav.WebDav');
216
$this->_registerTestEventListener('Gallery::Error', $this->_module);
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);
223
return $this->failWithStatus($ret);
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');
232
function testHandleEventErrorPermissionDenied() {
233
GalleryUtilities::removeRequestVariable('controller');
234
GalleryUtilities::putRequestVariable('view', 'webdav.WebDav');
235
$this->_registerTestEventListener('Gallery::Error', $this->_module);
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);
242
return $this->failWithStatus($ret);
245
$this->assertEquals(array(array('suppressBody' => 1)), $results);
246
$this->assertEquals(1,
247
WebDavModuleTestHttpAuthInterface::_requestAuthenticationCalls('read'),
248
'httpauth has not been called');
252
class WebDavModuleTestRewriteApi {
255
function &_fetchActiveRulesForModule($moduleId) {
260
function &_getActivateCalls() {
261
static $activateCalls;
262
return $activateCalls;
265
function isCompatibleWithApi($version) {
266
return array(null, true);
269
function fetchActiveRulesForModule($moduleId) {
270
return array(null, WebDavModuleTestRewriteApi::_fetchActiveRulesForModule($moduleId));
273
function activateRewriteRulesForModule($moduleId) {
274
$activateCalls =& WebDavModuleTestRewriteApi::_getActivateCalls();
275
$activateCalls[] = array($moduleId);
279
class WebDavModuleTestPlatform extends GalleryPlatform {
283
function fsockopen($target, $port, &$errno, &$errstr, $timeout) {
287
function feof($handle) {
288
return empty($this->_buffer);
291
function fgets($handle, $length) {
292
if (empty($this->_buffer)) {
296
if (strpos($this->_buffer, "\n") < $length) {
297
$length = strpos($this->_buffer, "\n") + 1;
300
return $this->fread($handle, $length);
303
function fread($handle, $length) {
304
if (empty($this->_buffer)) {
308
$buffer = substr($this->_buffer, 0, $length);
309
$this->_buffer = substr($this->_buffer, $length);
313
function fwrite($handle, $string, $length=0) {
314
if (isset($this->_messages[$string])) {
315
$this->_buffer = $this->_messages[$string];
316
return strlen($string);
320
function fflush($handle) {
324
function fclose($handle) {
329
class WebDavModuleTestHttpAuthInterface /* extends HttpAuthInterface_1_0 */ {
330
function _requestAuthenticationCalls($mode=false) {
332
if ($mode == 'reset') {
334
} else if ($mode == 'read') {
341
function requestAuthentication() {
342
WebDavModuleTestHttpAuthInterface::_requestAuthenticationCalls();