From 323a494cd16bd202d89260f756519c2d76f2f9fe Mon Sep 17 00:00:00 2001 From: OpenShift guest Date: Mon, 25 Mar 2013 18:21:48 -0400 Subject: [PATCH 1/8] [ticket/11458] Search for permission language files in extensions Extensions that add new permission masks only need to add a permission file in the language folder of the extension. The file must start with 'permissions_' eg 'permissions_blog.php'. The permission language file will be automatically included when viewing/setting permissions. PHPBB3-11458 --- phpBB/includes/functions_admin.php | 37 ++++++++++-------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/phpBB/includes/functions_admin.php b/phpBB/includes/functions_admin.php index d273b9fb3a..5d71f55d1a 100644 --- a/phpBB/includes/functions_admin.php +++ b/phpBB/includes/functions_admin.php @@ -3040,38 +3040,25 @@ function tidy_database() */ function add_permission_language() { - global $user, $phpEx; + global $user, $phpEx, $phpbb_extension_manager; // First of all, our own file. We need to include it as the first file because it presets all relevant variables. $user->add_lang('acp/permissions_phpbb'); - $files_to_add = array(); + // add permission language files from extensions + $finder = $phpbb_extension_manager->get_finder(); - // Now search in acp and mods folder for permissions_ files. - foreach (array('acp/', 'mods/') as $path) + $lang_files = $finder + ->prefix('permissions_') + ->suffix(".$phpEx") + ->extension_directory('/language/' . $user->lang_name) + ->core_path('language/' . $user->lang_name . '/mods') + ->find(); + + foreach ($lang_files as $lang_file => $ext_name) { - $dh = @opendir($user->lang_path . $user->lang_name . '/' . $path); - - if ($dh) - { - while (($file = readdir($dh)) !== false) - { - if ($file !== 'permissions_phpbb.' . $phpEx && strpos($file, 'permissions_') === 0 && substr($file, -(strlen($phpEx) + 1)) === '.' . $phpEx) - { - $files_to_add[] = $path . substr($file, 0, -(strlen($phpEx) + 1)); - } - } - closedir($dh); - } + $user->add_lang_ext($ext_name, $lang_file); } - - if (!sizeof($files_to_add)) - { - return false; - } - - $user->add_lang($files_to_add); - return true; } /** From 474b4a60a527a4c0cd853872ce80ae50fdd2f374 Mon Sep 17 00:00:00 2001 From: OpenShift guest Date: Sat, 13 Apr 2013 10:49:59 -0400 Subject: [PATCH 2/8] [ticket/11458] We still auto add language files from the mods and acp folders in the language directory, so we revert some changes here PHPBB3-11458 --- phpBB/includes/functions_admin.php | 35 ++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/phpBB/includes/functions_admin.php b/phpBB/includes/functions_admin.php index 5d71f55d1a..b5efa49159 100644 --- a/phpBB/includes/functions_admin.php +++ b/phpBB/includes/functions_admin.php @@ -3045,20 +3045,47 @@ function add_permission_language() // First of all, our own file. We need to include it as the first file because it presets all relevant variables. $user->add_lang('acp/permissions_phpbb'); - // add permission language files from extensions + $files_to_add = array(); + + // Now search in acp and mods folder for permissions_ files. + foreach (array('acp/', 'mods/') as $path) + { + $dh = @opendir($user->lang_path . $user->lang_name . '/' . $path); + + if ($dh) + { + while (($file = readdir($dh)) !== false) + { + if ($file !== 'permissions_phpbb.' . $phpEx && strpos($file, 'permissions_') === 0 && substr($file, -(strlen($phpEx) + 1)) === '.' . $phpEx) + { + $files_to_add[] = $path . substr($file, 0, -(strlen($phpEx) + 1)); + } + } + closedir($dh); + } + } + + // find permission language files from extensions $finder = $phpbb_extension_manager->get_finder(); - $lang_files = $finder + $ext_lang_files = $finder ->prefix('permissions_') ->suffix(".$phpEx") ->extension_directory('/language/' . $user->lang_name) - ->core_path('language/' . $user->lang_name . '/mods') ->find(); - foreach ($lang_files as $lang_file => $ext_name) + foreach ($ext_lang_files as $lang_file => $ext_name) { $user->add_lang_ext($ext_name, $lang_file); } + + if (!sizeof($files_to_add)) + { + return false; + } + + $user->add_lang($files_to_add); + return true; } /** From 84c815a12e7c2c816035e3b05e8ea7c88f0f4534 Mon Sep 17 00:00:00 2001 From: OpenShift guest Date: Sat, 13 Apr 2013 11:24:47 -0400 Subject: [PATCH 3/8] [ticket/11458] Add functional test Since there is no test method to include extension language files, a functional test seems more appropriate. We add a permission mask 'acl_u_foo' with translation found in extenion 'bar' and confirm that the permission language file 'permissions_foo.php' from 'bar' was added by asserting that 'Can view foo' exists when viewing user permissions in acp PHPBB3-11458 --- .../extension_permission_lang_test.php | 59 +++++++++++++++++++ .../foo/bar/language/en/permissions_foo.php | 6 ++ 2 files changed, 65 insertions(+) create mode 100644 tests/functional/extension_permission_lang_test.php create mode 100644 tests/functional/fixtures/ext/foo/bar/language/en/permissions_foo.php diff --git a/tests/functional/extension_permission_lang_test.php b/tests/functional/extension_permission_lang_test.php new file mode 100644 index 0000000000..c3d136de49 --- /dev/null +++ b/tests/functional/extension_permission_lang_test.php @@ -0,0 +1,59 @@ +phpbb_extension_manager = $this->get_extension_manager(); + + $this->purge_cache(); + + $this->login(); + $this->admin_login(); + $this->add_lang('acp/permissions'); + } + + public function test_auto_include_permission_lang_from_extensions() + { + $this->phpbb_extension_manager->enable('foo/bar'); + + // User permissions + $crawler = $this->request('GET', 'adm/index.php?i=acp_permissions&icat=16&mode=setting_user_global&sid=' . $this->sid); + $this->assert_response_success(); + $this->assertContains('Can view foo', $this->client->getResponse()->getContent()); + } + + public function permissions_data() + { + return array( + // description + // permission type + // permission name + // mode + // object name + // object id + array( + 'user permission', + 'u_', + 'acl_u_foo', + 'setting_user_global', + 'user_id', + 2, + ), + ); + } +} diff --git a/tests/functional/fixtures/ext/foo/bar/language/en/permissions_foo.php b/tests/functional/fixtures/ext/foo/bar/language/en/permissions_foo.php new file mode 100644 index 0000000000..cd4b9a32d1 --- /dev/null +++ b/tests/functional/fixtures/ext/foo/bar/language/en/permissions_foo.php @@ -0,0 +1,6 @@ + array('lang' => 'Can view foo', 'cat' => 'misc'), +)); From 2c910999b090502cf6a3ac2add68de763ea897ca Mon Sep 17 00:00:00 2001 From: OpenShift guest Date: Sat, 13 Apr 2013 13:56:02 -0400 Subject: [PATCH 4/8] [ticket/11458] Fix test PHPBB-11458 --- tests/functional/extension_permission_lang_test.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/functional/extension_permission_lang_test.php b/tests/functional/extension_permission_lang_test.php index c3d136de49..1368a628d8 100644 --- a/tests/functional/extension_permission_lang_test.php +++ b/tests/functional/extension_permission_lang_test.php @@ -34,7 +34,14 @@ class phpbb_functional_extension_permission_lang_test extends phpbb_functional_t // User permissions $crawler = $this->request('GET', 'adm/index.php?i=acp_permissions&icat=16&mode=setting_user_global&sid=' . $this->sid); $this->assert_response_success(); - $this->assertContains('Can view foo', $this->client->getResponse()->getContent()); + + // Select admin + $form = $crawler->selectButton($this->lang('SUBMIT'))->form(); + $data = array('username[0]' => 'admin'); + $form->setValues($data); + $crawler = $this->client->submit($form); + $this->assert_response_success(); + $this->assertContains('Can view foo', $crawler->filter('body')->text()); } public function permissions_data() From 9e2acdab9aea7762761e81b3306a950ab627434d Mon Sep 17 00:00:00 2001 From: OpenShift guest Date: Sun, 14 Apr 2013 17:53:38 -0400 Subject: [PATCH 5/8] [ticket/11458] Fix functional test again Add 'u_foo' to phpbb_acl_options Make sure extension and fixtures are added before running test PHPBB3-11458 --- .../extension_permission_lang_test.php | 84 ++++++++++++++----- 1 file changed, 64 insertions(+), 20 deletions(-) diff --git a/tests/functional/extension_permission_lang_test.php b/tests/functional/extension_permission_lang_test.php index 1368a628d8..234cbbbaf2 100644 --- a/tests/functional/extension_permission_lang_test.php +++ b/tests/functional/extension_permission_lang_test.php @@ -14,9 +14,73 @@ class phpbb_functional_extension_permission_lang_test extends phpbb_functional_t { protected $phpbb_extension_manager; + static protected $fixtures = array( + 'foo/bar/language/en/permissions_foo.php', + ); + + /** + * This should only be called once before the tests are run. + * This is used to copy the fixtures to the phpBB install + */ + static public function setUpBeforeClass() + { + global $phpbb_root_path; + parent::setUpBeforeClass(); + + $directories = array( + $phpbb_root_path . 'ext/foo/bar/', + $phpbb_root_path . 'ext/foo/bar/language/', + $phpbb_root_path . 'ext/foo/bar/language/en/', + ); + + foreach ($directories as $dir) + { + if (!is_dir($dir)) + { + mkdir($dir, 0777, true); + } + } + + foreach (self::$fixtures as $fixture) + { + copy( + "tests/functional/fixtures/ext/$fixture", + "{$phpbb_root_path}ext/$fixture"); + } + } + + /** + * This should only be called once after the tests are run. + * This is used to remove the fixtures from the phpBB install + */ + static public function tearDownAfterClass() + { + global $phpbb_root_path; + + foreach (self::$fixtures as $fixture) + { + unlink("{$phpbb_root_path}ext/$fixture"); + } + + rmdir("{$phpbb_root_path}ext/foo/bar/language/en"); + rmdir("{$phpbb_root_path}ext/foo/bar/language"); + rmdir("{$phpbb_root_path}ext/foo/bar"); + rmdir("{$phpbb_root_path}ext/foo"); + } + public function setUp() { parent::setUp(); + + $this->get_db(); + + $acl_ary = array( + 'auth_option' => 'u_foo', + 'is_global' => 1, + ); + + $sql = 'INSERT INTO phpbb_acl_options ' . $this->db->sql_build_array('INSERT', $acl_ary); + $this->db->sql_query($sql); $this->phpbb_extension_manager = $this->get_extension_manager(); @@ -43,24 +107,4 @@ class phpbb_functional_extension_permission_lang_test extends phpbb_functional_t $this->assert_response_success(); $this->assertContains('Can view foo', $crawler->filter('body')->text()); } - - public function permissions_data() - { - return array( - // description - // permission type - // permission name - // mode - // object name - // object id - array( - 'user permission', - 'u_', - 'acl_u_foo', - 'setting_user_global', - 'user_id', - 2, - ), - ); - } } From 109b1a3a952f8a590f6613c8d940e6d01b755af7 Mon Sep 17 00:00:00 2001 From: OpenShift guest Date: Thu, 9 May 2013 23:12:19 -0400 Subject: [PATCH 6/8] [ticket/11458] Use helper to create/move/delete directories/files PHPBB3-11458 --- .../extension_permission_lang_test.php | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/tests/functional/extension_permission_lang_test.php b/tests/functional/extension_permission_lang_test.php index 234cbbbaf2..6f1048279a 100644 --- a/tests/functional/extension_permission_lang_test.php +++ b/tests/functional/extension_permission_lang_test.php @@ -14,8 +14,12 @@ class phpbb_functional_extension_permission_lang_test extends phpbb_functional_t { protected $phpbb_extension_manager; + static private $helper; + + static private $copied_files = array(); + static protected $fixtures = array( - 'foo/bar/language/en/permissions_foo.php', + 'foo/bar/language/en/', ); /** @@ -27,26 +31,21 @@ class phpbb_functional_extension_permission_lang_test extends phpbb_functional_t global $phpbb_root_path; parent::setUpBeforeClass(); - $directories = array( - $phpbb_root_path . 'ext/foo/bar/', - $phpbb_root_path . 'ext/foo/bar/language/', - $phpbb_root_path . 'ext/foo/bar/language/en/', - ); + self::$helper = new phpbb_test_case_helpers(self); - foreach ($directories as $dir) + self::$copied_files = array(); + + if (file_exists($phpbb_root_path . 'ext/')) { - if (!is_dir($dir)) - { - mkdir($dir, 0777, true); - } + // First, move any extensions setup on the board to a temp directory + self::$copied_files = self::$helper->copy_dir($phpbb_root_path . 'ext/', $phpbb_root_path . 'store/temp_ext/'); + + // Then empty the ext/ directory on the board (for accurate test cases) + self::$helper->empty_dir($phpbb_root_path . 'ext/'); } - foreach (self::$fixtures as $fixture) - { - copy( - "tests/functional/fixtures/ext/$fixture", - "{$phpbb_root_path}ext/$fixture"); - } + // Copy our ext/ files from the test case to the board + self::$copied_files = array_merge(self::$copied_files, self::$helper->copy_dir(dirname(__FILE__) . '/fixtures/ext/' . $fixture, $phpbb_root_path . 'ext/' . $fixture)); } /** @@ -56,16 +55,20 @@ class phpbb_functional_extension_permission_lang_test extends phpbb_functional_t static public function tearDownAfterClass() { global $phpbb_root_path; - - foreach (self::$fixtures as $fixture) + + if (file_exists($phpbb_root_path . 'store/temp_ext/')) { - unlink("{$phpbb_root_path}ext/$fixture"); + // Copy back the board installed extensions from the temp directory + self::$helper->copy_dir($phpbb_root_path . 'store/temp_ext/', $phpbb_root_path . 'ext/'); } - rmdir("{$phpbb_root_path}ext/foo/bar/language/en"); - rmdir("{$phpbb_root_path}ext/foo/bar/language"); - rmdir("{$phpbb_root_path}ext/foo/bar"); - rmdir("{$phpbb_root_path}ext/foo"); + // Remove all of the files we copied around (from board ext -> temp_ext, from test ext -> board ext) + self::$helper->remove_files(self::$copied_files); + + if (file_exists($phpbb_root_path . 'store/temp_ext/')) + { + self::$helper->empty_dir($phpbb_root_path . 'store/temp_ext/'); + } } public function setUp() From a50d23078e8964396bda1e2c9cfc6e503f4f6fb9 Mon Sep 17 00:00:00 2001 From: OpenShift guest Date: Fri, 10 May 2013 10:15:25 -0400 Subject: [PATCH 7/8] [ticket/11458] Use finder to get all permission language files PHPBB3-11458 --- phpBB/includes/functions_admin.php | 47 ++++++++---------------------- 1 file changed, 12 insertions(+), 35 deletions(-) diff --git a/phpBB/includes/functions_admin.php b/phpBB/includes/functions_admin.php index b5efa49159..d9e445cff9 100644 --- a/phpBB/includes/functions_admin.php +++ b/phpBB/includes/functions_admin.php @@ -3042,50 +3042,27 @@ function add_permission_language() { global $user, $phpEx, $phpbb_extension_manager; - // First of all, our own file. We need to include it as the first file because it presets all relevant variables. - $user->add_lang('acp/permissions_phpbb'); - - $files_to_add = array(); - - // Now search in acp and mods folder for permissions_ files. - foreach (array('acp/', 'mods/') as $path) - { - $dh = @opendir($user->lang_path . $user->lang_name . '/' . $path); - - if ($dh) - { - while (($file = readdir($dh)) !== false) - { - if ($file !== 'permissions_phpbb.' . $phpEx && strpos($file, 'permissions_') === 0 && substr($file, -(strlen($phpEx) + 1)) === '.' . $phpEx) - { - $files_to_add[] = $path . substr($file, 0, -(strlen($phpEx) + 1)); - } - } - closedir($dh); - } - } - - // find permission language files from extensions + // add permission language files from extensions $finder = $phpbb_extension_manager->get_finder(); - $ext_lang_files = $finder + $lang_files = $finder ->prefix('permissions_') ->suffix(".$phpEx") + ->core_path('language/' . $user->lang_name . '/') ->extension_directory('/language/' . $user->lang_name) ->find(); - foreach ($ext_lang_files as $lang_file => $ext_name) + foreach ($lang_files as $lang_file => $ext_name) { - $user->add_lang_ext($ext_name, $lang_file); + if ($ext_name === '/') + { + $user->add_lang($lang_file); + } + else + { + $user->add_lang_ext($ext_name, $lang_file); + } } - - if (!sizeof($files_to_add)) - { - return false; - } - - $user->add_lang($files_to_add); - return true; } /** From 48e1be58db26ba09f7421082038f1f6353a38e91 Mon Sep 17 00:00:00 2001 From: OpenShift guest Date: Sat, 11 May 2013 02:38:40 -0400 Subject: [PATCH 8/8] [ticket/11458] Update functional test Show that the phpbb permission lanuage file is being included PHPBB3-11458 --- tests/functional/extension_permission_lang_test.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/functional/extension_permission_lang_test.php b/tests/functional/extension_permission_lang_test.php index 6f1048279a..26ec4d28a1 100644 --- a/tests/functional/extension_permission_lang_test.php +++ b/tests/functional/extension_permission_lang_test.php @@ -108,6 +108,11 @@ class phpbb_functional_extension_permission_lang_test extends phpbb_functional_t $form->setValues($data); $crawler = $this->client->submit($form); $this->assert_response_success(); + + // language from language/en/acp/permissions_phpbb.php + $this->assertContains('Can attach files', $crawler->filter('body')->text()); + + // language from ext/foo/bar/language/en/permissions_foo.php $this->assertContains('Can view foo', $crawler->filter('body')->text()); } }