~ubuntu-branches/ubuntu/lucid/gallery2/lucid

« back to all changes in this revision

Viewing changes to modules/core/test/phpunit/ItemEditThemePluginTest.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
 
 * ItemEditTheme controller tests
23
 
 * @package GalleryCore
24
 
 * @subpackage PHPUnit
25
 
 * @author Bharat Mediratta <bharat@menalto.com>
26
 
 * @version $Revision: 15513 $
27
 
 */
28
 
class ItemEditThemePluginTest extends ItemEditPluginTestCase {
29
 
 
30
 
    function ItemEditThemePluginTest($methodName) {
31
 
        $this->ItemEditPluginTestCase($methodName, 'core', 'ItemEditTheme');
32
 
    }
33
 
 
34
 
    function setUp() {
35
 
        global $gallery;
36
 
 
37
 
        parent::setUp();
38
 
 
39
 
        /* Create a random album */
40
 
        list ($ret, $this->_album) = $this->_createRandomAlbum(
41
 
            $this->_getRootId(), array('theme' => 'ItemEditThemeControllerTest'));
42
 
        if ($ret) {
43
 
            return $this->failWithStatus($ret);
44
 
        }
45
 
 
46
 
        /* Create child album here, since test theme is too rudimentary */
47
 
        list ($ret, $this->_childAlbum) = $this->_createRandomAlbum($this->_album->getId());
48
 
        if ($ret) {
49
 
            print $ret->getAsHtml();
50
 
            return $this->failWithStatus($ret);
51
 
        }
52
 
 
53
 
        /*
54
 
         * Also check the grandchild album - to make sure that descendents don't
55
 
         * just return children
56
 
         */
57
 
        list ($ret, $this->_grandchildAlbum) = $this->_createRandomAlbum(
58
 
                $this->_childAlbum->getId());
59
 
        if ($ret) {
60
 
            print $ret->getAsHtml();
61
 
            return $this->failWithStatus($ret);
62
 
        }
63
 
 
64
 
        $this->_markForCleanup($this->_album);
65
 
 
66
 
        $gallery->setPlatform(new ItemEditThemeTestPlatform($gallery->getPlatform()));
67
 
 
68
 
        $this->_preferred = null;
69
 
 
70
 
        /* Reset test theme */
71
 
        ItemEditThemeControllerTestTheme::reset();
72
 
    }
73
 
 
74
 
    function _setTheme(&$item, $themeId) {
75
 
        list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($item->getId());
76
 
        if ($ret) {
77
 
            return $this->failWithStatus($ret);
78
 
        }
79
 
 
80
 
        $item->setTheme($themeId);
81
 
        $ret = $item->save();
82
 
        if ($ret) {
83
 
            return $this->failWithStatus($ret);
84
 
        }
85
 
 
86
 
        $ret = GalleryCoreApi::releaseLocks($lockId);
87
 
        if ($ret) {
88
 
            return $this->failWithStatus($ret);
89
 
        }
90
 
    }
91
 
 
92
 
    function testUndo() {
93
 
        /* Use valid inputs */
94
 
        GalleryUtilities::putRequestVariable('form[action][undo]', 1);
95
 
 
96
 
        /* Perform the request and verify that we succeeded */
97
 
        $results = $this->handleRequest($this->_album, $this->_preferred);
98
 
        $this->assertEquals(array(array(), null), $results);
99
 
    }
100
 
 
101
 
    function testSave() {
102
 
        GalleryUtilities::putRequestVariable('form[key][rows]', 3);
103
 
        GalleryUtilities::putRequestVariable('form[key][columns]', 4);
104
 
        GalleryUtilities::putRequestVariable('form[action][save]', 1);
105
 
 
106
 
        /* Perform the request and verify that we succeeded */
107
 
        $results = $this->handleRequest($this->_album, $this->_preferred);
108
 
        $this->assertEquals(array(array(), $this->_translate('Successfully saved theme settings')),
109
 
                            $results);
110
 
 
111
 
        /* Verify */
112
 
        $this->assertEquals(array('setParameter(rows, 3)',
113
 
                                  'setParameter(columns, 4)'),
114
 
                            ItemEditThemeControllerTestTheme::getMap());
115
 
    }
116
 
 
117
 
