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
* Test RewriteUrlGenerator functionality
26
* @author Douglas Cau <douglas@cau.se>
27
* @version $Revision: 15513 $
29
class RewriteModuleTest extends GalleryTestCase {
33
function RewriteModuleTest($methodName) {
34
$this->GalleryTestCase($methodName);
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);
47
$this->_platform = new RewriteModuleMockPlatform();
48
$gallery->setPlatform($this->_platform);
50
$ret = $this->_markPluginParametersForCleanup('module', 'rewrite');
52
print $ret->getAsHtml();
53
return $this->failWithStatus($ret);
56
/* Set up our environment */
57
foreach (array('parserId' => 'rewritemodulemock',
58
'activeRules' => serialize(array()))
60
$ret = GalleryCoreApi::setPluginParameter('module', 'rewrite', $key, $value);
62
print $ret->getAsHtml();
63
return $this->failWithStatus($ret);
67
/* Make sure we use the mock parser */
68
GalleryCoreApi::requireOnce('modules/rewrite/classes/RewriteHelper.class');
69
list ($ret, $parser) = RewriteHelper::getRewriteParser(true);
71
print $ret->getAsHtml();
72
return $this->failWithStatus($ret);
74
$this->assertEquals('rewritemodulemock', $parser->getParserId(), 'parserId');
76
/* Register a new module */
77
GalleryDataCache::reset();
78
list ($ret, $this->_module) = GalleryCoreApi::loadPlugin('module', 'rewritemodulemock');
80
print $ret->getAsHtml();
81
return $this->failWithStatus($ret);
85
$ret = $this->_module->installOrUpgrade();
87
print $ret->getAsHtml();
88
return $this->failWithStatus($ret);
91
/* Use empty table by default */
92
$ret = GalleryCoreApi::setPluginParameter('module', 'rewrite', 'activeRules',
95
print $ret->getAsHtml();
96
return $this->failWithStatus($ret);
99
/* Dummy active rules */
100
$this->_activeRules['rewritemodulemock'][0] = 'phpunit/dummy/%itemId%.html';
103
function tearDown() {
104
$ret = GalleryCoreApi::removePlugin('module', 'rewritemodulemock');
106
$this->failWithStatus($ret);
112
function testHandleEvent1() {
113
$ret = GalleryCoreApi::setPluginParameter('module', 'rewrite',
114
'history.rewritemodulemock',
115
serialize(array('phpunit/dummy/%itemId%.html')));
117
return $this->failWithStatus($ret);
120
/* Activate the module */
121
list ($ret, $redirect) = $this->_module->activate();
123
return $this->failWithStatus($ret);
125
$this->assert(empty($redirect), 'activate redirect');
127
list ($ret, $history) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
128
'history.rewritemodulemock');
130
return $this->failWithStatus($ret);
132
$this->assert(empty($history), 'empty history (activate)');
134
/* Make sure the history rule was activated */
135
list ($ret, $activeRules) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
138
return $this->failWithStatus($ret);
140
$this->assertEquals($this->_activeRules, unserialize($activeRules), 'rules (activate)');
142
/* Deactivate the module */
143
list ($ret, $redirect) = $this->_module->deactivate();
145
return $this->failWithStatus($ret);
147
$this->assert(empty($redirect), 'deactivate redirect');
149
/* Make sure the rule has been removed */
150
list ($ret, $activeRules) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
153
return $this->failWithStatus($ret);
155
$this->assertEquals(array(), unserialize($activeRules), 'rules (deactivate)');
157
list ($ret, $history) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
158
'history.rewritemodulemock');
160
return $this->failWithStatus($ret);
162
$this->assertEquals(array('phpunit/dummy/%itemId%.html'),
163
unserialize($history),
166
/* Uninstall the module */
167
$ret = $this->_module->uninstall();
169
return $this->failWithStatus($ret);
172
list ($ret, $history) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
173
'history.rewritemodulemock');
175
return $this->failWithStatus($ret);
177
$this->assert(empty($history), 'empty history (uninstall)');
179
/* Make sure the active rules are still empty */
180
list ($ret, $activeRules) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
183
return $this->failWithStatus($ret);
185
$this->assertEquals(array(), unserialize($activeRules), 'active rules (uninstall)');
188
function testHandleEvent2() {
189
/* Activate the module */
190
list ($ret, $redirect) = $this->_module->activate();
192
return $this->failWithStatus($ret);
194
$this->assert(empty($redirect), '2nd activate redirect');
196
/* Make sure no rules were activated */
197
list ($ret, $activeRules) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
200
return $this->failWithStatus($ret);
202
$this->assertEquals(array(), unserialize($activeRules), 'rules (reactivate)');
204
/* Deactivate the module */
205
list ($ret, $redirect) = $this->_module->deactivate();
207
return $this->failWithStatus($ret);
209
$this->assert(empty($redirect), '2nd deactivate redirect');
211
/* Make sure no history was saved */
212
list ($ret, $history) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
213
'history.rewritemodulemock');
215
return $this->failWithStatus($ret);
217
$this->assert(empty($history), 'empty history (uninstall)');
219
/* Uninstall the module */
220
$ret = $this->_module->uninstall();
222
return $this->failWithStatus($ret);
226
function testHandleEventWithDupes() {
227
$ret = GalleryCoreApi::setPluginParameter('module', 'rewrite',
228
'history.rewritemodulemock',
229
serialize(array('phpunit/dummy/%itemId%.html')));
231
return $this->failWithStatus($ret);
234
$expectedRules['rewrite'][0] = 'phpunit/dummy/%itemId%.html';
235
$ret = GalleryCoreApi::setPluginParameter('module', 'rewrite', 'activeRules',
236
serialize($expectedRules));
238
return $this->failWithStatus($ret);
241
/* Activate the module */
242
list ($ret, $redirect) = $this->_module->activate();
244
return $this->failWithStatus($ret);
246
$this->assert(empty($redirect), 'activate redirect');
248
/* Make sure the history was deleted */
249
list ($ret, $history) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
250
'history.rewritemodulemock');
252
return $this->failWithStatus($ret);
254
$this->assert(empty($history), 'empty history (activate)');
256
/* Make sure the rules were not changed */
257
list ($ret, $activeRules) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
260
return $this->failWithStatus($ret);
262
$this->assertEquals($expectedRules, unserialize($activeRules), 'active rules (activate)');
264
/* Deactivate the module */
265
list ($ret, $redirect) = $this->_module->deactivate();
267
return $this->failWithStatus($ret);
269
$this->assert(empty($redirect), 'deactivate redirect');
271
/* Make sure no history was saved */
272
list ($ret, $history) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
273
'history.rewritemodulemock');
275
return $this->failWithStatus($ret);
277
$this->assert(empty($history), 'empty history (dectivate)');
279
/* Make sure the rules still were not changed */
280
list ($ret, $activeRules) = GalleryCoreApi::getPluginParameter('module', 'rewrite',
283
return $this->failWithStatus($ret);
285
$this->assertEquals($expectedRules, unserialize($activeRules), 'rules (dectivate)');
287
/* Uninstall the module */
288
$ret = $this->_module->uninstall();
290
return $this->failWithStatus($ret);
296
* Mock module for this test
299
* @subpackage PHPUnit
301
class RewriteModuleMockModule extends GalleryModule {
302
function RewriteModuleMockModule() {
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());
315
function getRewriteRules() {
316
return array(array('pattern' => 'phpunit/dummy/%itemId%.htm'));
320
GalleryCoreApi::requireOnce('modules/rewrite/classes/RewriteParser.class');
323
* Mock rewrite parser for this test
325
class RewriteModuleMockParser extends RewriteParser {
326
function RewriteModuleMockParser() {
327
$this->_setParserId('rewritemodulemock');
328
$this->_setParserType('mock');
331
function saveActiveRules($activeRules=null, $rewriteModule=null) {
332
if (is_null($activeRules)) {
333
list ($ret, $activeRules) = GalleryCoreApi::getPluginParameter(
334
'module', 'rewrite', 'activeRules');
336
return array($ret, null, null);
338
$activeRules = unserialize($activeRules);
341
$regexRules = array();
342
$shortUrls = array();
343
if (!empty($activeRules)) {
344
list ($ret, $code, $regexRules, $shortUrls, $errorId) = RewriteHelper::parseActiveRules(
345
$activeRules, $this, $rewriteModule);
347
return array($ret, null, null);
349
if ($code != REWRITE_STATUS_OK) {
350
return array(GalleryStatus::success(), $code, $errorId);
354
$ret = GalleryCoreApi::setPluginParameter(
355
'module', 'rewrite', 'shortUrls', serialize($shortUrls));
357
return array($ret, null, null);
360
$ret = GalleryCoreApi::setPluginParameter(
361
'module', 'rewrite', 'activeRules', serialize($activeRules));
363
return array($ret, null, null);
366
return array(null, REWRITE_STATUS_OK, null);
371
* Test platform for this test
373
class RewriteModuleMockPlatform extends GalleryPlatform {
375
function file_exists($file) {
376
if (strpos($file, 'modules/rewritemodulemock/module.inc') !== false) {
380
return parent::file_exists($file);
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);
391
array_push($this->_list, 'rewritemodulemock');
392
return 'rewritemodulemock';
395
return parent::opendir($path);
398
function is_dir($path) {
399
if (strpos($path, '/modules/rewritemodulemock') !== false) {
403
return parent::is_dir($path);
406
function readdir($handle) {
407
if ($handle == 'rewritemodulemock') {
408
return array_shift($this->_list);
411
return parent::readdir($handle);
414
function closedir($handle) {
415
if ($handle == 'rewritemodulemock') {
420
return parent::closedir($handle);
423
function recursiveRmdir($path) {