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

« back to all changes in this revision

Viewing changes to modules/exif/test/phpunit/ExifDescriptionOptionTest.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('lib/tools/phpunit/ItemAddOptionTestCase.class');
22
 
 
23
 
/**
24
 
 * ExifDescriptionOption tests
25
 
 * @package Exif
26
 
 * @subpackage PHPUnit
27
 
 * @author Alan Harder <alan.harder@sun.com>
28
 
 * @author Georg Rehfeld <rehfeld@georg-rehfeld.de>
29
 
 * @author Jozsef R.Nagy <jozsef.rnagy@site.hu>
30
 
 * @version $Revision: 15513 $
31
 
 */
32
 
class ExifDescriptionOptionTest extends ItemAddOptionTestCase {
33
 
 
34
 
    function ExifDescriptionOptionTest($methodName) {
35
 
        $this->ItemAddOptionTestCase($methodName, 'exif', 'ExifDescriptionOption');
36
 
    }
37
 
 
38
 
    function setUp() {
39
 
        global $gallery;
40
 
        parent::setUp();
41
 
        GalleryCoreApi::requireOnce('modules/exif/classes/ExifHelper.class');
42
 
 
43
 
        list ($ret, $this->_album) = $this->_createRandomAlbum($this->_getRootId());
44
 
        if ($ret) {
45
 
            print $ret->getAsHtml();
46
 
            return $this->failWithStatus($ret);
47
 
        }
48
 
        $this->_markForCleanup($this->_album);
49
 
 
50
 
        $ret = $this->_markPluginParametersForCleanup('module', 'exif');
51
 
        if ($ret) {
52
 
            print $ret->getAsHtml();
53
 
            return $this->failWithStatus($ret);
54
 
        }
55
 
 
56
 
        $this->_items = array();
57
 
        list ($ret, $this->_items[0]) = $this->_createRandomDataItem(
58
 
                $this->_album->getId(),
59
 
                'image/jpeg',
60
 
                array('summary' => 'original summary 0',
61
 
                      'description' => 'original description 0'),
62
 
                dirname(__FILE__) . '/../data/descr.jpg');
63
 
        if ($ret) {
64
 
            print $ret->getAsHtml();
65
 
            return $this->failWithStatus($ret);
66
 
        }
67
 
        list ($ret, $this->_items[1]) = $this->_createRandomDataItem(
68
 
                $this->_album->getId(),
69
 
                'image/jpeg',
70
 
                array('title' => 'original title',
71
 
                      'summary' => 'original summary',
72
 
                      'description' => 'original description',
73
 
                      'keywords' => 'original keyword 0; original keyword 1'),
74
 
                dirname(__FILE__) . '/../data/exif-iptc-xmp-ps.jpg');
75
 
        if ($ret) {
76
 
            print $ret->getAsHtml();
77
 
            return $this->failWithStatus($ret);
78
 
        }
79
 
 
80
 
        $this->_noWarnings = array();
81
 
        $this->_noErrors = array();
82
 
    }
83
 
 
84
 
    function testCreateDescription() {
85
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'exif', 'addOption',
86
 
                                                  EXIF_ITEM_DESCRIPTION);
87
 
        if ($ret) {
88
 
            return $this->failWithStatus($ret);
89
 
        }
90
 
 
91
 
        $result = $this->_handleRequestAfterAdd(array(), $this->_items);
92
 
        $this->assertEquals(array($this->_noErrors, $this->_noWarnings), $result);
93
 
 
94
 
        list ($ret, $item) = $this->_items[0]->refresh();
95
 
        if ($ret) {
96
 
            return $this->failWithStatus($ret);
97
 
        }
98
 
 
99
 
        $this->assertEquals('exif description', $item->getDescription(), 'description 0');
100
 
        $this->assertEquals('original summary 0', $item->getSummary(), 'summary 0');
101
 
    }
102
 
 
103
 
    function testMultipleCreateDescription() {
104
 
 
105
 
        $totalTestItems = 3;
106
 
        for ($j = 1; $j < $totalTestItems; $j++) {
107
 
            list ($ret, $this->_items[$j]) =
108
 
                $this->_createRandomDataItem($this->_album->getId(), 'image/jpeg',
109
 
                                             array('summary' => "original summary $j",
110
 
                                                   'description' => "original description $j"),
111
 
                                             dirname(__FILE__) . '/../data/descr.jpg');
112
 
            if ($ret) {
113
 
                return $this->failWithStatus($ret);
114
 
            }
115
 
        }
116
 
 
117
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'exif', 'addOption',
118
 
                                                  EXIF_ITEM_DESCRIPTION);
119
 
        if ($ret) {
120
 
            return $this->failWithStatus($ret);
121
 
        }
