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.
22
* ItemAdd controller tests
23
* @package GalleryCore
25
* @author Bharat Mediratta <bharat@menalto.com>
26
* @version $Revision: 15513 $
28
class ItemAddControllerTest extends GalleryControllerTestCase {
30
function ItemAddControllerTest($methodName) {
31
$this->GalleryControllerTestCase($methodName, 'core.ItemAdd');
37
list ($ret, $this->_album) = $this->_createRandomAlbum($this->_getRootId(),
38
array('orderBy' => 'orderWeight'));
40
print $ret->getAsHtml();
41
return $this->failWithStatus($ret);
43
$this->_markForCleanup($this->_album);
45
$path = 'modules/core/test/phpunit/ItemAddControllerTest.class';
46
$ret = GalleryCoreApi::registerFactoryImplementation(
47
'ItemAddPlugin', 'ItemAddControllerTestPluginSuccess',
48
'ItemAddControllerTestPluginSuccess', $path, 'coreTest', null);
50
print $ret->getAsHtml();
51
return $this->failWithStatus($ret);
54
$ret = GalleryCoreApi::registerFactoryImplementation(
55
'ItemAddPlugin', 'ItemAddControllerTestPluginContinue',
56
'ItemAddControllerTestPluginContinue', $path, 'coreTest', null);
58
print $ret->getAsHtml();
59
return $this->failWithStatus($ret);
62
$ret = GalleryCoreApi::registerFactoryImplementation(
63
'ItemAddPlugin', 'ItemAddControllerTestPluginFail', 'ItemAddControllerTestPluginFail',
64
$path, 'coreTest', null);
66
print $ret->getAsHtml();
67
return $this->failWithStatus($ret);
70
$ret = GalleryCoreApi::registerFactoryImplementation(
71
'GalleryToolkit', 'ItemAddControllerTestToolkit', 'ItemAddControllerTestToolkit',
72
$path, 'coreTest', null);
74
print $ret->getAsHtml();
75
return $this->failWithStatus($ret);
77
$this->_markToolkitForCleanup('ItemAddControllerTestToolkit');
78
$this->_markFactoryModuleForCleanup('coreTest');
80
$ret = GalleryCoreApi::registerToolkitOperation(
81
'ItemAddControllerTestToolkit', array('test/archive'), 'extract',
82
array(), 'test-description');
84
print $ret->getAsHtml();
85
return $this->failWithStatus($ret);
88
$this->_controller->setOptionInstances(array());
91
function testHandleRequestContinueAdding() {
92
GalleryUtilities::putRequestVariable('itemId', $this->_album->getId());
93
GalleryUtilities::putRequestVariable('addPlugin', 'ItemAddControllerTestPluginContinue');
95
$results = $this->handleRequest();
96
$this->assertEquals(array('delegate' =>
97
array('view' => 'core.ItemAdmin',
98
'subView' => 'core.ItemAdd',
99
'addPlugin' => 'ItemAddControllerTestPluginContinue'),
105
function testHandleRequestDoneAdding() {
106
list ($ret, $item) = GalleryTestCase::_createRandomDataItem($this->_album->getId());
108
return $this->failWithStatus($ret);
111
GalleryUtilities::putRequestVariable('itemId', $this->_album->getId());
112
GalleryUtilities::putRequestVariable('addPlugin', 'ItemAddControllerTestPluginSuccess');
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()))),
126
function testHandleRequestWithOption() {
127
list ($ret, $item) = GalleryTestCase::_createRandomDataItem($this->_album->getId());
129
return $this->failWithStatus($ret);
132
$this->_controller->setOptionInstances(
133
array('ItemAddTestOption' => new ItemAddTestOption()));
135
GalleryUtilities::putRequestVariable('itemId', $this->_album->getId());
136
GalleryUtilities::putRequestVariable('addPlugin', 'ItemAddControllerTestPluginSuccess');
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')),
150
function testHandleRequestWithArchiveExtract() {
151
$sortAddedFilesFunc =
152
create_function('$a, $b', 'return strcmp($a[\'fileName\'], $b[\'fileName\']);');
154
list ($ret, $archive) = $this->_createRandomDataItem(
155
$this->_album->getId(), 'test/archive',
156
array('pathComponent' => 'foo.zip', 'title' => 'foo', 'summary' => 'hoho'));
158
return $this->failWithStatus($ret);
160
list ($ret, $item) = GalleryTestCase::_createRandomDataItem($this->_album->getId());
162
return $this->failWithStatus($ret);
165
$this->_controller->setOptionInstances(
166
array('ItemAddTestOption' => new ItemAddTestOption()));
168
GalleryUtilities::putRequestVariable('itemId', $this->_album->getId());
169
GalleryUtilities::putRequestVariable('addPlugin', 'ItemAddControllerTestPluginSuccess');
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'];
182
* Sort the added files list by filename because we can't guarantee
183
* the file order across platforms.
185
usort($addedFiles, $sortAddedFilesFunc);
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');
195
list ($ret, $childIds) = GalleryCoreApi::fetchChildItemIds($this->_album);
197
return $this->failWithStatus($ret);
199
$this->assert(!in_array($archive->getId(), $childIds), 'archive item has been deleted');
201
list ($ret, $item) = GalleryCoreApi::loadEntitiesById($addedFiles[1]['id']);
203
return $this->failWithStatus($ret);
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');
210
function testHandleRequestWithErrors() {
211
GalleryUtilities::putRequestVariable('itemId', $this->_album->getId());
212
GalleryUtilities::putRequestVariable('addPlugin', 'ItemAddControllerTestPluginFail');
214
$results = $this->handleRequest();
215
$this->assertEquals(array('delegate' =>
216
array('view' => 'core.ItemAdmin',
217
'subView' => 'core.ItemAdd',
218
'addPlugin' => 'ItemAddControllerTestPluginFail'),
220
'error' => array('failed')),
226
* ItemAddPlugin that doesn't fail, nor complete
227
* @package GalleryCore
228
* @subpackage PHPUnit
230
class ItemAddControllerTestPluginContinue {
231
function handleRequest($form, &$item) {
232
if (!GalleryUtilities::isExactlyA($item, 'GalleryAlbumItem')) {
234
array(GalleryCoreApi::error(ERROR_BAD_PARAMETER), null, null);
237
return array(null, array(), array());
242
* ItemAddPlugin that always completes
243
* @package GalleryCore
244
* @subpackage PHPUnit
246
class ItemAddControllerTestPluginSuccess {
247
function handleRequest($form, &$item) {
248
if (!GalleryUtilities::isExactlyA($item, 'GalleryAlbumItem')) {
250
array(GalleryCoreApi::error(ERROR_BAD_PARAMETER), null, null);
253
/* Pretend we just added item(s) that are already there */
254
list ($ret, $childIds) = GalleryCoreApi::fetchChildItemIds($item);
256
return array($ret, null, null);
258
$addedFiles = array();
259
foreach ($childIds as $id) {
260
$addedFiles[] = array('fileName' => 'foo-' . $id, 'id' => $id, 'warnings' => array());
263
return array(null, array(), array('addedFiles' => $addedFiles));
268
* ItemAddPlugin that always fails
269
* @package GalleryCore
270
* @subpackage PHPUnit
272
class ItemAddControllerTestPluginFail {
273
function handleRequest($form, &$item) {
274
if (!GalleryUtilities::isExactlyA($item, 'GalleryAlbumItem')) {
276
array(GalleryCoreApi::error(ERROR_BAD_PARAMETER), null, null);
278
return array(null, array('failed'), array());
284
* @package GalleryCore
285
* @subpackage PHPUnit
287
class ItemAddTestOption extends ItemAddOption {
288
function ItemAddTestOption() {
291
function handleRequestAfterAdd($form, $items) {
294
for ($i = 0; $i < count($items); $i++) {
295
$errors[] = 'an error';
296
$warnings[$i] = array();
297
$warnings[$i][] = 'a warning';
299
return array(null, $errors, $warnings);
302
function isAppropriate() {
303
return array(null, true);
307
GalleryCoreApi::requireOnce('modules/core/classes/GalleryToolkit.class');
312
* @package GalleryCore
313
* @subpackage PHPUnit
316
class ItemAddControllerTestToolkit extends GalleryToolkit {
318
function performOperation($mimeType, $operationName, $sourceFilename,
319
$destFilename, $parameters, $context=array()) {
321
$platform =& $gallery->getPlatform();
323
if ($operationName != 'extract') {
324
return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER),
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');
333
return array(null, $mimeType, $context);
337
function sortAddedFiles($a, $b) {
338
return strcmp($a['fileName'], $b['fileName']);