mirror of
https://github.com/phpbb/phpbb.git
synced 2025-05-04 06:39:14 +02:00
[ticket/10631] Metadata manager tests
PHPBB3-10631
This commit is contained in:
parent
8c5786636a
commit
500879520c
@ -178,36 +178,37 @@ class phpbb_extension_metadata_manager
|
||||
'version' => '#.+#',
|
||||
);
|
||||
|
||||
if (isset($fields[$name]))
|
||||
switch ($name)
|
||||
{
|
||||
if (!isset($this->metadata[$name]))
|
||||
{
|
||||
throw new phpbb_extension_exception("Required meta field '$name' has not been set.");
|
||||
}
|
||||
case 'all':
|
||||
$this->validate('display');
|
||||
|
||||
if (!preg_match($fields[$name], $this->metadata[$name]))
|
||||
{
|
||||
throw new phpbb_extension_exception("Meta field '$name' is invalid.");
|
||||
}
|
||||
}
|
||||
$this->validate_enable();
|
||||
break;
|
||||
|
||||
// Validate all fields
|
||||
if ($name == 'all')
|
||||
{
|
||||
$this->validate('display');
|
||||
case 'display':
|
||||
foreach ($fields as $field => $data)
|
||||
{
|
||||
$this->validate($field);
|
||||
}
|
||||
|
||||
$this->validate_enable();
|
||||
}
|
||||
$this->validate_authors();
|
||||
break;
|
||||
|
||||
// Validate display fields
|
||||
if ($name == 'display')
|
||||
{
|
||||
foreach ($fields as $field => $data)
|
||||
{
|
||||
$this->validate($field);
|
||||
}
|
||||
default:
|
||||
if (isset($fields[$name]))
|
||||
{
|
||||
if (!isset($this->metadata[$name]))
|
||||
{
|
||||
throw new phpbb_extension_exception("Required meta field '$name' has not been set.");
|
||||
}
|
||||
|
||||
$this->validate_authors();
|
||||
if (!preg_match($fields[$name], $this->metadata[$name]))
|
||||
{
|
||||
throw new phpbb_extension_exception("Meta field '$name' is invalid.");
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -218,7 +219,7 @@ class phpbb_extension_metadata_manager
|
||||
*
|
||||
* @return boolean True when passes validation, throws exception if invalid
|
||||
*/
|
||||
private function validate_authors()
|
||||
public function validate_authors()
|
||||
{
|
||||
if (empty($this->metadata['authors']))
|
||||
{
|
||||
@ -258,7 +259,7 @@ class phpbb_extension_metadata_manager
|
||||
*
|
||||
* @return boolean True when passes validation
|
||||
*/
|
||||
private function validate_require_phpbb()
|
||||
public function validate_require_phpbb()
|
||||
{
|
||||
if (!isset($this->metadata['require']['phpbb']))
|
||||
{
|
||||
@ -273,7 +274,7 @@ class phpbb_extension_metadata_manager
|
||||
*
|
||||
* @return boolean True when passes validation
|
||||
*/
|
||||
private function validate_require_php()
|
||||
public function validate_require_php()
|
||||
{
|
||||
if (!isset($this->metadata['require']['php']))
|
||||
{
|
||||
|
22
tests/extension/ext/foo/composer.json
Normal file
22
tests/extension/ext/foo/composer.json
Normal file
@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "foo/example",
|
||||
"type": "phpbb3-extension",
|
||||
"description": "An example/sample extension to be used for testing purposes in phpBB Development.",
|
||||
"version": "1.0.0",
|
||||
"time": "2012-02-15 01:01:01",
|
||||
"licence": "GNU GPL v2",
|
||||
"authors": [{
|
||||
"name": "Nathan Guse",
|
||||
"username": "EXreaction",
|
||||
"email": "nathaniel.guse@gmail.com",
|
||||
"homepage": "http://lithiumstudios.org",
|
||||
"role": "N/A"
|
||||
}],
|
||||
"require": {
|
||||
"php": ">=5.3",
|
||||
"phpbb": "3.1.0-dev"
|
||||
},
|
||||
"extra": {
|
||||
"display-name": "phpBB Foo Extension"
|
||||
}
|
||||
}
|
374
tests/extension/metadata_manager_test.php
Normal file
374
tests/extension/metadata_manager_test.php
Normal file
@ -0,0 +1,374 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @package testing
|
||||
* @copyright (c) 2011 phpBB Group
|
||||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
||||
*
|
||||
*/
|
||||
|
||||
class metadata_manager_test extends phpbb_database_test_case
|
||||
{
|
||||
protected $class_loader;
|
||||
protected $extension_manager;
|
||||
protected $phpbb_root_path;
|
||||
|
||||
public function getDataSet()
|
||||
{
|
||||
return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/extensions.xml');
|
||||
}
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->phpbb_root_path = dirname(__FILE__) . '/';
|
||||
|
||||
$this->extension_manager = new phpbb_extension_manager(
|
||||
$this->new_dbal(),
|
||||
new phpbb_config(array()),
|
||||
'phpbb_ext',
|
||||
$this->phpbb_root_path,
|
||||
'.php',
|
||||
new phpbb_mock_cache
|
||||
);
|
||||
}
|
||||
|
||||
// Should fail from missing composer.json
|
||||
public function test_bar()
|
||||
{
|
||||
$ext_name = 'bar';
|
||||
|
||||
$manager = new phpbb_extension_metadata_manager_test(
|
||||
$ext_name,
|
||||
$this->new_dbal(),
|
||||
$this->extension_manager,
|
||||
$this->phpbb_root_path,
|
||||
'.php',
|
||||
new phpbb_template(
|
||||
$this->phpbb_root_path,
|
||||
'.php',
|
||||
new phpbb_config(array()),
|
||||
new phpbb_user(),
|
||||
new phpbb_style_resource_locator()
|
||||
),
|
||||
new phpbb_config(array())
|
||||
);
|
||||
|
||||
try
|
||||
{
|
||||
$manager->get_metadata();
|
||||
}
|
||||
catch(phpbb_extension_exception $e){}
|
||||
|
||||
$this->assertEquals((string) $e, 'The required file does not exist: ' . $this->phpbb_root_path . $this->extension_manager->get_extension_path($ext_name) . 'composer.json');
|
||||
}
|
||||
|
||||
// Should be the same as a direct json_decode of the composer.json file
|
||||
public function test_foo()
|
||||
{
|
||||
$ext_name = 'foo';
|
||||
|
||||
$manager = new phpbb_extension_metadata_manager_test(
|
||||
$ext_name,
|
||||
$this->new_dbal(),
|
||||
$this->extension_manager,
|
||||
$this->phpbb_root_path,
|
||||
'.php',
|
||||
new phpbb_template(
|
||||
$this->phpbb_root_path,
|
||||
'.php',
|
||||
new phpbb_config(array()),
|
||||
new phpbb_user(),
|
||||
new phpbb_style_resource_locator()
|
||||
),
|
||||
new phpbb_config(array())
|
||||
);
|
||||
|
||||
try
|
||||
{
|
||||
$metadata = $manager->get_metadata();
|
||||
}
|
||||
catch(phpbb_extension_exception $e)
|
||||
{
|
||||
$this->fail($e);
|
||||
}
|
||||
|
||||
$json = json_decode(file_get_contents($this->phpbb_root_path . 'ext/foo/composer.json'), true);
|
||||
|
||||
$this->assertEquals($metadata, $json);
|
||||
}
|
||||
|
||||
public function test_validator()
|
||||
{
|
||||
$ext_name = 'validator';
|
||||
|
||||
$manager = new phpbb_extension_metadata_manager_test(
|
||||
$ext_name,
|
||||
$this->new_dbal(),
|
||||
$this->extension_manager,
|
||||
$this->phpbb_root_path,
|
||||
'.php',
|
||||
new phpbb_template(
|
||||
$this->phpbb_root_path,
|
||||
'.php',
|
||||
new phpbb_config(array()),
|
||||
new phpbb_user(),
|
||||
new phpbb_style_resource_locator()
|
||||
),
|
||||
new phpbb_config(array(
|
||||
'version' => '3.1.0',
|
||||
))
|
||||
);
|
||||
|
||||
// Non-existant data
|
||||
try
|
||||
{
|
||||
$manager->validate('name');
|
||||
}
|
||||
catch(phpbb_extension_exception $e) {}
|
||||
$this->assertEquals((string) $e, 'Required meta field \'name\' has not been set.');
|
||||
|
||||
try
|
||||
{
|
||||
$manager->validate('type');
|
||||
}
|
||||
catch(phpbb_extension_exception $e) {}
|
||||
$this->assertEquals((string) $e, 'Required meta field \'type\' has not been set.');
|
||||
|
||||
try
|
||||
{
|
||||
$manager->validate('licence');
|
||||
}
|
||||
catch(phpbb_extension_exception $e) {}
|
||||
$this->assertEquals((string) $e, 'Required meta field \'licence\' has not been set.');
|
||||
|
||||
try
|
||||
{
|
||||
$manager->validate('version');
|
||||
}
|
||||
catch(phpbb_extension_exception $e) {}
|
||||
$this->assertEquals((string) $e, 'Required meta field \'version\' has not been set.');
|
||||
|
||||
try
|
||||
{
|
||||
$manager->validate_authors();
|
||||
}
|
||||
catch(phpbb_extension_exception $e) {}
|
||||
$this->assertEquals((string) $e, 'Required meta field \'authors\' has not been set.');
|
||||
|
||||
$manager->merge_metadata(array(
|
||||
'authors' => array(
|
||||
array(),
|
||||
),
|
||||
));
|
||||
|
||||
try
|
||||
{
|
||||
$manager->validate_authors();
|
||||
}
|
||||
catch(phpbb_extension_exception $e) {}
|
||||
$this->assertEquals((string) $e, 'Required meta field \'author name\' has not been set.');
|
||||
|
||||
|
||||
// Invalid data
|
||||
$manager->set_metadata(array(
|
||||
'name' => 'asdf',
|
||||
'type' => 'asdf',
|
||||
'licence' => '',
|
||||
'version' => '',
|
||||
));
|
||||
|
||||
try
|
||||
{
|
||||
$manager->validate('name');
|
||||
}
|
||||
catch(phpbb_extension_exception $e) {}
|
||||
$this->assertEquals((string) $e, 'Meta field \'name\' is invalid.');
|
||||
|
||||
try
|
||||
{
|
||||
$manager->validate('type');
|
||||
}
|
||||
catch(phpbb_extension_exception $e) {}
|
||||
$this->assertEquals((string) $e, 'Meta field \'type\' is invalid.');
|
||||
|
||||
try
|
||||
{
|
||||
$manager->validate('licence');
|
||||
}
|
||||
catch(phpbb_extension_exception $e) {}
|
||||
$this->assertEquals((string) $e, 'Meta field \'licence\' is invalid.');
|
||||
|
||||
try
|
||||
{
|
||||
$manager->validate('version');
|
||||
}
|
||||
catch(phpbb_extension_exception $e) {}
|
||||
$this->assertEquals((string) $e, 'Meta field \'version\' is invalid.');
|
||||
|
||||
|
||||
// Valid data
|
||||
$manager->set_metadata(array(
|
||||
'name' => 'test/foo',
|
||||
'type' => 'phpbb3-extension',
|
||||
'licence' => 'GPL v2',
|
||||
'version' => '1.0.0',
|
||||
));
|
||||
|
||||
try
|
||||
{
|
||||
$this->assertEquals(true, $manager->validate('enable'));
|
||||
}
|
||||
catch(phpbb_extension_exception $e)
|
||||
{
|
||||
$this->fail($e);
|
||||
}
|
||||
|
||||
|
||||
// Too high of requirements
|
||||
$manager->merge_metadata(array(
|
||||
'require' => array(
|
||||
'php' => '10.0.0',
|
||||
'phpbb' => '3.2.0', // config is set to 3.1.0
|
||||
),
|
||||
));
|
||||
|
||||
try
|
||||
{
|
||||
$this->assertEquals(false, $manager->validate_require_php());
|
||||
$this->assertEquals(false, $manager->validate_require_phpbb());
|
||||
}
|
||||
catch(phpbb_extension_exception $e)
|
||||
{
|
||||
$this->fail($e);
|
||||
}
|
||||
|
||||
|
||||
// Too high of requirements
|
||||
$manager->merge_metadata(array(
|
||||
'require' => array(
|
||||
'php' => '5.3.0',
|
||||
'phpbb' => '3.1.0-beta', // config is set to 3.1.0
|
||||
),
|
||||
));
|
||||
|
||||
try
|
||||
{
|
||||
$this->assertEquals(true, $manager->validate_require_php());
|
||||
$this->assertEquals(true, $manager->validate_require_phpbb());
|
||||
}
|
||||
catch(phpbb_extension_exception $e)
|
||||
{
|
||||
$this->fail($e);
|
||||
}
|
||||
|
||||
|
||||
// Too high of requirements
|
||||
$manager->merge_metadata(array(
|
||||
'require' => array(
|
||||
'php' => '>' . phpversion(),
|
||||
'phpbb' => '>3.1.0', // config is set to 3.1.0
|
||||
),
|
||||
));
|
||||
|
||||
try
|
||||
{
|
||||
$this->assertEquals(false, $manager->validate_require_php());
|
||||
$this->assertEquals(false, $manager->validate_require_phpbb());
|
||||
}
|
||||
catch(phpbb_extension_exception $e)
|
||||
{
|
||||
$this->fail($e);
|
||||
}
|
||||
|
||||
|
||||
// Too high of current install
|
||||
$manager->merge_metadata(array(
|
||||
'require' => array(
|
||||
'php' => '<' . phpversion(),
|
||||
'phpbb' => '<3.1.0', // config is set to 3.1.0
|
||||
),
|
||||
));
|
||||
|
||||
try
|
||||
{
|
||||
$this->assertEquals(false, $manager->validate_require_php());
|
||||
$this->assertEquals(false, $manager->validate_require_phpbb());
|
||||
}
|
||||
catch(phpbb_extension_exception $e)
|
||||
{
|
||||
$this->fail($e);
|
||||
}
|
||||
|
||||
|
||||
// Matching requirements
|
||||
$manager->merge_metadata(array(
|
||||
'require' => array(
|
||||
'php' => phpversion(),
|
||||
'phpbb' => '3.1.0', // config is set to 3.1.0
|
||||
),
|
||||
));
|
||||
|
||||
try
|
||||
{
|
||||
$this->assertEquals(true, $manager->validate_require_php());
|
||||
$this->assertEquals(true, $manager->validate_require_phpbb());
|
||||
}
|
||||
catch(phpbb_extension_exception $e)
|
||||
{
|
||||
$this->fail($e);
|
||||
}
|
||||
|
||||
|
||||
// Matching requirements
|
||||
$manager->merge_metadata(array(
|
||||
'require' => array(
|
||||
'php' => '>=' . phpversion(),
|
||||
'phpbb' => '>=3.1.0', // config is set to 3.1.0
|
||||
),
|
||||
));
|
||||
|
||||
try
|
||||
{
|
||||
$this->assertEquals(true, $manager->validate_require_php());
|
||||
$this->assertEquals(true, $manager->validate_require_phpbb());
|
||||
}
|
||||
catch(phpbb_extension_exception $e)
|
||||
{
|
||||
$this->fail($e);
|
||||
}
|
||||
|
||||
|
||||
// Matching requirements
|
||||
$manager->merge_metadata(array(
|
||||
'require' => array(
|
||||
'php' => '<=' . phpversion(),
|
||||
'phpbb' => '<=3.1.0', // config is set to 3.1.0
|
||||
),
|
||||
));
|
||||
|
||||
try
|
||||
{
|
||||
$this->assertEquals(true, $manager->validate_require_php());
|
||||
$this->assertEquals(true, $manager->validate_require_phpbb());
|
||||
}
|
||||
catch(phpbb_extension_exception $e)
|
||||
{
|
||||
$this->fail($e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class phpbb_extension_metadata_manager_test extends phpbb_extension_metadata_manager
|
||||
{
|
||||
public function set_metadata($metadata)
|
||||
{
|
||||
$this->metadata = $metadata;
|
||||
}
|
||||
|
||||
public function merge_metadata($metadata)
|
||||
{
|
||||
$this->metadata = array_merge($this->metadata, $metadata);
|
||||
}
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @package testing
|
||||
* @copyright (c) 2011 phpBB Group
|
||||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
||||
*
|
||||
*/
|
||||
|
||||
class phpbb_extension_metadata_manager_test extends phpbb_database_test_case
|
||||
{
|
||||
protected $class_loader;
|
||||
protected $extension_manager;
|
||||
|
||||
public function getDataSet()
|
||||
{
|
||||
return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/extensions.xml');
|
||||
}
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->extension_manager = new phpbb_extension_manager(
|
||||
$this->new_dbal(),
|
||||
new phpbb_config(array()),
|
||||
'phpbb_ext',
|
||||
dirname(__FILE__) . '/',
|
||||
'.php',
|
||||
new phpbb_mock_cache
|
||||
);
|
||||
}
|
||||
|
||||
public function test_bar()
|
||||
{
|
||||
$phpbb_extension_metadata_manager = new phpbb_extension_metadata_manager(
|
||||
'bar',
|
||||
$this->new_dbal(),
|
||||
$this->extension_manager,
|
||||
dirname(__FILE__) . '/',
|
||||
'.php',
|
||||
new phpbb_template(
|
||||
dirname(__FILE__) . '/',
|
||||
'.php',
|
||||
new phpbb_config(array()),
|
||||
new phpbb_user(),
|
||||
new phpbb_style_resource_locator()
|
||||
),
|
||||
new phpbb_config(array())
|
||||
);
|
||||
|
||||
//$this->assertEquals(array('bar', 'foo', 'vendor/moo'), array_keys($this->extension_manager->all_available()));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user