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
* ItemEditPhoto controller tests
23
* @package GalleryCore
25
* @author Bharat Mediratta <bharat@menalto.com>
26
* @version $Revision: 15513 $
28
class ItemEditPhotoPluginTest extends ItemEditPluginTestCase {
30
function ItemEditPhotoPluginTest($methodName) {
31
$this->ItemEditPluginTestCase($methodName, 'core', 'ItemEditPhoto');
37
list ($ret, $this->_album) = $this->_createRandomAlbum($this->_getRootId());
39
return $this->failWithStatus($ret);
41
$this->_markForCleanup($this->_album);
43
/* Register a dummy toolkit */
44
$path = 'modules/core/test/phpunit/ItemEditPhotoPluginTest.class';
45
$ret = GalleryCoreApi::registerFactoryImplementation(
46
'GalleryToolkit', 'ItemEditPhotoPluginTestToolkit', 'ItemEditPhotoPluginTestToolkit',
47
$path, 'coreTest', null);
49
print $ret->getAsHtml();
50
return $this->failWithStatus($ret);
52
$this->_markToolkitForCleanup('ItemEditPhotoPluginTestToolkit');
54
/* Register operations that we can perform on our mock data items */
55
$ret = GalleryCoreApi::registerToolkitOperation('ItemEditPhotoPluginTestToolkit',
56
array('test/file'), 'scale',
57
array(array('type' => 'int', 'description' => 'test'),
58
array('type' => 'int', 'description' => 'test')),
61
return $this->failWithStatus($ret);
64
$ret = GalleryCoreApi::registerToolkitOperation(
65
'ItemEditPhotoPluginTestToolkit', array('test/file'), 'convert-to-image/jpeg',
66
array(), 'test-description', 'image/jpeg');
68
return $this->failWithStatus($ret);
71
$ret = GalleryCoreApi::registerToolkitOperation('ItemEditPhotoPluginTestToolkit',
72
array('test/file'), 'rotate',
73
array(array('type' => 'int', 'description' => 'test')),
76
return $this->failWithStatus($ret);
79
$ret = GalleryCoreApi::registerToolkitOperation('ItemEditPhotoPluginTestToolkit',
80
array('test/file'), 'thumbnail',
81
array(array('type' => 'int', 'description' => 'test'),
82
array('type' => 'int', 'description' => 'test')),
83
'test-description', '');
85
print $ret->getAsHtml();
86
return $this->failWithStatus($ret);
89
$ret = GalleryCoreApi::registerToolkitOperation('ItemEditPhotoPluginTestToolkit',
90
array('test/file'), 'resize',
91
array(array('type' => 'int', 'description' => 'test'),
92
array('type' => 'int', 'description' => 'test')),
93
'test-description', '');
95
print $ret->getAsHtml();
96
return $this->failWithStatus($ret);
99
list ($ret, $this->_item) =
100
$this->_createRandomDataItem($this->_album->getId(), 'test/file');
102
print $ret->getAsHtml();
103
return $this->failWithStatus($ret);
106
$this->_unused = null;
109
function testEditResizes() {
110
list ($ret, $derivative[0]) =
111
$this->_createDerivative($this->_item, $this->_item->getId(),
112
DERIVATIVE_TYPE_IMAGE_PREFERRED, 'rotate|90');
114
return $this->failWithStatus($ret);
117
list ($ret, $derivative[1]) =
118
$this->_createDerivative($this->_item, $derivative[0]->getId(),
119
DERIVATIVE_TYPE_IMAGE_THUMBNAIL, 'thumbnail|200');
121
return $this->failWithStatus($ret);
124
list ($ret, $derivative[2]) = $this->_createDerivative(
125
$this->_item, $derivative[0]->getId(),
126
DERIVATIVE_TYPE_IMAGE_RESIZE, 'scale|500,500;convert-to-image/jpeg', 'test/file',
127
array('postFilterOperations' => 'test-operation|0'));
129
return $this->failWithStatus($ret);
131
/* Use valid inputs */
132
GalleryUtilities::putRequestVariable('form[action][save]', 1);
134
for ($i = 0; $i < 3; $i++) {
135
GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][active]', 1);
136
GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][width]', 250 * ($i+1));
137
GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][height]', 250 * ($i+1));
139
GalleryUtilities::putRequestVariable('form[resizes][2][width]', '50%');
140
GalleryUtilities::putRequestVariable('form[resizes][2][height]', 600);
142
/* Perform the request and verify that we succeeded */
143
$results = $this->handleRequest($this->_item, $this->_unused);
144
$this->assertEquals(array(array(), $this->_translate('Changes saved successfully')),
147
/* Examine the resizes and make sure they line up */
148
list ($ret, $resizeTable) =
149
GalleryCoreApi::fetchResizesByItemIds(array($this->_item->getId()));
151
return $this->failWithStatus($ret);
154
if (empty($resizeTable[$this->_item->getId()])) {
155
$this->assert(false, 'no resizes!');
157
$resizeTable = $resizeTable[$this->_item->getId()];
158
$this->assertEquals(3, count($resizeTable), 'resize count');
159
for ($i = 0; $i < 3; $i++) {
160
$resize = $resizeTable[$i];
161
if (!isset($resize)) {
164
$this->assertEquals($derivative[0]->getId(), $resize->getDerivativeSourceId(),
165
'Derivative Source id');
166
$expectedOperations = ($i < 2)
167
? ('scale|' . ($i ? 250 : 500) . ',' . ($i ? 250 : 500)) : 'scale|50%,600';
168
$expectedOperations .= ';convert-to-image/jpeg';
169
$this->assertEquals($expectedOperations, $resize->getDerivativeOperations(), $i);
170
/* check that postfilteroperations are maintained and added to new resizes.. */
171
$this->assertEquals('test-operation|0', $resize->getPostFilterOperations(),
174
/* Verify we reuse existing derivatives when possible */
175
$this->assertEquals($derivative[2]->getId(), $resizeTable[0]->getId(), 'derivative id');
179
function testEditResizesMissing() {
180
/* Use valid inputs */
181
GalleryUtilities::putRequestVariable('form[action][save]', 1);
183
for ($i = 0; $i < 3; $i++) {
184
GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][active]', 1);
187
/* Perform the request and verify that we failed */
188
$results = $this->handleRequest($this->_item, $this->_unused);
189
$this->assertEquals(array(array('form[error][resizes][0][size][missing]',
190
'form[error][resizes][1][size][missing]',
191
'form[error][resizes][2][size][missing]'),
196
function testEditResizesInvalid() {
197
/* Use valid inputs */
198
GalleryUtilities::putRequestVariable('form[action][save]', 1);
200
for ($i = 0; $i < 3; $i++) {
201
GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][active]', 1);
202
GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][width]', 'x' . rand());
203
GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][height]', 100);
205
GalleryUtilities::putRequestVariable('form[resizes][2][width]', 100);
206
GalleryUtilities::putRequestVariable('form[resizes][2][height]', 'x' . rand());
208
/* Perform the request and verify that we failed */
209
$results = $this->handleRequest($this->_item, $this->_unused);
210
$this->assertEquals(array(array('form[error][resizes][0][size][invalid]',
211
'form[error][resizes][1][size][invalid]',
212
'form[error][resizes][2][size][invalid]'),
217
function testEditResizesNoDerivative() {
218
$itemId = $this->_item->getId();
219
/* Use valid inputs */
220
GalleryUtilities::putRequestVariable('form[action][save]', 1);
222
for ($i = 0; $i < 3; $i++) {
223
GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][active]', 1);
224
GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][width]', 100 * ($i+1));
225
GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][height]', 100 * ($i+1));
227
GalleryUtilities::putRequestVariable('form[resizes][2][height]', 600);
229
/* Perform the request and verify that we succeeded */
230
$results = $this->handleRequest($this->_item, $this->_unused);
231
$this->assertEquals(array(array(), $this->_translate('Changes saved successfully')),
234
/* Examine the resizes and make sure they line up */
235
list ($ret, $resizeTable) =
236
GalleryCoreApi::fetchResizesByItemIds(array($itemId));
238
return $this->failWithStatus($ret);
240
list ($ret, $preferred) =
241
GalleryCoreApi::fetchPreferredSource($this->_item);
243
return $this->failWithStatus($ret);
246
if (!empty($resizeTable[$itemId])) {
247
$this->assertEquals(3, sizeof($resizeTable[$this->_item->getId()]));
248
for ($i = 0; $i < 3; $i++) {
249
$this->assertEquals($itemId, $resizeTable[$itemId][$i]->
250
getDerivativeSourceId(), 'Derivative Source id');
251
$expectedOperations =
252
($i < 2) ? ('scale|' . 100 * ($i+1) . ',' . 100 * ($i+1)) : 'scale|300,600';
253
$expectedOperations .= ';convert-to-image/jpeg';
256
$resizeTable[$itemId][$i]->getDerivativeOperations());
261
function testEditResizesRaw() {
263
* Test the situation when the original file is "raw" - cannot be resized or
264
* rotated; however prefered can
266
$itemId = $this->_item->getId();
267
$ret = GalleryCoreApi::registerToolkitOperation(
268
'ItemEditPhotoPluginTestToolkit', array('test/blob'), 'convert-to-image/jpeg',
269
array(), 'test-description', 'image/jpeg');
271
return $this->failWithStatus($ret);
274
list ($ret, $this->_blob) =
275
$this->_createRandomDataItem($this->_album->getId(), 'test/blob');
277
return $this->failWithStatus($ret);
279
list ($ret, $derivative[0]) =
280
$this->_createDerivative($this->_item, $itemId,
281
DERIVATIVE_TYPE_IMAGE_PREFERRED, 'convert-to-image/jpeg');
283
return $this->failWithStatus($ret);
286
/* Use valid inputs */
287
GalleryUtilities::putRequestVariable('form[action][save]', 1);
289
for ($i = 0; $i < 3; $i++) {
290
GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][active]', 1);
291
GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][width]', 100 * ($i+1));
292
GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][height]', 100 * ($i+1));
295
/* Perform the request and verify that we succeeded */
296
$results = $this->handleRequest($this->_item, $this->_unused);
297
$this->assertEquals(array(array(), $this->_translate('Changes saved successfully')),
299
list ($ret, $resizeTable) = GalleryCoreApi::fetchDerivativesByItemIds(
300
array($this->_item->getId()));
302
return $this->failWithStatus($ret);
304
$this->assert($resizeTable[$itemId], "no derivative");
305
$preferredId = $resizeTable[$itemId][0]->getId();
306
/* check that the derivatives are from the preferred source, not from the original */
307
$this->assertEquals(4, sizeof($resizeTable[$itemId]));
308
for ($i = 1; $i < 4; $i++) {
309
$this->assertEquals($preferredId, $resizeTable[$itemId][$i]->
310
getDerivativeSourceId(), 'Derivative Source id');
311
$this->assertEquals('scale|' . 100 * $i . ',' . 100 * $i . ';convert-to-image/jpeg',
312
$resizeTable[$itemId][$i]->
313
getDerivativeOperations(), 'Derivative Source id');
318
GalleryCoreApi::requireOnce('modules/core/classes/GalleryToolkit.class');
323
class ItemEditPhotoPluginTestToolkit extends GalleryToolkit {
325
* @see GalleryToolkit::performOperation
328
function performOperation($mimeType, $operationName, $sourceFilename,
329
$destFilename, $parameters, $context=array()) {
331
$platform =& $gallery->getPlatform();
333
if ($sourceFilename != $destFilename) {
334
$platform->copy($sourceFilename, $destFilename);
337
$fd = $platform->fopen($destFilename, 'a');
338
$platform->fwrite($fd, $operationName);
339
$platform->fclose($fd);
341
return array(null, $mimeType, $context);
345
* @see GalleryToolkit::mergeOperations
348
function mergeOperations($operation1, $args1, $operation2, $args2) {
349
/* Do something stupid, but predictable */
351
$operation1 . '+' . $operation2,
352
array_merge($args1, array('+'), $args2));