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

« back to all changes in this revision

Viewing changes to modules/core/test/phpunit/AdminThemesControllerTest.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/core/classes/GalleryTheme.class');
22
 
 
23
 
/**
24
 
 * AdminThemes controller tests
25
 
 * @package GalleryCore
26
 
 * @subpackage PHPUnit
27
 
 * @author Bharat Mediratta <bharat@menalto.com>
28
 
 * @version $Revision: 15513 $
29
 
 */
30
 
class AdminThemesControllerTest extends GalleryControllerTestCase {
31
 
 
32
 
    function AdminThemesControllerTest($methodName) {
33
 
        $this->GalleryControllerTestCase($methodName, 'core.AdminThemes');
34
 
    }
35
 
 
36
 
    function setUp() {
37
 
        parent::setUp();
38
 
 
39
 
        $this->_testTheme =& AdminThemesControllerTestTheme::getSingleton();
40
 
        $this->_testTheme->resetCalls();
41
 
 
42
 
        $ret = $this->_markPluginParametersForCleanup('module', 'core');
43
 
        if ($ret) {
44
 
            print $ret->getAsHtml();
45
 
            return $ret;
46
 
        }
47
 
    }
48
 
 
49
 
    function testSaveTheme() {
50
 
        GalleryUtilities::putRequestVariable('themeId', 'testThemeId2');
51
 
        GalleryUtilities::putRequestVariable('form[key][rows]', '2');
52
 
        GalleryUtilities::putRequestVariable('form[key][columns]', '4');
53
 
        GalleryUtilities::putRequestVariable('form[action][saveTheme]', 1);
54
 
 
55
 
        /* Perform the request and verify that we succeeded */
56
 
        $results = $this->handleRequest();
57
 
        $this->assertEquals(
58
 
            array('redirect' => array('view' => 'core.SiteAdmin',
59
 
                                      'subView' => 'core.AdminThemes',
60
 
                                      'themeId' => 'testThemeId2'),
61
 
                  'status' => array('savedTheme' => 'Test Theme 2'),
62
 
                  'error' => array()),
63
 
            $results);
64
 
 
65
 
        $map =& TestThemeId2Theme::getMap();
66
 
        $this->assertEquals(2, (int)$map['rows']);
67
 
        $this->assertEquals(4, (int)$map['columns']);
68
 
    }
69
 
 
70
 
    function testUndoTheme() {
71
 
        GalleryUtilities::putRequestVariable('themeId', 'testThemeId2');
72
 
        GalleryUtilities::putRequestVariable('form[key][rows]', '2');
73
 
        GalleryUtilities::putRequestVariable('form[key][columns]', '4');
74
 
        GalleryUtilities::putRequestVariable('form[action][undoTheme]', 1);
75
 
 
76
 
        /* Perform the request and verify that we succeeded */
77
 
        $results = $this->handleRequest();
78
 
        $this->assertEquals(
79
 
            array('redirect' => array('view' => 'core.SiteAdmin',
80
 
                                      'subView' => 'core.AdminThemes',
81
 
                                      'themeId' => 'testThemeId2'),
82
 
                  'status' => array('restoredTheme' => 'Test Theme 2'),
83
 
                  'error' => array()),
84
 
            $results);
85
 
    }
86
 
 
87
 
    function testSaveThemeWithValidationErrors() {
88
 
        GalleryUtilities::putRequestVariable('themeId', 'testThemeId1');
89
 
        GalleryUtilities::putRequestVariable('form[key][rows]', '2');
90
 
        GalleryUtilities::putRequestVariable('form[key][columns]', '4');
91
 
        GalleryUtilities::putRequestVariable('form[action][saveTheme]', 1);
92
 
 
93
 
        /* Perform the request and verify that we succeeded */
94
 
        $results = $this->handleRequest();
95
 
        $this->assertEquals(
96
 
            array('delegate' => array('view' => 'core.SiteAdmin',
97
 
                                      'subView' => 'core.AdminThemes'),
98
 
                  'status' => array(),
99
 
                  'error' => array('form[error][key][rows][invalid]',
100
 
                                   'form[error][key][columns][invalid]')),
101
 
            $results);
102
 
    }
103
 
 
104
 
