* @license GNU General Public License, version 2 (GPL-2.0) * * For full copyright and license information, please see * the docs/CREDITS.txt file. * */ require_once dirname(__FILE__) . '/template_test_case.php'; class phpbb_template_extension_test extends phpbb_template_template_test_case { protected function setup_engine(array $new_config = []) { global $config, $phpbb_container, $phpbb_dispatcher, $phpbb_root_path, $phpEx; $defaults = $this->config_defaults(); $defaults = array_merge($defaults, [ 'allow_avatar' => true, 'allow_avatar_upload' => true, ]); $config = new \phpbb\config\config(array_merge($defaults, $new_config)); $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx); $this->lang = $lang = new \phpbb\language\language($lang_loader); $this->user = new \phpbb\user($lang, '\phpbb\datetime'); $this->user->style['style_path'] = 'chameleon'; global $auth, $request, $symfony_request, $user; $user = new phpbb_mock_user(); $user->optionset('user_id', 2); $auth = $this->getMockBuilder('phpbb\auth\auth') ->disableOriginalConstructor() ->setMethods(['acl_get']) ->getMock(); $auth->method('acl_get') ->willReturn(true); $filesystem = $this->createMock('\phpbb\filesystem\filesystem'); $filesystem->expects($this->any()) ->method('exists') ->with($this->stringContains('theme/png/')) ->will($this->returnValueMap([ ['phpBB/styles/chameleon/theme/png/phone.png', true], ['phpBB/styles/chameleon/theme/png/pencil.png', true], ['phpBB/styles/chameleon/theme/png/user.png', false], ])); $request = new phpbb_mock_request; $symfony_request = new \phpbb\symfony_request( $request ); $phpbb_path_helper = new \phpbb\path_helper( $symfony_request, $request, $phpbb_root_path, $phpEx ); $storage = $this->getMockBuilder('\phpbb\storage\storage') ->disableOriginalConstructor() ->getMock(); $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); $phpbb_container = new phpbb_mock_container_builder(); $files = new phpbb\files\factory($phpbb_container); $upload_avatar_driver = new phpbb\avatar\driver\upload($config, $phpbb_root_path, $phpEx, $storage, $phpbb_path_helper, $phpbb_dispatcher, $files, new \bantu\IniGetWrapper\IniGetWrapper()); $upload_avatar_driver->set_name('avatar.driver.upload'); $phpbb_container->set('avatar.manager', new \phpbb\avatar\manager($config, $phpbb_dispatcher, [ $upload_avatar_driver, ])); $phpbb_container->set('path_helper', $phpbb_path_helper); $class = new ReflectionClass('\phpbb\avatar\manager'); $enabled_drivers = $class->getProperty('enabled_drivers'); $enabled_drivers->setAccessible(true); $enabled_drivers->setValue(false); $this->template_path = $this->test_path . '/templates'; $cache_path = $phpbb_root_path . 'cache/twig'; $context = new \phpbb\template\context(); $loader = new \phpbb\template\twig\loader([]); $twig = new \phpbb\template\twig\environment( $config, $filesystem, $phpbb_path_helper, $cache_path, null, $loader, new \phpbb\event\dispatcher($phpbb_container), [ 'cache' => false, 'debug' => false, 'auto_reload' => true, 'autoescape' => false, ] ); $this->template = new phpbb\template\twig\twig( $phpbb_path_helper, $config, $context, $twig, $cache_path, $this->user, [ new \phpbb\template\twig\extension($context, $twig, $this->lang), new \phpbb\template\twig\extension\avatar(), new \phpbb\template\twig\extension\config($config), new \phpbb\template\twig\extension\icon($this->user), new \phpbb\template\twig\extension\username(), ] ); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); $this->template->set_custom_style('tests', [ $this->template_path, $phpbb_root_path . 'styles/all/imgs', $phpbb_root_path . 'styles/all/template', ]); } public function data_template_extensions() { return [ [ 'avatar_user.html', [ 'row' => [ 'user_avatar' => 'great_avatar.png', 'user_avatar_type' => 'avatar.driver.upload', 'user_avatar_width' => 90, 'user_avatar_height' => 90, ], 'alt' => 'foo' ], [], [], 'foo', [] ], [ 'avatar_user.html', [ 'row' => [ 'user_avatar' => 'great_avatar.png', 'user_avatar_type' => 'avatar.driver.upload', 'user_avatar_width' => 90, 'user_avatar_height' => 90, ], 'alt' => 'foo', 'ignore_config' => true, 'lazy' => true, ], [], [], 'foo', [] ], [ 'avatar_user.html', [ 'row' => [ 'user_avatar' => 'foo@bar.com', 'user_avatar_type' => 'avatar.driver.gravatar', 'user_avatar_width' => 90, 'user_avatar_height' => 90, ], 'alt' => 'foo' ], [], [], '', [] ], [ 'extension_username_test.html', [ 'mode' => 'profile', 'user_id' => 2, 'username' => 'admin', 'user_colour' => 'abcdef', 'guest_username' => 'lol', ], [], [], 'phpBB/memberlist.php?mode=viewprofile&u=2', [] ], [ 'extension_username_test.html', [ 'mode' => 'profile', 'user_id' => 2, 'username' => 'admin', 'user_colour' => 'abcdef', 'guest_username' => 'lol', 'custom_profile_url' => 'http://lol.bar', ], [], [], 'http://lol.bar&u=2', [] ], [ 'extension_username_test.html', [ 'mode' => 'full', 'user_id' => 2, 'username' => 'admin', 'user_colour' => 'abcdef', 'guest_username' => 'lol', ], [], [], 'admin', [] ], [ 'extension_username_test.html', [ 'mode' => 'no_profile', 'user_id' => 2, 'username' => 'admin', 'user_colour' => 'abcdef', 'guest_username' => 'lol', ], [], [], 'admin', [] ], [ 'extension_config_test.html', [ 'config_name' => 'allow_avatar', ], [], [], '1', [] ], [ 'extension_config_test.html', [ 'config_name' => 'does not exist', ], [], [], '', [] ], [ 'extension_config_test.html', [ 'config_name' => 'tpl_allow_php', ], [], [], '', [] ], ]; } /** * @dataProvider data_template_extensions */ public function test_template_extensions($file, $vars, $block_vars, $destroy_array, $expected, $lang_vars = []) { $this->run_template($file, $vars, $block_vars, $destroy_array, $expected, $lang_vars); } public function data_template_icon_extension() { return [ /** Font: default */ [ [ 'type' => 'font', 'icon' => 'phone', 'title' => 'ICON_PHONE', 'hidden' => false, 'classes' => '', 'attributes' => [], ], [ 'ICON_PHONE' => 'Phone icon', ], 'Phone icon', ], /** Font: all options */ [ [ 'type' => 'font', 'icon' => 'pencil', 'title' => 'ICON_PENCIL', 'hidden' => true, 'classes' => 'a-class another-class', 'attributes' => [ 'data-attr-1' => 'true', 'data-attr-2' => 'two', ], ], [ 'ICON_PENCIL' => 'Pencil icon', ], ' Pencil icon' ], /** Font: icons array */ [ [ 'type' => 'font', 'icon' => [ 'bullhorn' => false, 'star' => false, 'lock' => true, 'fire' => false, 'file' => true, ], 'title' => 'ICON_TOPIC', 'hidden' => false, 'classes' => '', 'attributes' => [], ], [ 'ICON_TOPIC' => 'Topic icon', ], ' Topic icon', ], /** Font: icons array with no key for the default */ [ [ 'type' => 'font', 'icon' => [ 'bullhorn' => false, 'star' => false, 'lock' => false, 'fire' => false, 'file', ], 'title' => 'ICON_TOPIC', 'hidden' => false, 'classes' => '', 'attributes' => [], ], [ 'ICON_TOPIC' => 'Topic icon', ], ' Topic icon', ], /** Iconify: default */ [ [ 'type' => 'iconify', 'icon' => 'fa:phone', 'title' => '', 'hidden' => false, 'classes' => '', 'attributes' => [], ], [], '', ], /** Iconify: all options */ [ [ 'type' => 'iconify', 'icon' => 'mdi:pencil', 'title' => 'ICON_PENCIL', 'hidden' => true, 'classes' => 'icon-lg', 'attributes' => [ 'style' => 'color: #12a3eb;', ], ], [ 'ICON_PENCIL' => 'Pencil icon', ], ' Pencil icon', ], /** PNG: default */ [ [ 'type' => 'png', 'icon' => 'phone', 'title' => 'ICON_PHONE', 'hidden' => false, 'classes' => '', 'attributes' => [], ], [ 'ICON_PHONE' => 'Phone icon', ], 'Phone icon', ], /** PNG: all options */ [ [ 'type' => 'png', 'icon' => 'pencil', 'title' => 'ICON_PENCIL', 'hidden' => true, 'classes' => 'my-class', 'attributes' => [ 'data-url' => 'my-test-url/test-page.php?u=2', ], ], [ 'ICON_PENCIL' => 'Pencil icon', ], 'Pencil icon', ], /** PNG: Not found */ [ [ 'type' => 'png', 'icon' => 'user', 'title' => 'ICON_USER', 'hidden' => false, 'classes' => 'my-class', 'attributes' => [], ], [ 'ICON_USER' => 'User icon', ], ' User icon ', ], /** SVG: default */ [ [ 'type' => 'svg', 'icon' => 'phone', 'title' => 'ICON_PHONE', 'hidden' => false, 'classes' => '', 'attributes' => [], ], [ 'ICON_PHONE' => 'Phone icon', ], ' Phone icon ', ], /** SVG: all options */ [ [ 'type' => 'svg', 'icon' => 'pencil', 'title' => 'ICON_PENCIL', 'hidden' => true, 'classes' => 'my-svg-class', 'attributes' => [ 'data-ajax' => 'my_ajax_callback', ], ], [ 'ICON_PENCIL' => 'Pencil icon', ], '', ], /** SVG: Not found */ [ [ 'type' => 'svg', 'icon' => 'not-existent', 'title' => 'Just a title', 'hidden' => false, 'classes' => '', 'attributes' => [], ], [], ' Just a title ', ], /** SVG: Sanitization */ [ [ 'type' => 'svg', 'icon' => 'dirty', 'title' => '', 'hidden' => false, 'classes' => '', 'attributes' => [], ], [], ' ', ], ]; } /** * @dataProvider data_template_icon_extension */ public function test_template_icon_extension($vars, $lang_vars, $expected) { $file = 'extension_icon_test.html'; $this->template->set_filenames(['test' => $file]); $this->template->assign_vars($vars); foreach ($lang_vars as $name => $value) { self::$language_reflection_lang->setValue($this->lang, array_merge( self::$language_reflection_lang->getValue($this->lang), [$name => $value] )); } $expected = str_replace(["\n", "\r", "\t"], '', $expected); $output = str_replace(["\n", "\r", "\t"], '', $this->display('test')); /** * SVGs need their random identifier replaced (normalized). * The 'user' is a PNG, but not existent, so it returns a 404 SVG. */ if ($vars['type'] === 'svg' || $vars['icon'] === 'user') { $prefix = strtolower(str_replace(' ', '_', $vars['title'])) . '-'; $output = preg_replace('/' . $prefix . '\d+/', $prefix . '123456789', $output); } $this->assertEquals($expected, $output, "Testing {$file}"); } }