3
* Gallery - a web based photo album viewer and editor
4
* Copyright (C) 2000-2006 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
* @version $Revision: 1.2 $ $Date: 2006/01/10 04:38:42 $
22
* @package RepositoryTools
24
* @author Jozef Selesi <selesi at gmail dot com>
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');
33
* Test RepositoryDescriptor functionality
35
* @package RepositoryTools
38
class RepositoryDescriptorTest extends GalleryTestCase {
42
var $_expectedDescriptor;
43
var $_galleryTranslator;
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');
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'));
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));
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));
91
include(dirname(__FILE__) . '/../data/SampleDescriptors.inc');
92
$this->_expectedDescriptor = $sampleModuleDescriptor;
99
$gallery->_translator = $this->_galleryTranslator;
103
* Test that a valid descriptor is being generated.
105
function testDescriptorGeneration() {
108
$descriptor = new RepositoryDescriptor();
109
$descriptor->setTemplate(new RepositoryTestTemplate());
110
$descriptor->setUtilities($this->_utilities);
111
$descriptor->_outputDir = $this->_descriptorDir;
113
$ret = $descriptor->generate($this->_testModule);
115
$this->failWithStatus($ret);
118
$this->assertEquals($this->_expectedDescriptor, $descriptor->_data,
119
'Descriptor does not match expected data.');
122
function testGetDirectoriesInPackage() {
123
$descriptor = new RepositoryDescriptor();
124
$descriptor->setTemplate(new RepositoryTestTemplate());
125
$descriptor->setUtilities($this->_utilities);
126
$descriptor->_outputDir = $this->_descriptorDir;
128
$ret = $descriptor->generate($this->_testModule);
130
$this->failWithStatus($ret);
133
$this->assertEquals(array(
137
), $descriptor->getDirectoriesInPackage('base'));
139
$this->assertEquals(array(
141
'locale/en_US/LC_MESSAGES/',
142
), $descriptor->getDirectoriesInPackage('lang-en_US'));
144
$this->assertEquals(array(
146
'locale/pt_BR/LC_MESSAGES/',
147
), $descriptor->getDirectoriesInPackage('lang-pt_BR'));
150
function testGetFilesInPackage() {
151
$descriptor = new RepositoryDescriptor();
152
$descriptor->setTemplate(new RepositoryTestTemplate());
153
$descriptor->setUtilities($this->_utilities);
154
$descriptor->_outputDir = $this->_descriptorDir;
156
$ret = $descriptor->generate($this->_testModule);
158
$this->failWithStatus($ret);
161
$this->assertEquals(array(
169
), $descriptor->getFilesInPackage('base'));
171
$this->assertEquals(array(
173
'locale/en_US/LC_MESSAGES/',
174
), $descriptor->getDirectoriesInPackage('lang-en_US'));
176
$this->assertEquals(array(
179
), $descriptor->getDirectoriesInPackage('test'));
182
function testGetPackages() {
183
$descriptor = new RepositoryDescriptor();
184
$descriptor->setTemplate(new RepositoryTestTemplate());
185
$descriptor->setUtilities($this->_utilities);
186
$descriptor->_outputDir = $this->_descriptorDir;
188
$ret = $descriptor->generate($this->_testModule);
190
$this->failWithStatus($ret);
193
$this->assertEquals(array(
200
), $descriptor->getPackages());
204
class RepositoryDescriptorTestPlugin extends GalleryPlugin {
205
function setPluginType($type) {
206
$this->_type = $type;
209
function getPluginType() {
213
function setRequiredModuleApi($requirement) {
214
$this->_requiredModuleApi = $requirement;
217
function getRequiredModuleApi() {
218
return $this->_requiredModuleApi;
221
function setRequiredThemeApi($requirement) {
222
$this->_requiredThemeApi = $requirement;
225
function getRequiredThemeApi() {
226
return $this->_requiredThemeApi;
229
function setGroup($group, $groupLabel) {
230
$this->_group = array('group' => $group, 'groupLabel' => $groupLabel);
233
function getGroup() {
234
return array('group' => $this->_group['group'],
235
'groupLabel' => $this->_group['groupLabel']);
239
class RepositoryDescriptorTestTranslator {
242
function init($languageCode) {
243
$this->_languageCode = $languageCode;
246
function getLanguageData() {
247
return array(array('en' => array('US' => 'Desc1', 'GB' => 'Desc2'),
248
'pt' => array('BR' => 'Desc3', 'PT' => 'Desc4')),
252
function translateDomain($domain, $string) {
253
return array(null, sprintf(
254
'%s-%s-%s', $this->_languageCode, $domain, $string['text']));
258
class RepositoryDescriptorTestUtilities {
260
var $_extractedTimestamp;
261
var $_extractedRevision;
265
function RepositoryDescriptorTestUtilities($expectedFile, $dateString, $revision, $timestamp) {
266
$this->_dateString = $dateString;
267
$this->_extractedTimestamp = $timestamp;
268
$this->_extractedRevision = $revision;
269
$this->_expectedFile = $expectedFile;
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]"),
280
function extractRevision($string) {
281
return array(null, $this->_extractedRevision);
284
function extractTimestamp($string) {
285
return array(null, $this->_extractedTimestamp);
290
* Platform for testing repository tools.
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.
297
class RepositoryDescriptorTestPlatform {
298
var $pluginFilesystem;
304
function RepositoryDescriptorTestPlatform($pluginFilesystem, $pluginDir, $descriptorDir) {
305
$this->pluginDir = $pluginDir;
306
$this->_descriptorDir = $descriptorDir;
307
$this->pluginFilesystem = $pluginFilesystem;
310
* Create an array of all possible files with their full paths,
311
* so is_file can easily determine what it should return.
313
foreach ($this->pluginFilesystem as $directory => $files) {
314
foreach ($files as $file) {
315
$this->pluginFileList[$directory . $file] = 1;
319
/* Reset directory position pointer. */
320
$this->dirPosition = array();
323
function fopen($file, $mode) {
327
function fread($file, $bytes) {
331
function fclose($file) {
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.
343
function realpath($path) {
344
return isset($this->pluginFilesystem[$path])
345
? substr($path, 0, strlen($path)-1) : $this->pluginDir;
348
function is_dir($path) {
349
if (isset($this->pluginFilesystem[$path]) || $path == $this->_descriptorDir) {
356
function is_file($path) {
357
return isset($this->pluginFileList[$path]);
360
function opendir($path) {
361
if ($this->is_dir($path)) {
368
function readdir($path) {
369
if ($this->is_dir($path)) {
370
return $this->getNextFile($path);
372
return "Tried to read invalid path [$path]";
376
function filesize($path) {
380
function chdir($path) {
384
function file_get_contents($file) {
389
* This function implements the functionality of PHP's readdir($path),
390
* but using our test filesystem defined in $this->pluginFilesystem.
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;
397
$this->dirPosition[$readPath]++;
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];
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]];
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];
418
/* Now only three special 'files' remain unread: CVS, . and .. */
419
$fileIndex = $this->dirPosition[$readPath]
420
- count($this->pluginFilesystem[$readPath]) - count($subDirectories);
422
switch ($fileIndex) {
433
/* All files and subdirectories of the current path have been returned. */
437
/* We should never get here. If we do, is_dir will fail the test. */
438
return "Error: file index out of range [$fileIndex]";
b'\\ No newline at end of file'