2012-07-28 14:59:55 -05:00
< ? php
/**
*
* @ package testing
* @ copyright ( c ) 2011 phpBB Group
* @ license http :// opensource . org / licenses / gpl - 2.0 . php GNU General Public License v2
*
*/
2013-04-01 11:18:46 +02:00
class phpbb_extension_metadata_manager_test extends phpbb_database_test_case
2012-07-28 14:59:55 -05:00
{
protected $class_loader ;
protected $extension_manager ;
2012-07-29 20:08:30 -05:00
protected $cache ;
protected $config ;
protected $db ;
2012-07-28 14:59:55 -05:00
protected $phpbb_root_path ;
2012-07-29 20:08:30 -05:00
protected $phpEx ;
protected $template ;
protected $user ;
2012-07-28 14:59:55 -05:00
public function getDataSet ()
{
return $this -> createXMLDataSet ( dirname ( __FILE__ ) . '/fixtures/extensions.xml' );
}
protected function setUp ()
{
parent :: setUp ();
2012-07-29 20:08:30 -05:00
$this -> cache = new phpbb_mock_cache ();
2013-09-10 14:01:09 +02:00
$this -> config = new \phpbb\config\config ( array (
2012-07-29 20:08:30 -05:00
'version' => '3.1.0' ,
));
$this -> db = $this -> new_dbal ();
2013-09-10 14:01:09 +02:00
$this -> db_tools = new \phpbb\db\tools ( $this -> db );
2012-07-28 14:59:55 -05:00
$this -> phpbb_root_path = dirname ( __FILE__ ) . '/' ;
2013-04-24 17:27:24 -05:00
$this -> phpEx = 'php' ;
2013-09-10 14:01:09 +02:00
$this -> user = new \phpbb\user ();
2013-02-13 21:12:50 -06:00
$this -> table_prefix = 'phpbb_' ;
2012-07-29 20:08:30 -05:00
2013-09-10 14:01:09 +02:00
$this -> template = new \phpbb\template\twig\twig (
2012-07-29 20:08:30 -05:00
$this -> phpbb_root_path ,
$this -> phpEx ,
$this -> config ,
$this -> user ,
2013-09-10 14:01:09 +02:00
new \phpbb\template\context ()
2012-07-29 20:08:30 -05:00
);
2012-07-28 14:59:55 -05:00
2013-09-10 14:01:09 +02:00
$this -> migrator = new \phpbb\db\migrator (
2013-03-03 19:54:22 -06:00
$this -> config ,
$this -> db ,
$this -> db_tools ,
'phpbb_migrations' ,
$this -> phpbb_root_path ,
'php' ,
$this -> table_prefix ,
array ()
);
2013-05-01 14:09:08 -05:00
$container = new phpbb_mock_container_builder ();
$container -> set ( 'migrator' , $migrator );
2013-09-10 14:01:09 +02:00
$this -> extension_manager = new \phpbb\extension\manager (
2013-05-01 14:09:08 -05:00
$container ,
2012-07-30 13:36:51 -05:00
$this -> db ,
2012-07-29 20:08:30 -05:00
$this -> config ,
2013-09-10 14:01:09 +02:00
new \phpbb\filesystem (),
2012-07-28 14:59:55 -05:00
'phpbb_ext' ,
$this -> phpbb_root_path ,
2012-07-29 20:08:30 -05:00
$this -> phpEx ,
$this -> cache
2012-07-28 14:59:55 -05:00
);
}
// Should fail from missing composer.json
public function test_bar ()
{
$ext_name = 'bar' ;
2012-07-29 20:08:30 -05:00
$manager = $this -> get_metadata_manager ( $ext_name );
2012-07-28 14:59:55 -05:00
try
{
$manager -> get_metadata ();
}
2013-09-10 14:01:09 +02:00
catch ( \phpbb\extension\exception $e ){}
2012-07-28 14:59:55 -05:00
$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' ;
2012-07-29 20:08:30 -05:00
$manager = $this -> get_metadata_manager ( $ext_name );
2012-07-28 14:59:55 -05:00
try
{
$metadata = $manager -> get_metadata ();
}
2013-09-10 14:01:09 +02:00
catch ( \phpbb\extension\exception $e )
2012-07-28 14:59:55 -05:00
{
$this -> fail ( $e );
}
$json = json_decode ( file_get_contents ( $this -> phpbb_root_path . 'ext/foo/composer.json' ), true );
$this -> assertEquals ( $metadata , $json );
}
2012-08-27 17:39:32 -05:00
public function test_validator_non_existant ()
2012-07-28 14:59:55 -05:00
{
$ext_name = 'validator' ;
2012-07-29 20:08:30 -05:00
$manager = $this -> get_metadata_manager ( $ext_name );
2012-07-28 14:59:55 -05:00
// Non-existant data
try
{
$manager -> validate ( 'name' );
2012-08-27 17:39:32 -05:00
$this -> fail ( 'Exception not triggered' );
}
2013-09-10 14:01:09 +02:00
catch ( \phpbb\extension\exception $e )
2012-08-27 17:39:32 -05:00
{
$this -> assertEquals (( string ) $e , 'Required meta field \'name\' has not been set.' );
2012-07-28 14:59:55 -05:00
}
try
{
$manager -> validate ( 'type' );
2012-08-27 17:39:32 -05:00
$this -> fail ( 'Exception not triggered' );
}
2013-09-10 14:01:09 +02:00
catch ( \phpbb\extension\exception $e )
2012-08-27 17:39:32 -05:00
{
$this -> assertEquals (( string ) $e , 'Required meta field \'type\' has not been set.' );
2012-07-28 14:59:55 -05:00
}
try
{
$manager -> validate ( 'licence' );
2012-08-27 17:39:32 -05:00
$this -> fail ( 'Exception not triggered' );
}
2013-09-10 14:01:09 +02:00
catch ( \phpbb\extension\exception $e )
2012-08-27 17:39:32 -05:00
{
$this -> assertEquals (( string ) $e , 'Required meta field \'licence\' has not been set.' );
2012-07-28 14:59:55 -05:00
}
try
{
$manager -> validate ( 'version' );
2012-08-27 17:39:32 -05:00
$this -> fail ( 'Exception not triggered' );
}
2013-09-10 14:01:09 +02:00
catch ( \phpbb\extension\exception $e )
2012-08-27 17:39:32 -05:00
{
$this -> assertEquals (( string ) $e , 'Required meta field \'version\' has not been set.' );
2012-07-28 14:59:55 -05:00
}
try
{
$manager -> validate_authors ();
2012-08-27 17:39:32 -05:00
$this -> fail ( 'Exception not triggered' );
}
2013-09-10 14:01:09 +02:00
catch ( \phpbb\extension\exception $e )
2012-08-27 17:39:32 -05:00
{
$this -> assertEquals (( string ) $e , 'Required meta field \'authors\' has not been set.' );
2012-07-28 14:59:55 -05:00
}
$manager -> merge_metadata ( array (
'authors' => array (
array (),
),
));
try
{
$manager -> validate_authors ();
2012-08-27 17:39:32 -05:00
$this -> fail ( 'Exception not triggered' );
2012-07-28 14:59:55 -05:00
}
2013-09-10 14:01:09 +02:00
catch ( \phpbb\extension\exception $e )
2012-08-27 17:39:32 -05:00
{
$this -> assertEquals (( string ) $e , 'Required meta field \'author name\' has not been set.' );
}
}
public function test_validator_invalid ()
{
$ext_name = 'validator' ;
2012-07-28 14:59:55 -05:00
2012-08-27 17:39:32 -05:00
$manager = $this -> get_metadata_manager ( $ext_name );
2012-07-28 14:59:55 -05:00
// Invalid data
$manager -> set_metadata ( array (
'name' => 'asdf' ,
'type' => 'asdf' ,
'licence' => '' ,
'version' => '' ,
));
try
{
$manager -> validate ( 'name' );
2012-08-27 17:39:32 -05:00
$this -> fail ( 'Exception not triggered' );
}
2013-09-10 14:01:09 +02:00
catch ( \phpbb\extension\exception $e )
2012-08-27 17:39:32 -05:00
{
$this -> assertEquals (( string ) $e , 'Meta field \'name\' is invalid.' );
2012-07-28 14:59:55 -05:00
}
try
{
$manager -> validate ( 'type' );
2012-08-27 17:39:32 -05:00
$this -> fail ( 'Exception not triggered' );
}
2013-09-10 14:01:09 +02:00
catch ( \phpbb\extension\exception $e )
2012-08-27 17:39:32 -05:00
{
$this -> assertEquals (( string ) $e , 'Meta field \'type\' is invalid.' );
2012-07-28 14:59:55 -05:00
}
try
{
$manager -> validate ( 'licence' );
2012-08-27 17:39:32 -05:00
$this -> fail ( 'Exception not triggered' );
}
2013-09-10 14:01:09 +02:00
catch ( \phpbb\extension\exception $e )
2012-08-27 17:39:32 -05:00
{
$this -> assertEquals (( string ) $e , 'Meta field \'licence\' is invalid.' );
2012-07-28 14:59:55 -05:00
}
try
{
$manager -> validate ( 'version' );
2012-08-27 17:39:32 -05:00
$this -> fail ( 'Exception not triggered' );
}
2013-09-10 14:01:09 +02:00
catch ( \phpbb\extension\exception $e )
2012-08-27 17:39:32 -05:00
{
$this -> assertEquals (( string ) $e , 'Meta field \'version\' is invalid.' );
2012-07-28 14:59:55 -05:00
}
2012-08-27 17:39:32 -05:00
}
public function test_validator_valid ()
{
$ext_name = 'validator' ;
2012-07-28 14:59:55 -05:00
2012-08-27 17:39:32 -05:00
$manager = $this -> get_metadata_manager ( $ext_name );
2012-07-28 14:59:55 -05:00
// 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' ));
}
2013-09-10 14:01:09 +02:00
catch ( \phpbb\extension\exception $e )
2012-07-28 14:59:55 -05:00
{
$this -> fail ( $e );
}
2012-08-27 17:39:32 -05:00
}
2012-07-28 14:59:55 -05:00
2012-08-27 17:39:32 -05:00
public function test_validator_requirements ()
{
$ext_name = 'validator' ;
$manager = $this -> get_metadata_manager ( $ext_name );
2012-07-28 14:59:55 -05:00
// 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 ());
}
2013-09-10 14:01:09 +02:00
catch ( \phpbb\extension\exception $e )
2012-07-28 14:59:55 -05:00
{
$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 ());
}
2013-09-10 14:01:09 +02:00
catch ( \phpbb\extension\exception $e )
2012-07-28 14:59:55 -05:00
{
$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 ());
}
2013-09-10 14:01:09 +02:00
catch ( \phpbb\extension\exception $e )
2012-07-28 14:59:55 -05:00
{
$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 ());
}
2013-09-10 14:01:09 +02:00
catch ( \phpbb\extension\exception $e )
2012-07-28 14:59:55 -05:00
{
$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 ());
}
2013-09-10 14:01:09 +02:00
catch ( \phpbb\extension\exception $e )
2012-07-28 14:59:55 -05:00
{
$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 ());
}
2013-09-10 14:01:09 +02:00
catch ( \phpbb\extension\exception $e )
2012-07-28 14:59:55 -05:00
{
$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 ());
}
2013-09-10 14:01:09 +02:00
catch ( \phpbb\extension\exception $e )
2012-07-28 14:59:55 -05:00
{
$this -> fail ( $e );
}
}
2012-07-29 20:08:30 -05:00
/**
* Get an instance of the metadata manager
*
* @ param string $ext_name
2013-04-04 21:36:32 +02:00
* @ return phpbb_mock_metadata_manager
2012-07-29 20:08:30 -05:00
*/
private function get_metadata_manager ( $ext_name )
{
2013-04-04 21:36:32 +02:00
return new phpbb_mock_metadata_manager (
2012-07-29 20:08:30 -05:00
$ext_name ,
2013-03-18 23:15:27 +01:00
$this -> config ,
2012-07-29 20:08:30 -05:00
$this -> extension_manager ,
$this -> template ,
2013-03-18 23:15:27 +01:00
$this -> phpbb_root_path
2012-07-29 20:08:30 -05:00
);
}
2012-07-28 14:59:55 -05:00
}