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

« back to all changes in this revision

Viewing changes to modules/rewrite/test/phpunit/RewriteApiTest.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
 
GalleryCoreApi::requireOnce('modules/rewrite/classes/RewriteHelper.class');
22
 
 
23
 
/**
24
 
 * Test RewriteApi functionality
25
 
 *
26
 
 * @package Rewrite
27
 
 * @subpackage PHPUnit
28
 
 * @author Douglas Cau <douglas@cau.se>
29
 
 * @version $Revision: 15513 $
30
 
 */
31
 
class RewriteApiTest extends GalleryTestCase {
32
 
 
33
 
    function RewriteApiTest($methodName) {
34
 
        $this->GalleryTestCase($methodName);
35
 
    }
36
 
 
37
 
    function setUp() {
38
 
        global $gallery;
39
 
        parent::setUp();
40
 
 
41
 
        $ret = $this->_markPluginParametersForCleanup('module', 'rewrite');
42
 
        if ($ret) {
43
 
            print $ret->getAsHtml();
44
 
            return $this->failWithStatus($ret);
45
 
        }
46
 
 
47
 
        /* Set up our mock parser */
48
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'rewrite',
49
 
                                                  'parserId', 'rewriteapimock');
50
 
        if ($ret) {
51
 
            print $ret->getAsHtml();
52
 
            return $this->failWithStatus($ret);
53
 
        }
54
 
 
55
 
        /* Make sure we use the mock parser */
56
 
        GalleryCoreApi::requireOnce('modules/rewrite/classes/RewriteHelper.class');
57
 
        list ($ret, $parser) = RewriteHelper::getRewriteParser(true);
58
 
        if ($ret) {
59
 
            print $ret->getAsHtml();
60
 
            return $this->failWithStatus($ret);
61
 
        }
62
 
        $this->assertEquals('rewriteapimock', $parser->getParserId(), 'parserId');
63
 
 
64
 
        /* Load the api class */
65
 
        list ($ret, $this->_rewriteApi) = GalleryCoreApi::newFactoryInstance('RewriteApi');
66
 
        if ($ret) {
67
 
            print $ret->getAsHtml();
68
 
            return $this->failWithStatus($ret);
69
 
        }
70
 
 
71
 
        /* Set gallery in embedded mode */
72
 
        GalleryDataCache::put('G2_EMBED', 1, true);
73
 
    }
74
 
 
75
 
    function tearDown() {
76
 
        global $gallery;
77
 
 
78
 
        GalleryDataCache::remove('G2_EMBED');
79
 
 
80
 
        parent::tearDown();
81
 
    }
82
 
 
83
 
    function testIsCompatibleWithApi() {
84
 
        $currentVersion = $this->_rewriteApi->getApiVersion();
85
 
 
86
 
        list ($ret, $isCompatible) = $this->_rewriteApi->isCompatibleWithApi($currentVersion);
87
 
        if ($ret) {
88
 
            return $this->failWithStatus($ret);
89
 
        }
90
 
        $this->assert($isCompatible, 'same version');
91
 
 
92
 
        $compatibleVersion = array($currentVersion[0], 0);
93
 
        list ($ret, $isCompatible) = $this->_rewriteApi->isCompatibleWithApi($compatibleVersion);
94
 
        if ($ret) {
95
 
            return $this->failWithStatus($ret);
96
 
        }
97
 
        $this->assert($isCompatible, 'should be compatible, requiring same or lower minor version');
98
 
 
99
 
        $incompatibleVersion = array(--$currentVersion[0], 0);
100
 
        list ($ret, $isCompatible) = $this->_rewriteApi->isCompatibleWithApi($incompatibleVersion);
101
 
        if ($ret) {
102
 
            return $this->failWithStatus($ret);
103
 
        }
104
 
        $this->assert(!$isCompatible, 'should be incompatible, requiring lower major version');
105
 
    }
106
 
 
107
 
    function testGetParserId() {
108
 
        $this->assertEquals('rewriteapimock', $this->_rewriteApi->getParserId());
109
 
    }
110
 
 
111
 
    function testGetParserType() {
112
 
        $this->assertEquals('mock', $this->_rewriteApi->getParserType());
113
 
    }
114
 
 
115
 
    function testNeedsEmbedConfig1() {
116
 
        list ($ret, $needsConfiguration) = $this->_rewriteApi->needsEmbedConfig();
117
 
        if ($ret) {
118
 
            return $this->failWithStatus($ret);
119
 
        }
120
 
 
121
 
        $this->assertEquals(true, $needsConfiguration);
122
 
    }