    function testInvalidSave() {
118
 
        GalleryUtilities::putRequestVariable('form[key][rows]', '3x');
119
 
        GalleryUtilities::putRequestVariable('form[key][columns]', 4);
120
 
        GalleryUtilities::putRequestVariable('form[action][save]', 1);
121
 
 
122
 
        /* Perform the request and verify that we succeeded */
123
 
        $results = $this->handleRequest($this->_album, $this->_preferred);
124
 
        $this->assertEquals(array(array('form[error][key][rows][invalid]'), null),
125
 
                            $results);
126
 
 
127
 
        /* Verify that we have a detailed error message */
128
 
        $this->assert(GalleryUtilities::hasRequestVariable('form[errorMessage]'));
129
 
 
130
 
        $errorMessage = GalleryUtilities::getRequestVariables('form[errorMessage]');
131
 
        $this->assertEquals('You must enter a number greater than 0', $errorMessage['rows']);
132
 
    }
133
 
 
134
 
    function testSaveWithGlobal() {
135
 
        GalleryUtilities::putRequestVariable('form[key][rows]', 3);
136
 
        GalleryUtilities::putRequestVariable('form[key][columns]', 4);
137
 
        GalleryUtilities::putRequestVariable('form[useGlobal][columns]', 'on');
138
 
        GalleryUtilities::putRequestVariable('form[action][save]', 1);
139
 
 
140
 
        /* Perform the request and verify that we succeeded */
141
 
        $results = $this->handleRequest($this->_album, $this->_preferred);
142
 
        $this->assertEquals(array(array(), $this->_translate('Successfully saved theme settings')),
143
 
                            $results);
144
 
 
145
 
        /* Verify */
146
 
        $this->assertEquals(array('setParameter(rows, 3)',
147
 
                                  sprintf('removeParameter(columns, %d)', $this->_album->getId())),
148
 
                            ItemEditThemeControllerTestTheme::getMap());
149
 
    }
150
 
 
151
 
    /**
152
 
     * Test applying options from 'Theme' tab to sub-albums
153
 
     */
154
 
    function testChangeInDescendents() {
155
 
        GalleryUtilities::putRequestVariable('form[key][rows]', 3);
156
 
        GalleryUtilities::putRequestVariable('form[key][columns]', 4);
157
 
        GalleryUtilities::putRequestVariable('form[action][save]', 1);
158
 
        GalleryUtilities::putRequestVariable('form[changeInDescendents][theme]', 1);
159
 
 
160
 
        /* Perform the request and verify that we succeeded */
161
 
        $results = $this->handleRequest($this->_album, $this->_preferred);
162
 
        $this->assertEquals(array(array(), $this->_translate('Successfully saved theme settings')),
163
 
                            $results);
164
 
        /* Verify - 3 albums should be set */
165
 
        $this->assertEquals(array('setParameter(rows, 3)',
166
 
                                  'setParameter(rows, 3)',
167
 
                                  'setParameter(rows, 3)',
168
 
                                  'setParameter(columns, 4)',
169
 
                                  'setParameter(columns, 4)',
170
 
                                  'setParameter(columns, 4)'),
171
 
                            ItemEditThemeControllerTestTheme::getMap());
172
 
    }
173
 
 
174
 
    /**
175
 
     * Test applying options from 'Theme' tab to sub-albums with no subalbum
176
 
     */
177
 
