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

« back to all changes in this revision

Viewing changes to modules/core/test/phpunit/ItemAddControllerTest.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
 
 * ItemAdd controller tests
23
 
 * @package GalleryCore
24
 
 * @subpackage PHPUnit
25
 
 * @author Bharat Mediratta <bharat@menalto.com>
26
 
 * @version $Revision: 15513 $
27
 
 */
28
 
class ItemAddControllerTest extends GalleryControllerTestCase {
29
 
 
30
 
    function ItemAddControllerTest($methodName) {
31
 
        $this->GalleryControllerTestCase($methodName, 'core.ItemAdd');
32
 
    }
33
 
 
34
 
    function setUp() {
35
 
        parent::setUp();
36
 
 
37
 
        list ($ret, $this->_album) = $this->_createRandomAlbum($this->_getRootId(),
38
 
                                                               array('orderBy' => 'orderWeight'));
39
 
        if ($ret) {
40
 
            print $ret->getAsHtml();
41
 
            return $this->failWithStatus($ret);
42
 
        }
43
 
        $this->_markForCleanup($this->_album);
44
 
 
45
 
        $path = 'modules/core/test/phpunit/ItemAddControllerTest.class';
46
 
        $ret = GalleryCoreApi::registerFactoryImplementation(
47
 
            'ItemAddPlugin', 'ItemAddControllerTestPluginSuccess',
48
 
            'ItemAddControllerTestPluginSuccess', $path, 'coreTest', null);
49
 
        if ($ret) {
50
 
            print $ret->getAsHtml();
51
 
            return $this->failWithStatus($ret);
52
 
        }
53
 
 
54
 
        $ret = GalleryCoreApi::registerFactoryImplementation(
55
 
            'ItemAddPlugin', 'ItemAddControllerTestPluginContinue',
56
 
            'ItemAddControllerTestPluginContinue', $path, 'coreTest', null);
57
 
        if ($ret) {
58
 
            print $ret->getAsHtml();
59
 
            return $this->failWithStatus($ret);
60
 
        }
61
 
 
62
 
        $ret = GalleryCoreApi::registerFactoryImplementation(
63
 
            'ItemAddPlugin', 'ItemAddControllerTestPluginFail', 'ItemAddControllerTestPluginFail',
64
 
            $path, 'coreTest', null);
65
 
        if ($ret) {
66
 
            print $ret->getAsHtml();
67
 
            return $this->failWithStatus($ret);
68
 
        }
69
 
 
70
 
        $ret = GalleryCoreApi::registerFactoryImplementation(
71
 
            'GalleryToolkit', 'ItemAddControllerTestToolkit', 'ItemAddControllerTestToolkit',
72
 
            $path, 'coreTest', null);
73
 
        if ($ret) {
74
 
            print $ret->getAsHtml();
75
 
            return $this->failWithStatus($ret);
76
 
        }
77
 
        $this->_markToolkitForCleanup('ItemAddControllerTestToolkit');
78
 
        $this->_markFactoryModuleForCleanup('coreTest');
79
 
 
80
 
        $ret = GalleryCoreApi::registerToolkitOperation(
81
 
            'ItemAddControllerTestToolkit', array('test/archive'), 'extract',
82
 
            array(), 'test-description');
83
 
        if ($ret) {
84
 
            print $ret->getAsHtml();
85
 
            return $this->failWithStatus($ret);
86
 
        }
87
 
 
88
 
        $this->_controller->setOptionInstances(array());
89
 
    }
90
 
 
91
 
    function testHandleRequestContinueAdding() {
92
 
        GalleryUtilities::putRequestVariable('itemId', $this->_album->getId());
93
 
        GalleryUtilities::putRequestVariable('addPlugin', 'ItemAddControllerTestPluginContinue');
94
 
 
95
 
        $results = $this->handleRequest();
96
 
        $this->assertEquals(array('delegate' =>
97
 
                                  array('view' => 'core.ItemAdmin',
98
 
                                        'subView' => 'core.ItemAdd',
99
 
                                        'addPlugin' => 'ItemAddControllerTestPluginContinue'),
100
 
                                  'status' => array(),
101
 
                                  'error' => array()),
102
 
                            $results);
103
 
    }
104
 
 
105
 
