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

« back to all changes in this revision

Viewing changes to modules/customfield/test/phpunit/CustomFieldItemEditPluginTest.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/customfield/classes/CustomFieldHelper.class');
22
 
 
23
 
/**
24
 
 * CustomFieldItemEdit controller tests
25
 
 * @package CustomField
26
 
 * @subpackage PHPUnit
27
 
 * @author Alan Harder <alan.harder@sun.com>
28
 
 * @version $Revision: 15513 $
29
 
 */
30
 
class CustomFieldItemEditPluginTest extends ItemEditPluginTestCase {
31
 
 
32
 
    function CustomFieldItemEditPluginTest($methodName) {
33
 
        $this->ItemEditPluginTestCase($methodName, 'customfield', 'CustomFieldItemEdit');
34
 
    }
35
 
 
36
 
    function setUp() {
37
 
        parent::setUp();
38
 
 
39
 
        list ($ret, $this->_albumG) = $this->_createRandomAlbum($this->_getRootId());
40
 
        if ($ret) {
41
 
            print $ret->getAsHtml();
42
 
            return $this->failWithStatus($ret);
43
 
        }
44
 
        $this->_markForCleanup($this->_albumG);
45
 
        list ($ret, $this->_itemG) = $this->_createRandomDataItem($this->_albumG->getId(),
46
 
                                     'image/test', array(), __FILE__, 'GalleryPhotoItem');
47
 
        if ($ret) {
48
 
            print $ret->getAsHtml();
49
 
            return $this->failWithStatus($ret);
50
 
        }
51
 
        list ($ret, $this->_albumS) = $this->_createRandomAlbum($this->_getRootId());
52
 
        if ($ret) {
53
 
            print $ret->getAsHtml();
54
 
            return $this->failWithStatus($ret);
55
 
        }
56
 
        $this->_markForCleanup($this->_albumS);
57
 
        list ($ret, $this->_itemS) = $this->_createRandomDataItem($this->_albumS->getId(),
58
 
                                     'video/test', array(), __FILE__, 'GalleryMovieItem');
59
 
        if ($ret) {
60
 
            print $ret->getAsHtml();
61
 
            return $this->failWithStatus($ret);
62
 
        }
63
 
 
64
 
        $ret = $this->_markPluginParametersForCleanup('module', 'customfield');
65
 
        if ($ret) {
66
 
            print $ret->getAsHtml();
67
 
            return $ret;
68
 
        }
69
 
 
70
 
        $ret = CustomFieldHelper::saveParameters(array(
71
 
            'common' => 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())
76
 
            ),
77
 
            'album' => array(),
78
 
            'photo' => 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'))
83
 
            )
84
 
        ));
85
 
        if ($ret) {
86
 
            print $ret->getAsHtml();
87
 
            return $this->failWithStatus($ret);
88
 
        }
89
 
        $ret = CustomFieldHelper::saveParameters(array(
90
 
            'common' => array(
91
 
                array('field' => 'Category',
92
 
                      'summary' => true, 'detail' => false, 'choices' => array('A', 'B', 'C'))
93
 
            ),
94
 
            'album' => array(
95
 
                array('field' => 'Catalog Id',
96
 
                      'summary' => false, 'detail' => true, 'choices' => array()),
97
 
                array('field' => 'Year',
98
 
                      'summary' => false, 'detail' => true, 'choices' => array())
99
 
            ),
100
 
            'photo' => array(
101
 
                array('field' => 'Photographer',
102
 
                      'summary' => true, 'detail' => true, 'choices' => array()),
103
 
            )
104
 
        ), $this->_albumS->getId());
105
 
        if ($ret) {
106
 
            print $ret->getAsHtml();
107
 
            return $this->failWithStatus($ret);
108
 
        }
109
 
    }
110
 
 
111
 
    function testCustomFieldPlugin() {
112
 
        /* Reset */
113
 
        GalleryUtilities::putRequestVariable('form[action][reset]', '1');
114
 
        $results = $this->handleRequest($this->itemG, $this->_itemG);
115
 
        $this->assertEquals(array(array(), null), $results, 'Reset');
116
 
 
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);
123
 
 
124
 
        $results = $this->handleRequest($this->_itemG, $this->_itemG);
125
 
        $this->assertEquals(array(array(), $this->_translate('Changes saved successfully')),
126
 
                            $results);
127
 
 
128
 
        list ($ret, $data) = CustomFieldHelper::fetchFieldValues(array($this->_itemG), 'summary');
129
 
        if ($ret) {
130
 
            return $this->failWithStatus($ret);
131
 
        }
132
 
        $this->assertEquals(array('Location' => 'There'),
133
 
                $data[$this->_itemG->getId()], 'Summary data');
134
 
 
135
 
        list ($ret, $data) = CustomFieldHelper::fetchFieldValues(array($this->_itemG), 'detail');
136
 
        if ($ret) {
137
 
            return $this->failWithStatus($ret);
138
 
        }
139
 
        $this->assertEquals(array('Catalog Id' => '123', 'Location' => 'There'),
140
 
                            $data[$this->_itemG->getId()], 'Detail data');
