From fc7a0aee8f5517d4c84e4339b098ce9fd2201d46 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 24 May 2013 23:57:12 -0400 Subject: [PATCH] CRM-12499 - Add CRM_Utils_String::parsePrefix ---------------------------------------- * CRM-12499: Allow users with 'access user profiles' to access $userRecordUrl http://issues.civicrm.org/jira/browse/CRM-12499 --- CRM/Utils/String.php | 19 +++++++++++++++++++ tests/phpunit/CRM/Utils/StringTest.php | 17 +++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/CRM/Utils/String.php b/CRM/Utils/String.php index 9289125691..1e8cde11a9 100644 --- a/CRM/Utils/String.php +++ b/CRM/Utils/String.php @@ -631,5 +631,24 @@ class CRM_Utils_String { return $result; } + /** + * Examples: + * "admin foo" => array(NULL,"admin foo") + * "cms:admin foo" => array("cms", "admin foo") + * + * @param string $string e.g. "view all contacts". Syntax: "[prefix:]name" + * @return array (0 => string|NULL $prefix, 1 => string $value) + */ + public static function parsePrefix($delim, $string, $defaultPrefix = NULL) { + $pos = strpos($string, $delim); + if ($pos === FALSE) { + return array($defaultPrefix, $string); + } + else { + return array(substr($string, 0, $pos), substr($string, 1+$pos)); + } + } + + } diff --git a/tests/phpunit/CRM/Utils/StringTest.php b/tests/phpunit/CRM/Utils/StringTest.php index 8096603c30..b330713b4c 100644 --- a/tests/phpunit/CRM/Utils/StringTest.php +++ b/tests/phpunit/CRM/Utils/StringTest.php @@ -104,5 +104,22 @@ class CRM_Utils_StringTest extends CiviUnitTestCase { $this->assertRegExp('/^[12345678]+$/', $actual); } } + + public function parsePrefixData() { + $cases = array(); + $cases[] = array('administer CiviCRM', NULL, array(NULL, 'administer CiviCRM')); + $cases[] = array('administer CiviCRM', 'com_civicrm', array('com_civicrm', 'administer CiviCRM')); + $cases[] = array('Drupal:access user profiles', NULL, array('Drupal', 'access user profiles')); + $cases[] = array('Joomla:component:perm', NULL, array('Joomla', 'component:perm')); + return $cases; + } + + /** + * @dataProvider parsePrefixData + */ + public function testParsePrefix($input, $defaultPrefix, $expected) { + $actual = CRM_Utils_String::parsePrefix(':', $input, $defaultPrefix); + $this->assertEquals($expected, $actual); + } } -- 2.25.1