    function testSaveDefaults() {
105
 
        GalleryUtilities::putRequestVariable('form[default][orderBy]', 'viewCount');
106
 
        GalleryUtilities::putRequestVariable('form[default][orderDirection]', 'desc');
107
 
        GalleryUtilities::putRequestVariable('form[default][presort]', 'albumsFirst');
108
 
        GalleryUtilities::putRequestVariable('form[default][theme]', 'AdminThemesControllerTest');
109
 
        GalleryUtilities::putRequestVariable('form[default][newAlbumsUseDefaults]', 'true');
110
 
        GalleryUtilities::putRequestVariable('form[default][doesNotExist]', 'anything');
111
 
        GalleryUtilities::putRequestVariable('form[action][saveDefaults]', 1);
112
 
        GalleryUtilities::putRequestVariable('themeId', 'currentThemeId');
113
 
 
114
 
        /* Perform the request and verify that we succeeded */
115
 
        $results = $this->handleRequest();
116
 
        $this->assertEquals(array('redirect' => array('view' => 'core.SiteAdmin',
117
 
                                                      'subView' => 'core.AdminThemes',
118
 
                                                      'themeId' => 'AdminThemesControllerTest'),
119
 
                                  'status' => array('savedDefaults' => 1),
120
 
                                  'error' => array()),
121
 
                            $results);
122
 
 
123
 
        list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'core');
124
 
        if ($ret) {
125
 
            return $this->failWithStatus($ret);
126
 
        }
127
 
 
128
 
        $this->assertEquals('albumsFirst|viewCount', $params['default.orderBy'], 'orderBy');
129
 
        $this->assertEquals('desc', $params['default.orderDirection'], 'orderDirection');
130
 
        $this->assertEquals('AdminThemesControllerTest', $params['default.theme'], 'theme');
131
 
        $this->assertEquals('true', $params['default.newAlbumsUseDefaults'],
132
 
                            'newAlbumsUseDefaults');
133
 
        $this->assert(!isset($params['default.doesNotExist']), 'parameter not whitelisted');
134
 
    }
135
 
 
136
 
    function testSaveDefaultsValidatesDefaultTheme() {
137
 
        GalleryUtilities::putRequestVariable('form[default][orderBy]', 'viewCount');
138
 
        GalleryUtilities::putRequestVariable('form[default][orderDirection]', 'desc');
139
 
        GalleryUtilities::putRequestVariable('form[default][presort]', 'albumsFirst');
140
 
        GalleryUtilities::putRequestVariable('form[default][theme]', 'doesNotExist');
141
 
        GalleryUtilities::putRequestVariable('form[default][newAlbumsUseDefaults]', 'true');
142
 
        GalleryUtilities::putRequestVariable('form[action][saveDefaults]', 1);
143
 
        GalleryUtilities::putRequestVariable('themeId', 'anything');
144
 
 
145
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'core', 'default.theme', 'matrix');
146
 
        if ($ret) {
147
 
            return $this->failWithStatus($ret);
148
 
        }
149
 
 
150
 
        /* Perform the request and verify that we get an error */
151
 
        $results = $this->handleRequest(ERROR_BAD_PARAMETER);
152
 
        $this->assertEquals(null, $results);
153
 
 
154
 
        /* Verify that the default theme has not changed */
155
 
        list ($ret, $themeId) =
156
 
                GalleryCoreApi::getPluginParameter('module', 'core', 'default.theme');
157
 
        if ($ret) {
158
 
            return $this->failWithStatus($ret);
159
 
        }
160
 
 
161
 
        $this->assertEquals('matrix', $themeId);
162
 
    }