141
 
 
142
 
        list ($ret, $data) =
143
 
                CustomFieldHelper::fetchFieldValues(array($this->_itemG), null, 'photo');
144
 
        if ($ret) {
145
 
            return $this->failWithStatus($ret);
146
 
        }
147
 
        $this->assertEquals(array('Catalog Id' => '123', 'My Data' => '',
148
 
                                  'Location' => 'There', 'Category' => 'B'),
149
 
                            $data[$this->_itemG->getId()], 'All data');
150
 
 
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);
157
 
 
158
 
        $results = $this->handleRequest($this->_albumS, $this->_albumS);
159
 
        $this->assertEquals(array(array(), $this->_translate('Changes saved successfully')),
160
 
                            $results);
161
 
 
162
 
        list ($ret, $data) = CustomFieldHelper::fetchFieldValues(array($this->_albumS), 'summary');
163
 
        if ($ret) {
164
 
            return $this->failWithStatus($ret);
165
 
        }
166
 
        $this->assertEquals(array('Category' => 'C'),
167
 
                $data[$this->_albumS->getId()], 'Summary data 2');
168
 
 
169
 
        list ($ret, $data) = CustomFieldHelper::fetchFieldValues(array($this->_albumS), 'detail');
170
 
        if ($ret) {
171
 
            return $this->failWithStatus($ret);
172
 
        }
173
 
        $this->assertEquals(array('Catalog Id' => '456', 'Year' => '2004'),
174
 
                            $data[$this->_albumS->getId()], 'Detail data 2');
175
 
 
176
 
        list ($ret, $data) =
177
 
                CustomFieldHelper::fetchFieldValues(array($this->_albumS), null, 'album');
178
 
        if ($ret) {
179
 
            return $this->failWithStatus($ret);
180
 
        }
181
 
        $this->assertEquals(array('Category' => 'C', 'Catalog Id' => '456', 'Year' => '2004'),
182
 
                            $data[$this->_albumS->getId()], 'All data 2');
183
 
 
184
 
        /* ==== Item Admin functions -- set album-specific settings ==== */
185
 
 
186
 
        /* Reset */
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');
192
 
 
193
 
        /* Add field */
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');
201
 
 
202
 
        /* Add duplicate */
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');
210
 
 
211
 
        /* Add picklist */
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');
220
 
 
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');
230
 
 
231
 
        /* Check data so far */
232
 
        list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'customfield',
233
 
                                                                        $this->_albumS->getId());
234
 
        if ($ret) {
235
 
            return $this->failWithStatus($ret);
236
 
        }
237
 
        ksort($params);
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');
242
 
 
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');
260
 
 
261
 
        /* Check data so far */
262
 
        list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'customfield',
263
 
                                                                        $this->_albumS->getId());
264
 
        if ($ret) {
265
 
            return $this->failWithStatus($ret);
266
 
        }
267
 
        ksort($params);
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');
272
 
 
273
 
        /* Move fields */
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');
282
 
 
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');
291
 
 
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');
300
 
 
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');
309
 
 
310
 
        /* Remove field */
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');
319
 
 
320
 
        /* Check data so far */
321
 
        list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'customfield',
322
 
                                                                        $this->_albumS->getId());
323
 
        if ($ret) {
324
 
            return $this->failWithStatus($ret);
325
 
        }
326
 
        ksort($params);
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');
331
 
 
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');
341
 
 
342
 
        /* Check data so far */
343
 
        list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'customfield',
344
 
                                                                        $this->_albumS->getId());
345
 
        if ($ret) {
346
 
            return $this->failWithStatus($ret);
347
 
        }
348
 
        ksort($params);
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');
353
 
 
354
 
        /* Check appropriate field values have been removed with settings changes */
355
 
        list ($ret, $data) = CustomFieldHelper::fetchFieldValues(array($this->_albumS));
356
 
        if ($ret) {
357
 
            return $this->failWithStatus($ret);
358
 
        }
359
 
        $this->assertEquals(array('Category' => 'C'),
360
 
                            $data[$this->_albumS->getId()], 'Field values');
361
 
    }
362
 
 
363
 
    function testMaliciousMarkup() {
364
 
        $ret = $this->_setMarkupType('html', true);
365
 
        if ($ret) {
366
 
            return $this->failWithStatus($ret);
367
 
        }
368
 
 
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);
376
 
 
377
 
        $results = $this->handleRequest($this->_itemG, $this->_itemG);
378
 
        $this->assertEquals(array(array(), $this->_translate('Changes saved successfully')),
379
 
                            $results);
380
 
 
381
 
        list ($ret, $data) = CustomFieldHelper::fetchFieldValues(array($this->_itemG), 'summary');
382
 
        if ($ret) {
383
 
            return $this->failWithStatus($ret);
384
 
        }
385
 
        $this->assertEquals(array('My Data' => 'fun', 'Location' => '&lt;p&gt;Click&lt;/p&gt;'),
386
 
                            $data[$this->_itemG->getId()], 'Summary data');
387
 
    }
388
 
}
389
 
?>