122
 
 
123
 
        $result = $this->_handleRequestAfterAdd(array(), $this->_items);
124
 
        $this->assertEquals(array($this->_noErrors, $this->_noWarnings), $result);
125
 
 
126
 
        for ($j = 0; $j < $totalTestItems; $j++) {
127
 
            list ($ret, $item) = $this->_items[$j]->refresh();
128
 
            if ($ret) {
129
 
                return $this->failWithStatus($ret);
130
 
            }
131
 
            $this->assertEquals('exif description', $item->getDescription(), "description $j");
132
 
            $this->assertEquals("original summary $j", $item->getSummary(), "summary $j");
133
 
        }
134
 
    }
135
 
 
136
 
    function testCreateWithCheckDisabled() {
137
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'exif', 'addOption', '0');
138
 
        if ($ret) {
139
 
            return $this->failWithStatus($ret);
140
 
        }
141
 
 
142
 
        $result = $this->_handleRequestAfterAdd(array(), $this->_items);
143
 
        $this->assertEquals(array($this->_noErrors, $this->_noWarnings), $result);
144
 
 
145
 
        list ($ret, $item) = $this->_items[1]->refresh();
146
 
        if ($ret) {
147
 
            return $this->failWithStatus($ret);
148
 
        }
149
 
 
150
 
        $this->assertEquals('original title', $item->getTitle(), 'title');
151
 
        $this->assertEquals('original description', $item->getDescription(), 'description');
152
 
        $this->assertEquals('original summary', $item->getSummary(), 'summary');
153
 
        $this->assertEquals('original keyword 0; original keyword 1', $item->getKeywords(),
154
 
            'keywords mismatch');
155
 
    }
156
 
 
157
 
    function testMultipleCreateWithCheckDisabled() {
158
 
 
159
 
        $totalTestItems = 3;
160
 
        for ($j = 1; $j < $totalTestItems; $j++) {
161
 
            list ($ret, $this->_items[$j]) =
162
 
                $this->_createRandomDataItem($this->_album->getId(), 'image/jpeg',
163
 
                                             array('summary' => "original summary $j",
164
 
                                                   'description' => "original description $j"),
165
 
                                             dirname(__FILE__) . '/../data/descr.jpg');
166
 
            if ($ret) {
167
 
                return $this->failWithStatus($ret);
168
 
            }
169
 
        }
170
 
 
171
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'exif', 'addOption', '0');
172
 
        if ($ret) {
173
 
            return $this->failWithStatus($ret);
174
 
        }
175
 
 
176
 
        $result = $this->_handleRequestAfterAdd(array(), $this->_items);
177
 
        $this->assertEquals(array($this->_noErrors, $this->_noWarnings), $result);
178
 
 
179
 
        for ($j = 0; $j < $totalTestItems; $j++) {
180
 
            list ($ret, $item) = $this->_items[$j]->refresh();
181
 
            if ($ret) {
182
 
                return $this->failWithStatus($ret);
183
 
            }
184
 
            $this->assertEquals("original description $j", $item->getDescription(),
185
 
                                "description $j");
186
 
            $this->assertEquals("original summary $j", $item->getSummary(), "summary $j");
187
 
        }
188
 
    }
189
 
 
190
 
    function testTitleKeywords() {
191
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'exif', 'addOption',
192
 
                                                  IPTC_ITEM_KEYWORDS | IPTC_ITEM_TITLE);
193
 
        if ($ret) {
194
 
            return $this->failWithStatus($ret);
195
 
        }
196
 
 
197
 
        $result = $this->_handleRequestAfterAdd(array(), $this->_items);
198
 
        $this->assertEquals(array($this->_noErrors, $this->_noWarnings), $result);
199
 
 
200
 
        list ($ret, $item) = $this->_items[1]->refresh();
201
 
        if ($ret) {
202
 
            return $this->failWithStatus($ret);
203
 
        }
204
 
 
205
 
        $this->assertEquals('ObjectName, Dokumenttitel', $item->getTitle(), 'title mismatch');
206
 
        $this->assertEquals('keyword0; keyword1; keyword2', $item->getKeywords(),
207
 
            'keywords mismatch');
208
 
    }
209
 
 
210
 
    function testRotateOptionOff() {
211
 
        list ($ret, $this->_items[0]) =
212
 
                $this->_createRandomDataItem($this->_album->getId(), 'image/jpeg',
213
 
                                             array(),
214
 
                                             dirname(__FILE__) . '/../data/descr.jpg');
215
 
        if ($ret) {
216
 
            return $this->failWithStatus($ret);
217
 
        }
218
 
 
219
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'exif', 'addOption',
220
 
                                                        EXIF_ITEM_ROTATE_PRESERVE);
221
 
        if ($ret) {
222
 
            return $this->failWithStatus($ret);
223
 
        }
