Skip to content
Snippets Groups Projects
Unverified Commit 02246aa7 authored by Jaime Pérez Crespo's avatar Jaime Pérez Crespo
Browse files

Add a test for SimpleSAML\Utils\Attributes::getAttributeNamespace()

Also make the default namespace parameter mandatory, so that the function is not ADFS-specific.
parent 1a9369d8
No related branches found
No related tags found
No related merge requests found
......@@ -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));
}
}
......@@ -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')
);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment