~ubuntu-branches/ubuntu/quantal/gallery2/quantal

« back to all changes in this revision

Viewing changes to modules/rewrite/test/phpunit/RewriteModuleTest.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
 
 * Test RewriteUrlGenerator functionality
23
 
 *
24
 
 * @package Rewrite
25
 
 * @subpackage PHPUnit
26
 
 * @author Douglas Cau <douglas@cau.se>
27
 
 * @version $Revision: 15513 $
28
 
 */
29
 
class RewriteModuleTest extends GalleryTestCase {
30
 
    var $_module;
31
 
    var $_activeRules;
32
 
 
33
 
    function RewriteModuleTest($methodName) {
34
 
        $this->GalleryTestCase($methodName);
35
 
    }
36
 
 
37
 
    function setUp() {
38
 
        global $gallery;
39
 
        parent::setUp();
40
 
 
41
 
        /* Enable event listener (normally disabled for unit tests) */
42
 
        $listener = new RewriteModule();
43
 
        $this->_registerTestEventListener('Gallery::DeactivatePlugin', $listener);
44
 
        $this->_registerTestEventListener('Gallery::ActivatePlugin', $listener);
45
 
        $this->_registerTestEventListener('Gallery::UninstallPlugin', $listener);
46
 
 
47
 
        $this->_platform = new RewriteModuleMockPlatform();
48
 
        $gallery->setPlatform($this->_platform);
49
 
 
50
 
        $ret = $this->_markPluginParametersForCleanup('module', 'rewrite');
51
 
        if ($ret) {
52
 
                print $ret->getAsHtml();
53
 
                return $this->failWithStatus($ret);
54
 
        }
55
 
 
56
 
        /* Set up our environment */
57
 
        foreach (array('parserId' => 'rewritemodulemock',
58
 
                       'activeRules' => serialize(array()))
59
 
                as $key => $value) {
60
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'rewrite', $key, $value);
61
 
            if ($ret) {
62
 
                print $ret->getAsHtml();
63
 
                return $this->failWithStatus($ret);
64
 
            }
65
 
        }
66
 
 
67
 
        /* Make sure we use the mock parser */
68
 
        GalleryCoreApi::requireOnce('modules/rewrite/classes/RewriteHelper.class');
69
 
        list ($ret, $parser) = RewriteHelper::getRewriteParser(true);
70
 
        if ($ret) {
71
 
            print $ret->getAsHtml();
72
 
            return $this->failWithStatus($ret);
73
 
        }
74
 
        $this->assertEquals('rewritemodulemock', $parser->getParserId(), 'parserId');
75
 
 
76
 
        /* Register a new module */
77
 
        GalleryDataCache::reset();
78
 
        list ($ret, $this->_module) = GalleryCoreApi::loadPlugin('module', 'rewritemodulemock');
79
 
        if ($ret) {
80
 
            print $ret->getAsHtml();
81
 
            return $this->failWithStatus($ret);
82
 
        }
83
 
 
84
 
        /* Install it */
85
 
        $ret = $this->_module->installOrUpgrade();
86
 
        if ($ret) {
87
 
            print $ret->getAsHtml();
88
 
            return $this->failWithStatus($ret);
89
 
        }
90
 
 
91
 
        /* Use empty table by default */
92
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'rewrite', 'activeRules',
93
 
            serialize(array()));
94
 
        if ($ret) {
95
 
            print $ret->getAsHtml();
96
 
            return $this->failWithStatus($ret);
97
 
        }
98
 
 
99
 
        /* Dummy active rules */
100
 
        $this->_activeRules['rewritemodulemock'][0] = 'phpunit/dummy/%itemId%.html';
101
 
    }
102
 
 
103
 
    function tearDown() {
104
 
        $ret = GalleryCoreApi::removePlugin('module', 'rewritemodulemock');
105
 
        if ($ret) {
106
 
            $this->failWithStatus($ret);
107
 
        }
108
 
 
109
 
        parent::tearDown();
110
 
    }
111
 
 
112
 
    function testHandleEvent1() {
113
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'rewrite',
114
 
            'history.rewritemodulemock',
115
 
            serialize(array('phpunit/dummy/%itemId%.html')));
116
 
        if ($ret) {
117
 
            return $this->failWithStatus($ret);
118
 
        }
119
 
 
120
 
        /* Activate the module */
121
 
        list ($ret, $redirect) = $this->_module->activate();
122
 
        if ($ret) {
123
 
            return $this->failWithStatus($ret);
124
 
        }
125
 
        $this->assert(empty($redirect), 'activate redirect');
126
 
 
127
 
        list ($ret, $history) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
128
 
            'history.rewritemodulemock');
129
 
        if ($ret) {
130
 
            return $this->failWithStatus($ret);
131
 
        }
132
 
        $this->assert(empty($history), 'empty history (activate)');
133
 
 
134
 
        /* Make sure the history rule was activated */
135
 
        list ($ret, $activeRules) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
136
 
            'activeRules');
137
 
        if ($ret) {
138
 
            return $this->failWithStatus($ret);
139
 
        }
140
 
        $this->assertEquals($this->_activeRules, unserialize($activeRules), 'rules (activate)');
141
 
 
142
 
        /* Deactivate the module */
143
 
        list ($ret, $redirect) = $this->_module->deactivate();
144
 
        if ($ret) {
145
 
            return $this->failWithStatus($ret);
146
 
        }
147
 
        $this->assert(empty($redirect), 'deactivate redirect');
148
 
 
149
 
        /* Make sure the rule has been removed */
150
 
        list ($ret, $activeRules) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
151
 
            'activeRules');
152
 
        if ($ret) {
153
 
            return $this->failWithStatus($ret);
154
 
        }
155
 
        $this->assertEquals(array(), unserialize($activeRules), 'rules (deactivate)');
156
 
 
157
 
        list ($ret, $history) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
158
 
            'history.rewritemodulemock');
159
 
        if ($ret) {
160
 
            return $this->failWithStatus($ret);
161
 
        }
162
 
        $this->assertEquals(array('phpunit/dummy/%itemId%.html'),
163
 
                            unserialize($history),
164
 
                            'saved history');
165
 
 
166
 
        /* Uninstall the module */
167
 
        $ret = $this->_module->uninstall();
168
 
        if ($ret) {
169
 
            return $this->failWithStatus($ret);
170
 
        }
171
 
 
172
 
        list ($ret, $history) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
173
 
            'history.rewritemodulemock');
174
 
        if ($ret) {
175
 
            return $this->failWithStatus($ret);
176
 
        }
177
 
        $this->assert(empty($history), 'empty history (uninstall)');
178
 
 
179
 
        /* Make sure the active rules are still empty */
180
 
        list ($ret, $activeRules) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
181
 
            'activeRules');
182
 
        if ($ret) {
183
 
            return $this->failWithStatus($ret);
184
 
        }
185
 
        $this->assertEquals(array(), unserialize($activeRules), 'active rules (uninstall)');
186
 
    }
187
 
 
188
 
    function testHandleEvent2() {
189
 
        /* Activate the module */
190
 
        list ($ret, $redirect) = $this->_module->activate();
191
 
        if ($ret) {
192
 
            return $this->failWithStatus($ret);
193
 
        }
194
 
        $this->assert(empty($redirect), '2nd activate redirect');
195
 
 
196
 
        /* Make sure no rules were activated */
197
 
        list ($ret, $activeRules) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
198
 
            'activeRules');
199
 
        if ($ret) {
200
 
            return $this->failWithStatus($ret);
201
 
        }
202
 
        $this->assertEquals(array(), unserialize($activeRules), 'rules (reactivate)');
203
 
 
204
 
        /* Deactivate the module */
205
 
        list ($ret, $redirect) = $this->_module->deactivate();
206
 
        if ($ret) {
207
 
            return $this->failWithStatus($ret);
208
 
        }
209
 
        $this->assert(empty($redirect), '2nd deactivate redirect');
210
 
 
211
 
        /* Make sure no history was saved */
212
 
        list ($ret, $history) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
213
 
            'history.rewritemodulemock');
214
 
        if ($ret) {
215
 
            return $this->failWithStatus($ret);
216
 
        }
217
 
        $this->assert(empty($history), 'empty history (uninstall)');
218
 
 
219
 
        /* Uninstall the module */
220
 
        $ret = $this->_module->uninstall();
221
 
        if ($ret) {
222
 
            return $this->failWithStatus($ret);
223
 
        }
224
 
    }
225
 
 
226
 
    function testHandleEventWithDupes() {
227
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'rewrite',
228
 
            'history.rewritemodulemock',
229
 
            serialize(array('phpunit/dummy/%itemId%.html')));
230
 
        if ($ret) {
231
 
            return $this->failWithStatus($ret);
232
 
        }
233
 
 
234
 
        $expectedRules['rewrite'][0] = 'phpunit/dummy/%itemId%.html';
235
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'rewrite', 'activeRules',
236
 
            serialize($expectedRules));
237
 
        if ($ret) {
238
 
            return $this->failWithStatus($ret);
239
 
        }
240
 
 
241
 
        /* Activate the module */
242
 
        list ($ret, $redirect) = $this->_module->activate();
243
 
        if ($ret) {
244
 
            return $this->failWithStatus($ret);
245
 
        }
246
 
        $this->assert(empty($redirect), 'activate redirect');
247
 
 
248
 
        /* Make sure the history was deleted */
249
 
        list ($ret, $history) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
250
 
            'history.rewritemodulemock');
251
 
        if ($ret) {
252
 
            return $this->failWithStatus($ret);
253
 
        }
254
 
        $this->assert(empty($history), 'empty history (activate)');
255
 
 
256
 
        /* Make sure the rules were not changed */
257
 
        list ($ret, $activeRules) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
258
 
            'activeRules');
259
 
        if ($ret) {
260
 
            return $this->failWithStatus($ret);
261
 
        }
262
 
        $this->assertEquals($expectedRules, unserialize($activeRules), 'active rules (activate)');
263
 
 
264
 
        /* Deactivate the module */
265
 
        list ($ret, $redirect) = $this->_module->deactivate();
266
 
        if ($ret) {
267
 
            return $this->failWithStatus($ret);
268
 
        }
269
 
        $this->assert(empty($redirect), 'deactivate redirect');
270
 
 
271
 
        /* Make sure no history was saved */
272
 
        list ($ret, $history) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
273
 
            'history.rewritemodulemock');
274
 
        if ($ret) {
275
 
            return $this->failWithStatus($ret);
276
 
        }
277
 
        $this->assert(empty($history), 'empty history (dectivate)');
278
 
 
279
 
        /* Make sure the rules still were not changed */
280
 
        list ($ret, $activeRules) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
281
 
            'activeRules');
282
 
        if ($ret) {
283
 
            return $this->failWithStatus($ret);
284
 
        }
285
 
        $this->assertEquals($expectedRules, unserialize($activeRules), 'rules (dectivate)');
286
 
 
287
 
        /* Uninstall the module */
288
 
        $ret = $this->_module->uninstall();
289
 
        if ($ret) {
290
 
            return $this->failWithStatus($ret);
291
 
        }
292
 
    }
293
 
}
294
 
 
295
 
/**
296
 
 * Mock module for this test
297
 
 *
298
 
 * @package Rewrite
299
 
 * @subpackage PHPUnit
300
 
 */
301
 
class RewriteModuleMockModule extends GalleryModule {
302
 
    function RewriteModuleMockModule() {
303
 
        global $gallery;
304
 
 
305
 
        $this->setId('rewritemodulemock');
306
 
        $this->setName('RewriteModule Mock Module');
307
 
        $this->setDescription('Unit test module');
308
 
        $this->setVersion('1');
309
 
        $this->setGroup('data', 'Gallery');
310
 
        $this->setCallbacks('');
311
 
        $this->setRequiredCoreApi(GalleryCoreApi::getApiVersion());
312
 
        $this->setRequiredModuleApi(GalleryModule::getApiVersion());
313
 
    }
314
 
 
315
 