    function testChangeInDescendentsLeaf() {
178
 
        GalleryUtilities::putRequestVariable('form[key][rows]', 3);
179
 
        GalleryUtilities::putRequestVariable('form[key][columns]', 4);
180
 
        GalleryUtilities::putRequestVariable('form[action][save]', 1);
181
 
        GalleryUtilities::putRequestVariable('form[changeInDescendents][theme]', 1);
182
 
 
183
 
        $this->_setTheme($this->_grandchildAlbum, 'ItemEditThemeControllerTest');
184
 
 
185
 
        /* Perform the request and verify that we succeeded */
186
 
        $results = $this->handleRequest($this->_grandchildAlbum, $this->_preferred);
187
 
        $this->assertEquals(array(array(), $this->_translate('Successfully saved theme settings')),
188
 
                            $results);
189
 
        $this->assertEquals(array('setParameter(rows, 3)',
190
 
                                  'setParameter(columns, 4)'),
191
 
                            ItemEditThemeControllerTestTheme::getMap());
192
 
    }
193
 
 
194
 
    function testChooseTheme() {
195
 
        $this->_setTheme($this->_album, null);
196
 
 
197
 
        GalleryUtilities::putRequestVariable('form[theme]', 'ItemEditThemeControllerTest');
198
 
        GalleryUtilities::putRequestVariable('form[action][chooseTheme]', 1);
199
 
        GalleryUtilities::putRequestVariable(
200
 
            'form[serialNumber]', $this->_album->getSerialNumber());
201
 
 
202
 
        /* Perform the request and verify that we succeeded */
203
 
        $results = $this->handleRequest($this->_album, $this->_preferred);
204
 
        $this->assertEquals(array(array(), $this->_translate('Successfully changed theme')),
205
 
                            $results);
206
 
 
207
 
        list ($ret, $this->_album) = $this->_album->refresh();
208
 
        if ($ret) {
209
 
            $this->failWithStatus($ret);
210
 
        }
211
 
        $this->assertEquals('ItemEditThemeControllerTest', $this->_album->getTheme());
212
 
    }
213
 
 
214
 
    function testChooseDefaultTheme() {
215
 
        $this->_setTheme($this->_album, 'ItemEditThemeControllerTest');
216
 
 
217
 
        GalleryUtilities::putRequestVariable('form[action][chooseTheme]', 1);
218
 
        GalleryUtilities::putRequestVariable(
219
 
            'form[serialNumber]', $this->_album->getSerialNumber());
220
 
 
221
 
        /* Perform the request and verify that we succeeded */
222
 
        $results = $this->handleRequest($this->_album, $this->_preferred);
223
 
        $this->assertEquals(array(array(), $this->_translate('Successfully changed theme')),
224
 
                            $results);
225
 
 
226
 
        list ($ret, $this->_album) = $this->_album->refresh();
227
 
        if ($ret) {
228
 
            $this->failWithStatus($ret);
229
 
        }
230
 
        $themeId = $this->_album->getTheme();
231
 
        $this->assert(empty($themeId));
232
 
    }
233
 
 
234
 
    function testChooseBogusTheme() {
235
 
        GalleryUtilities::putRequestVariable('form[theme]', 'BogusBogus!');
236
 
        GalleryUtilities::putRequestVariable('form[action][chooseTheme]', 1);
237
 
 
238
 
        $results = $this->handleRequest($this->_album, $this->_preferred, ERROR_BAD_PARAMETER);
239
 
    }
240
 
 
241
 
    function testChooseThemeInvalidSerialNumber() {
242
 
        GalleryUtilities::putRequestVariable('form[theme]', 'ItemEditThemeControllerTest');
243
 
        GalleryUtilities::putRequestVariable('form[action][chooseTheme]', 1);
244
 
        GalleryUtilities::putRequestVariable('form[serialNumber]', -1);
245
 
 
246
 
        $results = $this->handleRequest($this->_album, $this->_preferred, ERROR_OBSOLETE_DATA);
247
 
    }
248
 
 
249
 
