2011-06-09 05:13:26 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
2014-05-27 20:18:06 +02:00
|
|
|
* This file is part of the phpBB Forum Software package.
|
|
|
|
*
|
|
|
|
* @copyright (c) phpBB Limited <https://www.phpbb.com>
|
|
|
|
* @license GNU General Public License, version 2 (GPL-2.0)
|
|
|
|
*
|
|
|
|
* For full copyright and license information, please see
|
|
|
|
* the docs/CREDITS.txt file.
|
2011-06-09 05:13:26 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
class phpbb_extension_finder_test extends phpbb_test_case
|
|
|
|
{
|
2014-05-09 09:41:49 +02:00
|
|
|
/** @var \phpbb\extension\manager */
|
2011-06-09 05:13:26 +02:00
|
|
|
protected $extension_manager;
|
2014-05-09 09:41:49 +02:00
|
|
|
/** @var \phpbb\finder */
|
2011-06-09 05:13:26 +02:00
|
|
|
protected $finder;
|
|
|
|
|
2020-07-15 15:11:20 +07:00
|
|
|
protected function setUp(): void
|
2011-06-09 05:13:26 +02:00
|
|
|
{
|
2011-07-13 08:22:27 -04:00
|
|
|
$this->extension_manager = new phpbb_mock_extension_manager(
|
2017-12-03 17:19:59 +02:00
|
|
|
__DIR__ . '/',
|
2011-07-13 08:22:27 -04:00
|
|
|
array(
|
2013-12-12 04:29:48 -08:00
|
|
|
'vendor2/foo' => array(
|
|
|
|
'ext_name' => 'vendor2/foo',
|
2011-07-13 08:22:27 -04:00
|
|
|
'ext_active' => '1',
|
2013-12-12 04:29:48 -08:00
|
|
|
'ext_path' => 'ext/vendor2/foo/',
|
2011-07-13 08:22:27 -04:00
|
|
|
),
|
2013-12-12 04:29:48 -08:00
|
|
|
'vendor3/bar' => array(
|
|
|
|
'ext_name' => 'vendor3/bar',
|
2011-07-13 08:22:27 -04:00
|
|
|
'ext_active' => '1',
|
2013-12-12 04:29:48 -08:00
|
|
|
'ext_path' => 'ext/vendor3/bar/',
|
2011-07-13 08:22:27 -04:00
|
|
|
),
|
|
|
|
));
|
2011-06-09 05:13:26 +02:00
|
|
|
|
2011-07-13 08:22:27 -04:00
|
|
|
$this->finder = $this->extension_manager->get_finder();
|
2011-06-09 05:13:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_suffix_get_classes()
|
|
|
|
{
|
|
|
|
$classes = $this->finder
|
2013-07-14 01:32:34 -04:00
|
|
|
->core_path('phpbb/default/')
|
2011-11-18 16:42:53 +01:00
|
|
|
->extension_suffix('_class')
|
2011-06-09 05:13:26 +02:00
|
|
|
->get_classes();
|
|
|
|
|
|
|
|
sort($classes);
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2013-09-25 14:36:50 +02:00
|
|
|
'\phpbb\default\implementation',
|
2013-12-12 04:29:48 -08:00
|
|
|
'\vendor2\foo\a_class',
|
|
|
|
'\vendor2\foo\b_class',
|
|
|
|
'\vendor3\bar\my\hidden_class',
|
2011-06-09 05:13:26 +02:00
|
|
|
),
|
|
|
|
$classes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-05-09 09:41:49 +02:00
|
|
|
public function set_extensions_data()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(
|
|
|
|
array(),
|
|
|
|
array('\phpbb\default\implementation'),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
array('vendor3/bar'),
|
|
|
|
array(
|
|
|
|
'\phpbb\default\implementation',
|
|
|
|
'\vendor3\bar\my\hidden_class',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
array('vendor2/foo', 'vendor3/bar'),
|
|
|
|
array(
|
|
|
|
'\phpbb\default\implementation',
|
|
|
|
'\vendor2\foo\a_class',
|
|
|
|
'\vendor2\foo\b_class',
|
|
|
|
'\vendor3\bar\my\hidden_class',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider set_extensions_data
|
|
|
|
*/
|
|
|
|
public function test_set_extensions($extensions, $expected)
|
|
|
|
{
|
|
|
|
$classes = $this->finder
|
|
|
|
->set_extensions($extensions)
|
|
|
|
->core_path('phpbb/default/')
|
|
|
|
->extension_suffix('_class')
|
|
|
|
->get_classes();
|
|
|
|
|
|
|
|
sort($classes);
|
|
|
|
$this->assertEquals($expected, $classes);
|
|
|
|
}
|
|
|
|
|
2011-09-29 16:55:32 +02:00
|
|
|
public function test_get_directories()
|
2011-08-29 22:21:20 -04:00
|
|
|
{
|
|
|
|
$dirs = $this->finder
|
|
|
|
->directory('/type')
|
|
|
|
->get_directories();
|
|
|
|
|
|
|
|
sort($dirs);
|
|
|
|
$this->assertEquals(array(
|
2017-12-03 17:19:59 +02:00
|
|
|
__DIR__ . '/ext/vendor2/foo/type/',
|
2011-08-29 22:21:20 -04:00
|
|
|
), $dirs);
|
|
|
|
}
|
|
|
|
|
2011-09-29 16:55:32 +02:00
|
|
|
public function test_prefix_get_directories()
|
|
|
|
{
|
|
|
|
$dirs = $this->finder
|
2013-09-25 14:36:50 +02:00
|
|
|
->prefix('ty')
|
2011-09-29 16:55:32 +02:00
|
|
|
->get_directories();
|
|
|
|
|
|
|
|
sort($dirs);
|
|
|
|
$this->assertEquals(array(
|
2017-12-03 17:19:59 +02:00
|
|
|
__DIR__ . '/ext/vendor2/foo/sub/type/',
|
|
|
|
__DIR__ . '/ext/vendor2/foo/type/',
|
|
|
|
__DIR__ . '/ext/vendor2/foo/typewrong/',
|
2011-09-29 16:55:32 +02:00
|
|
|
), $dirs);
|
|
|
|
}
|
|
|
|
|
2011-08-22 02:17:00 -04:00
|
|
|
public function test_prefix_get_classes()
|
|
|
|
{
|
|
|
|
$classes = $this->finder
|
2013-07-14 01:32:34 -04:00
|
|
|
->core_path('phpbb/default/')
|
2011-11-18 16:42:53 +01:00
|
|
|
->extension_prefix('hidden_')
|
2011-08-22 02:17:00 -04:00
|
|
|
->get_classes();
|
|
|
|
|
|
|
|
sort($classes);
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2013-09-25 14:36:50 +02:00
|
|
|
'\phpbb\default\implementation',
|
2013-12-12 04:29:48 -08:00
|
|
|
'\vendor3\bar\my\hidden_class',
|
2011-08-22 02:17:00 -04:00
|
|
|
),
|
|
|
|
$classes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-06-09 05:13:26 +02:00
|
|
|
public function test_directory_get_classes()
|
|
|
|
{
|
|
|
|
$classes = $this->finder
|
2013-07-14 01:32:34 -04:00
|
|
|
->core_path('phpbb/default/')
|
2011-11-18 16:42:53 +01:00
|
|
|
->extension_directory('type')
|
2011-06-09 05:13:26 +02:00
|
|
|
->get_classes();
|
|
|
|
|
|
|
|
sort($classes);
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2013-09-25 14:36:50 +02:00
|
|
|
'\phpbb\default\implementation',
|
2013-12-12 04:29:48 -08:00
|
|
|
'\vendor2\foo\sub\type\alternative',
|
|
|
|
'\vendor2\foo\type\alternative',
|
2011-06-09 05:13:26 +02:00
|
|
|
),
|
|
|
|
$classes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_absolute_directory_get_classes()
|
|
|
|
{
|
|
|
|
$classes = $this->finder
|
|
|
|
->directory('/type/')
|
|
|
|
->get_classes();
|
|
|
|
|
|
|
|
sort($classes);
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2013-12-12 04:29:48 -08:00
|
|
|
'\vendor2\foo\type\alternative',
|
2011-06-09 05:13:26 +02:00
|
|
|
),
|
|
|
|
$classes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-05-26 18:30:04 +02:00
|
|
|
public function test_non_absolute_directory_get_classes()
|
|
|
|
{
|
|
|
|
$classes = $this->finder
|
|
|
|
->directory('type/')
|
|
|
|
->get_classes();
|
|
|
|
|
|
|
|
sort($classes);
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'\vendor2\foo\sub\type\alternative',
|
|
|
|
'\vendor2\foo\type\alternative',
|
|
|
|
),
|
|
|
|
$classes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-06-09 05:13:26 +02:00
|
|
|
public function test_sub_directory_get_classes()
|
|
|
|
{
|
|
|
|
$classes = $this->finder
|
|
|
|
->directory('/sub/type')
|
|
|
|
->get_classes();
|
|
|
|
|
|
|
|
sort($classes);
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2013-12-12 04:29:48 -08:00
|
|
|
'\vendor2\foo\sub\type\alternative',
|
2011-06-09 05:13:26 +02:00
|
|
|
),
|
|
|
|
$classes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-03-12 22:45:44 +01:00
|
|
|
public function test_uncleansub_directory_get_classes()
|
|
|
|
{
|
|
|
|
$classes = $this->finder
|
|
|
|
->directory('/sub/../sub/type')
|
|
|
|
->get_classes();
|
|
|
|
|
|
|
|
sort($classes);
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2013-12-12 04:29:48 -08:00
|
|
|
'\vendor2\foo\sub\type\alternative',
|
2013-03-12 22:45:44 +01:00
|
|
|
),
|
|
|
|
$classes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-05-14 18:56:16 -05:00
|
|
|
public function test_find_from_extension()
|
|
|
|
{
|
|
|
|
$files = $this->finder
|
|
|
|
->extension_directory('/type')
|
2017-12-03 17:19:59 +02:00
|
|
|
->find_from_extension('vendor2/foo', __DIR__ . '/ext/vendor2/foo/');
|
2013-05-14 18:56:16 -05:00
|
|
|
$classes = $this->finder->get_classes_from_files($files);
|
|
|
|
|
|
|
|
sort($classes);
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
2013-12-12 04:29:48 -08:00
|
|
|
'\vendor2\foo\type\alternative',
|
|
|
|
'\vendor2\foo\type\dummy\empty',
|
2013-05-14 18:56:16 -05:00
|
|
|
),
|
|
|
|
$classes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-03-02 11:55:28 -06:00
|
|
|
/**
|
|
|
|
* These do not work because of changes with PHPBB3-11386
|
2017-08-15 15:00:12 -04:00
|
|
|
* They do not seem necessary to me, so I am commenting them out for now
|
2011-08-29 18:43:45 -04:00
|
|
|
public function test_get_classes_create_cache()
|
|
|
|
{
|
|
|
|
$cache = new phpbb_mock_cache;
|
2017-12-03 17:19:59 +02:00
|
|
|
$finder = new \phpbb\finder(new \phpbb\filesystem\filesystem(), __DIR__ . '/', $cache, 'php', '_custom_cache_name');
|
2014-05-09 09:24:30 +02:00
|
|
|
$finder->set_extensions(array_keys($this->extension_manager->all_enabled()));
|
2011-08-29 18:43:45 -04:00
|
|
|
$files = $finder->suffix('_class.php')->get_files();
|
|
|
|
|
|
|
|
$expected_files = array(
|
2011-10-13 21:19:35 +02:00
|
|
|
'ext/bar/my/hidden_class.php' => 'bar',
|
|
|
|
'ext/foo/a_class.php' => 'foo',
|
|
|
|
'ext/foo/b_class.php' => 'foo',
|
2011-08-29 18:43:45 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
$query = array(
|
2011-11-18 16:42:53 +01:00
|
|
|
'core_path' => false,
|
|
|
|
'core_suffix' => '_class.php',
|
|
|
|
'core_prefix' => false,
|
|
|
|
'core_directory' => false,
|
|
|
|
'extension_suffix' => '_class.php',
|
|
|
|
'extension_prefix' => false,
|
|
|
|
'extension_directory' => false,
|
2011-08-29 22:21:20 -04:00
|
|
|
'is_dir' => false,
|
2011-08-29 18:43:45 -04:00
|
|
|
);
|
|
|
|
|
2011-08-29 18:57:21 -04:00
|
|
|
$cache->checkAssociativeVar($this, '_custom_cache_name', array(
|
2011-08-29 18:43:45 -04:00
|
|
|
md5(serialize($query)) => $expected_files,
|
2011-10-13 21:19:35 +02:00
|
|
|
), false);
|
2011-08-29 18:43:45 -04:00
|
|
|
}
|
|
|
|
|
2011-06-09 05:13:26 +02:00
|
|
|
public function test_cached_get_files()
|
|
|
|
{
|
|
|
|
$query = array(
|
2013-07-14 01:32:34 -04:00
|
|
|
'core_path' => 'phpbb/foo',
|
2011-11-18 16:42:53 +01:00
|
|
|
'core_suffix' => false,
|
|
|
|
'core_prefix' => false,
|
|
|
|
'core_directory' => 'bar',
|
|
|
|
'extension_suffix' => false,
|
|
|
|
'extension_prefix' => false,
|
|
|
|
'extension_directory' => false,
|
2011-08-29 22:21:20 -04:00
|
|
|
'is_dir' => false,
|
2011-06-09 05:13:26 +02:00
|
|
|
);
|
|
|
|
|
2014-05-06 18:27:30 +02:00
|
|
|
$finder = new \phpbb\finder(
|
2015-03-12 00:25:00 +01:00
|
|
|
new \phpbb\filesystem\filesystem(),
|
2017-12-03 17:19:59 +02:00
|
|
|
__DIR__ . '/',
|
2013-03-02 11:55:28 -06:00
|
|
|
new phpbb_mock_cache(array(
|
|
|
|
'_ext_finder' => array(
|
|
|
|
md5(serialize($query)) => array('file_name' => 'extension'),
|
|
|
|
),
|
|
|
|
))
|
|
|
|
);
|
2014-05-09 09:24:30 +02:00
|
|
|
$finder->set_extensions(array_keys($this->extension_manager->all_enabled()));
|
2011-06-09 05:13:26 +02:00
|
|
|
|
|
|
|
$classes = $finder
|
2011-11-18 16:42:53 +01:00
|
|
|
->core_path($query['core_path'])
|
|
|
|
->core_directory($query['core_directory'])
|
2011-06-09 05:13:26 +02:00
|
|
|
->get_files();
|
|
|
|
|
|
|
|
sort($classes);
|
|
|
|
$this->assertEquals(
|
2017-12-03 17:19:59 +02:00
|
|
|
array(__DIR__ . '/file_name'),
|
2011-06-09 05:13:26 +02:00
|
|
|
$classes
|
|
|
|
);
|
|
|
|
}
|
2013-03-02 11:55:28 -06:00
|
|
|
*/
|
2011-06-09 05:13:26 +02:00
|
|
|
}
|