123
 
 
124
 
    function testNeedsEmbedConfig2() {
125
 
        GalleryDataCache::remove('G2_EMBED');
126
 
        list ($ret, $needsConfiguration) = $this->_rewriteApi->needsEmbedConfig();
127
 
        if ($ret && !($ret->getErrorCode() & ERROR_UNSUPPORTED_OPERATION)) {
128
 
            return $this->failWithStatus($ret);
129
 
        }
130
 
 
131
 
        $this->assert(ERROR_UNSUPPORTED_OPERATION & $ret->getErrorCode());
132
 
    }
133
 
 
134
 
    function testFetchEmbedConfig1() {
135
 
        list ($ret, $params) = $this->_rewriteApi->fetchEmbedConfig();
136
 
        if ($ret) {
137
 
            return $this->failWithStatus($ret);
138
 
        }
139
 
 
140
 
        $this->assertEquals(array('phpunit' => 'test'), $params);
141
 
    }
142
 
 
143
 
    function testFetchEmbedConfig2() {
144
 
        GalleryDataCache::remove('G2_EMBED');
145
 
        list ($ret, $params) = $this->_rewriteApi->fetchEmbedConfig();
146
 
        if ($ret && !($ret->getErrorCode() & ERROR_UNSUPPORTED_OPERATION)) {
147
 
            return $this->failWithStatus($ret);
148
 
        }
149
 
 
150
 
        $this->assert(ERROR_UNSUPPORTED_OPERATION & $ret->getErrorCode());
151
 
    }
152
 
 
153
 
    function testSaveEmbedConfig1() {
154
 
        list ($ret, $code, $errstr) = $this->_rewriteApi->saveEmbedConfig(array('phpunit' => 1));
155
 
        if ($ret) {
156
 
            return $this->failWithStatus($ret);
157
 
        }
158
 
 
159
 
        $this->assertEquals(REWRITE_STATUS_OK, $code, 'code');
160
 
        $this->assertEquals(null, $errstr, 'errstr');
161
 
 
162
 
        list ($ret, $code, $errstr) = $this->_rewriteApi->saveEmbedConfig(array('phpunit' => 0));
163
 
        if ($ret) {
164
 
            return $this->failWithStatus($ret);
165
 
        }
166
 
 
167
 
        $this->assertEquals(REWRITE_STATUS_INVALID_PATTERN, $code, 'code');
168
 
        $this->assertEquals('Bad pattern', $errstr, 'errstr');
169
 
    }
170
 
 
171
 
    function testSaveEmbedConfig2() {
172
 
        GalleryDataCache::remove('G2_EMBED');
173
 
        list ($ret, $params) = $this->_rewriteApi->saveEmbedConfig(array());
174
 
        if ($ret && !($ret->getErrorCode() & ERROR_UNSUPPORTED_OPERATION)) {
175
 
            return $this->failWithStatus($ret);
176
 
        }
177
 
 
178
 
        $this->assert(ERROR_UNSUPPORTED_OPERATION & $ret->getErrorCode());
179
 
    }
180
 
 
181
 
    function testFetchActiveRulesForModule() {
182
 
        list ($ret, $expectedRules, $moduleId) =
183
 
            $this->_prepareActivateRewriteRulesForModule(REWRITE_STATUS_OK, true, true);
184
 
        if ($ret) {
185
 
            return $this->failWithStatus($ret);
186
 
        }
187
 
 
188
 
        /* Execute the method under test */
189
 
        list ($ret, $activeRules) = $this->_rewriteApi->fetchActiveRulesForModule($moduleId);
190
 
        if ($ret) {
191
 
            return $this->failWithStatus($ret);
192
 
        }
193
 
 
194
 
        /* Verify the results */
195
 
        $this->assertEquals(array_keys($expectedRules[$moduleId]), $activeRules, 'active rules');
196
 
    }
197
 
 
198
 
    function testFetchActiveRulesForModuleUsesHistory() {
199
 
        list ($ret, $expectedRules, $moduleId) =
200
 
            $this->_prepareActivateRewriteRulesForModule(REWRITE_STATUS_OK, false, true);
201
 
        if ($ret) {
202
 
            return $this->failWithStatus($ret);
203
 
        }
204
 
 
205
 
        /* Execute the method under test */
206
 
        list ($ret, $activeRules) = $this->_rewriteApi->fetchActiveRulesForModule($moduleId);
207
 
        if ($ret) {
208
 
            return $this->failWithStatus($ret);
209
 
        }
210
 
 
211
 
        /* Verify the results */
212
 
        $this->assertEquals(array('rule2', 'rule0'), $activeRules, 'active rules');
213
 
    }
