From 95032c31af61ced178ce8fe3494abdc514a8ebf2 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Fri, 11 Jul 2014 10:18:46 -0700 Subject: [PATCH 1/6] [ticket/12841] Allow extensions to position new config vars PHPBB3-12841 --- phpBB/includes/functions_acp.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/phpBB/includes/functions_acp.php b/phpBB/includes/functions_acp.php index 8453da6e6e..13b070b82e 100644 --- a/phpBB/includes/functions_acp.php +++ b/phpBB/includes/functions_acp.php @@ -656,3 +656,29 @@ function validate_range($value_ary, &$error) } } } + +/** +* Inserts new config display_vars into an exisiting display_vars array +* at the given position. +* +* @param array $display_vars An array of existing config display vars +* @param array $add_config_vars An array of new config display vars +* @param array $where Where to place the new config vars, +* before or after an exisiting config, as an array +* of the form: array('after' => 'config_name') +* @return array The array of config display vars +*/ +function insert_config_array($display_vars, $add_config_vars, $where) +{ + if (is_array($where) && array_key_exists(current($where), $display_vars)) + { + $position = array_search(current($where), array_keys($display_vars)) + ((key($where) == 'after') ? 1 : 0); + $display_vars = array_merge( + array_slice($display_vars, 0, $position), + $add_config_vars, + array_slice($display_vars, $position) + ); + } + + return $display_vars; +} From 14979e63992b661a6ed9d01c1b51ee528504e116 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Fri, 11 Jul 2014 13:40:45 -0700 Subject: [PATCH 2/6] [ticket/12841] Fix white space issues reported by sniffer PHPBB3-12841 --- phpBB/includes/functions_acp.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phpBB/includes/functions_acp.php b/phpBB/includes/functions_acp.php index 13b070b82e..264276338a 100644 --- a/phpBB/includes/functions_acp.php +++ b/phpBB/includes/functions_acp.php @@ -663,8 +663,8 @@ function validate_range($value_ary, &$error) * * @param array $display_vars An array of existing config display vars * @param array $add_config_vars An array of new config display vars -* @param array $where Where to place the new config vars, -* before or after an exisiting config, as an array +* @param array $where Where to place the new config vars, +* before or after an exisiting config, as an array * of the form: array('after' => 'config_name') * @return array The array of config display vars */ @@ -679,6 +679,6 @@ function insert_config_array($display_vars, $add_config_vars, $where) array_slice($display_vars, $position) ); } - + return $display_vars; } From 0d320186cf37ae81eb038a74664440317dddc721 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Fri, 11 Jul 2014 14:33:45 -0700 Subject: [PATCH 3/6] [ticket/12841] Add a tests for the new function PHPBB3-12841 --- tests/functions/insert_config_array_test.php | 142 +++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 tests/functions/insert_config_array_test.php diff --git a/tests/functions/insert_config_array_test.php b/tests/functions/insert_config_array_test.php new file mode 100644 index 0000000000..2ff7ae4853 --- /dev/null +++ b/tests/functions/insert_config_array_test.php @@ -0,0 +1,142 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +class phpbb_functions_insert_config_array_test extends phpbb_test_case +{ + public function config_display_vars() + { + return array( + 'legend1' => '', + 'acp_config_1' => array(), + 'acp_config_2' => array(), + 'acp_config_3' => array(), + 'acp_config_4' => array(), + 'acp_config_5' => array(), + ); + } + + public function insert_config_array_data() + { + return array( + array( // Add a new config after 1st array item + array('new_config_1' => array()), + array('after' => 'legend1'), + array( + 'legend1' => '', + 'new_config_1' => array(), + 'acp_config_1' => array(), + 'acp_config_2' => array(), + 'acp_config_3' => array(), + 'acp_config_4' => array(), + 'acp_config_5' => array(), + ), + ), + array( // Add a new config after last array item + array('new_config_1' => array()), + array('after' => 'acp_config_5'), + array( + 'legend1' => '', + 'acp_config_1' => array(), + 'acp_config_2' => array(), + 'acp_config_3' => array(), + 'acp_config_4' => array(), + 'acp_config_5' => array(), + 'new_config_1' => array(), + ), + ), + array( // Add a new config before 2nd array item + array('new_config_1' => array()), + array('before' => 'acp_config_1'), + array( + 'legend1' => '', + 'new_config_1' => array(), + 'acp_config_1' => array(), + 'acp_config_2' => array(), + 'acp_config_3' => array(), + 'acp_config_4' => array(), + 'acp_config_5' => array(), + ), + ), + array( // Add a new config before last config item + array('new_config_1' => array()), + array('before' => 'acp_config_5'), + array( + 'legend1' => '', + 'acp_config_1' => array(), + 'acp_config_2' => array(), + 'acp_config_3' => array(), + 'acp_config_4' => array(), + 'new_config_1' => array(), + 'acp_config_5' => array(), + ), + ), + array( // When an array key does not exist + array('new_config_1' => array()), + array('after' => 'foobar'), + array( + 'legend1' => '', + 'acp_config_1' => array(), + 'acp_config_2' => array(), + 'acp_config_3' => array(), + 'acp_config_4' => array(), + 'acp_config_5' => array(), + ), + ), + array( // When before|after is not used correctly (defaults to before) + array('new_config_1' => array()), + array('foobar' => 'acp_config_1'), + array( + 'legend1' => '', + 'new_config_1' => array(), + 'acp_config_1' => array(), + 'acp_config_2' => array(), + 'acp_config_3' => array(), + 'acp_config_4' => array(), + 'acp_config_5' => array(), + ), + ), + array( // Add a new config set after the last array item + array( + 'legend2' => array(), + 'new_config_1' => array(), + 'new_config_2' => array(), + 'new_config_3' => array(), + ), + array('after' => 'acp_config_5'), + array( + 'legend1' => '', + 'acp_config_1' => array(), + 'acp_config_2' => array(), + 'acp_config_3' => array(), + 'acp_config_4' => array(), + 'acp_config_5' => array(), + 'legend2' => array(), + 'new_config_1' => array(), + 'new_config_2' => array(), + 'new_config_3' => array(), + ), + ), + ); + } + + /** + * @dataProvider insert_config_array_data + */ + public function test_insert_config_array($new_config, $position, $expected) + { + $config_array = $this->config_display_vars(); + $new_config_array = insert_config_array($config_array, $new_config, $position); + + $this->assertSame($expected, $new_config_array); + } +} From 056584680b4603e93c5e428a1177b09fbea88f30 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Thu, 24 Jul 2014 07:44:35 -0700 Subject: [PATCH 4/6] [ticket/12841] prefix function name with phpbb_ PHPBB3-12841 --- phpBB/includes/functions_acp.php | 2 +- tests/functions/insert_config_array_test.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/includes/functions_acp.php b/phpBB/includes/functions_acp.php index 264276338a..2edb44d408 100644 --- a/phpBB/includes/functions_acp.php +++ b/phpBB/includes/functions_acp.php @@ -668,7 +668,7 @@ function validate_range($value_ary, &$error) * of the form: array('after' => 'config_name') * @return array The array of config display vars */ -function insert_config_array($display_vars, $add_config_vars, $where) +function phpbb_insert_config_array($display_vars, $add_config_vars, $where) { if (is_array($where) && array_key_exists(current($where), $display_vars)) { diff --git a/tests/functions/insert_config_array_test.php b/tests/functions/insert_config_array_test.php index 2ff7ae4853..8034ce982d 100644 --- a/tests/functions/insert_config_array_test.php +++ b/tests/functions/insert_config_array_test.php @@ -135,7 +135,7 @@ class phpbb_functions_insert_config_array_test extends phpbb_test_case public function test_insert_config_array($new_config, $position, $expected) { $config_array = $this->config_display_vars(); - $new_config_array = insert_config_array($config_array, $new_config, $position); + $new_config_array = phpbb_insert_config_array($config_array, $new_config, $position); $this->assertSame($expected, $new_config_array); } From 6576e0eb88908b40174dd64cbcb6842011afd91e Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Sun, 27 Jul 2014 23:03:43 -0500 Subject: [PATCH 5/6] [ticket/12841] Update comments in tests making after default PHPBB3-12841 --- tests/functions/insert_config_array_test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/functions/insert_config_array_test.php b/tests/functions/insert_config_array_test.php index 8034ce982d..e0ea3428b8 100644 --- a/tests/functions/insert_config_array_test.php +++ b/tests/functions/insert_config_array_test.php @@ -92,7 +92,7 @@ class phpbb_functions_insert_config_array_test extends phpbb_test_case 'acp_config_5' => array(), ), ), - array( // When before|after is not used correctly (defaults to before) + array( // When after|before is not used correctly (defaults to before) array('new_config_1' => array()), array('foobar' => 'acp_config_1'), array( From ced76b5f4daed59f7b58fadc8c955c7b76373cf5 Mon Sep 17 00:00:00 2001 From: Matt Friedman Date: Mon, 28 Jul 2014 22:27:01 -0500 Subject: [PATCH 6/6] [ticket/12841] Make config position default to after PHPBB3-12841 --- phpBB/includes/functions_acp.php | 5 +++-- tests/functions/insert_config_array_test.php | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/phpBB/includes/functions_acp.php b/phpBB/includes/functions_acp.php index 2edb44d408..c46cc3421d 100644 --- a/phpBB/includes/functions_acp.php +++ b/phpBB/includes/functions_acp.php @@ -665,14 +665,15 @@ function validate_range($value_ary, &$error) * @param array $add_config_vars An array of new config display vars * @param array $where Where to place the new config vars, * before or after an exisiting config, as an array -* of the form: array('after' => 'config_name') +* of the form: array('after' => 'config_name') or +* array('before' => 'config_name'). * @return array The array of config display vars */ function phpbb_insert_config_array($display_vars, $add_config_vars, $where) { if (is_array($where) && array_key_exists(current($where), $display_vars)) { - $position = array_search(current($where), array_keys($display_vars)) + ((key($where) == 'after') ? 1 : 0); + $position = array_search(current($where), array_keys($display_vars)) + ((key($where) == 'before') ? 0 : 1); $display_vars = array_merge( array_slice($display_vars, 0, $position), $add_config_vars, diff --git a/tests/functions/insert_config_array_test.php b/tests/functions/insert_config_array_test.php index e0ea3428b8..bfcb05862e 100644 --- a/tests/functions/insert_config_array_test.php +++ b/tests/functions/insert_config_array_test.php @@ -92,13 +92,13 @@ class phpbb_functions_insert_config_array_test extends phpbb_test_case 'acp_config_5' => array(), ), ), - array( // When after|before is not used correctly (defaults to before) + array( // When after|before is not used correctly (defaults to after) array('new_config_1' => array()), array('foobar' => 'acp_config_1'), array( 'legend1' => '', - 'new_config_1' => array(), 'acp_config_1' => array(), + 'new_config_1' => array(), 'acp_config_2' => array(), 'acp_config_3' => array(), 'acp_config_4' => array(),