    function testHandleRequestDoneAdding() {
106
 
        list ($ret, $item) = GalleryTestCase::_createRandomDataItem($this->_album->getId());
107
 
        if ($ret) {
108
 
            return $this->failWithStatus($ret);
109
 
        }
110
 
 
111
 
        GalleryUtilities::putRequestVariable('itemId', $this->_album->getId());
112
 
        GalleryUtilities::putRequestVariable('addPlugin', 'ItemAddControllerTestPluginSuccess');
113
 
 
114
 
        $results = $this->handleRequest();
115
 
        $this->assertEquals(array('redirect' => array('view' => 'core.ItemAdmin',
116
 
                                                      'subView' => 'core.ItemAddConfirmation',
117
 
                                                      'itemId' => $this->_album->getId()),
118
 
                                  'status' => array('addedFiles' => array(
119
 
                                                        array('fileName' => 'foo-' . $item->getId(),
120
 
                                                              'id' => $item->getId(),
121
 
                                                              'warnings' => array()))),
122
 
                                  'error' => array()),
123
 
                            $results);
124
 
    }
125
 
 
126
 
    function testHandleRequestWithOption() {
127
 
        list ($ret, $item) = GalleryTestCase::_createRandomDataItem($this->_album->getId());
128
 
        if ($ret) {
129
 
            return $this->failWithStatus($ret);
130
 
        }
131
 
 
132
 
        $this->_controller->setOptionInstances(
133
 
            array('ItemAddTestOption' => new ItemAddTestOption()));
134
 
 
135
 
        GalleryUtilities::putRequestVariable('itemId', $this->_album->getId());
136
 
        GalleryUtilities::putRequestVariable('addPlugin', 'ItemAddControllerTestPluginSuccess');
137
 
 
138
 
        $results = $this->handleRequest();
139
 
        $this->assertEquals(array('redirect' => array('view' => 'core.ItemAdmin',
140
 
                                                      'subView' => 'core.ItemAddConfirmation',
141
 
                                                      'itemId' => $this->_album->getId()),
142
 
                                  'status' => array('addedFiles' => array(
143
 
                                                        array('fileName' => 'foo-' . $item->getId(),
144
 
                                                              'id' => $item->getId(),
145
 
                                                              'warnings' => array('a warning')))),
146
 
                                  'error' => array('an error')),
147
 
                            $results);
148
 
    }
149
 
 
150
 
