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/customfield/classes/CustomFieldHelper.class');
24
* CustomFieldItemEdit controller tests
25
* @package CustomField
27
* @author Alan Harder <alan.harder@sun.com>
28
* @version $Revision: 15513 $
30
class CustomFieldItemEditPluginTest extends ItemEditPluginTestCase {
32
function CustomFieldItemEditPluginTest($methodName) {
33
$this->ItemEditPluginTestCase($methodName, 'customfield', 'CustomFieldItemEdit');
39
list ($ret, $this->_albumG) = $this->_createRandomAlbum($this->_getRootId());
41
print $ret->getAsHtml();
42
return $this->failWithStatus($ret);
44
$this->_markForCleanup($this->_albumG);
45
list ($ret, $this->_itemG) = $this->_createRandomDataItem($this->_albumG->getId(),
46
'image/test', array(), __FILE__, 'GalleryPhotoItem');
48
print $ret->getAsHtml();
49
return $this->failWithStatus($ret);
51
list ($ret, $this->_albumS) = $this->_createRandomAlbum($this->_getRootId());
53
print $ret->getAsHtml();
54
return $this->failWithStatus($ret);
56
$this->_markForCleanup($this->_albumS);
57
list ($ret, $this->_itemS) = $this->_createRandomDataItem($this->_albumS->getId(),
58
'video/test', array(), __FILE__, 'GalleryMovieItem');
60
print $ret->getAsHtml();
61
return $this->failWithStatus($ret);
64
$ret = $this->_markPluginParametersForCleanup('module', 'customfield');
66
print $ret->getAsHtml();
70
$ret = CustomFieldHelper::saveParameters(array(
72
array('field' => 'Catalog Id',
73
'summary' => false, 'detail' => true, 'choices' => array()),
74
array('field' => 'My Data',
75
'summary' => true, 'detail' => true, 'choices' => array())
79
array('field' => 'Location',
80
'summary' => true, 'detail' => true, 'choices' => array()),
81
array('field' => 'Category',
82
'summary' => false, 'detail' => false, 'choices' => array('A', 'B'))
86
print $ret->getAsHtml();
87
return $this->failWithStatus($ret);
89
$ret = CustomFieldHelper::saveParameters(array(
91
array('field' => 'Category',
92
'summary' => true, 'detail' => false, 'choices' => array('A', 'B', 'C'))
95
array('field' => 'Catalog Id',
96
'summary' => false, 'detail' => true, 'choices' => array()),
97
array('field' => 'Year',
98
'summary' => false, 'detail' => true, 'choices' => array())
101
array('field' => 'Photographer',
102
'summary' => true, 'detail' => true, 'choices' => array()),
104
), $this->_albumS->getId());
106
print $ret->getAsHtml();
107
return $this->failWithStatus($ret);
111
function testCustomFieldPlugin() {
113
GalleryUtilities::putRequestVariable('form[action][reset]', '1');
114
$results = $this->handleRequest($this->itemG, $this->_itemG);
115
$this->assertEquals(array(array(), null), $results, 'Reset');
117
/* Set field values (global settings) */
118
GalleryUtilities::putRequestVariable('form[fields][Catalog Id]', '123');
119
GalleryUtilities::putRequestVariable('form[fields][My Data]', '');
120
GalleryUtilities::putRequestVariable('form[fields][Location]', 'There');
121
GalleryUtilities::putRequestVariable('form[fields][Category]', 'B');
122
GalleryUtilities::putRequestVariable('form[action][save]', 1);
124
$results = $this->handleRequest($this->_itemG, $this->_itemG);
125
$this->assertEquals(array(array(), $this->_translate('Changes saved successfully')),
128
list ($ret, $data) = CustomFieldHelper::fetchFieldValues(array($this->_itemG), 'summary');
130
return $this->failWithStatus($ret);
132
$this->assertEquals(array('Location' => 'There'),
133
$data[$this->_itemG->getId()], 'Summary data');
135
list ($ret, $data) = CustomFieldHelper::fetchFieldValues(array($this->_itemG), 'detail');
137
return $this->failWithStatus($ret);
139
$this->assertEquals(array('Catalog Id' => '123', 'Location' => 'There'),
140
$data[$this->_itemG->getId()], 'Detail data');
143
CustomFieldHelper::fetchFieldValues(array($this->_itemG), null, 'photo');
145
return $this->failWithStatus($ret);
147
$this->assertEquals(array('Catalog Id' => '123', 'My Data' => '',
148
'Location' => 'There', 'Category' => 'B'),
149
$data[$this->_itemG->getId()], 'All data');
151
/* Set field values (album-specific settings) */
152
GalleryUtilities::removeFormVariables('form');
153
GalleryUtilities::putRequestVariable('form[fields][Category]', 'C');
154
GalleryUtilities::putRequestVariable('form[fields][Catalog Id]', '456');
155
GalleryUtilities::putRequestVariable('form[fields][Year]', '2004');
156
GalleryUtilities::putRequestVariable('form[action][save]', 1);
158
$results = $this->handleRequest($this->_albumS, $this->_albumS);
159
$this->assertEquals(array(array(), $this->_translate('Changes saved successfully')),
162
list ($ret, $data) = CustomFieldHelper::fetchFieldValues(array($this->_albumS), 'summary');
164
return $this->failWithStatus($ret);
166
$this->assertEquals(array('Category' => 'C'),
167
$data[$this->_albumS->getId()], 'Summary data 2');
169
list ($ret, $data) = CustomFieldHelper::fetchFieldValues(array($this->_albumS), 'detail');
171
return $this->failWithStatus($ret);
173
$this->assertEquals(array('Catalog Id' => '456', 'Year' => '2004'),
174
$data[$this->_albumS->getId()], 'Detail data 2');
177
CustomFieldHelper::fetchFieldValues(array($this->_albumS), null, 'album');
179
return $this->failWithStatus($ret);
181
$this->assertEquals(array('Category' => 'C', 'Catalog Id' => '456', 'Year' => '2004'),
182
$data[$this->_albumS->getId()], 'All data 2');
184
/* ==== Item Admin functions -- set album-specific settings ==== */
187
GalleryUtilities::removeFormVariables('form');
188
GalleryUtilities::putRequestVariable('form[cfAdmin]', '1');
189
GalleryUtilities::putRequestVariable('form[action][reset]', '1');
190
$results = $this->handleRequest($this->_albumS, $this->_albumS);
191
$this->assertEquals(array(array(), null), $results, 'Reset 2');
194
GalleryUtilities::removeFormVariables('form');
195
GalleryUtilities::putRequestVariable('form[cfAdmin]', '1');
196
GalleryUtilities::putRequestVariable('form[photo][newField]', 'Photo Fun');
197
GalleryUtilities::putRequestVariable('form[action][add][photo]', '1');
198
$results = $this->handleRequest($this->_albumS, $this->_albumS);
199
$this->assertEquals(array(array(), $this->_translate('New field added successfully')),
200
$results, 'Add Photo Field');
203
GalleryUtilities::removeFormVariables('form');
204
GalleryUtilities::putRequestVariable('form[cfAdmin]', '1');
205
GalleryUtilities::putRequestVariable('form[common][newField]', 'Photo Fun');
206
GalleryUtilities::putRequestVariable('form[action][add][common]', '1');
207
$results = $this->handleRequest($this->_albumS, $this->_albumS);
208
$this->assertEquals(array(array(), '<div class="giError">' .
209
$this->_translate('Field name already in use') . '</div>'), $results, 'Add Duplicate');
212
GalleryUtilities::removeFormVariables('form');
213
GalleryUtilities::putRequestVariable('form[cfAdmin]', '1');
214
GalleryUtilities::putRequestVariable('form[pickField]', 'Photo Fun');
215
GalleryUtilities::putRequestVariable('form[picklist]', "One\n Test\nHere ");
216
GalleryUtilities::putRequestVariable('form[action][picklist]', '1');
217
$results = $this->handleRequest($this->_albumS, $this->_albumS);
218
$this->assertEquals(array(array(), $this->_translate('Picklist updated successfully')),
219
$results, 'Set Picklist');
221
/* Update picklist */
222
GalleryUtilities::removeFormVariables('form');
223
GalleryUtilities::putRequestVariable('form[cfAdmin]', '1');
224
GalleryUtilities::putRequestVariable('form[pickField]', 'Photo Fun');
225
GalleryUtilities::putRequestVariable('form[picklist]', "\nHere\n Test\nOne");
226
GalleryUtilities::putRequestVariable('form[action][picklist]', '1');
227
$results = $this->handleRequest($this->_albumS, $this->_albumS);
228
$this->assertEquals(array(array(), $this->_translate('Picklist updated successfully')),
229
$results, 'Update Picklist');
231
/* Check data so far */
232
list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'customfield',
233
$this->_albumS->getId());
235
return $this->failWithStatus($ret);
238
$this->assertEquals(array('album' => 'Catalog Id`0`1|Year`0`1',
239
'common' => 'Category`1`0`A`B`C',
240
'photo' => 'Photographer`1`1|Photo Fun`0`1`Here`Test`One'),
241
$params, 'Check Data 1');
243
/* Update display settings */
244
GalleryUtilities::removeFormVariables('form');
245
GalleryUtilities::putRequestVariable('form[cfAdmin]', '1');
246
GalleryUtilities::putRequestVariable('form[common][index][0]', '0');
247
GalleryUtilities::putRequestVariable('form[common][summary][0]', 'on');
248
GalleryUtilities::putRequestVariable('form[common][detail][0]', 'on');
249
GalleryUtilities::putRequestVariable('form[album][index][0]', '1');
250
GalleryUtilities::putRequestVariable('form[album][index][1]', '0');
251
GalleryUtilities::putRequestVariable('form[album][detail][0]', 'on');
252
GalleryUtilities::putRequestVariable('form[photo][index][0]', '0');
253
GalleryUtilities::putRequestVariable('form[photo][index][1]', '1');
254
GalleryUtilities::putRequestVariable('form[photo][summary][0]', 'on');
255
GalleryUtilities::putRequestVariable('form[photo][detail][0]', 'on');
256
GalleryUtilities::putRequestVariable('form[action][save]', '1');
257
$results = $this->handleRequest($this->_albumS, $this->_albumS);
258
$this->assertEquals(array(array(),
259
$this->_translate('Display settings saved successfully')), $results, 'Update Display');
261
/* Check data so far */
262
list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'customfield',
263
$this->_albumS->getId());
265
return $this->failWithStatus($ret);
268
$this->assertEquals(array('album' => 'Year`0`1|Catalog Id`0`0',
269
'common' => 'Category`1`1`A`B`C',
270
'photo' => 'Photographer`1`1|Photo Fun`0`0`Here`Test`One'),
271
$params, 'Check Data 2');
274
GalleryUtilities::removeFormVariables('form');
275
GalleryUtilities::putRequestVariable('form[cfAdmin]', '1');
276
GalleryUtilities::putRequestVariable('form[common][goField]', 'Category');
277
GalleryUtilities::putRequestVariable('form[common][goAction]', 'album');
278
GalleryUtilities::putRequestVariable('form[action][go][common]', '1');
279
$results = $this->handleRequest($this->_albumS, $this->_albumS);
280
$this->assertEquals(array(array(), $this->_translate('Field moved successfully')),
281
$results, 'Move Field to Album');
283
GalleryUtilities::removeFormVariables('form');
284
GalleryUtilities::putRequestVariable('form[cfAdmin]', '1');
285
GalleryUtilities::putRequestVariable('form[photo][goField]', 'Photo Fun');
286
GalleryUtilities::putRequestVariable('form[photo][goAction]', 'common');
287
GalleryUtilities::putRequestVariable('form[action][go][photo]', '1');
288
$results = $this->handleRequest($this->_albumS, $this->_albumS);
289
$this->assertEquals(array(array(), $this->_translate('Field moved successfully')),
290
$results, 'Move Field to Common');
292
GalleryUtilities::removeFormVariables('form');
293
GalleryUtilities::putRequestVariable('form[cfAdmin]', '1');
294
GalleryUtilities::putRequestVariable('form[album][goField]', 'Year');
295
GalleryUtilities::putRequestVariable('form[album][goAction]', 'common');
296
GalleryUtilities::putRequestVariable('form[action][go][album]', '1');
297
$results = $this->handleRequest($this->_albumS, $this->_albumS);
298
$this->assertEquals(array(array(), $this->_translate('Field moved successfully')),
299
$results, 'Move Field to Common 2');
301
GalleryUtilities::removeFormVariables('form');
302
GalleryUtilities::putRequestVariable('form[cfAdmin]', '1');
303
GalleryUtilities::putRequestVariable('form[common][goField]', 'Year');
304
GalleryUtilities::putRequestVariable('form[common][goAction]', 'photo');
305
GalleryUtilities::putRequestVariable('form[action][go][common]', '1');
306
$results = $this->handleRequest($this->_albumS, $this->_albumS);
307
$this->assertEquals(array(array(), $this->_translate('Field moved successfully')),
308
$results, 'Move Field to Photo');
311
GalleryUtilities::removeFormVariables('form');
312
GalleryUtilities::putRequestVariable('form[cfAdmin]', '1');
313
GalleryUtilities::putRequestVariable('form[album][goField]', 'Catalog Id');
314
GalleryUtilities::putRequestVariable('form[album][goAction]', 'remove');
315
GalleryUtilities::putRequestVariable('form[action][go][album]', '1');
316
$results = $this->handleRequest($this->_albumS, $this->_albumS);
317
$this->assertEquals(array(array(), $this->_translate('Field removed successfully')),
318
$results, 'Remove Field');
320
/* Check data so far */
321
list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'customfield',
322
$this->_albumS->getId());
324
return $this->failWithStatus($ret);
327
$this->assertEquals(array('album' => 'Category`1`1`A`B`C',
328
'common' => 'Photo Fun`0`0`Here`Test`One',
329
'photo' => 'Photographer`1`1|Year`0`1'),
330
$params, 'Check Data 3');
332
/* Remove picklist */
333
GalleryUtilities::removeFormVariables('form');
334
GalleryUtilities::putRequestVariable('form[cfAdmin]', '1');
335
GalleryUtilities::putRequestVariable('form[pickField]', 'Photo Fun');
336
GalleryUtilities::putRequestVariable('form[picklist]', " \n ");
337
GalleryUtilities::putRequestVariable('form[action][picklist]', '1');
338
$results = $this->handleRequest($this->_albumS, $this->_albumS);
339
$this->assertEquals(array(array(), $this->_translate('Picklist updated successfully')),
340
$results, 'Remove Picklist');
342
/* Check data so far */
343
list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'customfield',
344
$this->_albumS->getId());
346
return $this->failWithStatus($ret);
349
$this->assertEquals(array('album' => 'Category`1`1`A`B`C',
350
'common' => 'Photo Fun`0`0',
351
'photo' => 'Photographer`1`1|Year`0`1'),
352
$params, 'Check Data 4');
354
/* Check appropriate field values have been removed with settings changes */
355
list ($ret, $data) = CustomFieldHelper::fetchFieldValues(array($this->_albumS));
357
return $this->failWithStatus($ret);
359
$this->assertEquals(array('Category' => 'C'),
360
$data[$this->_albumS->getId()], 'Field values');
363
function testMaliciousMarkup() {
364
$ret = $this->_setMarkupType('html', true);
366
return $this->failWithStatus($ret);
369
GalleryUtilities::putRequestVariable('form[fields][Catalog Id]', '1');
370
GalleryUtilities::putRequestVariable('form[fields][My Data]',
371
'fun<script>alert(1);</script>');
372
GalleryUtilities::putRequestVariable('form[fields][Location]',
373
'<p onclick="alert(2)">Click</p>');
374
GalleryUtilities::putRequestVariable('form[fields][Category]', 'A');
375
GalleryUtilities::putRequestVariable('form[action][save]', 1);
377
$results = $this->handleRequest($this->_itemG, $this->_itemG);
378
$this->assertEquals(array(array(), $this->_translate('Changes saved successfully')),
381
list ($ret, $data) = CustomFieldHelper::fetchFieldValues(array($this->_itemG), 'summary');
383
return $this->failWithStatus($ret);
385
$this->assertEquals(array('My Data' => 'fun', 'Location' => '<p>Click</p>'),
386
$data[$this->_itemG->getId()], 'Summary data');