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/core/classes/GalleryTheme.class');
24
* AdminThemes controller tests
25
* @package GalleryCore
27
* @author Bharat Mediratta <bharat@menalto.com>
28
* @version $Revision: 15513 $
30
class AdminThemesControllerTest extends GalleryControllerTestCase {
32
function AdminThemesControllerTest($methodName) {
33
$this->GalleryControllerTestCase($methodName, 'core.AdminThemes');
39
$this->_testTheme =& AdminThemesControllerTestTheme::getSingleton();
40
$this->_testTheme->resetCalls();
42
$ret = $this->_markPluginParametersForCleanup('module', 'core');
44
print $ret->getAsHtml();
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);
55
/* Perform the request and verify that we succeeded */
56
$results = $this->handleRequest();
58
array('redirect' => array('view' => 'core.SiteAdmin',
59
'subView' => 'core.AdminThemes',
60
'themeId' => 'testThemeId2'),
61
'status' => array('savedTheme' => 'Test Theme 2'),
65
$map =& TestThemeId2Theme::getMap();
66
$this->assertEquals(2, (int)$map['rows']);
67
$this->assertEquals(4, (int)$map['columns']);
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);
76
/* Perform the request and verify that we succeeded */
77
$results = $this->handleRequest();
79
array('redirect' => array('view' => 'core.SiteAdmin',
80
'subView' => 'core.AdminThemes',
81
'themeId' => 'testThemeId2'),
82
'status' => array('restoredTheme' => 'Test Theme 2'),
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);
93
/* Perform the request and verify that we succeeded */
94
$results = $this->handleRequest();
96
array('delegate' => array('view' => 'core.SiteAdmin',
97
'subView' => 'core.AdminThemes'),
99
'error' => array('form[error][key][rows][invalid]',
100
'form[error][key][columns][invalid]')),
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');
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),
123
list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'core');
125
return $this->failWithStatus($ret);
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');
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');
145
$ret = GalleryCoreApi::setPluginParameter('module', 'core', 'default.theme', 'matrix');
147
return $this->failWithStatus($ret);
150
/* Perform the request and verify that we get an error */
151
$results = $this->handleRequest(ERROR_BAD_PARAMETER);
152
$this->assertEquals(null, $results);
154
/* Verify that the default theme has not changed */
155
list ($ret, $themeId) =
156
GalleryCoreApi::getPluginParameter('module', 'core', 'default.theme');
158
return $this->failWithStatus($ret);
161
$this->assertEquals('matrix', $themeId);
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');
173
/* Prepare the observed value */
174
$originalOrderBy = 'albumsFirst|viewCount';
175
$ret = GalleryCoreApi::setPluginParameter('module', 'core',
176
'default.orderBy', $originalOrderBy);
178
return $this->failWithStatus($ret);
181
/* Perform the request and verify that we get an error */
182
$results = $this->handleRequest(ERROR_BAD_PARAMETER);
183
$this->assertEquals(null, $results);
185
/* Verify that the value has not changed */
186
list ($ret, $orderBy) =
187
GalleryCoreApi::getPluginParameter('module', 'core', 'default.orderBy');
189
return $this->failWithStatus($ret);
192
$this->assertEquals($originalOrderBy, $orderBy);
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');
204
/* Prepare the observed value */
205
$originalOrderBy = 'albumsFirst|viewCount';
206
$ret = GalleryCoreApi::setPluginParameter('module', 'core',
207
'default.orderBy', $originalOrderBy);
209
return $this->failWithStatus($ret);
212
/* Perform the request and verify that we get an error */
213
$results = $this->handleRequest(ERROR_BAD_PARAMETER);
214
$this->assertEquals(null, $results);
216
/* Verify that the value has not changed */
217
list ($ret, $orderBy) =
218
GalleryCoreApi::getPluginParameter('module', 'core', 'default.orderBy');
220
return $this->failWithStatus($ret);
223
$this->assertEquals($originalOrderBy, $orderBy);
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');
235
/* Prepare the observed value */
236
$originalOrderDirection = 'desc';
237
$ret = GalleryCoreApi::setPluginParameter('module', 'core', 'default.orderDirection',
238
$originalOrderDirection);
240
return $this->failWithStatus($ret);
243
/* Perform the request and verify that we get an error */
244
$results = $this->handleRequest(ERROR_BAD_PARAMETER);
245
$this->assertEquals(null, $results);
247
/* Verify that the value has not changed */
248
list ($ret, $orderBy) =
249
GalleryCoreApi::getPluginParameter('module', 'core', 'default.orderDirection');
251
return $this->failWithStatus($ret);
254
$this->assertEquals($originalOrderDirection, $orderBy);
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');
266
/* Prepare the observed value */
267
$originalNewAlbumDefaults = 'true';
268
$ret = GalleryCoreApi::setPluginParameter('module', 'core', 'default.newAlbumsUseDefaults',
269
$originalNewAlbumDefaults);
271
return $this->failWithStatus($ret);
274
/* Perform the request and verify that we get an error */
275
$results = $this->handleRequest(ERROR_BAD_PARAMETER);
276
$this->assertEquals(null, $results);
278
/* Verify that the value has not changed */
279
list ($ret, $newAlbumDefaults) =
280
GalleryCoreApi::getPluginParameter('module', 'core', 'default.newAlbumsUseDefaults');
282
return $this->failWithStatus($ret);
285
$this->assertEquals($originalNewAlbumDefaults, $newAlbumDefaults);
290
* Test Theme that fails to validate settings
292
class TestThemeId1Theme extends GalleryTheme {
295
* @see GalleryTheme::getSettings
297
function getSettings($itemId=0) {
299
$settings[] = array('key' => 'rows',
300
'name' => 'Rows per album page',
301
'type' => 'text-field',
302
'typeParams' => array('size' => 2),
304
$settings[] = array('key' => 'columns',
305
'name' => 'Columns per album page',
306
'type' => 'text-field',
307
'typeParams' => array('size' => 2),
309
return array(null, $settings);
313
* @see GalleryTheme::validateSettings
315
function validateSettings($settings) {
317
$error['rows'] = 'rows error';
318
$error['columns'] = 'columns error';
322
function TestThemeId1Theme() {
323
$this->setId('testThemeId1');
324
$this->setName('Test Theme 1');
327
function isActive() {
328
return array(null, true);
333
* Test Theme that successfully validates settings
335
class TestThemeId2Theme extends GalleryTheme {
345
function setParameter($key, $value) {
346
$map =& $this->getMap();
351
function TestThemeId2Theme() {
352
$this->setId('testThemeId2');
353
$this->setName('Test Theme 2');
357
* @see GalleryTheme::getSettings
359
function getSettings($itemId=0) {
361
$settings[] = array('key' => 'rows',
362
'name' => 'Rows per album page',
363
'type' => 'text-field',
364
'typeParams' => array('size' => 2),
366
$settings[] = array('key' => 'columns',
367
'name' => 'Columns per album page',
368
'type' => 'text-field',
369
'typeParams' => array('size' => 2),
371
return array(null, $settings);
375
* @see GalleryTheme::validateSettings
377
function validateSettings($settings) {
381
function isActive() {
382
return array(null, true);
390
class AdminThemesControllerTestTheme {
392
function &getSingleton() {
394
if (!isset($singleton)) {
395
$singleton = new AdminThemesControllerTestTheme();
402
return 'AdminThemesControllerTest';
406
return 'AdminThemes Controller Test';
410
$this->_setCalled('init');
414
function installOrUpgrade() {
415
$this->_setCalled('installOrUpgrade');
419
function activate() {
420
$this->_setCalled('activate');
421
return array(null, array());
424
function uninstall() {
425
$this->_setCalled('uninstall');
429
function deactivate() {
430
$this->_setCalled('deactivate');
431
return array(null, array());
434
function _setCalled($function) {
435
$singleton = &$this->getSingleton();
436
$singleton->_callMap[$function] = 1;
439
function wasCalled($function) {
440
$singleton = &$this->getSingleton();
441
return isset($singleton->_callMap[$function]);
444
function resetCalls() {
445
$singleton = &$this->getSingleton();
446
$singleton->_callMap = array();
449
function _set($key, $val) {
450
$singleton = &$this->getSingleton();
451
$singleton->$key = $val;
454
function isActive() {
455
$singleton = &$this->getSingleton();
456
return array(null, $singleton->_isActive);
459
function isAdvancedSettings() {
460
$singleton = &$this->getSingleton();
461
return $singleton->_isAdvancedSettings;
464
function translate($str) {