163
 
 
164
 
    function testSaveDefaultsValidatesOrderBy() {
165
 
        GalleryUtilities::putRequestVariable('form[default][orderBy]', 'doesNotExist');
166
 
        GalleryUtilities::putRequestVariable('form[default][orderDirection]', 'desc');
167
 
        GalleryUtilities::putRequestVariable('form[default][presort]', 'viewedFirst');
168
 
        GalleryUtilities::putRequestVariable('form[default][theme]', 'matrix');
169
 
        GalleryUtilities::putRequestVariable('form[default][newAlbumsUseDefaults]', 'true');
170
 
        GalleryUtilities::putRequestVariable('form[action][saveDefaults]', 1);
171
 
        GalleryUtilities::putRequestVariable('themeId', 'anything');
172
 
 
173
 
        /* Prepare the observed value */
174
 
        $originalOrderBy = 'albumsFirst|viewCount';
175
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'core',
176
 
                                                  'default.orderBy', $originalOrderBy);
177
 
        if ($ret) {
178
 
            return $this->failWithStatus($ret);
179
 
        }
180
 
 
181
 
        /* Perform the request and verify that we get an error */
182
 
        $results = $this->handleRequest(ERROR_BAD_PARAMETER);
183
 
        $this->assertEquals(null, $results);
184
 
 
185
 
        /* Verify that the value has not changed */
186
 
        list ($ret, $orderBy) =
187
 
                GalleryCoreApi::getPluginParameter('module', 'core', 'default.orderBy');
188
 
        if ($ret) {
189
 
            return $this->failWithStatus($ret);
190
 
        }
191
 
 
192
 
        $this->assertEquals($originalOrderBy, $orderBy);
193
 
    }
194
 
 
195
 
    function testSaveDefaultsValidatesPreSort() {
196
 
        GalleryUtilities::putRequestVariable('form[default][orderBy]', 'title');
197
 
        GalleryUtilities::putRequestVariable('form[default][orderDirection]', 'desc');
198
 
        GalleryUtilities::putRequestVariable('form[default][presort]', 'doesNotExist');
199
 
        GalleryUtilities::putRequestVariable('form[default][theme]', 'matrix');
200
 
        GalleryUtilities::putRequestVariable('form[default][newAlbumsUseDefaults]', 'true');
201
 
        GalleryUtilities::putRequestVariable('form[action][saveDefaults]', 1);
202
 
        GalleryUtilities::putRequestVariable('themeId', 'anything');
203
 
 
204
 
        /* Prepare the observed value */
205
 
        $originalOrderBy = 'albumsFirst|viewCount';
206
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'core',
207
 
                                                  'default.orderBy', $originalOrderBy);
208
 
        if ($ret) {
209
 
            return $this->failWithStatus($ret);
210
 
        }
211
 
 
212
 
        /* Perform the request and verify that we get an error */
213
 
        $results = $this->handleRequest(ERROR_BAD_PARAMETER);
214
 
        $this->assertEquals(null, $results);
215
 
 
216
 
        /* Verify that the value has not changed */
217
 
        list ($ret, $orderBy) =
218
 
                GalleryCoreApi::getPluginParameter('module', 'core', 'default.orderBy');
219
 
        if ($ret) {
220
 
            return $this->failWithStatus($ret);
221
 
        }
222
 
 
223
 
        $this->assertEquals($originalOrderBy, $orderBy);
224
 
    }
225
 
 
226
 
    function testSaveDefaultsValidatesOrderDirection() {
227
 
        GalleryUtilities::putRequestVariable('form[default][orderBy]', 'viewCount');
228
 
        GalleryUtilities::putRequestVariable('form[default][orderDirection]', 'doesNotExist');
229
 
        GalleryUtilities::putRequestVariable('form[default][presort]', 'albumsFirst');
230
 
        GalleryUtilities::putRequestVariable('form[default][theme]', 'matrix');
231
 
        GalleryUtilities::putRequestVariable('form[default][newAlbumsUseDefaults]', 'true');
232
 
        GalleryUtilities::putRequestVariable('form[action][saveDefaults]', 1);
233
 
        GalleryUtilities::putRequestVariable('themeId', 'anything');
234
 
 
235
 
        /* Prepare the observed value */
236
 
        $originalOrderDirection = 'desc';
237
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'core', 'default.orderDirection',
238
 
                                                  $originalOrderDirection);
239
 
        if ($ret) {
240
 
            return $this->failWithStatus($ret);
241
 
        }