214
 
 
215
 
    function testActivateRewriteRulesForModule() {
216
 
        list ($ret, $expectedRules, $moduleId) = $this->_prepareActivateRewriteRulesForModule();
217
 
        if ($ret) {
218
 
            return $this->failWithStatus($ret);
219
 
        }
220
 
 
221
 
        /* Execute the method under test */
222
 
        list ($ret, $success) = $this->_rewriteApi->activateRewriteRulesForModule($moduleId);
223
 
        if ($ret) {
224
 
            return $this->failWithStatus($ret);
225
 
        }
226
 
 
227
 
        /* Verify the results */
228
 
        $this->assert($success, 'should have succeeded');
229
 
 
230
 
        $expectedRules[$moduleId]['rule1'] = array('pattern' => 'abc');
231
 
        $expectedRules[$moduleId]['rule2'] = array('pattern' => '123');
232
 
        $this->assertEquals($expectedRules, $_SERVER['saveActiveRules'], 'saved rules');
233
 
    }
234
 
 
235
 
    function testActivateRewriteRulesForModuleOnlySpecificRule() {
236
 
        list ($ret, $expectedRules, $moduleId) = $this->_prepareActivateRewriteRulesForModule();
237
 
        if ($ret) {
238
 
            return $this->failWithStatus($ret);
239
 
        }
240
 
 
241
 
        /* Execute the method under test */
242
 
        list ($ret, $success) =
243
 
                $this->_rewriteApi->activateRewriteRulesForModule($moduleId, array('rule2'));
244
 
        if ($ret) {
245
 
            return $this->failWithStatus($ret);
246
 
        }
247
 
 
248
 
        /* Verify the results */
249
 
        $this->assert($success, 'should have succeeded');
250
 
        $expectedRules[$moduleId]['rule2'] = array('pattern' => '123');
251
 
 
252
 
        $this->assertEquals($expectedRules, $_SERVER['saveActiveRules'], 'saved rules');
253
 
    }
254
 
 
255
 
    function testActivateRewriteRulesForModuleReturnsFalseWhenNotConfigured() {
256
 
        list ($ret, $expectedRules, $moduleId) =
257
 
                $this->_prepareActivateRewriteRulesForModule(REWRITE_STATUS_INVALID_PATTERN);
258
 
        if ($ret) {
259
 
            return $this->failWithStatus($ret);
260
 
        }
261
 
 
262
 
        /* Execute the method under test */
263
 
        list ($ret, $success) = $this->_rewriteApi->activateRewriteRulesForModule($moduleId);
264
 
        if ($ret) {
265
 
            return $this->failWithStatus($ret);
266
 
        }
267
 
 
268
 
        /* Verify the results */
269
 
        $this->assert(!$success, 'should have failed');
270
 
    }
271
 
 
272
 
    function testActivateRewriteRulesForModuleUsesPatternHistory() {
273
 
        list ($ret, $expectedRules, $moduleId) =
274
 
                $this->_prepareActivateRewriteRulesForModule(REWRITE_STATUS_OK, true, true);
275
 
        if ($ret) {
276
 
            return $this->failWithStatus($ret);
277
 
        }
278
 
 
279
 
        /* Execute the method under test */
280
 
        list ($ret, $success) = $this->_rewriteApi->activateRewriteRulesForModule($moduleId);
281
 
        if ($ret) {
282
 
            return $this->failWithStatus($ret);
283
 
        }
284
 
 
285
 
        /* Verify the results */
286
 
        $this->assert($success, 'should have succeeded');
287
 
 
288
 
        $expectedRules[$moduleId]['rule1'] = array('pattern' => 'existing pattern for r1');
289
 
        $expectedRules[$moduleId]['rule2'] = array('pattern' => 'old pattern for r2');
290
 
        $this->assertEquals($expectedRules, $_SERVER['saveActiveRules'], 'saved rules');
291
 
 
292
 
        list ($ret, $history) = RewriteHelper::getHistoryForModule($moduleId);
293
 
        if ($ret) {
294
 
            return $this->failWithStatus($ret);
295
 
        }
296
 
        $this->assertEquals(array('rule0' => array('pattern' => 'foo')), $history,
297
 
                            'history for rule1 should have been removed');
298
 
    }