    function testChooseThemeInDescendents() {
250
 
        $this->_setTheme($this->_album, null);
251
 
        $this->_setTheme($this->_childAlbum, null);
252
 
        $this->_setTheme($this->_grandchildAlbum, null);
253
 
 
254
 
        GalleryUtilities::putRequestVariable('form[theme]', 'ItemEditThemeControllerTest');
255
 
        GalleryUtilities::putRequestVariable('form[changeInDescendents][theme]', 1);
256
 
        GalleryUtilities::putRequestVariable('form[action][chooseTheme]', 1);
257
 
        GalleryUtilities::putRequestVariable(
258
 
            'form[serialNumber]', $this->_album->getSerialNumber());
259
 
 
260
 
        /* Perform the request and verify that we succeeded */
261
 
        $results = $this->handleRequest($this->_album, $this->_preferred);
262
 
        $this->assertEquals(array(array(), $this->_translate('Successfully changed theme')),
263
 
                            $results);
264
 
 
265
 
        list ($ret, $album) = $this->_album->refresh();
266
 
        if ($ret) {
267
 
            $this->failWithStatus($ret);
268
 
        }
269
 
        list ($ret, $childAlbum) = $this->_childAlbum->refresh();
270
 
        if ($ret) {
271
 
            $this->failWithStatus($ret);
272
 
        }
273
 
        list ($ret, $grandchildAlbum) = $this->_grandchildAlbum->refresh();
274
 
        if ($ret) {
275
 
            $this->failWithStatus($ret);
276
 
        }
277
 
 
278
 
        $this->assertEquals('ItemEditThemeControllerTest', $album->getTheme(), 'album');
279
 
        $this->assertEquals('ItemEditThemeControllerTest', $childAlbum->getTheme(), 'child');
280
 
        $this->assertEquals('ItemEditThemeControllerTest',
281
 
                            $grandchildAlbum->getTheme(), 'grandchild');
282
 
    }
283
 
}
284
 
 
285
 
/**
286
 
 * Test theme
287
 
 */
288
 
class ItemEditThemeControllerTestTheme {
289
 
    function getId() {
290
 
        return 'ItemEditThemeControllerTest';
291
 
    }
292
 
 
293
 
    /**
294
 
     * @see GalleryTheme::getSettings
295
 
     */
296
 
    function getSettings($itemId=0) {
297
 
        $settings = array();
298
 
        $settings[] = array('key' => 'rows',
299
 
                            'name' => 'Rows per album page',
300
 
                            'type' => 'text-field',
301
 
                            'typeParams' => array('size' => 2),
302
 
                            'value' => 4);
303
 
        $settings[] = array('key' => 'columns',
304
 
                            'name' => 'Columns per album page',
305
 
                            'type' => 'text-field',
306
 
                            'typeParams' => array('size' => 2),
307
 
                            'value' => 3);
308
 
        return array(null, $settings);
309
 
    }
310
 
 
311
 
    /**
312
 
     * @see GalleryTheme::validateSettings
313
 
     */
314
 
    function validateSettings($settings) {
315
 
        $error = array();
316
 
        if (empty($settings['rows']) || !is_numeric($settings['rows'])) {
317
 
            $error['rows'] = $this->translate('You must enter a number greater than 0');
318
 
        }
319
 
 
320
 
        if (empty($settings['columns']) || !is_numeric($settings['columns'])) {
321
 
            $error['columns'] = $this->translate('You must enter a number greater than 0');
322
 
        }
323
 
 
324
 
        return $error;
325
 
    }
326
 
 
327
 
    /**
328
 
     * @see GalleryTheme::isAdvancedSettings
329
 
     */
330
 
    function isAdvancedSettings() {
331
 
        return false;
332
 
    }
333
 
 
334
 
    function isActive() {
335
 
        return array(null, true);
336
 
    }
337
 
 
338
 
    function translate($text) {
339
 
        return $text;
340
 
    }
341
 
 
342
 
