From 2ce73baeab35adc3515a04c1db38af62c98d5c93 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sat, 31 Mar 2012 22:07:04 +0300 Subject: [PATCH 1/5] [ticket/10733] Extending get_source_file_for_handle Extending resource locator's function get_source_file_for_handle to find all files. This modified function should be used by template events to locate all templates before compiling them. PHPBB3-10733 --- phpBB/includes/style/resource_locator.php | 31 ++++++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/phpBB/includes/style/resource_locator.php b/phpBB/includes/style/resource_locator.php index 39505cedb5..1b35752111 100644 --- a/phpBB/includes/style/resource_locator.php +++ b/phpBB/includes/style/resource_locator.php @@ -185,9 +185,12 @@ class phpbb_style_resource_locator * handle to a path without any filesystem or styles tree checks. * * @param string $handle Template handle (i.e. "friendly" template name) + * @param bool $find_all If true, each root path will be checked and function + * will return array of files instead of string and will not + * trigger a error if template does not exist * @return string Source file path */ - public function get_source_file_for_handle($handle) + public function get_source_file_for_handle($handle, $find_all = false) { // If we don't have a file assigned to this handle, die. if (!isset($this->files['style'][0][$handle])) @@ -198,20 +201,40 @@ class phpbb_style_resource_locator // locate a source file that exists $source_file = $this->files['style'][0][$handle]; $tried = $source_file; + $found = false; + $found_all = array(); foreach ($this->roots as $root_key => $root_paths) { foreach ($root_paths as $root_index => $root) { $source_file = $this->files[$root_key][$root_index][$handle]; + $tried .= ', ' . $source_file; if (file_exists($source_file)) { - return $source_file; + $found = true; + break; + } + } + if ($found) + { + if ($find_all) + { + $found_all[] = $source_file; + $found = false; + } + else + { + break; } - $tried .= ', ' . $source_file; } } // search failed - trigger_error("style resource locator: File for handle $handle does not exist. Could not find: $tried", E_USER_ERROR); + if (!$found && !$find_all) + { + trigger_error("style resource locator: File for handle $handle does not exist. Could not find: $tried", E_USER_ERROR); + } + + return ($find_all) ? $found_all : $source_file; } } From f80512f1066ebfc09d2a4ffac412c56a3fb247de Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 1 Apr 2012 09:52:55 +0300 Subject: [PATCH 2/5] [ticket/10733] Adding functions to locate resources Adding $style->locate() and $template->locate() functions PHPBB3-10733 --- phpBB/includes/style/resource_locator.php | 51 +++++++++++++++++++++++ phpBB/includes/style/style.php | 28 +++++++++++++ phpBB/includes/style/template.php | 36 ++++++++++++++++ 3 files changed, 115 insertions(+) diff --git a/phpBB/includes/style/resource_locator.php b/phpBB/includes/style/resource_locator.php index 1b35752111..3e6dd5d6aa 100644 --- a/phpBB/includes/style/resource_locator.php +++ b/phpBB/includes/style/resource_locator.php @@ -237,4 +237,55 @@ class phpbb_style_resource_locator return ($find_all) ? $found_all : $source_file; } + + /** + * Locates source file path, accounting for styles tree and verifying that + * the path exists. + * + * Unlike previous functions, this function works without template handle + * and it can search for more than one file. If more than one file name is + * specified, it will return location of file that it finds first. + * + * @param array $files List of files to locate. + * @param bool $return_default Determines what to return if file does not + * exist. If true, function will return location where file is + * supposed to be. If false, function will return false. + * @param bool $return_full_path If true, function will return full path + * to file. If false, function will return file name. This + * parameter can be used to check which one of set of files + * is available. + * @return string or boolean Source file path if file exists or $return_default is + * true. False if file does not exist and $return_default is false + */ + public function get_first_file_location($files, $return_default = false, $return_full_path = true) + { + // set default value + $default_result = false; + + // check all available paths + foreach ($this->roots as $root_paths) + { + foreach ($root_paths as $path) + { + // check all files + foreach ($files as $filename) + { + $source_file = $path . '/' . $filename; + if (file_exists($source_file)) + { + return ($return_full_path) ? $source_file : $filename; + } + + // assign first file as result if $return_default is true + if ($return_default && $default_result === false) + { + $default_result = $source_file; + } + } + } + } + + // search failed + return $default_result; + } } diff --git a/phpBB/includes/style/style.php b/phpBB/includes/style/style.php index dc1e71dff6..5ac61c9f10 100644 --- a/phpBB/includes/style/style.php +++ b/phpBB/includes/style/style.php @@ -150,4 +150,32 @@ class phpbb_style { $this->provider->set_ext_dir_prefix($ext_dir_prefix); } + + /** + * Locates source file path, accounting for styles tree and verifying that + * the path exists. + * + * @param string or array $files List of files to locate. If there is only + * one file, $files can be a string to make code easier to read. + * @param bool $return_default Determines what to return if file does not + * exist. If true, function will return location where file is + * supposed to be. If false, function will return false. + * @param bool $return_full_path If true, function will return full path + * to file. If false, function will return file name. This + * parameter can be used to check which one of set of files + * is available. + * @return string or boolean Source file path if file exists or $return_default is + * true. False if file does not exist and $return_default is false + */ + public function locate($files, $return_default = false, $return_full_path = true) + { + // convert string to array + if (is_string($files)) + { + $files = array($files); + } + + // use resource locator to find files + return $this->locator->get_first_file_location($files, $return_default, $return_full_path); + } } diff --git a/phpBB/includes/style/template.php b/phpBB/includes/style/template.php index 21a2e821dd..aebf1da603 100644 --- a/phpBB/includes/style/template.php +++ b/phpBB/includes/style/template.php @@ -456,4 +456,40 @@ class phpbb_style_template } include($file); } + + /** + * Locates source template path, accounting for styles tree and verifying that + * the path exists. + * + * @param string or array $files List of templates to locate. If there is only + * one template, $files can be a string to make code easier to read. + * @param bool $return_default Determines what to return if template does not + * exist. If true, function will return location where template is + * supposed to be. If false, function will return false. + * @param bool $return_full_path If true, function will return full path + * to template. If false, function will return template file name. + * This parameter can be used to check which one of set of template + * files is available. + * @return string or boolean Source template path if template exists or $return_default is + * true. False if template does not exist and $return_default is false + */ + public function locate($files, $return_default = false, $return_full_path = true) + { + // add tempalte path prefix + $templates = array(); + if (is_string($files)) + { + $templates[] = $this->template_path . $files; + } + else + { + foreach ($files as $file) + { + $templates[] = $this->template_path . $file; + } + } + + // use resource locator to find files + return $this->locator->get_first_file_location($templates, $return_default, $return_full_path); + } } From 2509853ca530b3b5e0d5b2e10080eeba5a29d937 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 1 Apr 2012 09:53:23 +0300 Subject: [PATCH 3/5] [ticket/10733] Adding test for locator Adding test for $template->locate PHPBB3-10733 --- tests/template/template_locate_test.php | 84 +++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 tests/template/template_locate_test.php diff --git a/tests/template/template_locate_test.php b/tests/template/template_locate_test.php new file mode 100644 index 0000000000..591a6afe0c --- /dev/null +++ b/tests/template/template_locate_test.php @@ -0,0 +1,84 @@ +setup_engine(); + + // Locate template + $result = $this->template->locate($files, $return_default, $return_full_path); + $this->assertEquals($result, $expected); + } + + protected function setup_engine(array $new_config = array()) + { + global $phpbb_root_path, $phpEx, $user; + + $defaults = $this->config_defaults(); + $config = new phpbb_config(array_merge($defaults, $new_config)); + + $this->template_path = dirname(__FILE__) . '/templates'; + $this->parent_template_path = dirname(__FILE__) . '/parent_templates'; + $this->style_resource_locator = new phpbb_style_resource_locator(); + $this->style_provider = new phpbb_style_path_provider(); + $this->template = new phpbb_style_template($phpbb_root_path, $phpEx, $config, $user, $this->style_resource_locator, $this->style_provider); + $this->style = new phpbb_style($phpbb_root_path, $phpEx, $config, $user, $this->style_resource_locator, $this->style_provider, $this->template); + $this->style->set_custom_style('tests', array($this->template_path, $this->parent_template_path), ''); + } +} From f5bac7686b1678dbd33eddd368d845237bb18943 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 1 Apr 2012 19:14:53 +0300 Subject: [PATCH 4/5] [ticket/10733] Removing static from data providers Removing static from data provider functions PHPBB3-10733 --- tests/dbal/select_test.php | 14 +++++++------- tests/dbal/write_test.php | 4 ++-- tests/group_positions/group_positions_test.php | 14 +++++++------- tests/request/request_var_test.php | 4 ++-- tests/security/extract_current_page_test.php | 2 +- tests/security/redirect_test.php | 2 +- tests/template/template_inheritance_test.php | 2 +- tests/template/template_locate_test.php | 2 +- tests/template/template_test.php | 4 ++-- tests/text_processing/make_clickable_test.php | 2 +- tests/utf/utf8_clean_string_test.php | 2 +- 11 files changed, 26 insertions(+), 26 deletions(-) diff --git a/tests/dbal/select_test.php b/tests/dbal/select_test.php index 21b12777dc..cc213f09bc 100644 --- a/tests/dbal/select_test.php +++ b/tests/dbal/select_test.php @@ -17,7 +17,7 @@ class phpbb_dbal_select_test extends phpbb_database_test_case return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/three_users.xml'); } - public static function return_on_error_select_data() + public function return_on_error_select_data() { return array( array('phpbb_users', "username_clean = 'bertie'", array(array('username_clean' => 'bertie'))), @@ -44,7 +44,7 @@ class phpbb_dbal_select_test extends phpbb_database_test_case $this->assertEquals($expected, $db->sql_fetchrowset($result)); } - public static function fetchrow_data() + public function fetchrow_data() { return array( array('', array(array('username_clean' => 'barfoo'), @@ -95,7 +95,7 @@ class phpbb_dbal_select_test extends phpbb_database_test_case $db->sql_freeresult($result); } - public static function fetchfield_data() + public function fetchfield_data() { return array( array('', array('barfoo', 'foobar', 'bertie')), @@ -125,7 +125,7 @@ class phpbb_dbal_select_test extends phpbb_database_test_case $this->assertEquals($expected, $ary); } - public static function query_limit_data() + public function query_limit_data() { return array( array(0, 0, array(array('username_clean' => 'barfoo'), @@ -166,7 +166,7 @@ class phpbb_dbal_select_test extends phpbb_database_test_case $this->assertEquals($expected, $ary); } - public static function like_expression_data() + public function like_expression_data() { // * = any_char; # = one_char return array( @@ -203,7 +203,7 @@ class phpbb_dbal_select_test extends phpbb_database_test_case $db->sql_freeresult($result); } - public static function in_set_data() + public function in_set_data() { return array( array('user_id', 3, false, false, array(array('username_clean' => 'bertie'))), @@ -277,7 +277,7 @@ class phpbb_dbal_select_test extends phpbb_database_test_case $db->sql_freeresult($result); } - public static function build_array_data() + public function build_array_data() { return array( array(array('username_clean' => 'barfoo'), array(array('username_clean' => 'barfoo'))), diff --git a/tests/dbal/write_test.php b/tests/dbal/write_test.php index 596c50a220..987161a831 100644 --- a/tests/dbal/write_test.php +++ b/tests/dbal/write_test.php @@ -16,7 +16,7 @@ class phpbb_dbal_write_test extends phpbb_database_test_case return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml'); } - public static function build_array_insert_data() + public function build_array_insert_data() { return array( array(array( @@ -104,7 +104,7 @@ class phpbb_dbal_write_test extends phpbb_database_test_case $db->sql_freeresult($result); } - public static function update_data() + public function update_data() { return array( array( diff --git a/tests/group_positions/group_positions_test.php b/tests/group_positions/group_positions_test.php index fd9f57e78f..c17e25511b 100644 --- a/tests/group_positions/group_positions_test.php +++ b/tests/group_positions/group_positions_test.php @@ -15,7 +15,7 @@ class phpbb_group_positions_test extends phpbb_database_test_case return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/group_positions.xml'); } - public static function get_group_value_data() + public function get_group_value_data() { return array( array('teampage', 1, 0), @@ -38,7 +38,7 @@ class phpbb_group_positions_test extends phpbb_database_test_case $this->assertEquals($expected, $test_class->get_group_value($group_id)); } - public static function get_group_count_data() + public function get_group_count_data() { return array( array('teampage', 2), @@ -59,7 +59,7 @@ class phpbb_group_positions_test extends phpbb_database_test_case $this->assertEquals($expected, $test_class->get_group_count()); } - public static function add_group_data() + public function add_group_data() { return array( array('teampage', 1, array( @@ -93,7 +93,7 @@ class phpbb_group_positions_test extends phpbb_database_test_case $this->assertEquals($expected, $db->sql_fetchrowset($result)); } - public static function delete_group_data() + public function delete_group_data() { return array( array('teampage', 1, false, array( @@ -147,7 +147,7 @@ class phpbb_group_positions_test extends phpbb_database_test_case $this->assertEquals($expected, $db->sql_fetchrowset($result)); } - public static function move_up_data() + public function move_up_data() { return array( array('teampage', 1, array( @@ -186,7 +186,7 @@ class phpbb_group_positions_test extends phpbb_database_test_case $this->assertEquals($expected, $db->sql_fetchrowset($result)); } - public static function move_down_data() + public function move_down_data() { return array( array('teampage', 1, array( @@ -225,7 +225,7 @@ class phpbb_group_positions_test extends phpbb_database_test_case $this->assertEquals($expected, $db->sql_fetchrowset($result)); } - public static function move_data() + public function move_data() { return array( array('teampage', 1, 1, array( diff --git a/tests/request/request_var_test.php b/tests/request/request_var_test.php index 1fa0afae13..0e85d4694b 100644 --- a/tests/request/request_var_test.php +++ b/tests/request/request_var_test.php @@ -112,7 +112,7 @@ class phpbb_request_var_test extends phpbb_test_case $this->assertEquals($expected, $result, 'Testing deep access to multidimensional input arrays: ' . $path); } - public static function deep_access() + public function deep_access() { return array( // array(path, default, expected result) @@ -123,7 +123,7 @@ class phpbb_request_var_test extends phpbb_test_case ); } - public static function request_variables() + public function request_variables() { return array( // strings diff --git a/tests/security/extract_current_page_test.php b/tests/security/extract_current_page_test.php index b4a475ffb3..d77cbbcaf3 100644 --- a/tests/security/extract_current_page_test.php +++ b/tests/security/extract_current_page_test.php @@ -13,7 +13,7 @@ require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; class phpbb_security_extract_current_page_test extends phpbb_security_test_base { - public static function security_variables() + public function security_variables() { return array( array('http://localhost/phpBB/index.php', 'mark=forums&x=">', 'mark=forums&x=%22%3E%3Cscript%3Ealert(/XSS/);%3C/script%3E'), diff --git a/tests/security/redirect_test.php b/tests/security/redirect_test.php index da318209e2..1325466137 100644 --- a/tests/security/redirect_test.php +++ b/tests/security/redirect_test.php @@ -13,7 +13,7 @@ require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php'; class phpbb_security_redirect_test extends phpbb_security_test_base { - public static function provider() + public function provider() { // array(Input -> redirect(), expected triggered error (else false), expected returned result url (else false)) return array( diff --git a/tests/template/template_inheritance_test.php b/tests/template/template_inheritance_test.php index 7348da9a4a..a76658701a 100644 --- a/tests/template/template_inheritance_test.php +++ b/tests/template/template_inheritance_test.php @@ -14,7 +14,7 @@ class phpbb_template_template_inheritance_test extends phpbb_template_template_t /** * @todo put test data into templates/xyz.test */ - public static function template_data() + public function template_data() { return array( // First element of the array is test name - keep them distinct diff --git a/tests/template/template_locate_test.php b/tests/template/template_locate_test.php index 591a6afe0c..8edbd6a008 100644 --- a/tests/template/template_locate_test.php +++ b/tests/template/template_locate_test.php @@ -11,7 +11,7 @@ require_once dirname(__FILE__) . '/template_test_case.php'; class phpbb_template_template_locate_test extends phpbb_template_template_test_case { - public static function template_data() + public function template_data() { return array( // First element of the array is test name - keep them distinct diff --git a/tests/template/template_test.php b/tests/template/template_test.php index edf621e16c..739bbe9387 100644 --- a/tests/template/template_test.php +++ b/tests/template/template_test.php @@ -15,7 +15,7 @@ class phpbb_template_template_test extends phpbb_template_template_test_case /** * @todo put test data into templates/xyz.test */ - public static function template_data() + public function template_data() { return array( /* @@ -394,7 +394,7 @@ class phpbb_template_template_test extends phpbb_template_template_test_case $this->run_template('php.html', array(), array(), array(), 'test', $cache_file); } - public static function alter_block_array_data() + public function alter_block_array_data() { return array( array( diff --git a/tests/text_processing/make_clickable_test.php b/tests/text_processing/make_clickable_test.php index 8697907311..d94fac2ae4 100644 --- a/tests/text_processing/make_clickable_test.php +++ b/tests/text_processing/make_clickable_test.php @@ -12,7 +12,7 @@ require_once dirname(__FILE__) . '/../../phpBB/includes/functions_content.php'; class phpbb_text_processing_make_clickable_test extends phpbb_test_case { - public static function make_clickable_data() + public function make_clickable_data() { // value => whether it should work $prefix_texts = array( diff --git a/tests/utf/utf8_clean_string_test.php b/tests/utf/utf8_clean_string_test.php index 70bd549d5b..ae11e00fbd 100644 --- a/tests/utf/utf8_clean_string_test.php +++ b/tests/utf/utf8_clean_string_test.php @@ -11,7 +11,7 @@ require_once dirname(__FILE__) . '/../../phpBB/includes/utf/utf_tools.php'; class phpbb_utf_utf8_clean_string_test extends phpbb_test_case { - public static function cleanable_strings() + public function cleanable_strings() { return array( array('MiXed CaSe', 'mixed case', 'Checking case folding'), From eaba2ed9ca6025358de0432bb569cfb2e3ca6985 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 1 Apr 2012 19:20:42 +0300 Subject: [PATCH 5/5] [ticket/10733] Fixing test Changing expected and result in locator test PHPBB3-10733 --- tests/template/template_locate_test.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/template/template_locate_test.php b/tests/template/template_locate_test.php index 8edbd6a008..89a4ae6fb1 100644 --- a/tests/template/template_locate_test.php +++ b/tests/template/template_locate_test.php @@ -17,38 +17,38 @@ class phpbb_template_template_locate_test extends phpbb_template_template_test_c // First element of the array is test name - keep them distinct array( 'simple inheritance - only parent template exists', + dirname(__FILE__) . '/parent_templates/parent_only.html', 'parent_only.html', false, true, - dirname(__FILE__) . '/parent_templates/parent_only.html', ), array( 'simple inheritance - only child template exists', + dirname(__FILE__) . '/templates/child_only.html', 'child_only.html', false, true, - dirname(__FILE__) . '/templates/child_only.html', ), array( 'simple inheritance - both parent and child templates exist', + dirname(__FILE__) . '/templates/parent_and_child.html', 'parent_and_child.html', false, true, - dirname(__FILE__) . '/templates/parent_and_child.html', ), array( 'find first template - only child template exists in main style', + 'child_only.html', array('parent_only.html', 'child_only.html'), false, false, - 'child_only.html', ), array( 'find first template - both templates exist in main style', + 'parent_and_child.html', array('parent_and_child.html', 'child_only.html'), false, false, - 'parent_and_child.html', ), ); } @@ -56,14 +56,14 @@ class phpbb_template_template_locate_test extends phpbb_template_template_test_c /** * @dataProvider template_data */ - public function test_template($name, $files, $return_default, $return_full_path, $expected) + public function test_template($name, $expected, $files, $return_default, $return_full_path) { // Reset the engine state $this->setup_engine(); // Locate template $result = $this->template->locate($files, $return_default, $return_full_path); - $this->assertEquals($result, $expected); + $this->assertSame($expected, $result); } protected function setup_engine(array $new_config = array())