~ubuntu-branches/ubuntu/hardy/gallery2/hardy-security

« back to all changes in this revision

Viewing changes to lib/tools/repository/test/phpunit/RepositoryDescriptorTest.class

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2006-04-16 16:42:35 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060416164235-8uy0u4bfjdxpge2o
Tags: 2.1.1-1
* New upstream release (Closes: #362936)
  + Bugfixes for Postgres7 (Closes: #359000, #362152)

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-2006 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
 */
 
20
/**
 
21
 * @version $Revision: 1.2 $ $Date: 2006/01/10 04:38:42 $
 
22
 * @package RepositoryTools
 
23
 * @subpackage PHPUnit
 
24
 * @author Jozef Selesi <selesi at gmail dot com>
 
25
 */
 
26
 
 
27
GalleryCoreApi::requireOnce('lib/tools/repository/classes/RepositoryObject.class');
 
28
GalleryCoreApi::requireOnce('lib/tools/repository/classes/RepositoryDescriptor.class');
 
29
GalleryCoreApi::requireOnce(
 
30
    'lib/tools/repository/test/phpunit/RepositoryTestTemplate.class');
 
31
 
 
32
/**
 
33
 * Test RepositoryDescriptor functionality
 
34
 *
 
35
 * @package RepositoryTools
 
36
 * @subpackage PHPUnit
 
37
 */
 
38
class RepositoryDescriptorTest extends GalleryTestCase {
 
39
 
 
40
    var $_testModule;
 
41
    var $_descriptorDir;
 
42
    var $_expectedDescriptor;
 
43
    var $_galleryTranslator;
 
44
 
 
45
    function setUp() {
 
46
        parent::setUp();
 
47
        global $gallery;
 
48
 
 
49
        /* Set up utilities. */
 
50
        $this->_utilities = new RepositoryDescriptorTestUtilities('/.*[strings\.raw|\.po]$/',
 
51
            '# $file.po,v 2.2 1970/04/11 13:13:00 $', '2.2', '19700411131300');
 
52
 
 
53
        /* Set up plugin files and directories. */
 
54
        $pluginDir = 'pluginpath';
 
55
        $this->_descriptorDir = 'descriptorDir/';
 
56
        $pluginFilesystem = array (
 
57
            $pluginDir . '/' => array('MANIFEST', 'module.inc', 'callbacks.inc'),
 
58
            $pluginDir . '/locale/' => array(),
 
59
            $pluginDir . '/locale/en_US/' => array(),
 
60
            $pluginDir . '/locale/en_GB/' => array(),
 
61
            $pluginDir . '/locale/pt_BR/' => array(),
 
62
            $pluginDir . '/locale/pt_PT/' => array(),
 
63
            $pluginDir . '/locale/en_US/LC_MESSAGES/' => array('modules_mockmodule.mo'),
 
64
            $pluginDir . '/locale/en_GB/LC_MESSAGES/' => array('modules_mockmodule.mo'),
 
65
            $pluginDir . '/locale/pt_BR/LC_MESSAGES/' => array('modules_mockmodule.mo'),
 
66
            $pluginDir . '/locale/pt_PT/LC_MESSAGES/' => array('modules_mockmodule.mo'),
 
67
            $pluginDir . '/images/' => array('image1.gif', 'image2.gif'),
 
68
            $pluginDir . '/test/' => array(),
 
69
            $pluginDir . '/test/phpunit/' => array('testMockModule.class'),
 
70
            $pluginDir . '/po/' => array(
 
71
                'strings.raw', 'GNUmakefile', 'en_US.po', 'en_GB.po', 'pt_PT.po', 'pt_BR.po'));
 
72
 
 
73
        /* Put our mock objects in place. */
 
74
        $gallery->setConfig('repository.templates', dirname(__FILE__) . '/../data/');
 
75
        $this->_galleryTranslator = $gallery->_translator;
 
76
        $gallery->_translator = new RepositoryDescriptorTestTranslator();
 
77
        $gallery->setPlatform(new RepositoryDescriptorTestPlatform(
 
78
            $pluginFilesystem, $pluginDir, $this->_descriptorDir));
 
79
 
 
80
        /* Set up plugin. */
 
81
        $this->_testModule = new RepositoryDescriptorTestPlugin();
 
82
        $this->_testModule->setPluginType('module');
 
83
        $this->_testModule->setId('testModule');
 
84
        $this->_testModule->setName('Test Module');
 
85
        $this->_testModule->setDescription('This is a test module.');
 
86
        $this->_testModule->setVersion('1.0.0');
 
87
        $this->_testModule->setGroup('testGroup', 'Test Group');
 
88
        $this->_testModule->setRequiredCoreApi(array(1, 2));
 
89
        $this->_testModule->setRequiredModuleApi(array(3, 4));
 
90
 
 
91
        include(dirname(__FILE__) . '/../data/SampleDescriptors.inc');
 
92
        $this->_expectedDescriptor = $sampleModuleDescriptor;
 
93
    }
 
94
 
 
95
    function tearDown() {
 
96
        parent::tearDown();
 
97
 
 
98
        global $gallery;
 
99
        $gallery->_translator = $this->_galleryTranslator;
 
100
    }
 
101
 
 
102
    /**
 
103
     * Test that a valid descriptor is being generated.
 
104
     */
 
105
    function testDescriptorGeneration() {
 
106
        global $gallery;
 
107
 
 
108
        $descriptor = new RepositoryDescriptor();
 
109
        $descriptor->setTemplate(new RepositoryTestTemplate());
 
110
        $descriptor->setUtilities($this->_utilities);
 
111
        $descriptor->_outputDir = $this->_descriptorDir;
 
112
 
 
113
        $ret = $descriptor->generate($this->_testModule);
 
114
        if ($ret) {
 
115
            $this->failWithStatus($ret);
 
116
        }
 
117
 
 
118
        $this->assertEquals($this->_expectedDescriptor, $descriptor->_data,
 
119
            'Descriptor does not match expected data.');
 
120
    }
 
121
 
 
122
    function testGetDirectoriesInPackage() {
 
123
        $descriptor = new RepositoryDescriptor();
 
124
        $descriptor->setTemplate(new RepositoryTestTemplate());
 
125
        $descriptor->setUtilities($this->_utilities);
 
126
        $descriptor->_outputDir = $this->_descriptorDir;
 
127
 
 
128
        $ret = $descriptor->generate($this->_testModule);
 
129
        if ($ret) {
 
130
            $this->failWithStatus($ret);
 
131
        }
 
132
 
 
133
        $this->assertEquals(array(
 
134
            'locale/',
 
135
            'images/',
 
136
            'po/',
 
137
            ), $descriptor->getDirectoriesInPackage('base'));
 
138
 
 
139
        $this->assertEquals(array(
 
140
            'locale/en_US/',
 
141
            'locale/en_US/LC_MESSAGES/',
 
142
            ), $descriptor->getDirectoriesInPackage('lang-en_US'));
 
143
 
 
144
        $this->assertEquals(array(
 
145
            'locale/pt_BR/',
 
146
            'locale/pt_BR/LC_MESSAGES/',
 
147
            ), $descriptor->getDirectoriesInPackage('lang-pt_BR'));
 
148
    }
 
149
 
 
150
    function testGetFilesInPackage() {
 
151
        $descriptor = new RepositoryDescriptor();
 
152
        $descriptor->setTemplate(new RepositoryTestTemplate());
 
153
        $descriptor->setUtilities($this->_utilities);
 
154
        $descriptor->_outputDir = $this->_descriptorDir;
 
155
 
 
156
        $ret = $descriptor->generate($this->_testModule);
 
157
        if ($ret) {
 
158
            $this->failWithStatus($ret);
 
159
        }
 
160
 
 
161
        $this->assertEquals(array(
 
162
            'images/image1.gif',
 
163
            'images/image2.gif',
 
164
            'po/strings.raw',
 
165
            'po/GNUmakefile',
 
166
            'MANIFEST',
 
167
            'module.inc',
 
168
            'callbacks.inc',
 
169
            ), $descriptor->getFilesInPackage('base'));
 
170
 
 
171
        $this->assertEquals(array(
 
172
            'locale/en_US/',
 
173
            'locale/en_US/LC_MESSAGES/',
 
174
            ), $descriptor->getDirectoriesInPackage('lang-en_US'));
 
175
 
 
176
        $this->assertEquals(array(
 
177
            'test/',
 
178
            'test/phpunit/',
 
179
            ), $descriptor->getDirectoriesInPackage('test'));
 
180
    }
 
181
 
 
182
    function testGetPackages() {
 
183
        $descriptor = new RepositoryDescriptor();
 
184
        $descriptor->setTemplate(new RepositoryTestTemplate());
 
185
        $descriptor->setUtilities($this->_utilities);
 
186
        $descriptor->_outputDir = $this->_descriptorDir;
 
187
 
 
188
        $ret = $descriptor->generate($this->_testModule);
 
189
        if ($ret) {
 
190
            $this->failWithStatus($ret);
 
191
        }
 
192
 
 
193
        $this->assertEquals(array(
 
194
            'base',
 
195
            'lang-en_US',
 
196
            'lang-en_GB',
 
197
            'lang-pt_BR',
 
198
            'lang-pt_PT',
 
199
            'test',
 
200
            ), $descriptor->getPackages());
 
201
    }
 
202
}
 
203
 
 
204
class RepositoryDescriptorTestPlugin extends GalleryPlugin {
 
205
    function setPluginType($type) {
 
206
        $this->_type = $type;
 
207
    }
 
208
 
 
209
    function getPluginType() {
 
210
        return $this->_type;
 
211
    }
 
212
 
 
213
    function setRequiredModuleApi($requirement) {
 
214
        $this->_requiredModuleApi = $requirement;
 
215
    }
 
216
 
 
217
    function getRequiredModuleApi() {
 
218
        return $this->_requiredModuleApi;
 
219
    }
 
220
 
 
221
    function setRequiredThemeApi($requirement) {
 
222
        $this->_requiredThemeApi = $requirement;
 
223
    }
 
224
 
 
225
    function getRequiredThemeApi() {
 
226
        return $this->_requiredThemeApi;
 
227
    }
 
228
 
 
229
    function setGroup($group, $groupLabel) {
 
230
        $this->_group = array('group' => $group, 'groupLabel' => $groupLabel);
 
231
    }
 
232
 
 
233
    function getGroup() {
 
234
        return array('group' => $this->_group['group'],
 
235
                     'groupLabel' => $this->_group['groupLabel']);
 
236
    }
 
237
}
 
238
 
 
239
class RepositoryDescriptorTestTranslator {
 
240
    var $_languageCode;
 
241
 
 
242
    function init($languageCode) {
 
243
        $this->_languageCode = $languageCode;
 
244
    }
 
245
 
 
246
    function getLanguageData() {
 
247
        return array(array('en' => array('US' => 'Desc1', 'GB' => 'Desc2'),
 
248
                           'pt' => array('BR' => 'Desc3', 'PT' => 'Desc4')),
 
249
                     array());
 
250
    }
 
251
 
 
252
    function translateDomain($domain, $string) {
 
253
        return array(null, sprintf(
 
254
            '%s-%s-%s', $this->_languageCode, $domain, $string['text']));
 
255
    }
 
256
}
 
257
 
 
258
class RepositoryDescriptorTestUtilities {
 
259
 
 
260
    var $_extractedTimestamp;
 
261
    var $_extractedRevision;
 
262
    var $_dateString;
 
263
    var $_expectedFile;
 
264
 
 
265
    function RepositoryDescriptorTestUtilities($expectedFile, $dateString, $revision, $timestamp) {
 
266
        $this->_dateString = $dateString;
 
267
        $this->_extractedTimestamp = $timestamp;
 
268
        $this->_extractedRevision = $revision;
 
269
        $this->_expectedFile = $expectedFile;
 
270
    }
 
271
 
 
272
    function getFirstBytesFromFile($file, $bytes) {
 
273
        return preg_match($this->_expectedFile, $file)
 
274
               ? array(null, $this->_dateString)
 
275
               : array(GalleryCoreApi::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__,
 
276
                                            "Expected strings.raw in [$file]"),
 
277
                       '');
 
278
    }
 
279
 
 
280
    function extractRevision($string) {
 
281
        return array(null, $this->_extractedRevision);
 
282
    }
 
283
 
 
284
    function extractTimestamp($string) {
 
285
        return array(null, $this->_extractedTimestamp);
 
286
    }
 
287
}
 
288
 
 
289
/**
 
290
 * Platform for testing repository tools.
 
291
 *
 
292
 * A relatively complex platform is required for testing the descriptor generation
 
293
 * because it heavily relies on interacting with the filesystem. All functions used
 
294
 * by RepositoryDescriptor have been reimplemented to interact with an array which
 
295
 * simulates the plugin's files and directories.
 
296
 */
 
297
class RepositoryDescriptorTestPlatform {
 
298
    var $pluginFilesystem;
 
299
    var $pluginFileList;
 
300
    var $pluginDir;
 
301
    var $descriptorDir;
 
302
    var $dirPosition;
 
303
 
 
304
    function RepositoryDescriptorTestPlatform($pluginFilesystem, $pluginDir, $descriptorDir) {
 
305
        $this->pluginDir = $pluginDir;
 
306
        $this->_descriptorDir = $descriptorDir;
 
307
        $this->pluginFilesystem = $pluginFilesystem;
 
308
 
 
309
        /*
 
310
         * Create an array of all possible files with their full paths,
 
311
         * so is_file can easily determine what it should return.
 
312
         */
 
313
        foreach ($this->pluginFilesystem as $directory => $files) {
 
314
            foreach ($files as $file) {
 
315
                $this->pluginFileList[$directory . $file] = 1;
 
316
            }
 
317
        }
 
318
 
 
319
        /* Reset directory position pointer. */
 
320
        $this->dirPosition = array();
 
321
    }
 
322
 
 
323
    function fopen($file, $mode) {
 
324
        return true;
 
325
    }
 
326
 
 
327
    function fread($file, $bytes) {
 
328
        return '';
 
329
    }
 
330
 
 
331
    function fclose($file) {
 
332
        return true;
 
333
    }
 
334
 
 
335
    /**
 
336
     * realpath is used to make sure there are no trailing slashes in
 
337
     * paths, so we implement that here. We also have to check if the
 
338
     * specified file is one of the files in our mock plugin, because
 
339
     * realpath is also used on the plugin base directory. Since the
 
340
     * GalleryCoreApi can't get the base base directory of our mock plugin
 
341
     * we return 'pluginpath' to be the base.
 
342
     */
 
343
    function realpath($path) {
 
344
        return isset($this->pluginFilesystem[$path])
 
345
               ? substr($path, 0, strlen($path)-1) : $this->pluginDir;
 
346
    }
 
347
 
 
348
    function is_dir($path) {
 
349
        if (isset($this->pluginFilesystem[$path]) || $path == $this->_descriptorDir) {
 
350
            return true;
 
351
        } else {
 
352
            return false;
 
353
        }
 
354
    }
 
355
 
 
356
    function is_file($path) {
 
357
        return isset($this->pluginFileList[$path]);
 
358
    }
 
359
 
 
360
    function opendir($path) {
 
361
        if ($this->is_dir($path)) {
 
362
            return $path;
 
363
        } else {
 
364
            return false;
 
365
        }
 
366
    }
 
367
 
 
368
    function readdir($path) {
 
369
        if ($this->is_dir($path)) {
 
370
            return $this->getNextFile($path);
 
371
        } else {
 
372
            return "Tried to read invalid path [$path]";
 
373
        }
 
374
    }
 
375
 
 
376
    function filesize($path) {
 
377
        return 10;
 
378
    }
 
379
 
 
380
    function chdir($path) {
 
381
        return true;
 
382
    }
 
383
 
 
384
    function file_get_contents($file) {
 
385
        return $file;
 
386
    }
 
387
 
 
388
    /**
 
389
     * This function implements the functionality of PHP's readdir($path),
 
390
     * but using our test filesystem defined in $this->pluginFilesystem.
 
391
     */
 
392
    function getNextFile($readPath) {
 
393
        /* We use a member array to track our position in various directories. */
 
394
        if (!isset($this->dirPosition[$readPath])) {
 
395
            $this->dirPosition[$readPath] = 0;
 
396
        } else {
 
397
            $this->dirPosition[$readPath]++;
 
398
        }
 
399
 
 
400
        /* Compile a list of all subdirectories of specified path. */
 
401
        $subDirectories = array();
 
402
        foreach ($this->pluginFilesystem as $directory => $files) {
 
403
            if (preg_match(sprintf('/^%s([a-z_]*\/)$/iU', str_replace('/', '\/', $readPath)),
 
404
                    $directory, $subDirectory)) {
 
405
                $subDirectories[] = $subDirectory[1];
 
406
            }
 
407
        }
 
408
 
 
409
        /* Check if there are any more subdirectories in current path. */
 
410
        if (array_key_exists($this->dirPosition[$readPath], $subDirectories)) {
 
411
            return $subDirectories[$this->dirPosition[$readPath]];
 
412
        } else {
 
413
            /* No more subdirectories, return files, if there are any. */
 
414
            $fileIndex = $this->dirPosition[$readPath] - count($subDirectories);
 
415
            if (count($this->pluginFilesystem[$readPath]) > $fileIndex) {
 
416
                return $this->pluginFilesystem[$readPath][$fileIndex];
 
417
            } else {
 
418
                /* Now only three special 'files' remain unread: CVS, . and .. */
 
419
                $fileIndex = $this->dirPosition[$readPath]
 
420
                    - count($this->pluginFilesystem[$readPath]) - count($subDirectories);
 
421
 
 
422
                switch ($fileIndex) {
 
423
                case 0:
 
424
                    return '.';
 
425
 
 
426
                case 1:
 
427
                    return '..';
 
428
 
 
429
                case 2:
 
430
                    return 'CVS';
 
431
 
 
432
                case 3:
 
433
                    /* All files and subdirectories of the current path have been returned. */
 
434
                    return false;
 
435
 
 
436
                default:
 
437
                    /* We should never get here. If we do, is_dir will fail the test. */
 
438
                    return "Error: file index out of range [$fileIndex]";
 
439
                }
 
440
            }
 
441
        }
 
442
    }
 
443
}
 
444
?>
 
 
b'\\ No newline at end of file'