~ubuntu-branches/ubuntu/quantal/gallery2/quantal

« back to all changes in this revision

Viewing changes to modules/gd/test/phpunit/TestGdFunctionality.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
 
 * An implementation of the GdFunctionality that offers pseudo-gd-
23
 
 * functionality. It allows to set the PHP environment and will then
24
 
 * simulate its inner workings.
25
 
 *
26
 
 * @package Gd
27
 
 * @subpackage PHPUnit
28
 
 * @author Ernesto Baschny <ernst@baschny.de>
29
 
 * @version $Revision: 15513 $
30
 
 */
31
 
class TestGdFunctionality {
32
 
 
33
 
    /*
34
 
     * Our current environment
35
 
     */
36
 
    var $_gdEnvironment;
37
 
 
38
 
    /*
39
 
     * Keep track of our resources
40
 
     */
41
 
    var $_resourceCounter = 0;
42
 
    var $_undestroyedResourceCounter = 0;
43
 
    var $_resources;
44
 
    var $_outputFiles;
45
 
 
46
 
    function TestGdFunctionality() {
47
 
    }
48
 
 
49
 
    /**
50
 
     * Set the environment we want to simulate
51
 
     * -> 1. PHP 4.1.0, GD 1.8.2 not bundled (4.1.0|1.6.2|0)
52
 
     *    2. PHP 4.2.0, GD 1.8.2 not bundled
53
 
     * -> 3. PHP 4.3.1, GD 2.0 bundled       (4.3.1|2.0|1)
54
 
     *    4. PHP 4.3.9, GD 1.6.2 not bundled
55
 
     * -> 5. PHP 4.3.9RC1, GD 2.0.28 bundled (4.3.9RC1|2.0.28|1)
56
 
     *    6. PHP 5.0.1, GD 2.0.28 bundled
57
 
     */
58
 
    function setEnvironment($environment) {
59
 
        $this->_gdEnvironment = $environment;
60
 
    }
61
 
 
62
 
    function getEnvironmentName() {
63
 
        return $this->_gdEnvironment['name'];
64
 
    }
65
 
 
66
 
    /**
67
 
     * Return the name of a new resource
68
 
     *
69
 
     * From a filename like '200x300.gif' set the sizes x=200 y=300
70
 
     *
71
 
     */
72
 
    function getNewResource($filename=null) {
73
 
        $this->_resourceCounter++;
74
 
        $this->_undestroyedResourceCounter++;
75
 
        $name = 'res #' . $this->_resourceCounter;
76
 
        $this->_resources[$name] = array(
77
 
            'x' => 0,
78
 
            'y' => 0,
79
 
            'trueColor' => false,
80
 
        );
81
 
        if (isset($filename)) {
82
 
            $this->_resources[$name]['filename'] = $filename;
83
 
            if (preg_match('/(\d+)x(\d+)\./i', $filename, $matches)) {
84
 
                $this->_resources[$name]['x'] = (int) $matches[1];
85
 
                $this->_resources[$name]['y'] = (int) $matches[2];
86
 
            }
87
 
        }
88
 
        return $name;
89
 
    }
90
 
    /**
91
 
     * Make a copy of some resource
92
 
     */
93
 
    function copyResource($otherRes) {
94
 
        $this->_resourceCounter++;
95
 
        $this->_undestroyedResourceCounter++;
96
 
        $newRes = 'Resource #' . $this->_resourceCounter;
97
 
        $this->_resources[$newRes] = $this->_resources[$otherRes];
98
 
        return $newRes;
99
 
    }
100
 
    function setResourceData($res, $field, $value) {
101
 
        $this->_resources[$res][$field] = $value;
102
 
    }
103
 
    function getResourceData($res, $field) {
104
 
        if (isset($this->_resources[$res][$field])) {
105
 
            return $this->_resources[$res][$field];
106
 
        }
107
 
        return '';
108
 
    }
109
 
    function isValidResource($res) {
110
 
        return isset($this->_resources[$res]);
111
 
    }
112
 
    function saveResource($res, $filename, $mimeType) {
113
 
        $this->_outputFiles[$filename] = array(
114
 
            'x' => $this->getResourceData($res, 'x'),
115
 
            'y' => $this->getResourceData($res, 'y'),
116
 
            'mimeType' => $mimeType,
117
 
        );
118
 
        $other = $this->getResourceData($res, 'other');
119
 
        if (is_array($other)) {
120
 
            $this->_outputFiles[$filename] = array_merge($this->_outputFiles[$filename], $other);
121
 
        }
122
 
    }
123
 
    function getOutputFileData($filename) {
124
 
        if (!isset($this->_outputFiles[$filename])) {
125
 
            return null;
126
 
        }
127
 
        return $this->_outputFiles[$filename];
128
 
    }
129
 
 
130
 