299
 
 
300
 
    function testActivateRewriteRulesForModuleIgnoresExistingPatternsAndPatternHistoryOnRequest() {
301
 
        list ($ret, $expectedRules, $moduleId) =
302
 
                $this->_prepareActivateRewriteRulesForModule(REWRITE_STATUS_OK, true, true);
303
 
        if ($ret) {
304
 
            return $this->failWithStatus($ret);
305
 
        }
306
 
 
307
 
        /* Execute the method under test */
308
 
        list ($ret, $success) = $this->_rewriteApi->activateRewriteRulesForModule($moduleId,
309
 
                                                        null, true);
310
 
        if ($ret) {
311
 
            return $this->failWithStatus($ret);
312
 
        }
313
 
 
314
 
        /* Verify the results */
315
 
        $this->assert($success, 'should have succeeded');
316
 
 
317
 
        $expectedRules[$moduleId]['rule1'] = array('pattern' => 'abc');
318
 
        $expectedRules[$moduleId]['rule2'] = array('pattern' => '123');
319
 
        $this->assertEquals($expectedRules, $_SERVER['saveActiveRules'], 'saved rules');
320
 
 
321
 
        list ($ret, $history) = RewriteHelper::getHistoryForModule($moduleId);
322
 
        if ($ret) {
323
 
            return $this->failWithStatus($ret);
324
 
        }
325
 
        $this->assertEquals(array('rule0' => array('pattern' => 'foo')), $history,
326
 
                            'history for rule1 should have been removed');
327
 
    }
328
 
 
329
 
    function _prepareActivateRewriteRulesForModule($returnCode=REWRITE_STATUS_OK,
330
 
                                                   $hasActiveRulesForModule=false,
331
 
                                                   $hasHistory=false) {
332
 
        $module = new RewriteApiMockModule();
333
 
        $moduleId = $module->getId();
334
 
 
335
 
        $activeRules = array('a' => array('r1' => array('pattern' => 'abc'),
336
 
                                          'r2' => array('pattern' => '123')),
337
 
                             'b' => array('r1' => array('pattern' => '.*')));
338
 
        if ($hasActiveRulesForModule) {
339
 
            $activeRules[$moduleId]['rule1'] = array('pattern' => 'existing pattern for r1');
340
 
        }
341
 
 
342
 
        $expectedRules = $activeRules;
343
 
        $activeRules['returnCode'] = $returnCode;
344
 
 
345
 
        $ret = GalleryCoreApi::setPluginParameter(
346
 
            'module', 'rewrite', 'activeRules', serialize($activeRules));
347
 
        if ($ret) {
348
 
            return array($ret, null, null);
349
 
        }
350
 
        $_SERVER['saveActiveRules'] = null;
351
 
 
352
 
        if ($hasHistory) {
353
 
            /* Create a pattern history for that module */
354
 
            $ret = RewriteHelper::setHistoryForModule(
355
 
                $moduleId, array('rule2' => array('pattern' => 'old pattern for r2'),
356
 
                                 'rule0' => array('pattern' => 'foo')));
357
 
            if ($ret) {
358
 
                return array($ret, null, null);
359
 
            }
360
 
        }
361
 
 
362
 
        return array(null, $expectedRules, $moduleId);
363
 
    }
364
 
}
365
 
 
366
 
/**
367
 
 * Mock rewrite parser for this test
368
 
 */
369
 
GalleryCoreApi::requireOnce('modules/rewrite/classes/RewriteParser.class');
370
 
class RewriteApiMockParser extends RewriteParser {
371
 
    function RewriteApiMockParser() {
372
 
        $this->_setParserId('rewriteapimock');
373
 
        $this->_setParserType('mock');
374
 
    }
375
 
 
376
 
    function needsEmbedConfig() {
377
 
        return array(null, true);
378
 
    }
379
 
 
380
 
    function fetchEmbedConfig() {
381
 
        return array(null, array('phpunit' => 'test'));
382
 
    }
383
 
 
384
 
    function saveEmbedConfig($params) {
385
 
        if ($params['phpunit']) {
386
 
            return array(null, REWRITE_STATUS_OK, null);
387
 
        }
388
 
 
389
 
        return array(null, REWRITE_STATUS_INVALID_PATTERN, 'Bad pattern');
390
 
    }
391
 
 
392
 
    function saveActiveRules($activeRules) {
393
 
        $returnCode = $activeRules['returnCode'];
394
 
        unset($activeRules['returnCode']);
395
 
        $_SERVER['saveActiveRules'] = $activeRules;
396
 
        return array(null, $returnCode, null);
397
 
    }
398
 
}
399
 
 
400
 
/**
401
 
 * Mock module for tests
402
 
 *
403
 
 * @package Rewrite
404
 
 * @subpackage PHPUnit
405
 
 */
406
 
class RewriteApiMockModule extends GalleryModule {
407
 
    function RewriteApiMockModule() {
408
 
        $this->setId('rewriteapimock');
409
 
        $this->setRequiredCoreApi(GalleryCoreApi::getApiVersion());
410
 
        $this->setRequiredModuleApi(GalleryModule::getApiVersion());
411
 
    }
412
 
 
413
 
    function getRewriteRules() {
414
 
        return array('rule1' => array('pattern' => 'abc'),
415
 
                     'rule2' => array('pattern' => '123'));
416
 
    }
417
 
}
418
 
?>