224
 
 
225
 
        $result = $this->_handleRequestAfterAdd(array(), $this->_items);
226
 
        $this->assertEquals(array($this->_noErrors, $this->_noWarnings), $result);
227
 
 
228
 
        /* Verify that we do *not* have a preferred */
229
 
        list ($ret, $preferredIds) =
230
 
            GalleryCoreApi::fetchPreferredsByItemIds(array($this->_items[0]->getId()));
231
 
        if ($ret) {
232
 
            return $this->failWithStatus($ret);
233
 
        }
234
 
        $this->assertEquals(0, sizeof($preferredIds), 'there should not be any preferred');
235
 
    }
236
 
 
237
 
    function testRotateNoExifDataOptionOn() {
238
 
        list ($ret, $this->_items[0]) =
239
 
            $this->_createRandomDataItem($this->_album->getId(), 'image/test');
240
 
        if ($ret) {
241
 
            return $this->failWithStatus($ret);
242
 
        }
243
 
 
244
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'exif', 'addOption',
245
 
                                                        EXIF_ITEM_ROTATE);
246
 
        if ($ret) {
247
 
            return $this->failWithStatus($ret);
248
 
        }
249
 
 
250
 
        $result = $this->_handleRequestAfterAdd(array(), $this->_items);
251
 
        $this->assertEquals(array($this->_noErrors, $this->_noWarnings), $result);
252
 
 
253
 
    }
254
 
 
255
 
 
256
 
    function testRotateOptionOnOverwriteOriginal() {
257
 
        list ($ret, $this->_items[0]) =
258
 
                $this->_createRandomDataItem($this->_album->getId(), 'image/jpeg',
259
 
                                             array(),
260
 
                                             dirname(__FILE__) . '/../data/descr.jpg');
261
 
        if ($ret) {
262
 
            return $this->failWithStatus($ret);
263
 
        }
264
 
 
265
 
        /* Register a dummy toolkit */
266
 
        $path = 'modules/exif/test/phpunit/ExifDescriptionOptionTest.class';
267
 
        $ret = GalleryCoreApi::registerFactoryImplementation(
268
 
            'GalleryToolkit', 'ExifDescriptionOptionTestToolkit',
269
 
            'ExifDescriptionOptionTestToolkit', $path, 'exifTest', null, 0);
270
 
        if ($ret) {
271
 
            return $this->failWithStatus($ret);
272
 
        }
273
 
        $this->_markToolkitForCleanup('ExifDescriptionOptionTestToolkit');
274
 
 
275
 
        /* Register operations that we can perform on our mock data items */
276
 
        $ret = GalleryCoreApi::registerToolkitOperation(
277
 
            'ExifDescriptionOptionTestToolkit', array('image/jpeg'), 'rotate',
278
 
            array(array('type' => 'int', 'description' => 'test')), 'test-description', null, 0);
279
 
        if ($ret) {
280
 
            return $this->failWithStatus($ret);
281
 
        }
282
 
 
283
 
        /* Register property for dimensions */
284
 
        $ret = GalleryCoreApi::registerToolkitProperty('ExifDescriptionOptionTestToolkit',
285
 
                        array('image/test'), 'dimensions', 'int,int', 'Get the dimensions');
286
 
        if ($ret) {
287
 
            return $this->failWithStatus($ret);
288
 
        }
289
 
 
290
 
        /* Save current size */
291
 
        $originalWidth = $this->_items[0]->getWidth();
292
 
        $originalHeight = $this->_items[0]->getHeight();
293
 
 
294
 
        /* Save current serial */
295
 
        $originalSerial = $this->_items[0]->getSerialNumber();
296
 
 
297
 
        /* Initialize a Flag for checking rotation */
298
 
        global $gallery;
299
 
        $gallery->setConfig('rotated', 0);
300
 
 
301
 
        /* Perform the request and verify that we succeeded */
302
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'exif', 'addOption',
303
 
                                                        EXIF_ITEM_ROTATE);
304
 
        if ($ret) {
305
 
            return $this->failWithStatus($ret);
306
 
        }
307
 
 
308
 
        $result = $this->_handleRequestAfterAdd(array(), $this->_items);
309
 
        $this->assertEquals(array($this->_noErrors, $this->_noWarnings), $result);
310
 
 
311
 
        /* Verify that we don't have a preferred */
312
 
        list ($ret, $preferredIds) =
313
 
            GalleryCoreApi::fetchPreferredsByItemIds(array($this->_items[0]->getId()));
314
 
        if ($ret) {
315
 
            return $this->failWithStatus($ret);
316
 
        }
317
 
        $this->assertEquals(0, sizeof($preferredIds), 'there should be no preferreds');
318
 
 
319
 
        /* Verify that the item serial is NOT the same */