    function testHandleRequestWithArchiveExtract() {
151
 
        $sortAddedFilesFunc =
152
 
            create_function('$a, $b', 'return strcmp($a[\'fileName\'], $b[\'fileName\']);');
153
 
 
154
 
        list ($ret, $archive) = $this->_createRandomDataItem(
155
 
            $this->_album->getId(), 'test/archive',
156
 
            array('pathComponent' => 'foo.zip', 'title' => 'foo', 'summary' => 'hoho'));
157
 
        if ($ret) {
158
 
            return $this->failWithStatus($ret);
159
 
        }
160
 
        list ($ret, $item) = GalleryTestCase::_createRandomDataItem($this->_album->getId());
161
 
        if ($ret) {
162
 
            return $this->failWithStatus($ret);
163
 
        }
164
 
 
165
 
        $this->_controller->setOptionInstances(
166
 
            array('ItemAddTestOption' => new ItemAddTestOption()));
167
 
 
168
 
        GalleryUtilities::putRequestVariable('itemId', $this->_album->getId());
169
 
        GalleryUtilities::putRequestVariable('addPlugin', 'ItemAddControllerTestPluginSuccess');
170
 
 
171
 
        $results = $this->handleRequest();
172
 
        $this->assertEquals(array('view' => 'core.ItemAdmin',
173
 
                                            'subView' => 'core.ItemAddConfirmation',
174
 
                                            'itemId' => $this->_album->getId()),
175
 
                            $results['redirect'], 'redirect');
176
 
        $this->assertEquals(array('an error', 'an error', 'an error', 'an error'),
177
 
                            $results['error'], 'error');
178
 
        $this->assertEquals(4, count($results['status']['addedFiles']), 'added 4 items');
179
 
        $addedFiles = $results['status']['addedFiles'];
180
 
 
181
 
        /*
182
 
         * Sort the added files list by filename because we can't guarantee
183
 
         * the file order across platforms.
184
 
         */
185
 
        usort($addedFiles, $sortAddedFilesFunc);
186
 
 
187
 
        $this->assertEquals(array('bar.ext', 'blah.ext', 'foo-' . $item->getId(), 'foo.ext'),
188
 
                            array($addedFiles[0]['fileName'], $addedFiles[1]['fileName'],
189
 
                                  $addedFiles[2]['fileName'], $addedFiles[3]['fileName']), 'files');
190
 
        $this->assertEquals(array('a warning'), $addedFiles[0]['warnings'], 'warning 1');
191
 
        $this->assertEquals(array('a warning'), $addedFiles[1]['warnings'], 'warning 2');
192
 
        $this->assertEquals(array('a warning'), $addedFiles[2]['warnings'], 'warning 3');
193
 
        $this->assertEquals(array('a warning'), $addedFiles[3]['warnings'], 'warning 4');
194
 
 
195
 
        list ($ret, $childIds) = GalleryCoreApi::fetchChildItemIds($this->_album);
196
 
        if ($ret) {
197
 
            return $this->failWithStatus($ret);
198
 
        }
199
 
        $this->assert(!in_array($archive->getId(), $childIds), 'archive item has been deleted');
200
 
 
201
 
        list ($ret, $item) = GalleryCoreApi::loadEntitiesById($addedFiles[1]['id']);
202
 
        if ($ret) {
203
 
            return $this->failWithStatus($ret);
204
 
        }
205
 
        $this->assertEquals('blah', $item->getTitle(), 'title set with base filename');
206
 
        $this->assertEquals('hoho', $item->getSummary(), 'summary set from archive');
207
 
        $this->assert($item->getParentId() != $this->_album->getId(), 'should be in subalbum');
208
 
    }
209
 
 
210
 
    function testHandleRequestWithErrors() {
211
 
        GalleryUtilities::putRequestVariable('itemId', $this->_album->getId());
212
 
        GalleryUtilities::putRequestVariable('addPlugin', 'ItemAddControllerTestPluginFail');
213
 
 
214
 
        $results = $this->handleRequest();
215
 
        $this->assertEquals(array('delegate' =>
216
 
                                  array('view' => 'core.ItemAdmin',
217
 
                                        'subView' => 'core.ItemAdd',
218
 
                                        'addPlugin' => 'ItemAddControllerTestPluginFail'),
219
 
                                  'status' => array(),
220
 
                                  'error' => array('failed')),
221
 
                            $results);
222
 
    }
223
 
}
224
 
 
225
 
/**
226
 
 * ItemAddPlugin that doesn't fail, nor complete
227
 
 * @package GalleryCore
228
 
 * @subpackage PHPUnit
229
 
 */
230
 
class ItemAddControllerTestPluginContinue {
231
 
    function handleRequest($form, &$item) {
232
 
        if (!GalleryUtilities::isExactlyA($item, 'GalleryAlbumItem')) {
233
 
            return
234
 
                array(GalleryCoreApi::error(ERROR_BAD_PARAMETER), null, null);
235
 
        }
236
 
 
237
 
        return array(null, array(), array());
238
 
    }
239
 
}
240
 
 
241
 