242
 
 
243
 
        /* Perform the request and verify that we get an error */
244
 
        $results = $this->handleRequest(ERROR_BAD_PARAMETER);
245
 
        $this->assertEquals(null, $results);
246
 
 
247
 
        /* Verify that the value has not changed */
248
 
        list ($ret, $orderBy) =
249
 
                GalleryCoreApi::getPluginParameter('module', 'core', 'default.orderDirection');
250
 
        if ($ret) {
251
 
            return $this->failWithStatus($ret);
252
 
        }
253
 
 
254
 
        $this->assertEquals($originalOrderDirection, $orderBy);
255
 
    }
256
 
 
257
 
    function testSaveDefaultsValidatesNewAlbumsUseDefaults() {
258
 
        GalleryUtilities::putRequestVariable('form[default][orderBy]', 'viewCount');
259
 
        GalleryUtilities::putRequestVariable('form[default][orderDirection]', 'desc');
260
 
        GalleryUtilities::putRequestVariable('form[default][presort]', 'albumsFirst');
261
 
        GalleryUtilities::putRequestVariable('form[default][theme]', 'matrix');
262
 
        GalleryUtilities::putRequestVariable('form[default][newAlbumsUseDefaults]', 'doesNotExist');
263
 
        GalleryUtilities::putRequestVariable('form[action][saveDefaults]', 1);
264
 
        GalleryUtilities::putRequestVariable('themeId', 'anything');
265
 
 
266
 
        /* Prepare the observed value */
267
 
        $originalNewAlbumDefaults = 'true';
268
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'core', 'default.newAlbumsUseDefaults',
269
 
                                                  $originalNewAlbumDefaults);
270
 
        if ($ret) {
271
 
            return $this->failWithStatus($ret);
272
 
        }
273
 
 
274
 
        /* Perform the request and verify that we get an error */
275
 
        $results = $this->handleRequest(ERROR_BAD_PARAMETER);
276
 
        $this->assertEquals(null, $results);
277
 
 
278
 
        /* Verify that the value has not changed */
279
 
        list ($ret, $newAlbumDefaults) =
280
 
            GalleryCoreApi::getPluginParameter('module', 'core', 'default.newAlbumsUseDefaults');
281
 
        if ($ret) {
282
 
            return $this->failWithStatus($ret);
283
 
        }
284
 
 
285
 
        $this->assertEquals($originalNewAlbumDefaults, $newAlbumDefaults);
286
 
    }
287
 
}
288
 
 
289
 
/**
290
 
 * Test Theme that fails to validate settings
291
 
 */
292
 
class TestThemeId1Theme extends GalleryTheme {
293
 
 
294
 
    /**
295
 
     * @see GalleryTheme::getSettings
296
 
     */
297
 
    function getSettings($itemId=0) {
298
 
        $settings = array();
299
 
        $settings[] = array('key' => 'rows',
300
 
                            'name' => 'Rows per album page',
301
 
                            'type' => 'text-field',
302
 
                            'typeParams' => array('size' => 2),
303
 
                            'value' => 4);
304
 
        $settings[] = array('key' => 'columns',
305
 
                            'name' => 'Columns per album page',
306
 
                            'type' => 'text-field',
307
 
                            'typeParams' => array('size' => 2),
308
 
                            'value' => 99);
309
 
        return array(null, $settings);
310
 
    }
311
 
 
312
 
    /**
313
 
     * @see GalleryTheme::validateSettings
314
 
     */
315
 
    function validateSettings($settings) {
316
 
        $error = array();
317
 
        $error['rows'] = 'rows error';
318
 
        $error['columns'] = 'columns error';
319
 
        return $error;
320
 
    }
321
 
 
322
 
    function TestThemeId1Theme() {
323
 
        $this->setId('testThemeId1');
324
 
        $this->setName('Test Theme 1');
325
 
    }
326
 
 
327
 
    function isActive() {
328
 
        return array(null, true);
329
 
    }
330
 
}
331
 
 
332
 
/**
333
 
 * Test Theme that successfully validates settings
334
 
 */
335
 
class TestThemeId2Theme extends GalleryTheme {
336
 
 
337
 
