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.
21
GalleryCoreApi::requireOnce('modules/rewrite/classes/RewriteHelper.class');
24
* Test RewriteApi functionality
28
* @author Douglas Cau <douglas@cau.se>
29
* @version $Revision: 15513 $
31
class RewriteApiTest extends GalleryTestCase {
33
function RewriteApiTest($methodName) {
34
$this->GalleryTestCase($methodName);
41
$ret = $this->_markPluginParametersForCleanup('module', 'rewrite');
43
print $ret->getAsHtml();
44
return $this->failWithStatus($ret);
47
/* Set up our mock parser */
48
$ret = GalleryCoreApi::setPluginParameter('module', 'rewrite',
49
'parserId', 'rewriteapimock');
51
print $ret->getAsHtml();
52
return $this->failWithStatus($ret);
55
/* Make sure we use the mock parser */
56
GalleryCoreApi::requireOnce('modules/rewrite/classes/RewriteHelper.class');
57
list ($ret, $parser) = RewriteHelper::getRewriteParser(true);
59
print $ret->getAsHtml();
60
return $this->failWithStatus($ret);
62
$this->assertEquals('rewriteapimock', $parser->getParserId(), 'parserId');
64
/* Load the api class */
65
list ($ret, $this->_rewriteApi) = GalleryCoreApi::newFactoryInstance('RewriteApi');
67
print $ret->getAsHtml();
68
return $this->failWithStatus($ret);
71
/* Set gallery in embedded mode */
72
GalleryDataCache::put('G2_EMBED', 1, true);
78
GalleryDataCache::remove('G2_EMBED');
83
function testIsCompatibleWithApi() {
84
$currentVersion = $this->_rewriteApi->getApiVersion();
86
list ($ret, $isCompatible) = $this->_rewriteApi->isCompatibleWithApi($currentVersion);
88
return $this->failWithStatus($ret);
90
$this->assert($isCompatible, 'same version');
92
$compatibleVersion = array($currentVersion[0], 0);
93
list ($ret, $isCompatible) = $this->_rewriteApi->isCompatibleWithApi($compatibleVersion);
95
return $this->failWithStatus($ret);
97
$this->assert($isCompatible, 'should be compatible, requiring same or lower minor version');
99
$incompatibleVersion = array(--$currentVersion[0], 0);
100
list ($ret, $isCompatible) = $this->_rewriteApi->isCompatibleWithApi($incompatibleVersion);
102
return $this->failWithStatus($ret);
104
$this->assert(!$isCompatible, 'should be incompatible, requiring lower major version');
107
function testGetParserId() {
108
$this->assertEquals('rewriteapimock', $this->_rewriteApi->getParserId());
111
function testGetParserType() {
112
$this->assertEquals('mock', $this->_rewriteApi->getParserType());
115
function testNeedsEmbedConfig1() {
116
list ($ret, $needsConfiguration) = $this->_rewriteApi->needsEmbedConfig();
118
return $this->failWithStatus($ret);
121
$this->assertEquals(true, $needsConfiguration);
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);
131
$this->assert(ERROR_UNSUPPORTED_OPERATION & $ret->getErrorCode());
134
function testFetchEmbedConfig1() {
135
list ($ret, $params) = $this->_rewriteApi->fetchEmbedConfig();
137
return $this->failWithStatus($ret);
140
$this->assertEquals(array('phpunit' => 'test'), $params);
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);
150
$this->assert(ERROR_UNSUPPORTED_OPERATION & $ret->getErrorCode());
153
function testSaveEmbedConfig1() {
154
list ($ret, $code, $errstr) = $this->_rewriteApi->saveEmbedConfig(array('phpunit' => 1));
156
return $this->failWithStatus($ret);
159
$this->assertEquals(REWRITE_STATUS_OK, $code, 'code');
160
$this->assertEquals(null, $errstr, 'errstr');
162
list ($ret, $code, $errstr) = $this->_rewriteApi->saveEmbedConfig(array('phpunit' => 0));
164
return $this->failWithStatus($ret);
167
$this->assertEquals(REWRITE_STATUS_INVALID_PATTERN, $code, 'code');
168
$this->assertEquals('Bad pattern', $errstr, 'errstr');
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);
178
$this->assert(ERROR_UNSUPPORTED_OPERATION & $ret->getErrorCode());
181
function testFetchActiveRulesForModule() {
182
list ($ret, $expectedRules, $moduleId) =
183
$this->_prepareActivateRewriteRulesForModule(REWRITE_STATUS_OK, true, true);
185
return $this->failWithStatus($ret);
188
/* Execute the method under test */
189
list ($ret, $activeRules) = $this->_rewriteApi->fetchActiveRulesForModule($moduleId);
191
return $this->failWithStatus($ret);
194
/* Verify the results */
195
$this->assertEquals(array_keys($expectedRules[$moduleId]), $activeRules, 'active rules');
198
function testFetchActiveRulesForModuleUsesHistory() {
199
list ($ret, $expectedRules, $moduleId) =
200
$this->_prepareActivateRewriteRulesForModule(REWRITE_STATUS_OK, false, true);
202
return $this->failWithStatus($ret);
205
/* Execute the method under test */
206
list ($ret, $activeRules) = $this->_rewriteApi->fetchActiveRulesForModule($moduleId);
208
return $this->failWithStatus($ret);
211
/* Verify the results */
212
$this->assertEquals(array('rule2', 'rule0'), $activeRules, 'active rules');
215
function testActivateRewriteRulesForModule() {
216
list ($ret, $expectedRules, $moduleId) = $this->_prepareActivateRewriteRulesForModule();
218
return $this->failWithStatus($ret);
221
/* Execute the method under test */
222
list ($ret, $success) = $this->_rewriteApi->activateRewriteRulesForModule($moduleId);
224
return $this->failWithStatus($ret);
227
/* Verify the results */
228
$this->assert($success, 'should have succeeded');
230
$expectedRules[$moduleId]['rule1'] = array('pattern' => 'abc');
231
$expectedRules[$moduleId]['rule2'] = array('pattern' => '123');
232
$this->assertEquals($expectedRules, $_SERVER['saveActiveRules'], 'saved rules');
235
function testActivateRewriteRulesForModuleOnlySpecificRule() {
236
list ($ret, $expectedRules, $moduleId) = $this->_prepareActivateRewriteRulesForModule();
238
return $this->failWithStatus($ret);
241
/* Execute the method under test */
242
list ($ret, $success) =
243
$this->_rewriteApi->activateRewriteRulesForModule($moduleId, array('rule2'));
245
return $this->failWithStatus($ret);
248
/* Verify the results */
249
$this->assert($success, 'should have succeeded');
250
$expectedRules[$moduleId]['rule2'] = array('pattern' => '123');
252
$this->assertEquals($expectedRules, $_SERVER['saveActiveRules'], 'saved rules');
255
function testActivateRewriteRulesForModuleReturnsFalseWhenNotConfigured() {
256
list ($ret, $expectedRules, $moduleId) =
257
$this->_prepareActivateRewriteRulesForModule(REWRITE_STATUS_INVALID_PATTERN);
259
return $this->failWithStatus($ret);
262
/* Execute the method under test */
263
list ($ret, $success) = $this->_rewriteApi->activateRewriteRulesForModule($moduleId);
265
return $this->failWithStatus($ret);
268
/* Verify the results */
269
$this->assert(!$success, 'should have failed');
272
function testActivateRewriteRulesForModuleUsesPatternHistory() {
273
list ($ret, $expectedRules, $moduleId) =
274
$this->_prepareActivateRewriteRulesForModule(REWRITE_STATUS_OK, true, true);
276
return $this->failWithStatus($ret);
279
/* Execute the method under test */
280
list ($ret, $success) = $this->_rewriteApi->activateRewriteRulesForModule($moduleId);
282
return $this->failWithStatus($ret);
285
/* Verify the results */
286
$this->assert($success, 'should have succeeded');
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');
292
list ($ret, $history) = RewriteHelper::getHistoryForModule($moduleId);
294
return $this->failWithStatus($ret);
296
$this->assertEquals(array('rule0' => array('pattern' => 'foo')), $history,
297
'history for rule1 should have been removed');
300
function testActivateRewriteRulesForModuleIgnoresExistingPatternsAndPatternHistoryOnRequest() {
301
list ($ret, $expectedRules, $moduleId) =
302
$this->_prepareActivateRewriteRulesForModule(REWRITE_STATUS_OK, true, true);
304
return $this->failWithStatus($ret);
307
/* Execute the method under test */
308
list ($ret, $success) = $this->_rewriteApi->activateRewriteRulesForModule($moduleId,
311
return $this->failWithStatus($ret);
314
/* Verify the results */
315
$this->assert($success, 'should have succeeded');
317
$expectedRules[$moduleId]['rule1'] = array('pattern' => 'abc');
318
$expectedRules[$moduleId]['rule2'] = array('pattern' => '123');
319
$this->assertEquals($expectedRules, $_SERVER['saveActiveRules'], 'saved rules');
321
list ($ret, $history) = RewriteHelper::getHistoryForModule($moduleId);
323
return $this->failWithStatus($ret);
325
$this->assertEquals(array('rule0' => array('pattern' => 'foo')), $history,
326
'history for rule1 should have been removed');
329
function _prepareActivateRewriteRulesForModule($returnCode=REWRITE_STATUS_OK,
330
$hasActiveRulesForModule=false,
332
$module = new RewriteApiMockModule();
333
$moduleId = $module->getId();
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');
342
$expectedRules = $activeRules;
343
$activeRules['returnCode'] = $returnCode;
345
$ret = GalleryCoreApi::setPluginParameter(
346
'module', 'rewrite', 'activeRules', serialize($activeRules));
348
return array($ret, null, null);
350
$_SERVER['saveActiveRules'] = null;
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')));
358
return array($ret, null, null);
362
return array(null, $expectedRules, $moduleId);
367
* Mock rewrite parser for this test
369
GalleryCoreApi::requireOnce('modules/rewrite/classes/RewriteParser.class');
370
class RewriteApiMockParser extends RewriteParser {
371
function RewriteApiMockParser() {
372
$this->_setParserId('rewriteapimock');
373
$this->_setParserType('mock');
376
function needsEmbedConfig() {
377
return array(null, true);
380
function fetchEmbedConfig() {
381
return array(null, array('phpunit' => 'test'));
384
function saveEmbedConfig($params) {
385
if ($params['phpunit']) {
386
return array(null, REWRITE_STATUS_OK, null);
389
return array(null, REWRITE_STATUS_INVALID_PATTERN, 'Bad pattern');
392
function saveActiveRules($activeRules) {
393
$returnCode = $activeRules['returnCode'];
394
unset($activeRules['returnCode']);
395
$_SERVER['saveActiveRules'] = $activeRules;
396
return array(null, $returnCode, null);
401
* Mock module for tests
404
* @subpackage PHPUnit
406
class RewriteApiMockModule extends GalleryModule {
407
function RewriteApiMockModule() {
408
$this->setId('rewriteapimock');
409
$this->setRequiredCoreApi(GalleryCoreApi::getApiVersion());
410
$this->setRequiredModuleApi(GalleryModule::getApiVersion());
413
function getRewriteRules() {
414
return array('rule1' => array('pattern' => 'abc'),
415
'rule2' => array('pattern' => '123'));