From 02246aa71b7eb0de2ea195705ba5a3c00ec5cd37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Pe=CC=81rez=20Crespo?= <jaime.perez@uninett.no> Date: Fri, 1 Sep 2017 14:18:29 +0200 Subject: [PATCH] Add a test for SimpleSAML\Utils\Attributes::getAttributeNamespace() Also make the default namespace parameter mandatory, so that the function is not ADFS-specific. --- lib/SimpleSAML/Utils/Attributes.php | 12 +++++------- tests/lib/SimpleSAML/Utils/AttributesTest.php | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/SimpleSAML/Utils/Attributes.php b/lib/SimpleSAML/Utils/Attributes.php index 4a138f682..00e934f5e 100644 --- a/lib/SimpleSAML/Utils/Attributes.php +++ b/lib/SimpleSAML/Utils/Attributes.php @@ -111,24 +111,22 @@ class Attributes /** * Extract an attribute's namespace, or revert to default. * - * This function takes in a namespaced attribute name at splits it in a namespace/attribute name tuple. + * This function takes in a namespaced attribute name and splits it in a namespace/attribute name tuple. * When no namespace is found in the attribute name, it will be namespaced with the default namespace. * This default namespace can be overriden by supplying a second parameter to this function. * * @param string $name The namespaced attribute name. - * @param string $namespace The default namespace that should be used when no namespace is found (optional). + * @param string $defaultns The default namespace that should be used when no namespace is found. * * @return array The attribute name, split to the namespace and the actual attribute name. */ - public static function getAttributeNamespace($name, $namespace = 'http://schemas.xmlsoap.org/claims') + public static function getAttributeNamespace($name, $defaultns) { $slash = strrpos($name, '/'); if ($slash !== false) { - $namespace = substr($name, 0, $slash); + $defaultns = substr($name, 0, $slash); $name = substr($name, $slash + 1); } - $name = htmlspecialchars($name); - $namespace = htmlspecialchars($namespace); - return array($namespace, $name); + return array(htmlspecialchars($defaultns), htmlspecialchars($name)); } } diff --git a/tests/lib/SimpleSAML/Utils/AttributesTest.php b/tests/lib/SimpleSAML/Utils/AttributesTest.php index 41bf895fe..0dc083314 100644 --- a/tests/lib/SimpleSAML/Utils/AttributesTest.php +++ b/tests/lib/SimpleSAML/Utils/AttributesTest.php @@ -193,4 +193,23 @@ class AttributesTest extends \PHPUnit_Framework_TestCase 'Attribute array normalization failed' ); } + + + /** + * Test the getAttributeNamespace() function. + */ + public function testNamespacedAttributes() + { + // test for only the name + $this->assertEquals( + array('default', 'name'), + Attributes::getAttributeNamespace('name', 'default') + ); + + // test for a given namespace and multiple '/' + $this->assertEquals( + array('some/namespace', 'name'), + Attributes::getAttributeNamespace('some/namespace/name', 'default') + ); + } } -- GitLab