From 91b89b63742e3b6517a70db78b49b965d1ce4deb Mon Sep 17 00:00:00 2001 From: mrgoldy Date: Sun, 9 Dec 2018 17:36:19 +0100 Subject: [PATCH 1/8] [ticket/15905] Create multiple twig extensions PHPBB3-15905 --- .../default/container/services_twig.yml | 24 +++++ phpBB/phpbb/template/twig/extension/auth.php | 91 +++++++++++++++++++ .../phpbb/template/twig/extension/avatar.php | 80 ++++++++++++++++ .../phpbb/template/twig/extension/config.php | 64 +++++++++++++ .../template/twig/extension/username.php | 85 +++++++++++++++++ 5 files changed, 344 insertions(+) create mode 100644 phpBB/phpbb/template/twig/extension/auth.php create mode 100644 phpBB/phpbb/template/twig/extension/avatar.php create mode 100644 phpBB/phpbb/template/twig/extension/config.php create mode 100644 phpBB/phpbb/template/twig/extension/username.php diff --git a/phpBB/config/default/container/services_twig.yml b/phpBB/config/default/container/services_twig.yml index ce76a6a5ea..f3ad95207d 100644 --- a/phpBB/config/default/container/services_twig.yml +++ b/phpBB/config/default/container/services_twig.yml @@ -43,6 +43,25 @@ services: tags: - { name: twig.extension } + template.twig.extensions.auth: + class: phpbb\template\twig\extension\auth + arguments: + - '@auth' + tags: + - { name: twig.extension } + + template.twig.extensions.avatar: + class: phpbb\template\twig\extension\avatar + tags: + - { name: twig.extension } + + template.twig.extensions.config: + class: phpbb\template\twig\extension\config + arguments: + - '@config' + tags: + - { name: twig.extension } + template.twig.extensions.routing: class: phpbb\template\twig\extension\routing arguments: @@ -50,6 +69,11 @@ services: tags: - { name: twig.extension } + template.twig.extensions.username: + class: phpbb\template\twig\extension\username + tags: + - { name: twig.extension } + template.twig.extensions.debug: class: Twig_Extension_Debug diff --git a/phpBB/phpbb/template/twig/extension/auth.php b/phpBB/phpbb/template/twig/extension/auth.php new file mode 100644 index 0000000000..83a626e523 --- /dev/null +++ b/phpBB/phpbb/template/twig/extension/auth.php @@ -0,0 +1,91 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\template\twig\extension; + +class auth extends \Twig_Extension +{ + /** @var \phpbb\auth\auth */ + protected $auth; + + /** + * Constructor. + * + * @param \phpbb\auth\auth $auth Authentication object + */ + public function __construct(\phpbb\auth\auth $auth) + { + $this->auth = $auth; + } + + /** + * Get the name of this extension + * + * @return string + */ + public function getName() + { + return 'auth'; + } + + /** + * Returns a list of global functions to add to the existing list. + * + * @return array An array of global functions + */ + public function getFunctions() + { + return array( + new \Twig_SimpleFunction('auth', array($this, 'get_auth')), + new \Twig_SimpleFunction('auth_global', array($this, 'get_auth_global')), + ); + } + + /** + * Look up permission option(s). + * + * How to use in a template: + * - {{ auth(options, forum_id) }} + * + * The options are required, either as a single string 'a_' or as a twig array ['a_', 'm_']. + * The forum identifier is optional. + * + * @return bool + */ + public function get_auth() + { + $args = func_get_args(); + + $options = $args[0]; + $forum_id = isset($args[1]) ? (int) $args[1] : 0; + + return is_array($options) ? $this->auth->acl_gets($options, $forum_id) : $this->auth->acl_get($options, $forum_id); + } + + /** + * Look up permission option(s) for any forum + * + * How to use in a template: + * - {{ auth_global(options) }} + * + * The options are required, either as a single string 'a_' or as a twig array ['a_', 'm_']. + * + * @return bool + */ + public function get_auth_global() + { + $args = func_get_args(); + + return $this->auth->acl_getf_global($args); + } +} diff --git a/phpBB/phpbb/template/twig/extension/avatar.php b/phpBB/phpbb/template/twig/extension/avatar.php new file mode 100644 index 0000000000..e80357cb24 --- /dev/null +++ b/phpBB/phpbb/template/twig/extension/avatar.php @@ -0,0 +1,80 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\template\twig\extension; + +class avatar extends \Twig_Extension +{ + /** + * Get the name of this extension + * + * @return string + */ + public function getName() + { + return 'avatar'; + } + + /** + * Returns a list of global functions to add to the existing list. + * + * @return array An array of global functions + */ + public function getFunctions() + { + return array( + new \Twig_SimpleFunction('avatar', array($this, 'get_avatar')), + ); + } + + /** + * Get avatar for placing into templates. + * + * How to use in a template: + * - {{ avatar('mode', row, alt, ignore_config, lazy) }} + * + * The mode and row (group_row or user_row) are required. + * The other fields (alt|ignore_config|lazy) are optional. + * + * @uses \phpbb_get_group_avatar() + * @uses \phpbb_get_user_avatar() + * + * @return string The avatar HTML for the specified mode + */ + public function get_avatar() + { + $args = func_get_args(); + + $mode = (string) $args[0]; + $row = (array) $args[1]; + $alt = isset($args[2]) ? (string) $args[2] : false; + $ignore_config = isset($args[3]) ? (bool) $args[3] : false; + $lazy = isset($args[4]) ? (bool) $args[4] : false; + + // To prevent having to redefine alt attribute ('USER_AVATAR'|'GROUP_AVATAR'), we check if an alternative has been provided + switch ($mode) + { + case 'group': + return $alt ? phpbb_get_group_avatar($row, $alt, $ignore_config, $lazy) : phpbb_get_group_avatar($row); + break; + + case 'user': + return $alt ? phpbb_get_user_avatar($row, $alt, $ignore_config, $lazy) : phpbb_get_user_avatar($row); + break; + + default: + return ''; + break; + } + } +} diff --git a/phpBB/phpbb/template/twig/extension/config.php b/phpBB/phpbb/template/twig/extension/config.php new file mode 100644 index 0000000000..91b28dfccc --- /dev/null +++ b/phpBB/phpbb/template/twig/extension/config.php @@ -0,0 +1,64 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\template\twig\extension; + +class config extends \Twig_Extension +{ + /** @var \phpbb\config\config */ + protected $config; + + /** + * Constructor. + * + * @param \phpbb\config\config $config Configuration object + */ + public function __construct(\phpbb\config\config $config) + { + $this->config = $config; + } + + /** + * Get the name of this extension + * + * @return string + */ + public function getName() + { + return 'config'; + } + + /** + * Returns a list of global functions to add to the existing list. + * + * @return array An array of global functions + */ + public function getFunctions() + { + return array( + new \Twig_SimpleFunction('config', array($this, 'get_config')), + ); + } + + /** + * Retrieves a configuration value for use in templates. + * + * @return string The configuration value + */ + public function get_config() + { + $args = func_get_args(); + + return $this->config->offsetGet($args[0]); + } +} diff --git a/phpBB/phpbb/template/twig/extension/username.php b/phpBB/phpbb/template/twig/extension/username.php new file mode 100644 index 0000000000..aaeaf98df2 --- /dev/null +++ b/phpBB/phpbb/template/twig/extension/username.php @@ -0,0 +1,85 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\template\twig\extension; + +class username extends \Twig_Extension +{ + /** + * Get the name of this extension + * + * @return string + */ + public function getName() + { + return 'username'; + } + + /** + * Returns a list of global functions to add to the existing list. + * + * @return array An array of global functions + */ + public function getFunctions() + { + return array( + new \Twig_SimpleFunction('username', array($this, 'get_username')), + ); + } + + /** + * Get username details for placing into templates. + * + * How to use in a template: + * - {{ username('mode', user_id, username, user_colour, guest_username, custom_profile_url) }} + * - {{ username('mode', user_row, guest_username, custom_profile_url) }} + * It's possible to provide the user identifier, name and colour separately, + * or provide the entire user row at once as an array. + * + * The mode, user_id and username are required (separately or through a user row). + * The other fields (user_colour|guest_username|custom_profile_url) are optional. + * + * @uses \get_username_string() + * + * @return string A string based on what is wanted depending on $mode + */ + public function get_username() + { + $args = func_get_args(); + + $mode = $args[0]; + $user = $args[1]; + + // If the entire user row is provided + if (is_array($user)) + { + $user_id = isset($user['user_id']) ? $user['user_id'] : ''; + $username = isset($user['username']) ? $user['username'] : ''; + $user_colour = isset($user['user_colour']) ? $user['user_colour'] : ''; + $guest_username = isset($args[2]) ? $args[2] : false; + $custom_profile_url = isset($args[3]) ? $args[3] : false; + } + else + { + // Options are provided separately + $user_id = $user; + $username = $args[2]; + $user_colour = isset($args[3]) ? $args[3] : ''; + $guest_username = isset($args[4]) ? $args[4] : false; + $custom_profile_url = isset($args[5]) ? $args[5] : false; + } + + + return get_username_string($mode, $user_id, $username, $user_colour, $guest_username, $custom_profile_url); + } +} From 012fe1887963adecc30fa46b7c7069a29600666d Mon Sep 17 00:00:00 2001 From: mrgoldy Date: Sun, 9 Dec 2018 18:35:16 +0100 Subject: [PATCH 2/8] [ticket/15905] Correct line endings PHPBB3-15905 --- phpBB/phpbb/template/twig/extension/auth.php | 182 +++++++++--------- .../phpbb/template/twig/extension/avatar.php | 160 +++++++-------- .../phpbb/template/twig/extension/config.php | 128 ++++++------ .../template/twig/extension/username.php | 170 ++++++++-------- 4 files changed, 320 insertions(+), 320 deletions(-) diff --git a/phpBB/phpbb/template/twig/extension/auth.php b/phpBB/phpbb/template/twig/extension/auth.php index 83a626e523..9dbe306782 100644 --- a/phpBB/phpbb/template/twig/extension/auth.php +++ b/phpBB/phpbb/template/twig/extension/auth.php @@ -1,91 +1,91 @@ - - * @license GNU General Public License, version 2 (GPL-2.0) - * - * For full copyright and license information, please see - * the docs/CREDITS.txt file. - * - */ - -namespace phpbb\template\twig\extension; - -class auth extends \Twig_Extension -{ - /** @var \phpbb\auth\auth */ - protected $auth; - - /** - * Constructor. - * - * @param \phpbb\auth\auth $auth Authentication object - */ - public function __construct(\phpbb\auth\auth $auth) - { - $this->auth = $auth; - } - - /** - * Get the name of this extension - * - * @return string - */ - public function getName() - { - return 'auth'; - } - - /** - * Returns a list of global functions to add to the existing list. - * - * @return array An array of global functions - */ - public function getFunctions() - { - return array( - new \Twig_SimpleFunction('auth', array($this, 'get_auth')), - new \Twig_SimpleFunction('auth_global', array($this, 'get_auth_global')), - ); - } - - /** - * Look up permission option(s). - * - * How to use in a template: - * - {{ auth(options, forum_id) }} - * - * The options are required, either as a single string 'a_' or as a twig array ['a_', 'm_']. - * The forum identifier is optional. - * - * @return bool - */ - public function get_auth() - { - $args = func_get_args(); - - $options = $args[0]; - $forum_id = isset($args[1]) ? (int) $args[1] : 0; - - return is_array($options) ? $this->auth->acl_gets($options, $forum_id) : $this->auth->acl_get($options, $forum_id); - } - - /** - * Look up permission option(s) for any forum - * - * How to use in a template: - * - {{ auth_global(options) }} - * - * The options are required, either as a single string 'a_' or as a twig array ['a_', 'm_']. - * - * @return bool - */ - public function get_auth_global() - { - $args = func_get_args(); - - return $this->auth->acl_getf_global($args); - } -} + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\template\twig\extension; + +class auth extends \Twig_Extension +{ + /** @var \phpbb\auth\auth */ + protected $auth; + + /** + * Constructor. + * + * @param \phpbb\auth\auth $auth Authentication object + */ + public function __construct(\phpbb\auth\auth $auth) + { + $this->auth = $auth; + } + + /** + * Get the name of this extension + * + * @return string + */ + public function getName() + { + return 'auth'; + } + + /** + * Returns a list of global functions to add to the existing list. + * + * @return array An array of global functions + */ + public function getFunctions() + { + return array( + new \Twig_SimpleFunction('auth', array($this, 'get_auth')), + new \Twig_SimpleFunction('auth_global', array($this, 'get_auth_global')), + ); + } + + /** + * Look up permission option(s). + * + * How to use in a template: + * - {{ auth(options, forum_id) }} + * + * The options are required, either as a single string 'a_' or as a twig array ['a_', 'm_']. + * The forum identifier is optional. + * + * @return bool + */ + public function get_auth() + { + $args = func_get_args(); + + $options = $args[0]; + $forum_id = isset($args[1]) ? (int) $args[1] : 0; + + return is_array($options) ? $this->auth->acl_gets($options, $forum_id) : $this->auth->acl_get($options, $forum_id); + } + + /** + * Look up permission option(s) for any forum + * + * How to use in a template: + * - {{ auth_global(options) }} + * + * The options are required, either as a single string 'a_' or as a twig array ['a_', 'm_']. + * + * @return bool + */ + public function get_auth_global() + { + $args = func_get_args(); + + return $this->auth->acl_getf_global($args); + } +} diff --git a/phpBB/phpbb/template/twig/extension/avatar.php b/phpBB/phpbb/template/twig/extension/avatar.php index e80357cb24..7a17fd4b42 100644 --- a/phpBB/phpbb/template/twig/extension/avatar.php +++ b/phpBB/phpbb/template/twig/extension/avatar.php @@ -1,80 +1,80 @@ - - * @license GNU General Public License, version 2 (GPL-2.0) - * - * For full copyright and license information, please see - * the docs/CREDITS.txt file. - * - */ - -namespace phpbb\template\twig\extension; - -class avatar extends \Twig_Extension -{ - /** - * Get the name of this extension - * - * @return string - */ - public function getName() - { - return 'avatar'; - } - - /** - * Returns a list of global functions to add to the existing list. - * - * @return array An array of global functions - */ - public function getFunctions() - { - return array( - new \Twig_SimpleFunction('avatar', array($this, 'get_avatar')), - ); - } - - /** - * Get avatar for placing into templates. - * - * How to use in a template: - * - {{ avatar('mode', row, alt, ignore_config, lazy) }} - * - * The mode and row (group_row or user_row) are required. - * The other fields (alt|ignore_config|lazy) are optional. - * - * @uses \phpbb_get_group_avatar() - * @uses \phpbb_get_user_avatar() - * - * @return string The avatar HTML for the specified mode - */ - public function get_avatar() - { - $args = func_get_args(); - - $mode = (string) $args[0]; - $row = (array) $args[1]; - $alt = isset($args[2]) ? (string) $args[2] : false; - $ignore_config = isset($args[3]) ? (bool) $args[3] : false; - $lazy = isset($args[4]) ? (bool) $args[4] : false; - - // To prevent having to redefine alt attribute ('USER_AVATAR'|'GROUP_AVATAR'), we check if an alternative has been provided - switch ($mode) - { - case 'group': - return $alt ? phpbb_get_group_avatar($row, $alt, $ignore_config, $lazy) : phpbb_get_group_avatar($row); - break; - - case 'user': - return $alt ? phpbb_get_user_avatar($row, $alt, $ignore_config, $lazy) : phpbb_get_user_avatar($row); - break; - - default: - return ''; - break; - } - } -} + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\template\twig\extension; + +class avatar extends \Twig_Extension +{ + /** + * Get the name of this extension + * + * @return string + */ + public function getName() + { + return 'avatar'; + } + + /** + * Returns a list of global functions to add to the existing list. + * + * @return array An array of global functions + */ + public function getFunctions() + { + return array( + new \Twig_SimpleFunction('avatar', array($this, 'get_avatar')), + ); + } + + /** + * Get avatar for placing into templates. + * + * How to use in a template: + * - {{ avatar('mode', row, alt, ignore_config, lazy) }} + * + * The mode and row (group_row or user_row) are required. + * The other fields (alt|ignore_config|lazy) are optional. + * + * @uses \phpbb_get_group_avatar() + * @uses \phpbb_get_user_avatar() + * + * @return string The avatar HTML for the specified mode + */ + public function get_avatar() + { + $args = func_get_args(); + + $mode = (string) $args[0]; + $row = (array) $args[1]; + $alt = isset($args[2]) ? (string) $args[2] : false; + $ignore_config = isset($args[3]) ? (bool) $args[3] : false; + $lazy = isset($args[4]) ? (bool) $args[4] : false; + + // To prevent having to redefine alt attribute ('USER_AVATAR'|'GROUP_AVATAR'), we check if an alternative has been provided + switch ($mode) + { + case 'group': + return $alt ? phpbb_get_group_avatar($row, $alt, $ignore_config, $lazy) : phpbb_get_group_avatar($row); + break; + + case 'user': + return $alt ? phpbb_get_user_avatar($row, $alt, $ignore_config, $lazy) : phpbb_get_user_avatar($row); + break; + + default: + return ''; + break; + } + } +} diff --git a/phpBB/phpbb/template/twig/extension/config.php b/phpBB/phpbb/template/twig/extension/config.php index 91b28dfccc..cbf6e505c5 100644 --- a/phpBB/phpbb/template/twig/extension/config.php +++ b/phpBB/phpbb/template/twig/extension/config.php @@ -1,64 +1,64 @@ - - * @license GNU General Public License, version 2 (GPL-2.0) - * - * For full copyright and license information, please see - * the docs/CREDITS.txt file. - * - */ - -namespace phpbb\template\twig\extension; - -class config extends \Twig_Extension -{ - /** @var \phpbb\config\config */ - protected $config; - - /** - * Constructor. - * - * @param \phpbb\config\config $config Configuration object - */ - public function __construct(\phpbb\config\config $config) - { - $this->config = $config; - } - - /** - * Get the name of this extension - * - * @return string - */ - public function getName() - { - return 'config'; - } - - /** - * Returns a list of global functions to add to the existing list. - * - * @return array An array of global functions - */ - public function getFunctions() - { - return array( - new \Twig_SimpleFunction('config', array($this, 'get_config')), - ); - } - - /** - * Retrieves a configuration value for use in templates. - * - * @return string The configuration value - */ - public function get_config() - { - $args = func_get_args(); - - return $this->config->offsetGet($args[0]); - } -} + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\template\twig\extension; + +class config extends \Twig_Extension +{ + /** @var \phpbb\config\config */ + protected $config; + + /** + * Constructor. + * + * @param \phpbb\config\config $config Configuration object + */ + public function __construct(\phpbb\config\config $config) + { + $this->config = $config; + } + + /** + * Get the name of this extension + * + * @return string + */ + public function getName() + { + return 'config'; + } + + /** + * Returns a list of global functions to add to the existing list. + * + * @return array An array of global functions + */ + public function getFunctions() + { + return array( + new \Twig_SimpleFunction('config', array($this, 'get_config')), + ); + } + + /** + * Retrieves a configuration value for use in templates. + * + * @return string The configuration value + */ + public function get_config() + { + $args = func_get_args(); + + return $this->config->offsetGet($args[0]); + } +} diff --git a/phpBB/phpbb/template/twig/extension/username.php b/phpBB/phpbb/template/twig/extension/username.php index aaeaf98df2..c80396b116 100644 --- a/phpBB/phpbb/template/twig/extension/username.php +++ b/phpBB/phpbb/template/twig/extension/username.php @@ -1,85 +1,85 @@ - - * @license GNU General Public License, version 2 (GPL-2.0) - * - * For full copyright and license information, please see - * the docs/CREDITS.txt file. - * - */ - -namespace phpbb\template\twig\extension; - -class username extends \Twig_Extension -{ - /** - * Get the name of this extension - * - * @return string - */ - public function getName() - { - return 'username'; - } - - /** - * Returns a list of global functions to add to the existing list. - * - * @return array An array of global functions - */ - public function getFunctions() - { - return array( - new \Twig_SimpleFunction('username', array($this, 'get_username')), - ); - } - - /** - * Get username details for placing into templates. - * - * How to use in a template: - * - {{ username('mode', user_id, username, user_colour, guest_username, custom_profile_url) }} - * - {{ username('mode', user_row, guest_username, custom_profile_url) }} - * It's possible to provide the user identifier, name and colour separately, - * or provide the entire user row at once as an array. - * - * The mode, user_id and username are required (separately or through a user row). - * The other fields (user_colour|guest_username|custom_profile_url) are optional. - * - * @uses \get_username_string() - * - * @return string A string based on what is wanted depending on $mode - */ - public function get_username() - { - $args = func_get_args(); - - $mode = $args[0]; - $user = $args[1]; - - // If the entire user row is provided - if (is_array($user)) - { - $user_id = isset($user['user_id']) ? $user['user_id'] : ''; - $username = isset($user['username']) ? $user['username'] : ''; - $user_colour = isset($user['user_colour']) ? $user['user_colour'] : ''; - $guest_username = isset($args[2]) ? $args[2] : false; - $custom_profile_url = isset($args[3]) ? $args[3] : false; - } - else - { - // Options are provided separately - $user_id = $user; - $username = $args[2]; - $user_colour = isset($args[3]) ? $args[3] : ''; - $guest_username = isset($args[4]) ? $args[4] : false; - $custom_profile_url = isset($args[5]) ? $args[5] : false; - } - - - return get_username_string($mode, $user_id, $username, $user_colour, $guest_username, $custom_profile_url); - } -} + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\template\twig\extension; + +class username extends \Twig_Extension +{ + /** + * Get the name of this extension + * + * @return string + */ + public function getName() + { + return 'username'; + } + + /** + * Returns a list of global functions to add to the existing list. + * + * @return array An array of global functions + */ + public function getFunctions() + { + return array( + new \Twig_SimpleFunction('username', array($this, 'get_username')), + ); + } + + /** + * Get username details for placing into templates. + * + * How to use in a template: + * - {{ username('mode', user_id, username, user_colour, guest_username, custom_profile_url) }} + * - {{ username('mode', user_row, guest_username, custom_profile_url) }} + * It's possible to provide the user identifier, name and colour separately, + * or provide the entire user row at once as an array. + * + * The mode, user_id and username are required (separately or through a user row). + * The other fields (user_colour|guest_username|custom_profile_url) are optional. + * + * @uses \get_username_string() + * + * @return string A string based on what is wanted depending on $mode + */ + public function get_username() + { + $args = func_get_args(); + + $mode = $args[0]; + $user = $args[1]; + + // If the entire user row is provided + if (is_array($user)) + { + $user_id = isset($user['user_id']) ? $user['user_id'] : ''; + $username = isset($user['username']) ? $user['username'] : ''; + $user_colour = isset($user['user_colour']) ? $user['user_colour'] : ''; + $guest_username = isset($args[2]) ? $args[2] : false; + $custom_profile_url = isset($args[3]) ? $args[3] : false; + } + else + { + // Options are provided separately + $user_id = $user; + $username = $args[2]; + $user_colour = isset($args[3]) ? $args[3] : ''; + $guest_username = isset($args[4]) ? $args[4] : false; + $custom_profile_url = isset($args[5]) ? $args[5] : false; + } + + + return get_username_string($mode, $user_id, $username, $user_colour, $guest_username, $custom_profile_url); + } +} From 7989f3f71fd665aa743d947c7487d41c6f0a33d4 Mon Sep 17 00:00:00 2001 From: mrgoldy Date: Sun, 9 Dec 2018 23:52:50 +0100 Subject: [PATCH 3/8] [ticket/15905] Try with existing phpbb extension PHPBB3-15905 --- .../default/container/services_twig.yml | 8 +- phpBB/phpbb/template/twig/extension.php | 48 +++++++++- phpBB/phpbb/template/twig/extension/auth.php | 91 ------------------- .../template/twig/extension/username.php | 1 - tests/controller/common_helper_route.php | 6 +- tests/email/email_parsing_test.php | 8 ++ tests/extension/metadata_manager_test.php | 4 +- tests/template/template_allfolder_test.php | 4 +- tests/template/template_events_test.php | 4 +- tests/template/template_includecss_test.php | 4 +- tests/template/template_test_case.php | 4 +- .../template/template_test_case_with_tree.php | 4 +- 12 files changed, 77 insertions(+), 109 deletions(-) delete mode 100644 phpBB/phpbb/template/twig/extension/auth.php diff --git a/phpBB/config/default/container/services_twig.yml b/phpBB/config/default/container/services_twig.yml index f3ad95207d..367886804c 100644 --- a/phpBB/config/default/container/services_twig.yml +++ b/phpBB/config/default/container/services_twig.yml @@ -37,19 +37,13 @@ services: template.twig.extensions.phpbb: class: phpbb\template\twig\extension arguments: + - '@auth' - '@template_context' - '@template.twig.environment' - '@language' tags: - { name: twig.extension } - template.twig.extensions.auth: - class: phpbb\template\twig\extension\auth - arguments: - - '@auth' - tags: - - { name: twig.extension } - template.twig.extensions.avatar: class: phpbb\template\twig\extension\avatar tags: diff --git a/phpBB/phpbb/template/twig/extension.php b/phpBB/phpbb/template/twig/extension.php index c5b3db1aaf..5bb0c67291 100644 --- a/phpBB/phpbb/template/twig/extension.php +++ b/phpBB/phpbb/template/twig/extension.php @@ -15,6 +15,9 @@ namespace phpbb\template\twig; class extension extends \Twig_Extension { + /** @var \phpbb\auth\auth */ + protected $auth; + /** @var \phpbb\template\context */ protected $context; @@ -27,13 +30,14 @@ class extension extends \Twig_Extension /** * Constructor * + * @param \phpbb\auth\auth $auth * @param \phpbb\template\context $context * @param \phpbb\template\twig\environment $environment * @param \phpbb\language\language $language - * @return \phpbb\template\twig\extension */ - public function __construct(\phpbb\template\context $context, \phpbb\template\twig\environment $environment, $language) + public function __construct(\phpbb\auth\auth $auth, \phpbb\template\context $context, \phpbb\template\twig\environment $environment, $language) { + $this->auth = $auth; $this->context = $context; $this->environment = $environment; $this->language = $language; @@ -91,6 +95,8 @@ class extension extends \Twig_Extension return array( new \Twig_SimpleFunction('lang', array($this, 'lang')), new \Twig_SimpleFunction('lang_defined', array($this, 'lang_defined')), + new \Twig_SimpleFunction('auth', array($this, 'get_auth')), + new \Twig_SimpleFunction('auth_global', array($this, 'get_auth_global')), ); } @@ -198,4 +204,42 @@ class extension extends \Twig_Extension { return call_user_func_array([$this->language, 'is_set'], [$key]); } + + /** + * Look up permission option(s). + * + * How to use in a template: + * - {{ auth(options, forum_id) }} + * + * The options are required, either as a single string 'a_' or as a twig array ['a_', 'm_']. + * The forum identifier is optional. + * + * @return bool + */ + public function get_auth() + { + $args = func_get_args(); + + $options = $args[0]; + $forum_id = isset($args[1]) ? (int) $args[1] : 0; + + return is_array($options) ? $this->auth->acl_gets($options, $forum_id) : $this->auth->acl_get($options, $forum_id); + } + + /** + * Look up permission option(s) for any forum + * + * How to use in a template: + * - {{ auth_global(options) }} + * + * The options are required, either as a single string 'a_' or as a twig array ['a_', 'm_']. + * + * @return bool + */ + public function get_auth_global() + { + $args = func_get_args(); + + return $this->auth->acl_getf_global($args); + } } diff --git a/phpBB/phpbb/template/twig/extension/auth.php b/phpBB/phpbb/template/twig/extension/auth.php deleted file mode 100644 index 9dbe306782..0000000000 --- a/phpBB/phpbb/template/twig/extension/auth.php +++ /dev/null @@ -1,91 +0,0 @@ - - * @license GNU General Public License, version 2 (GPL-2.0) - * - * For full copyright and license information, please see - * the docs/CREDITS.txt file. - * - */ - -namespace phpbb\template\twig\extension; - -class auth extends \Twig_Extension -{ - /** @var \phpbb\auth\auth */ - protected $auth; - - /** - * Constructor. - * - * @param \phpbb\auth\auth $auth Authentication object - */ - public function __construct(\phpbb\auth\auth $auth) - { - $this->auth = $auth; - } - - /** - * Get the name of this extension - * - * @return string - */ - public function getName() - { - return 'auth'; - } - - /** - * Returns a list of global functions to add to the existing list. - * - * @return array An array of global functions - */ - public function getFunctions() - { - return array( - new \Twig_SimpleFunction('auth', array($this, 'get_auth')), - new \Twig_SimpleFunction('auth_global', array($this, 'get_auth_global')), - ); - } - - /** - * Look up permission option(s). - * - * How to use in a template: - * - {{ auth(options, forum_id) }} - * - * The options are required, either as a single string 'a_' or as a twig array ['a_', 'm_']. - * The forum identifier is optional. - * - * @return bool - */ - public function get_auth() - { - $args = func_get_args(); - - $options = $args[0]; - $forum_id = isset($args[1]) ? (int) $args[1] : 0; - - return is_array($options) ? $this->auth->acl_gets($options, $forum_id) : $this->auth->acl_get($options, $forum_id); - } - - /** - * Look up permission option(s) for any forum - * - * How to use in a template: - * - {{ auth_global(options) }} - * - * The options are required, either as a single string 'a_' or as a twig array ['a_', 'm_']. - * - * @return bool - */ - public function get_auth_global() - { - $args = func_get_args(); - - return $this->auth->acl_getf_global($args); - } -} diff --git a/phpBB/phpbb/template/twig/extension/username.php b/phpBB/phpbb/template/twig/extension/username.php index c80396b116..ef149693a0 100644 --- a/phpBB/phpbb/template/twig/extension/username.php +++ b/phpBB/phpbb/template/twig/extension/username.php @@ -79,7 +79,6 @@ class username extends \Twig_Extension $custom_profile_url = isset($args[5]) ? $args[5] : false; } - return get_username_string($mode, $user_id, $username, $user_colour, $guest_username, $custom_profile_url); } } diff --git a/tests/controller/common_helper_route.php b/tests/controller/common_helper_route.php index 447c10d10e..3d3578ab43 100644 --- a/tests/controller/common_helper_route.php +++ b/tests/controller/common_helper_route.php @@ -74,7 +74,9 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case protected function generate_route_objects() { - global $request; + global $request, $phpbb_root_path, $phpEx; + + $auth = $this->getMock('\phpbb\auth\auth'); $this->request = new phpbb_mock_request(); $this->request->overwrite('SCRIPT_NAME', $this->get_uri(), \phpbb\request\request_interface::SERVER); @@ -122,7 +124,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case 'autoescape' => false, ) ); - $this->template = new phpbb\template\twig\twig($this->phpbb_path_helper, $this->config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user))); + $this->template = new phpbb\template\twig\twig($this->phpbb_path_helper, $this->config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($auth, $context, $twig, $this->user))); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); $this->extension_manager = new phpbb_mock_extension_manager( diff --git a/tests/email/email_parsing_test.php b/tests/email/email_parsing_test.php index 629df9abb6..1b5c6aaa8c 100644 --- a/tests/email/email_parsing_test.php +++ b/tests/email/email_parsing_test.php @@ -66,7 +66,15 @@ class phpbb_email_parsing_test extends phpbb_test_case ); $phpbb_container->set('ext.manager', $extension_manager); + $auth = $this->getMock('\phpbb\auth\auth'); $context = new \phpbb\template\context(); + $twig_extension = new \phpbb\template\twig\extension($auth, $context, $lang); + $phpbb_container->set('template.twig.extensions.phpbb', $twig_extension); + + $twig_extensions_collection = new \phpbb\di\service_collection($phpbb_container); + $twig_extensions_collection->add('template.twig.extensions.phpbb'); + $phpbb_container->set('template.twig.extensions.collection', $twig_extensions_collection); + $twig = new \phpbb\template\twig\environment( $config, $filesystem, diff --git a/tests/extension/metadata_manager_test.php b/tests/extension/metadata_manager_test.php index a2f0542979..f2f5dc669e 100644 --- a/tests/extension/metadata_manager_test.php +++ b/tests/extension/metadata_manager_test.php @@ -36,6 +36,8 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case { parent::setUp(); + $auth = $this->getMock('\phpbb\auth\auth'); + $this->config = new \phpbb\config\config(array( 'version' => '3.1.0', )); @@ -111,7 +113,7 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case $lang = new \phpbb\language\language($lang_loader); $this->user = new \phpbb\user($lang, '\phpbb\datetime'); - $this->template = new phpbb\template\twig\twig($phpbb_path_helper, $this->config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user))); + $this->template = new phpbb\template\twig\twig($phpbb_path_helper, $this->config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($auth, $context, $twig, $this->user))); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); } diff --git a/tests/template/template_allfolder_test.php b/tests/template/template_allfolder_test.php index a9a8ecc287..60891a3668 100644 --- a/tests/template/template_allfolder_test.php +++ b/tests/template/template_allfolder_test.php @@ -26,6 +26,8 @@ class phpbb_template_allfolder_test extends phpbb_template_template_test_case { global $phpbb_root_path, $phpEx; + $auth = $this->getMock('\phpbb\auth\auth'); + $defaults = $this->config_defaults(); $config = new \phpbb\config\config(array_merge($defaults, $new_config)); $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx); @@ -75,7 +77,7 @@ class phpbb_template_allfolder_test extends phpbb_template_template_test_case 'autoescape' => false, ) ); - $this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user)), $this->extension_manager); + $this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($auth, $context, $twig, $this->user)), $this->extension_manager); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); $this->template_path = $this->test_path . '/templates'; diff --git a/tests/template/template_events_test.php b/tests/template/template_events_test.php index 9243390937..e879979803 100644 --- a/tests/template/template_events_test.php +++ b/tests/template/template_events_test.php @@ -131,6 +131,8 @@ Zeta test event in all', { global $phpbb_root_path, $phpEx, $user; + $auth = $this->getMock('\phpbb\auth\auth'); + $defaults = $this->config_defaults(); $config = new \phpbb\config\config(array_merge($defaults, $new_config)); @@ -169,7 +171,7 @@ Zeta test event in all', 'autoescape' => false, ) ); - $this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user)), $this->extension_manager); + $this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($auth, $context, $twig, $this->user)), $this->extension_manager); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); $this->template->set_custom_style(((!empty($style_names)) ? $style_names : 'silver'), array($this->template_path)); diff --git a/tests/template/template_includecss_test.php b/tests/template/template_includecss_test.php index 5f9875a556..0f26c095a7 100644 --- a/tests/template/template_includecss_test.php +++ b/tests/template/template_includecss_test.php @@ -25,6 +25,8 @@ class phpbb_template_template_includecss_test extends phpbb_template_template_te { global $phpbb_root_path, $phpEx, $user; + $auth = $this->getMock('\phpbb\auth\auth'); + $defaults = $this->config_defaults(); $config = new \phpbb\config\config(array_merge($defaults, $new_config)); @@ -68,7 +70,7 @@ class phpbb_template_template_includecss_test extends phpbb_template_template_te $twig, $cache_path, $this->user, - array(new \phpbb\template\twig\extension($context, $twig, $this->user)), + array(new \phpbb\template\twig\extension($auth, $context, $twig, $this->user)), new phpbb_mock_extension_manager( dirname(__FILE__) . '/', array( diff --git a/tests/template/template_test_case.php b/tests/template/template_test_case.php index 0389088ec8..f495c93f9d 100644 --- a/tests/template/template_test_case.php +++ b/tests/template/template_test_case.php @@ -73,6 +73,8 @@ class phpbb_template_template_test_case extends phpbb_test_case { global $phpbb_root_path, $phpEx; + $auth = $this->getMock('\phpbb\auth\auth'); + $defaults = $this->config_defaults(); $config = new \phpbb\config\config(array_merge($defaults, $new_config)); $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx); @@ -113,7 +115,7 @@ class phpbb_template_template_test_case extends phpbb_test_case 'autoescape' => false, ) ); - $this->template = new phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user))); + $this->template = new phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($auth, $context, $twig, $this->user))); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); $this->template->set_custom_style('tests', $this->template_path); } diff --git a/tests/template/template_test_case_with_tree.php b/tests/template/template_test_case_with_tree.php index c0238b6f03..756bedb042 100644 --- a/tests/template/template_test_case_with_tree.php +++ b/tests/template/template_test_case_with_tree.php @@ -19,6 +19,8 @@ class phpbb_template_template_test_case_with_tree extends phpbb_template_templat { global $phpbb_root_path, $phpEx, $user; + $auth = $this->getMock('\phpbb\auth\auth'); + $defaults = $this->config_defaults(); $config = new \phpbb\config\config(array_merge($defaults, $new_config)); @@ -56,7 +58,7 @@ class phpbb_template_template_test_case_with_tree extends phpbb_template_templat 'autoescape' => false, ) ); - $this->template = new phpbb\template\twig\twig($this->phpbb_path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user))); + $this->template = new phpbb\template\twig\twig($this->phpbb_path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($auth, $context, $twig, $this->user))); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); $this->template->set_custom_style('tests', array($this->template_path, $this->parent_template_path)); } From eee00652e7b608967a2ec5ee8fd165c2760be145 Mon Sep 17 00:00:00 2001 From: mrgoldy Date: Sun, 9 Dec 2018 23:58:34 +0100 Subject: [PATCH 4/8] [ticket/15905] Resolve conflicts PHPBB3-15905 --- phpBB/phpbb/template/twig/extension.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/phpBB/phpbb/template/twig/extension.php b/phpBB/phpbb/template/twig/extension.php index 5bb0c67291..384bac773a 100644 --- a/phpBB/phpbb/template/twig/extension.php +++ b/phpBB/phpbb/template/twig/extension.php @@ -95,6 +95,7 @@ class extension extends \Twig_Extension return array( new \Twig_SimpleFunction('lang', array($this, 'lang')), new \Twig_SimpleFunction('lang_defined', array($this, 'lang_defined')), + new \Twig_SimpleFunction('get_class', 'get_class'), new \Twig_SimpleFunction('auth', array($this, 'get_auth')), new \Twig_SimpleFunction('auth_global', array($this, 'get_auth_global')), ); @@ -196,10 +197,10 @@ class extension extends \Twig_Extension } /** - * Check if a language variable exists - * - * @return bool - */ + * Check if a language variable exists + * + * @return bool + */ public function lang_defined($key) { return call_user_func_array([$this->language, 'is_set'], [$key]); From 133dfd0a84ce258fadab5f48de45684869b14800 Mon Sep 17 00:00:00 2001 From: mrgoldy Date: Mon, 10 Dec 2018 00:10:27 +0100 Subject: [PATCH 5/8] [ticket/15905] Try it without auth PHPBB3-15905 --- .../default/container/services_twig.yml | 1 - phpBB/phpbb/template/twig/extension.php | 59 +------------------ tests/controller/common_helper_route.php | 6 +- tests/email/email_parsing_test.php | 3 +- tests/extension/metadata_manager_test.php | 4 +- tests/template/template_allfolder_test.php | 4 +- tests/template/template_events_test.php | 4 +- tests/template/template_includecss_test.php | 4 +- tests/template/template_test_case.php | 4 +- .../template/template_test_case_with_tree.php | 4 +- 10 files changed, 10 insertions(+), 83 deletions(-) diff --git a/phpBB/config/default/container/services_twig.yml b/phpBB/config/default/container/services_twig.yml index 367886804c..6f70155295 100644 --- a/phpBB/config/default/container/services_twig.yml +++ b/phpBB/config/default/container/services_twig.yml @@ -37,7 +37,6 @@ services: template.twig.extensions.phpbb: class: phpbb\template\twig\extension arguments: - - '@auth' - '@template_context' - '@template.twig.environment' - '@language' diff --git a/phpBB/phpbb/template/twig/extension.php b/phpBB/phpbb/template/twig/extension.php index 384bac773a..027abc44ec 100644 --- a/phpBB/phpbb/template/twig/extension.php +++ b/phpBB/phpbb/template/twig/extension.php @@ -15,9 +15,6 @@ namespace phpbb\template\twig; class extension extends \Twig_Extension { - /** @var \phpbb\auth\auth */ - protected $auth; - /** @var \phpbb\template\context */ protected $context; @@ -30,14 +27,12 @@ class extension extends \Twig_Extension /** * Constructor * - * @param \phpbb\auth\auth $auth * @param \phpbb\template\context $context * @param \phpbb\template\twig\environment $environment * @param \phpbb\language\language $language */ - public function __construct(\phpbb\auth\auth $auth, \phpbb\template\context $context, \phpbb\template\twig\environment $environment, $language) + public function __construct(\phpbb\template\context $context, \phpbb\template\twig\environment $environment, $language) { - $this->auth = $auth; $this->context = $context; $this->environment = $environment; $this->language = $language; @@ -94,10 +89,6 @@ class extension extends \Twig_Extension { return array( new \Twig_SimpleFunction('lang', array($this, 'lang')), - new \Twig_SimpleFunction('lang_defined', array($this, 'lang_defined')), - new \Twig_SimpleFunction('get_class', 'get_class'), - new \Twig_SimpleFunction('auth', array($this, 'get_auth')), - new \Twig_SimpleFunction('auth_global', array($this, 'get_auth_global')), ); } @@ -195,52 +186,4 @@ class extension extends \Twig_Extension return call_user_func_array(array($this->language, 'lang'), $args); } - - /** - * Check if a language variable exists - * - * @return bool - */ - public function lang_defined($key) - { - return call_user_func_array([$this->language, 'is_set'], [$key]); - } - - /** - * Look up permission option(s). - * - * How to use in a template: - * - {{ auth(options, forum_id) }} - * - * The options are required, either as a single string 'a_' or as a twig array ['a_', 'm_']. - * The forum identifier is optional. - * - * @return bool - */ - public function get_auth() - { - $args = func_get_args(); - - $options = $args[0]; - $forum_id = isset($args[1]) ? (int) $args[1] : 0; - - return is_array($options) ? $this->auth->acl_gets($options, $forum_id) : $this->auth->acl_get($options, $forum_id); - } - - /** - * Look up permission option(s) for any forum - * - * How to use in a template: - * - {{ auth_global(options) }} - * - * The options are required, either as a single string 'a_' or as a twig array ['a_', 'm_']. - * - * @return bool - */ - public function get_auth_global() - { - $args = func_get_args(); - - return $this->auth->acl_getf_global($args); - } } diff --git a/tests/controller/common_helper_route.php b/tests/controller/common_helper_route.php index 3d3578ab43..447c10d10e 100644 --- a/tests/controller/common_helper_route.php +++ b/tests/controller/common_helper_route.php @@ -74,9 +74,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case protected function generate_route_objects() { - global $request, $phpbb_root_path, $phpEx; - - $auth = $this->getMock('\phpbb\auth\auth'); + global $request; $this->request = new phpbb_mock_request(); $this->request->overwrite('SCRIPT_NAME', $this->get_uri(), \phpbb\request\request_interface::SERVER); @@ -124,7 +122,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case 'autoescape' => false, ) ); - $this->template = new phpbb\template\twig\twig($this->phpbb_path_helper, $this->config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($auth, $context, $twig, $this->user))); + $this->template = new phpbb\template\twig\twig($this->phpbb_path_helper, $this->config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user))); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); $this->extension_manager = new phpbb_mock_extension_manager( diff --git a/tests/email/email_parsing_test.php b/tests/email/email_parsing_test.php index 1b5c6aaa8c..2e083a6056 100644 --- a/tests/email/email_parsing_test.php +++ b/tests/email/email_parsing_test.php @@ -66,9 +66,8 @@ class phpbb_email_parsing_test extends phpbb_test_case ); $phpbb_container->set('ext.manager', $extension_manager); - $auth = $this->getMock('\phpbb\auth\auth'); $context = new \phpbb\template\context(); - $twig_extension = new \phpbb\template\twig\extension($auth, $context, $lang); + $twig_extension = new \phpbb\template\twig\extension($context, $lang); $phpbb_container->set('template.twig.extensions.phpbb', $twig_extension); $twig_extensions_collection = new \phpbb\di\service_collection($phpbb_container); diff --git a/tests/extension/metadata_manager_test.php b/tests/extension/metadata_manager_test.php index f2f5dc669e..a2f0542979 100644 --- a/tests/extension/metadata_manager_test.php +++ b/tests/extension/metadata_manager_test.php @@ -36,8 +36,6 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case { parent::setUp(); - $auth = $this->getMock('\phpbb\auth\auth'); - $this->config = new \phpbb\config\config(array( 'version' => '3.1.0', )); @@ -113,7 +111,7 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case $lang = new \phpbb\language\language($lang_loader); $this->user = new \phpbb\user($lang, '\phpbb\datetime'); - $this->template = new phpbb\template\twig\twig($phpbb_path_helper, $this->config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($auth, $context, $twig, $this->user))); + $this->template = new phpbb\template\twig\twig($phpbb_path_helper, $this->config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user))); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); } diff --git a/tests/template/template_allfolder_test.php b/tests/template/template_allfolder_test.php index 60891a3668..a9a8ecc287 100644 --- a/tests/template/template_allfolder_test.php +++ b/tests/template/template_allfolder_test.php @@ -26,8 +26,6 @@ class phpbb_template_allfolder_test extends phpbb_template_template_test_case { global $phpbb_root_path, $phpEx; - $auth = $this->getMock('\phpbb\auth\auth'); - $defaults = $this->config_defaults(); $config = new \phpbb\config\config(array_merge($defaults, $new_config)); $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx); @@ -77,7 +75,7 @@ class phpbb_template_allfolder_test extends phpbb_template_template_test_case 'autoescape' => false, ) ); - $this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($auth, $context, $twig, $this->user)), $this->extension_manager); + $this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user)), $this->extension_manager); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); $this->template_path = $this->test_path . '/templates'; diff --git a/tests/template/template_events_test.php b/tests/template/template_events_test.php index e879979803..9243390937 100644 --- a/tests/template/template_events_test.php +++ b/tests/template/template_events_test.php @@ -131,8 +131,6 @@ Zeta test event in all', { global $phpbb_root_path, $phpEx, $user; - $auth = $this->getMock('\phpbb\auth\auth'); - $defaults = $this->config_defaults(); $config = new \phpbb\config\config(array_merge($defaults, $new_config)); @@ -171,7 +169,7 @@ Zeta test event in all', 'autoescape' => false, ) ); - $this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($auth, $context, $twig, $this->user)), $this->extension_manager); + $this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user)), $this->extension_manager); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); $this->template->set_custom_style(((!empty($style_names)) ? $style_names : 'silver'), array($this->template_path)); diff --git a/tests/template/template_includecss_test.php b/tests/template/template_includecss_test.php index 0f26c095a7..5f9875a556 100644 --- a/tests/template/template_includecss_test.php +++ b/tests/template/template_includecss_test.php @@ -25,8 +25,6 @@ class phpbb_template_template_includecss_test extends phpbb_template_template_te { global $phpbb_root_path, $phpEx, $user; - $auth = $this->getMock('\phpbb\auth\auth'); - $defaults = $this->config_defaults(); $config = new \phpbb\config\config(array_merge($defaults, $new_config)); @@ -70,7 +68,7 @@ class phpbb_template_template_includecss_test extends phpbb_template_template_te $twig, $cache_path, $this->user, - array(new \phpbb\template\twig\extension($auth, $context, $twig, $this->user)), + array(new \phpbb\template\twig\extension($context, $twig, $this->user)), new phpbb_mock_extension_manager( dirname(__FILE__) . '/', array( diff --git a/tests/template/template_test_case.php b/tests/template/template_test_case.php index f495c93f9d..0389088ec8 100644 --- a/tests/template/template_test_case.php +++ b/tests/template/template_test_case.php @@ -73,8 +73,6 @@ class phpbb_template_template_test_case extends phpbb_test_case { global $phpbb_root_path, $phpEx; - $auth = $this->getMock('\phpbb\auth\auth'); - $defaults = $this->config_defaults(); $config = new \phpbb\config\config(array_merge($defaults, $new_config)); $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx); @@ -115,7 +113,7 @@ class phpbb_template_template_test_case extends phpbb_test_case 'autoescape' => false, ) ); - $this->template = new phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($auth, $context, $twig, $this->user))); + $this->template = new phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user))); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); $this->template->set_custom_style('tests', $this->template_path); } diff --git a/tests/template/template_test_case_with_tree.php b/tests/template/template_test_case_with_tree.php index 756bedb042..c0238b6f03 100644 --- a/tests/template/template_test_case_with_tree.php +++ b/tests/template/template_test_case_with_tree.php @@ -19,8 +19,6 @@ class phpbb_template_template_test_case_with_tree extends phpbb_template_templat { global $phpbb_root_path, $phpEx, $user; - $auth = $this->getMock('\phpbb\auth\auth'); - $defaults = $this->config_defaults(); $config = new \phpbb\config\config(array_merge($defaults, $new_config)); @@ -58,7 +56,7 @@ class phpbb_template_template_test_case_with_tree extends phpbb_template_templat 'autoescape' => false, ) ); - $this->template = new phpbb\template\twig\twig($this->phpbb_path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($auth, $context, $twig, $this->user))); + $this->template = new phpbb\template\twig\twig($this->phpbb_path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user))); $twig->setLexer(new \phpbb\template\twig\lexer($twig)); $this->template->set_custom_style('tests', array($this->template_path, $this->parent_template_path)); } From f21003f977e7f9c2b414885ad5e05333dbdcd0ac Mon Sep 17 00:00:00 2001 From: mrgoldy Date: Mon, 10 Dec 2018 00:19:50 +0100 Subject: [PATCH 6/8] [ticket/15905] Re-add add_class and lang_defined PHPBB3-15905 --- phpBB/phpbb/template/twig/extension.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/phpBB/phpbb/template/twig/extension.php b/phpBB/phpbb/template/twig/extension.php index 027abc44ec..1131a7f3aa 100644 --- a/phpBB/phpbb/template/twig/extension.php +++ b/phpBB/phpbb/template/twig/extension.php @@ -89,6 +89,8 @@ class extension extends \Twig_Extension { return array( new \Twig_SimpleFunction('lang', array($this, 'lang')), + new \Twig_SimpleFunction('lang_defined', array($this, 'lang_defined')), + new \Twig_SimpleFunction('get_class', 'get_class'), ); } @@ -186,4 +188,14 @@ class extension extends \Twig_Extension return call_user_func_array(array($this->language, 'lang'), $args); } + + /** + * Check if a language variable exists + * + * @return bool + */ + public function lang_defined($key) + { + return call_user_func_array([$this->language, 'is_set'], [$key]); + } } From e00a1548821ce500fbe5a39416c7b2e8619112a1 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 26 Sep 2019 22:33:52 +0200 Subject: [PATCH 7/8] [ticket/15905] Add tests for twig extensions PHPBB3-15905 --- tests/email/email_parsing_test.php | 7 - tests/template/extension_test.php | 253 ++++++++++++++++++ tests/template/templates/avatar_user.html | 1 + .../templates/extension_config_test.html | 1 + .../templates/extension_username_test.html | 1 + 5 files changed, 256 insertions(+), 7 deletions(-) create mode 100644 tests/template/extension_test.php create mode 100644 tests/template/templates/avatar_user.html create mode 100644 tests/template/templates/extension_config_test.html create mode 100644 tests/template/templates/extension_username_test.html diff --git a/tests/email/email_parsing_test.php b/tests/email/email_parsing_test.php index 2e083a6056..629df9abb6 100644 --- a/tests/email/email_parsing_test.php +++ b/tests/email/email_parsing_test.php @@ -67,13 +67,6 @@ class phpbb_email_parsing_test extends phpbb_test_case $phpbb_container->set('ext.manager', $extension_manager); $context = new \phpbb\template\context(); - $twig_extension = new \phpbb\template\twig\extension($context, $lang); - $phpbb_container->set('template.twig.extensions.phpbb', $twig_extension); - - $twig_extensions_collection = new \phpbb\di\service_collection($phpbb_container); - $twig_extensions_collection->add('template.twig.extensions.phpbb'); - $phpbb_container->set('template.twig.extensions.collection', $twig_extensions_collection); - $twig = new \phpbb\template\twig\environment( $config, $filesystem, diff --git a/tests/template/extension_test.php b/tests/template/extension_test.php new file mode 100644 index 0000000000..eba06b5c63 --- /dev/null +++ b/tests/template/extension_test.php @@ -0,0 +1,253 @@ + + * @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 = array()) + { + 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'); + + 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 = new \phpbb\filesystem\filesystem(); + $request = new phpbb_mock_request; + $symfony_request = new \phpbb\symfony_request( + $request + ); + $phpbb_path_helper = new \phpbb\path_helper( + $symfony_request, + $filesystem, + $request, + $phpbb_root_path, + $phpEx + ); + + $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, $filesystem, $phpbb_path_helper, $phpbb_dispatcher, $files); + $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); + + $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($filesystem); + $twig = new \phpbb\template\twig\environment( + $config, + $filesystem, + $phpbb_path_helper, + $cache_path, + null, + $loader, + new \phpbb\event\dispatcher($phpbb_container), + array( + '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\username(), + ] + ); + $twig->setLexer(new \phpbb\template\twig\lexer($twig)); + $this->template->set_custom_style('tests', $this->template_path); + } + + 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_get_user_avatar($file, $vars, $block_vars, $destroy_array, $expected, $lang_vars = []) + { + $this->run_template($file, $vars, $block_vars, $destroy_array, $expected, $lang_vars); + } +} diff --git a/tests/template/templates/avatar_user.html b/tests/template/templates/avatar_user.html new file mode 100644 index 0000000000..bd0b960611 --- /dev/null +++ b/tests/template/templates/avatar_user.html @@ -0,0 +1 @@ +{{ avatar('user', row, alt, ignore_config, lazy) }} diff --git a/tests/template/templates/extension_config_test.html b/tests/template/templates/extension_config_test.html new file mode 100644 index 0000000000..0f12ab2672 --- /dev/null +++ b/tests/template/templates/extension_config_test.html @@ -0,0 +1 @@ +{{ config(config_name) }} diff --git a/tests/template/templates/extension_username_test.html b/tests/template/templates/extension_username_test.html new file mode 100644 index 0000000000..a78bf0df62 --- /dev/null +++ b/tests/template/templates/extension_username_test.html @@ -0,0 +1 @@ +{{ username(mode, user_id, username, user_colour, guest_username, custom_profile_url) }} From f05022941de0c8ec647cc7bcf67be997d2370b77 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sat, 28 Sep 2019 11:58:02 +0200 Subject: [PATCH 8/8] [ticket/15905] Reset avatar manager enabled drivers property PHPBB3-15905 --- tests/template/extension_test.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/template/extension_test.php b/tests/template/extension_test.php index eba06b5c63..d633001060 100644 --- a/tests/template/extension_test.php +++ b/tests/template/extension_test.php @@ -62,6 +62,11 @@ class phpbb_template_extension_test extends phpbb_template_template_test_case ])); $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';