    function getRewriteRules() {
316
 
        return array(array('pattern' => 'phpunit/dummy/%itemId%.htm'));
317
 
    }
318
 
}
319
 
 
320
 
GalleryCoreApi::requireOnce('modules/rewrite/classes/RewriteParser.class');
321
 
 
322
 
/**
323
 
 * Mock rewrite parser for this test
324
 
 */
325
 
class RewriteModuleMockParser extends RewriteParser {
326
 
    function RewriteModuleMockParser() {
327
 
        $this->_setParserId('rewritemodulemock');
328
 
        $this->_setParserType('mock');
329
 
    }
330
 
 
331
 
    function saveActiveRules($activeRules=null, $rewriteModule=null) {
332
 
        if (is_null($activeRules)) {
333
 
            list ($ret, $activeRules) = GalleryCoreApi::getPluginParameter(
334
 
                'module', 'rewrite', 'activeRules');
335
 
            if ($ret) {
336
 
                return array($ret, null, null);
337
 
            }
338
 
            $activeRules = unserialize($activeRules);
339
 
        }
340
 
 
341
 
        $regexRules = array();
342
 
        $shortUrls = array();
343
 
        if (!empty($activeRules)) {
344
 
            list ($ret, $code, $regexRules, $shortUrls, $errorId) = RewriteHelper::parseActiveRules(
345
 
                $activeRules, $this, $rewriteModule);
346
 
            if ($ret) {
347
 
                return array($ret, null, null);
348
 
            }
349
 
            if ($code != REWRITE_STATUS_OK) {
350
 
                return array(GalleryStatus::success(), $code, $errorId);
351
 
            }
352
 
        }
353
 
 
354
 
        $ret = GalleryCoreApi::setPluginParameter(
355
 
            'module', 'rewrite', 'shortUrls', serialize($shortUrls));
356
 
        if ($ret) {
357
 
            return array($ret, null, null);
358
 
        }
359
 
 
360
 
        $ret = GalleryCoreApi::setPluginParameter(
361
 
            'module', 'rewrite', 'activeRules', serialize($activeRules));
362
 
        if ($ret) {
363
 
            return array($ret, null, null);
364
 
        }
365
 
 
366
 
        return array(null, REWRITE_STATUS_OK, null);
367
 
    }
368
 
}
369
 
 
370
 
/**
371
 
 * Test platform for this test
372
 
 */
373
 
class RewriteModuleMockPlatform extends GalleryPlatform {
374
 
 
375
 
    function file_exists($file) {
376
 
        if (strpos($file, 'modules/rewritemodulemock/module.inc') !== false) {
377
 
            return true;
378
 
        }
379
 
 
380
 
        return parent::file_exists($file);
381
 
    }
382
 
 
383
 
    function opendir($path) {
384
 
        if (strpos($path, '/modules/') !== false) {
385
 
            $this->_list = array();
386
 
            if ($dir = parent::opendir($path)) {
387
 
                while (false !== ($file = parent::readdir($dir))) {
388
 
                    array_push($this->_list, $file);
389
 
                }
390
 
            }
391
 
            array_push($this->_list, 'rewritemodulemock');
392
 
            return 'rewritemodulemock';
393
 
        }
394
 
 
395
 
        return parent::opendir($path);
396
 
    }
397
 
 
398
 
    function is_dir($path) {
399
 
        if (strpos($path, '/modules/rewritemodulemock') !== false) {
400
 
            return true;
401
 
        }
402
 
 
403
 
        return parent::is_dir($path);
404
 
    }
405
 
 
406
 
    function readdir($handle) {
407
 
        if ($handle == 'rewritemodulemock') {
408
 
            return array_shift($this->_list);
409
 
        }
410
 
 
411
 
        return parent::readdir($handle);
412
 
    }
413
 
 
414
 
    function closedir($handle) {
415
 
        if ($handle == 'rewritemodulemock') {
416
 
            unset($this->_list);
417
 
            return true;
418
 
        }
419
 
 
420
 
        return parent::closedir($handle);
421
 
    }
422
 
 
423
 
    function recursiveRmdir($path) {
424
 
        return true;
425
 
    }
426
 
}
427
 
?>