    function isValidFile($filename) {
131
 
        if (preg_match('/valid/i', $filename)) {
132
 
            return true;
133
 
        } else {
134
 
            return false;
135
 
        }
136
 
    }
137
 
 
138
 
    /*
139
 
     * Here is our implementation of the GdFunctionality-interface
140
 
     * This will do nothing with images, but simulate what might
141
 
     * happen.
142
 
     */
143
 
 
144
 
    function phpinfo($section=null) {
145
 
        if ($section != '8') {
146
 
            return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE), null);
147
 
        }
148
 
        if (!isset($this->_gdEnvironment['phpinfo(8)'])) {
149
 
            return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE, __FILE__, __LINE__,
150
 
                                              'Environment: ' . $this->getEnvironmentName()), null);
151
 
        }
152
 
        $out = strtr($this->_gdEnvironment['phpinfo(8)'],
153
 
                    array_flip(get_html_translation_table(HTML_ENTITIES)));
154
 
        return array(null, $out);
155
 
    }
156
 
 
157
 
    function tempnam($dir, $prefix) {
158
 
        return rand();
159
 
    }
160
 
 
161
 
    function copy($filename) {
162
 
        return true;
163
 
    }
164
 
 
165
 
    function unlink($filename) {
166
 
        if (!isset($this->_outputFiles[$filename])) {
167
 
            return false;
168
 
        }
169
 
        unset($this->_outputFiles[$filename]);
170
 
        return true;
171
 
    }
172
 
 
173
 
    function chmod($file) {
174
 
        /* ignore this for now */
175
 
    }
176
 
 
177
 
    function filesize($filename) {
178
 
        switch ($filename) {
179
 
        case 'compressMe.jpg':
180
 
            return 250 << 10;
181
 
        case '/valid/compressed.jpg':
182
 
            switch ($this->_counter--) {
183
 
            case 2:
184
 
                return 200 << 10;
185
 
            case 1:
186
 
                return 100 << 10;
187
 
            case 0:
188
 
                return 150 << 10;
189
 
            }
190
 
        }
191
 
        print "filesize $filename<br/>\n";
192
 
        return 0;
193
 
    }
194
 
 
195
 
    function functionExists($fct) {
196
 
        $fct = strtolower($fct);
197
 
        if (isset($this->_gdEnvironment['functions'][$fct])) {
198
 
            return $this->_gdEnvironment['functions'][$fct];
199
 
        }
200
 
        return false;
201
 
    }
202
 
 
203
 
    function imageTypes() {
204
 
        return $this->_gdEnvironment['imageTypes'];
205
 
    }
206
 
 
207
 
    function gd_info() {
208
 
        if (!$this->functionExists('gd_info')) {
209
 
            return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
210
 
                                        'Environment: ' . $this->getEnvironmentName()), null);
211
 
        }
212
 
        if (!isset($this->_gdEnvironment['gd_info'])) {
213
 
            return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE), null);
214
 
        }
215
 
        return array(null, $this->_gdEnvironment['gd_info']);
216
 
    }
217
 
 
218
 
    function getImageSize($filename) {
219
 
        return array(0, 0);
220
 
    }
221
 
 
222
 
    function imageCreate($width, $height) {
223
 
        if (!$this->functionExists('imageCreate')) {
224
 
            return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
225
 
                                              'Environment: ' . $this->getEnvironmentName()),
226
 
                         null);
227
 
        }
228
 
        $res = $this->getNewResource();
229
 
        $this->setResourceData($res, 'x', $width);
230
 
        $this->setResourceData($res, 'y', $height);
231
 
        $this->setResourceData($res, 'trueColor', false);
232
 
        return array(null, $res);
233
 
    }
234
 
 
235
 
    function imageCreateTruecolor($width, $height) {
236
 
        if (!$this->functionExists('imageCreateTruecolor')) {
237
 
            return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
238
 
                                              'Environment: ' . $this->getEnvironmentName()),
239
 
                         null);
240
 
        }
241
 
        $res = $this->getNewResource();
242
 
        $this->setResourceData($res, 'x', $width);
243
 
        $this->setResourceData($res, 'y', $height);
244
 
        $this->setResourceData($res, 'trueColor', true);
245
 
        return array(null, $res);
246
 
    }
247
 
 
248
 
    function imageCreateFromGif($filename) {
249
 
        if (!$this->functionExists('imageCreateFromGif')) {
250
 
            return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
251
 
                                              'Environment: ' . $this->getEnvironmentName()),
252
 
                         null);
253
 
        }
254
 
        $res = $this->getNewResource($filename);
255
 
        return array(null, $res);
256
 
    }
257
 
 
258
 
    function imageCreateFromJpeg($filename) {
259
 
        if (!$this->functionExists('imageCreateFromJpeg')) {
260
 
            return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
261
 
                                              'Environment: ' . $this->getEnvironmentName()),
262
 
                         null);