/**
242
 
 * ItemAddPlugin that always completes
243
 
 * @package GalleryCore
244
 
 * @subpackage PHPUnit
245
 
 */
246
 
class ItemAddControllerTestPluginSuccess {
247
 
    function handleRequest($form, &$item) {
248
 
        if (!GalleryUtilities::isExactlyA($item, 'GalleryAlbumItem')) {
249
 
            return
250
 
                array(GalleryCoreApi::error(ERROR_BAD_PARAMETER), null, null);
251
 
        }
252
 
 
253
 
        /* Pretend we just added item(s) that are already there */
254
 
        list ($ret, $childIds) = GalleryCoreApi::fetchChildItemIds($item);
255
 
        if ($ret) {
256
 
            return array($ret, null, null);
257
 
        }
258
 
        $addedFiles = array();
259
 
        foreach ($childIds as $id) {
260
 
            $addedFiles[] = array('fileName' => 'foo-' . $id, 'id' => $id, 'warnings' => array());
261
 
        }
262
 
 
263
 
        return array(null, array(), array('addedFiles' => $addedFiles));
264
 
    }
265
 
}
266
 
 
267
 
/**
268
 
 * ItemAddPlugin that always fails
269
 
 * @package GalleryCore
270
 
 * @subpackage PHPUnit
271
 
 */
272
 
class ItemAddControllerTestPluginFail {
273
 
    function handleRequest($form, &$item) {
274
 
        if (!GalleryUtilities::isExactlyA($item, 'GalleryAlbumItem')) {
275
 
            return
276
 
                array(GalleryCoreApi::error(ERROR_BAD_PARAMETER), null, null);
277
 
        }
278
 
        return array(null, array('failed'), array());
279
 
    }
280
 
}
281
 
 
282
 
/**
283
 
 * Test ItemAddOption
284
 
 * @package GalleryCore
285
 
 * @subpackage PHPUnit
286
 
 */
287
 
class ItemAddTestOption extends ItemAddOption {
288
 
    function ItemAddTestOption() {
289
 
    }
290
 
 
291
 
    function handleRequestAfterAdd($form, $items) {
292
 
        $errors = array();
293
 
        $warnings = array();
294
 
        for ($i = 0; $i < count($items); $i++) {
295
 
            $errors[] = 'an error';
296
 
            $warnings[$i] = array();
297
 
            $warnings[$i][] = 'a warning';
298
 
        }
299
 
        return array(null, $errors, $warnings);
300
 
    }
301
 
 
302
 
    function isAppropriate() {
303
 
        return array(null, true);
304
 
    }
305
 
}
306
 
 
307
 
GalleryCoreApi::requireOnce('modules/core/classes/GalleryToolkit.class');
308
 
 
309
 
/**
310
 
 * Test toolkit
311
 
 *
312
 
 * @package GalleryCore
313
 
 * @subpackage PHPUnit
314
 
 *
315
 
 */
316
 
class ItemAddControllerTestToolkit extends GalleryToolkit {
317
 
 
318
 
    function performOperation($mimeType, $operationName, $sourceFilename,
319
 
                              $destFilename, $parameters, $context=array()) {
320
 
        global $gallery;
321
 
        $platform =& $gallery->getPlatform();
322
 
 
323
 
        if ($operationName != 'extract') {
324
 
            return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER),
325
 
                         null, null);
326
 
        }
327
 
 
328
 
        $platform->copy(__FILE__, $destFilename . '/foo.ext');
329
 
        $platform->copy(__FILE__, $destFilename . '/bar.ext');
330
 
        $platform->mkdir($destFilename . '/subdir');
331
 
        $platform->copy(__FILE__, $destFilename . '/subdir/blah.ext');
332
 
 
333
 
        return array(null, $mimeType, $context);
334
 
    }
335
 
}
336
 
 
337
 
function sortAddedFiles($a, $b) {
338
 
    return strcmp($a['fileName'], $b['fileName']);
339
 
}
340
 
?>