    function removeParameter($key, $itemIds) {
343
 
        $map =& $this->getMap();
344
 
        foreach ($itemIds as $id) {
345
 
            $map[] = "removeParameter($key, $id)";
346
 
        }
347
 
        return null;
348
 
    }
349
 
 
350
 
    function setParameter($key, $value, $itemIds) {
351
 
        $map =& $this->getMap();
352
 
        foreach ($itemIds as $id) {
353
 
            $map[] = "setParameter($key, $value)";
354
 
        }
355
 
        return null;
356
 
    }
357
 
 
358
 
    function &getMap() {
359
 
        static $map;
360
 
        if (!isset($map)) {
361
 
            $map = array();
362
 
        }
363
 
        return $map;
364
 
    }
365
 
 
366
 
    function init() {
367
 
        return null;
368
 
    }
369
 
 
370
 
    function reset() {
371
 
        $map =& ItemEditThemeControllerTestTheme::getMap();
372
 
        $map = array();
373
 
    }
374
 
 
375
 
    function getName() {
376
 
        return 'ItemEditThemeControllerTest';
377
 
    }
378
 
}
379
 
 
380
 
/**
381
 
 * Test platform
382
 
 */
383
 
class ItemEditThemeTestPlatform {
384
 
 
385
 
    function ItemEditThemeTestPlatform($originalPlatform) {
386
 
        $this->_savedPlatform = $originalPlatform;
387
 
    }
388
 
 
389
 
    function opendir($path) {
390
 
        if ($path == dirname(__FILE__) . '/../../../../themes/') {
391
 
            return 'TEST_VALID_RESOURCE';
392
 
        } else {
393
 
            return null;
394
 
        }
395
 
    }
396
 
 
397
 
    function readdir($path) {
398
 
        static $paths;
399
 
        if (!isset($paths)) {
400
 
            $paths = array('.', '..', 'ItemEditThemeControllerTest');
401
 
        }
402
 
 
403
 
        if ($path == 'TEST_VALID_RESOURCE') {
404
 
            if (empty($paths)) {
405
 
                $paths = null;
406
 
                return null;
407
 
            } else {
408
 
                return array_shift($paths);
409
 
            }
410
 
        }
411
 
    }
412
 
 
413
 
    function fopen($filename, $modes) {
414
 
        /* Pass lock requests */
415
 
        if (preg_match("|\blocks\b|", $filename)) {
416
 
            return fopen($filename, $modes);
417
 
        }
418
 
 
419
 
        print "Unexpected fopen($filename, $modes)";
420
 
        return null;
421
 
    }
422
 
 
423
 
    function fclose($handle) {
424
 
        return fclose($handle); /* needed to release locks */
425
 
    }
426
 
 
427
 
    function getDirectorySeparator() {
428
 
        return DIRECTORY_SEPARATOR;
429
 
    }
430
 
 
431
 
    function unlink($path) {
432
 
        return unlink($path); /* needed to release locks */
433
 
    }
434
 
 
435
 
    function closedir($path) {
436
 
    }
437
 
 
438
 
    function realpath($path) {
439
 
        return realpath($path);
440
 
    }
441
 
 
442
 
    function file_exists($path) {
443
 
        $base = dirname(__FILE__) . '/../../../../themes/';
444
 
        switch($path) {
445
 
        case $base . 'ItemEditThemeControllerTest/theme.inc':
446
 
            return 1;
447
 
 
448
 
        default:
449
 
            return 0;
450
 
        }
451
 
    }
452
 
 
453
 
    function is_dir($path) {
454
 
        $base = dirname(__FILE__) . '/../../../../themes/';
455
 
        switch($path) {
456
 
        case $base . 'ItemEditThemeControllerTest':
457
 
            return 1;
458
 
 
459
 
        default:
460
 
            return 0;
461
 
        }
462
 
    }
463
 
 
464
 
    function flock($fd, $lockType, $flag) {
465
 
        return $this->_savedPlatform->flock($fd, $lockType, $flag);
466
 
    }
467
 
}
468
 
 
469
 
?>