263
 
        }
264
 
        $res = $this->getNewResource($filename);
265
 
        return array(null, $res);
266
 
    }
267
 
 
268
 
    function imageCreateFromPng($filename) {
269
 
        if (!$this->functionExists('imageCreateFromPng')) {
270
 
            return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
271
 
                                              'Environment: ' . $this->getEnvironmentName()),
272
 
                         null);
273
 
        }
274
 
        $res = $this->getNewResource($filename);
275
 
        return array(null, $res);
276
 
    }
277
 
 
278
 
    function imageCreateFromWbmp($filename) {
279
 
        if (!$this->functionExists('imageCreateFromWbmp')) {
280
 
            return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
281
 
                                              'Environment: ' . $this->getEnvironmentName()),
282
 
                         null);
283
 
        }
284
 
        $res = $this->getNewResource($filename);
285
 
        return array(null, $res);
286
 
    }
287
 
 
288
 
    function imageCreateFromXbm($filename) {
289
 
        if (!$this->functionExists('imageCreateFromXbm')) {
290
 
            return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
291
 
                                              'Environment: ' . $this->getEnvironmentName()),
292
 
                         null);
293
 
        }
294
 
        $res = $this->getNewResource($filename);
295
 
        return array(null, $res);
296
 
    }
297
 
 
298
 
    function imageCreateFromXpm($filename) {
299
 
        if (!$this->functionExists('imageCreateFromXpm')) {
300
 
            return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
301
 
                                              'Environment: ' . $this->getEnvironmentName()),
302
 
                         null);
303
 
        }
304
 
        $res = $this->getNewResource($filename);
305
 
        return array(null, $res);
306
 
    }
307
 
 
308
 
    function imageGif($res, $filename) {
309
 
        if (!$this->functionExists('imageGif')) {
310
 
            return GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
311
 
                                        'Environment: ' . $this->getEnvironmentName());
312
 
        }
313
 
        if (!$this->isValidResource($res)) {
314
 
            return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
315
 
        }
316
 
        if (!$this->isValidFile($filename)) {
317
 
            return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
318
 
        }
319
 
        $this->saveResource($res, $filename, 'image/gif');
320
 
        return null;
321
 
    }
322
 
 
323
 
    function imageJpeg($res, $filename, $quality) {
324
 
        if (!$this->functionExists('imageJpeg')) {
325
 
            return GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
326
 
                                        'Environment: ' . $this->getEnvironmentName());
327
 
        }
328
 
        if (!$this->isValidResource($res)) {
329
 
            return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
330
 
        }
331
 
        if (!$this->isValidFile($filename)) {
332
 
            return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
333
 
        }
334
 
        $this->saveResource($res, $filename, 'image/jpeg');
335
 
        return null;
336
 
    }
337
 
 
338
 
    function imagePng($res, $filename) {
339
 
        if (!$this->functionExists('imagePng')) {
340
 
            return GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
341
 
                                        'Environment: ' . $this->getEnvironmentName());
342
 
        }
343
 
        if (!$this->isValidResource($res)) {
344
 
            return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
345
 
        }
346
 
        if (!$this->isValidFile($filename)) {
347
 
            return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
348
 
        }
349
 
        $this->saveResource($res, $filename, 'image/png');
350
 
        return null;
351
 
    }
352
 
 
353
 
    function imageWbmp($res, $filename) {
354
 
        if (!$this->functionExists('imageWbmp')) {
355
 
            return GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
356
 
                                        'Environment: ' . $this->getEnvironmentName());
357
 
        }
358
 
        if (!$this->isValidResource($res)) {
359
 
            return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
360
 
        }
361
 
        if (!$this->isValidFile($filename)) {
362
 
            return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
363
 
        }
364
 
        $this->saveResource($res, $filename, 'image/vnd.wap.wbmp');
365
 
        return null;
366
 
    }
367
 
 
368
 
    function imageXbm($res, $filename) {
369
 
        if (!$this->functionExists('imageXbm')) {
370
 
            return GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
371
 
                                        'Environment: ' . $this->getEnvironmentName());
372
 
        }
373
 
        if (!$this->isValidResource($res)) {
374
 
            return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
375
 
        }
376
 
        if (!$this->isValidFile($filename)) {
377
 
            return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
378
 
        }
379
 
        $this->saveResource($res, $filename, 'image/x-xbitmap');
380
 
        return null;
381
 
    }
382
 
 
383
 
    function imageXpm($res, $filename) {
384
 
        if (!$this->functionExists('imageXpm')) {
385
 
            return GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
386
 
                                        'Environment: ' . $this->getEnvironmentName());
387
 
        }
388
 
        if (!$this->isValidResource($res)) {
389
 
            return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
390
 
        }