320
 
        list ($ret, $this->_items[0]) = $this->_items[0]->refresh();
321
 
        if ($ret) {
322
 
            return $this->failWithStatus($ret);
323
 
        }
324
 
 
325
 
        $this->assert($originalSerial != $this->_items[0]->getSerialNumber(),
326
 
                "Item serial should have changed ($originalSerial)");
327
 
 
328
 
        /* Verify that the item has been rotated - by our mock toolkit */
329
 
        $this->assertEquals(1, $gallery->getConfig('rotated'), 'a rotation should have been done');
330
 
 
331
 
        /* and that the new dimensions are correct, set by our toolkit */
332
 
        $newWidth = $this->_items[0]->getWidth();
333
 
        $newHeight = $this->_items[0]->getHeight();
334
 
 
335
 
        $this->assertEquals(10, $newWidth, 'width should be 10');
336
 
        $this->assertEquals(10, $newHeight, 'height should be 10');
337
 
 
338
 
    }
339
 
 
340
 
    function testRotateOptionOnPreserveOriginal() {
341
 
        list ($ret, $this->_items[0]) =
342
 
                $this->_createRandomDataItem($this->_album->getId(), 'image/jpeg',
343
 
                                             array(),
344
 
                                             dirname(__FILE__) . '/../data/descr.jpg');
345
 
        if ($ret) {
346
 
            return $this->failWithStatus($ret);
347
 
        }
348
 
 
349
 
        /* Save current serial */
350
 
        $originalSerial = $this->_items[0]->getSerialNumber();
351
 
 
352
 
        /* Perform the request and verify that we succeeded */
353
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'exif', 'addOption',
354
 
                                                (EXIF_ITEM_ROTATE | EXIF_ITEM_ROTATE_PRESERVE));
355
 
        if ($ret) {
356
 
            return $this->failWithStatus($ret);
357
 
        }
358
 
 
359
 
        $result = $this->_handleRequestAfterAdd(array(), $this->_items);
360
 
        $this->assertEquals(array($this->_noErrors, $this->_noWarnings), $result);
361
 
 
362
 
        /* Verify that we have a preferred */
363
 
        list ($ret, $preferredIds) =
364
 
            GalleryCoreApi::fetchPreferredsByItemIds(array($this->_items[0]->getId()));
365
 
        if ($ret) {
366
 
            return $this->failWithStatus($ret);
367
 
        }
368
 
        $this->assertEquals(1, sizeof($preferredIds), 'there should be a preferred');
369
 
 
370
 
        /* Verify that the item serial is the same */
371
 
        list ($ret, $this->_items[0]) = $this->_items[0]->refresh();
372
 
        if ($ret) {
373
 
            return $this->failWithStatus($ret);
374
 
        }
375
 
        $this->assertEquals($originalSerial, $this->_items[0]->getSerialNumber());
376
 
 
377
 
        /* Verify that the item was rotated */
378
 
        $this->_preferred = array_shift($preferredIds);
379
 
        $this->assertEquals('rotate|90', $this->_preferred->getDerivativeOperations());
380
 
    }
381
 
 
382
 
}
383
 
 
384
 
GalleryCoreApi::requireOnce('modules/core/classes/GalleryToolkit.class');
385
 
 
386
 
/**
387
 
 * Test toolkit
388
 
 */
389
 
class ExifDescriptionOptionTestToolkit extends GalleryToolkit {
390
 
    /**
391
 
     * @see GalleryToolkit::performOperation
392
 
     * Dummy operation
393
 
     */
394
 
    function performOperation($mimeType, $operationName, $sourceFilename,
395
 
                              $destFilename, $parameters, $context=array()) {
396
 
        global $gallery;
397
 
 
398
 
        $platform =& $gallery->getPlatform();
399
 
        if ($sourceFilename != $destFilename) {
400
 
            $platform->copy($sourceFilename, $destFilename);
401
 
        }
402
 
 
403
 
        $fd = $platform->fopen($destFilename, 'a');
404
 
        $platform->fwrite($fd, $operationName);
405
 
        $platform->fclose($fd);
406
 
        /* Clear cache to ensure new filesize will be seen */
407
 
        $platform->clearstatcache();
408
 
 
409
 
        /* Turn On the Flag */
410
 
        $gallery->setConfig('rotated', 1);
411
 
 
412
 
        return array(null, 'image/test', $context);
413
 
    }
414
 
 
415
 
    function getProperty($mimeType, $propertyName, $sourceFilename) {
416
 
        if ($propertyName != 'dimensions') {
417
 
            return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
418
 
                                              "$propertyName $sourceFilename"), null);
419
 
        }
420
 
 
421
 
        return array(null, array(10, 10));
422
 
    }
423
 
}
424
 
?>