Skip to content
Snippets Groups Projects
Commit 98c63208 authored by Jaime Perez Crespo's avatar Jaime Perez Crespo
Browse files

Reformat the AttributeValueMap class and its test to comply with our coding standards.

parent f00b5f63
No related branches found
No related tags found
No related merge requests found
...@@ -4,9 +4,10 @@ ...@@ -4,9 +4,10 @@
* Filter to create target attribute based on value(s) in source attribute * Filter to create target attribute based on value(s) in source attribute
* *
* @author Martin van Es, m7 * @author Martin van Es, m7
* @package simpleSAMLphp * @package SimpleSAMLphp
*/ */
class sspmod_core_Auth_Process_AttributeValueMap extends SimpleSAML_Auth_ProcessingFilter { class sspmod_core_Auth_Process_AttributeValueMap extends SimpleSAML_Auth_ProcessingFilter
{
/** /**
* The attributename we should assign values to (ie target) * The attributename we should assign values to (ie target)
...@@ -24,27 +25,29 @@ class sspmod_core_Auth_Process_AttributeValueMap extends SimpleSAML_Auth_Process ...@@ -24,27 +25,29 @@ class sspmod_core_Auth_Process_AttributeValueMap extends SimpleSAML_Auth_Process
private $values = array(); private $values = array();
/** /**
* Wether $sourceattribute should be kept * Whether $sourceattribute should be kept
*/ */
private $keep = false; private $keep = false;
/** /**
* Wether $target attribute values should be replaced by new values * Whether $target attribute values should be replaced by new values
*/ */
private $replace = false; private $replace = false;
/** /**
* Initialize this filter. * Initialize this filter.
* *
* @param array $config Configuration information about this filter. * @param array $config Configuration information about this filter.
* @param mixed $reserved For future use. * @param mixed $reserved For future use.
* @throws SimpleSAML_Error_Exception If the configuration is not valid.
*/ */
public function __construct($config, $reserved) { public function __construct($config, $reserved)
{
parent::__construct($config, $reserved); parent::__construct($config, $reserved);
assert('is_array($config)'); assert('is_array($config)');
/* Validate configuration. */ // validate configuration
foreach ($config as $name => $value) { foreach ($config as $name => $value) {
if (is_int($name)) { if (is_int($name)) {
// check if this is an option // check if this is an option
...@@ -58,17 +61,17 @@ class sspmod_core_Auth_Process_AttributeValueMap extends SimpleSAML_Auth_Process ...@@ -58,17 +61,17 @@ class sspmod_core_Auth_Process_AttributeValueMap extends SimpleSAML_Auth_Process
continue; continue;
} }
// Set targetattribute // set targetattribute
if ($name === 'targetattribute') { if ($name === 'targetattribute') {
$this->targetattribute = $value; $this->targetattribute = $value;
} }
// Set sourceattribute // set sourceattribute
if ($name === 'sourceattribute') { if ($name === 'sourceattribute') {
$this->sourceattribute = $value; $this->sourceattribute = $value;
} }
// Set values // set values
if ($name === 'values') { if ($name === 'values') {
$this->values = $value; $this->values = $value;
} }
...@@ -77,11 +80,12 @@ class sspmod_core_Auth_Process_AttributeValueMap extends SimpleSAML_Auth_Process ...@@ -77,11 +80,12 @@ class sspmod_core_Auth_Process_AttributeValueMap extends SimpleSAML_Auth_Process
/** /**
* Apply filter to add groups attribute. * Apply filter.
* *
* @param array &$request The current request * @param array &$request The current request
*/ */
public function process(&$request) { public function process(&$request)
{
SimpleSAML_Logger::debug('AttributeValueMap - process'); SimpleSAML_Logger::debug('AttributeValueMap - process');
assert('is_array($request)'); assert('is_array($request)');
...@@ -109,7 +113,10 @@ class sspmod_core_Auth_Process_AttributeValueMap extends SimpleSAML_Auth_Process ...@@ -109,7 +113,10 @@ class sspmod_core_Auth_Process_AttributeValueMap extends SimpleSAML_Auth_Process
if ($this->replace or !@is_array($attributes[$this->targetattribute])) { if ($this->replace or !@is_array($attributes[$this->targetattribute])) {
$attributes[$this->targetattribute] = $targetvalues; $attributes[$this->targetattribute] = $targetvalues;
} else { } else {
$attributes[$this->targetattribute] = array_unique(array_merge($attributes[$this->targetattribute], $targetvalues)); $attributes[$this->targetattribute] = array_unique(array_merge(
$attributes[$this->targetattribute],
$targetvalues
));
} }
} }
......
...@@ -13,16 +13,19 @@ class Test_Core_Auth_Process_AttributeValueMap extends PHPUnit_Framework_TestCas ...@@ -13,16 +13,19 @@ class Test_Core_Auth_Process_AttributeValueMap extends PHPUnit_Framework_TestCas
* @param array $request The request state. * @param array $request The request state.
* @return array The state array after processing. * @return array The state array after processing.
*/ */
private static function processFilter(array $config, array $request) { private static function processFilter(array $config, array $request)
{
$filter = new sspmod_core_Auth_Process_AttributeValueMap($config, null); $filter = new sspmod_core_Auth_Process_AttributeValueMap($config, null);
$filter->process($request); $filter->process($request);
return $request; return $request;
} }
/** /**
* Test the most basic functionality. * Test the most basic functionality.
*/ */
public function testBasic() { public function testBasic()
{
$config = array( $config = array(
'sourceattribute' => 'memberOf', 'sourceattribute' => 'memberOf',
'targetattribute' => 'eduPersonAffiliation', 'targetattribute' => 'eduPersonAffiliation',
...@@ -45,10 +48,12 @@ class Test_Core_Auth_Process_AttributeValueMap extends PHPUnit_Framework_TestCas ...@@ -45,10 +48,12 @@ class Test_Core_Auth_Process_AttributeValueMap extends PHPUnit_Framework_TestCas
$this->assertEquals($attributes['eduPersonAffiliation'], array('member')); $this->assertEquals($attributes['eduPersonAffiliation'], array('member'));
} }
/** /**
* Test basic functionality, remove duplicates * Test basic functionality, remove duplicates
*/ */
public function testNoDuplicates() { public function testNoDuplicates()
{
$config = array( $config = array(
'sourceattribute' => 'memberOf', 'sourceattribute' => 'memberOf',
'targetattribute' => 'eduPersonAffiliation', 'targetattribute' => 'eduPersonAffiliation',
...@@ -72,10 +77,12 @@ class Test_Core_Auth_Process_AttributeValueMap extends PHPUnit_Framework_TestCas ...@@ -72,10 +77,12 @@ class Test_Core_Auth_Process_AttributeValueMap extends PHPUnit_Framework_TestCas
$this->assertEquals($attributes['eduPersonAffiliation'], array('member', 'someValue')); $this->assertEquals($attributes['eduPersonAffiliation'], array('member', 'someValue'));
} }
/** /**
* Test the %replace functionality. * Test the %replace functionality.
*/ */
public function testReplace() { public function testReplace()
{
$config = array( $config = array(
'sourceattribute' => 'memberOf', 'sourceattribute' => 'memberOf',
'targetattribute' => 'eduPersonAffiliation', 'targetattribute' => 'eduPersonAffiliation',
...@@ -90,7 +97,7 @@ class Test_Core_Auth_Process_AttributeValueMap extends PHPUnit_Framework_TestCas ...@@ -90,7 +97,7 @@ class Test_Core_Auth_Process_AttributeValueMap extends PHPUnit_Framework_TestCas
$request = array( $request = array(
'Attributes' => array( 'Attributes' => array(
'memberOf' => array('theGroup'), 'memberOf' => array('theGroup'),
'eduPersonAffiliation' => array('someValue'), 'eduPersonAffiliation' => array('someValue'),
), ),
); );
$result = self::processFilter($config, $request); $result = self::processFilter($config, $request);
...@@ -100,10 +107,12 @@ class Test_Core_Auth_Process_AttributeValueMap extends PHPUnit_Framework_TestCas ...@@ -100,10 +107,12 @@ class Test_Core_Auth_Process_AttributeValueMap extends PHPUnit_Framework_TestCas
$this->assertEquals($attributes['eduPersonAffiliation'], array('member')); $this->assertEquals($attributes['eduPersonAffiliation'], array('member'));
} }
/** /**
* Test the %keep functionality. * Test the %keep functionality.
*/ */
public function testKeep() { public function testKeep()
{
$config = array( $config = array(
'sourceattribute' => 'memberOf', 'sourceattribute' => 'memberOf',
'targetattribute' => 'eduPersonAffiliation', 'targetattribute' => 'eduPersonAffiliation',
...@@ -118,7 +127,7 @@ class Test_Core_Auth_Process_AttributeValueMap extends PHPUnit_Framework_TestCas ...@@ -118,7 +127,7 @@ class Test_Core_Auth_Process_AttributeValueMap extends PHPUnit_Framework_TestCas
$request = array( $request = array(
'Attributes' => array( 'Attributes' => array(
'memberOf' => array('theGroup'), 'memberOf' => array('theGroup'),
'eduPersonAffiliation' => array('someValue'), 'eduPersonAffiliation' => array('someValue'),
), ),
); );
$result = self::processFilter($config, $request); $result = self::processFilter($config, $request);
...@@ -128,12 +137,14 @@ class Test_Core_Auth_Process_AttributeValueMap extends PHPUnit_Framework_TestCas ...@@ -128,12 +137,14 @@ class Test_Core_Auth_Process_AttributeValueMap extends PHPUnit_Framework_TestCas
$this->assertEquals($attributes['eduPersonAffiliation'], array('someValue','member')); $this->assertEquals($attributes['eduPersonAffiliation'], array('someValue','member'));
} }
/** /**
* Test unknown flag Exception * Test unknown flag Exception
* *
* @expectedException Exception * @expectedException Exception
*/ */
public function testUnknownFlag() { public function testUnknownFlag()
{
$config = array( $config = array(
'%test', '%test',
'values' => array( 'values' => array(
...@@ -147,15 +158,17 @@ class Test_Core_Auth_Process_AttributeValueMap extends PHPUnit_Framework_TestCas ...@@ -147,15 +158,17 @@ class Test_Core_Auth_Process_AttributeValueMap extends PHPUnit_Framework_TestCas
'memberOf' => array('theGroup'), 'memberOf' => array('theGroup'),
), ),
); );
$result = self::processFilter($config, $request); self::processFilter($config, $request);
} }
/** /**
* Test missing Source attribute * Test missing Source attribute
* *
* @expectedException Exception * @expectedException Exception
*/ */
public function testMissingSourceAttribute() { public function testMissingSourceAttribute()
{
$config = array( $config = array(
'targetattribute' => 'affiliation', 'targetattribute' => 'affiliation',
'values' => array( 'values' => array(
...@@ -169,15 +182,17 @@ class Test_Core_Auth_Process_AttributeValueMap extends PHPUnit_Framework_TestCas ...@@ -169,15 +182,17 @@ class Test_Core_Auth_Process_AttributeValueMap extends PHPUnit_Framework_TestCas
'memberOf' => array('theGroup'), 'memberOf' => array('theGroup'),
), ),
); );
$result = self::processFilter($config, $request); self::processFilter($config, $request);
} }
/** /**
* Test missing Target attribute * Test missing Target attribute
* *
* @expectedException Exception * @expectedException Exception
*/ */
public function testMissingTargetAttribute() { public function testMissingTargetAttribute()
{
$config = array( $config = array(
'sourceattribute' => 'memberOf', 'sourceattribute' => 'memberOf',
'values' => array( 'values' => array(
...@@ -191,6 +206,6 @@ class Test_Core_Auth_Process_AttributeValueMap extends PHPUnit_Framework_TestCas ...@@ -191,6 +206,6 @@ class Test_Core_Auth_Process_AttributeValueMap extends PHPUnit_Framework_TestCas
'memberOf' => array('theGroup'), 'memberOf' => array('theGroup'),
), ),
); );
$result = self::processFilter($config, $request); self::processFilter($config, $request);
} }
} }
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