391
 
        if (!$this->isValidFile($filename)) {
392
 
            return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
393
 
        }
394
 
        $this->saveResource($res, $filename, 'image/x-xpixmap');
395
 
        return null;
396
 
    }
397
 
 
398
 
    function imageCopyResampled($dstRes, $srcRes, $dstX, $dstY, $srcX,
399
 
                                $srcY, $dstW, $dstH, $srcW, $srcH) {
400
 
        if (!$this->functionExists('imageCopyResampled')) {
401
 
            return GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
402
 
                                        'Environment: ' . $this->getEnvironmentName());
403
 
        }
404
 
        if (!$this->isValidResource($srcRes) || !$this->isValidResource($dstRes)) {
405
 
            return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
406
 
        }
407
 
        return null;
408
 
    }
409
 
 
410
 
    function imageCopyResized($dstRes, $srcRes, $dstX, $dstY, $srcX,
411
 
                              $srcY, $dstW, $dstH, $srcW, $srcH) {
412
 
        if (!$this->functionExists('imageCopyResized')) {
413
 
            return GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
414
 
                                        'Environment: ' . $this->getEnvironmentName());
415
 
        }
416
 
        if (!$this->isValidResource($srcRes) || !$this->isValidResource($dstRes)) {
417
 
            return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
418
 
        }
419
 
        return null;
420
 
    }
421
 
 
422
 
    function imageCopyMerge($dstRes, $srcRes, $dstX, $dstY, $srcX, $srcY, $srcW, $srcH, $pct) {
423
 
        if (!$this->functionExists('imageCopyMerge')) {
424
 
            return GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
425
 
                                        'Environment: ' . $this->getEnvironmentName());
426
 
        }
427
 
        if (!$this->isValidResource($srcRes) || !$this->isValidResource($dstRes)) {
428
 
            return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
429
 
        }
430
 
        return null;
431
 
    }
432
 
 
433
 
    function imageCopy($dstRes, $srcRes, $dstX, $dstY, $srcX, $srcY, $srcW, $srcH) {
434
 
        if (!$this->functionExists('imageCopy')) {
435
 
            return GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
436
 
                                        'Environment: ' . $this->getEnvironmentName());
437
 
        }
438
 
        if (!$this->isValidResource($srcRes) || !$this->isValidResource($dstRes)) {
439
 
            return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
440
 
        }
441
 
 
442
 
        $this->setResourceData(
443
 
            $dstRes, 'other',
444
 
            array('copy' => array(
445
 
                      $this->getResourceData($srcRes, 'filename'),
446
 
                      $dstX, $dstY, $srcX, $srcY, $srcW, $srcH)));
447
 
        return null;
448
 
    }
449
 
 
450
 
    function imageSx($res) {
451
 
        if (!$this->functionExists('imageSx')) {
452
 
            return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
453
 
                                              'Environment: ' . $this->getEnvironmentName()),
454
 
                         null);
455
 
        }
456
 
        if (!$this->isValidResource($res)) {
457
 
            return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER),
458
 
                         null);
459
 
        }
460
 
        return array(null, $this->getResourceData($res, 'x'));
461
 
    }
462
 
 
463
 
    function imageSy($res) {
464
 
        if (!$this->functionExists('imageSy')) {
465
 
            return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
466
 
                                              'Environment: ' . $this->getEnvironmentName()),
467
 
                         null);
468
 
        }
469
 
        if (!$this->isValidResource($res)) {
470
 
            return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER),
471
 
                         null);
472
 
        }
473
 
        return array(null, $this->getResourceData($res, 'y'));
474
 
    }
475
 
 
476
 
    /* Currently this only handles rotation in 90deg-steps */
477
 
    function imageRotate($srcRes, $angle, $bgdColor) {
478
 
        if (!$this->functionExists('imageRotate')) {
479
 
            return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
480
 
                                              'Environment: ' . $this->getEnvironmentName()),
481
 
                         null);
482
 
        }
483
 
        if ($angle % 90 == 0) {
484
 
            /* Swap width and height */
485
 
            $newX = $this->getResourceData($srcRes, 'y');
486
 
            $newY = $this->getResourceData($srcRes, 'x');
487
 
        } else {
488
 
            $newX = $this->getResourceData($srcRes, 'x');
489
 
            $newY = $this->getResourceData($srcRes, 'y');
490
 
        }
491
 
        $res = $this->copyResource($srcRes);
492
 
        $this->setResourceData($res, 'x', $newX);
493
 
        $this->setResourceData($res, 'y', $newY);
494
 
        return array(null, $res);
495
 
    }
496
 
 
497
 
    function imagedestroy($res) {
498
 
        $this->_undestroyedResourceCounter--;
499
 
        $this->_resources[$res]['destroyed'] = true;
500
 
    }
501
 
}
502
 
?>