    function &getMap() {
338
 
        static $map;
339
 
        if (!isset($map)) {
340
 
            $map = array();
341
 
        }
342
 
        return $map;
343
 
    }
344
 
 
345
 
    function setParameter($key, $value) {
346
 
        $map =& $this->getMap();
347
 
        $map[$key] = $value;
348
 
        return null;
349
 
    }
350
 
 
351
 
    function TestThemeId2Theme() {
352
 
        $this->setId('testThemeId2');
353
 
        $this->setName('Test Theme 2');
354
 
    }
355
 
 
356
 
    /**
357
 
     * @see GalleryTheme::getSettings
358
 
     */
359
 
    function getSettings($itemId=0) {
360
 
        $settings = array();
361
 
        $settings[] = array('key' => 'rows',
362
 
                            'name' => 'Rows per album page',
363
 
                            'type' => 'text-field',
364
 
                            'typeParams' => array('size' => 2),
365
 
                            'value' => 4);
366
 
        $settings[] = array('key' => 'columns',
367
 
                            'name' => 'Columns per album page',
368
 
                            'type' => 'text-field',
369
 
                            'typeParams' => array('size' => 2),
370
 
                            'value' => 3);
371
 
        return array(null, $settings);
372
 
    }
373
 
 
374
 
    /**
375
 
     * @see GalleryTheme::validateSettings
376
 
     */
377
 
    function validateSettings($settings) {
378
 
        return array();
379
 
    }
380
 
 
381
 
    function isActive() {
382
 
        return array(null, true);
383
 
    }
384
 
}
385
 
 
386
 
 
387
 
/**
388
 
 * Test theme
389
 
 */
390
 
class AdminThemesControllerTestTheme {
391
 
 
392
 
    function &getSingleton() {
393
 
        static $singleton;
394
 
        if (!isset($singleton)) {
395
 
            $singleton = new AdminThemesControllerTestTheme();
396
 
        }
397
 
 
398
 
        return $singleton;
399
 
    }
400
 
 
401
 
    function getId() {
402
 
        return 'AdminThemesControllerTest';
403
 
    }
404
 
 
405
 
    function getName() {
406
 
        return 'AdminThemes Controller Test';
407
 
    }
408
 
 
409
 
    function init() {
410
 
        $this->_setCalled('init');
411
 
        return null;
412
 
    }
413
 
 
414
 
    function installOrUpgrade() {
415
 
        $this->_setCalled('installOrUpgrade');
416
 
        return null;
417
 
    }
418
 
 
419
 
    function activate() {
420
 
        $this->_setCalled('activate');
421
 
        return array(null, array());
422
 
    }
423
 
 
424
 
    function uninstall() {
425
 
        $this->_setCalled('uninstall');
426
 
        return null;
427
 
    }
428
 
 
429
 
    function deactivate() {
430
 
        $this->_setCalled('deactivate');
431
 
        return array(null, array());
432
 
    }
433
 
 
434
 
    function _setCalled($function) {
435
 
        $singleton = &$this->getSingleton();
436
 
        $singleton->_callMap[$function] = 1;
437
 
    }
438
 
 
439
 
    function wasCalled($function) {
440
 
        $singleton = &$this->getSingleton();
441
 
        return isset($singleton->_callMap[$function]);
442
 
    }
443
 
 
444
 
    function resetCalls() {
445
 
        $singleton = &$this->getSingleton();
446
 
        $singleton->_callMap = array();
447
 
    }
448
 
 
449
 
    function _set($key, $val) {
450
 
        $singleton = &$this->getSingleton();
451
 
        $singleton->$key = $val;
452
 
    }
453
 
 
454
 
    function isActive() {
455
 
        $singleton = &$this->getSingleton();
456
 
        return array(null, $singleton->_isActive);
457
 
    }
458
 
 
459
 
    function isAdvancedSettings() {
460
 
        $singleton = &$this->getSingleton();
461
 
        return $singleton->_isAdvancedSettings;
462
 
    }
463
 
 
464
 
    function translate($str) {
465
 
        return $str;
466
 
    }
467
 
}
468
 
?>