diff --git a/modules/core/docs/authproc_attributealter.txt b/modules/core/docs/authproc_attributealter.txt index e8eec7ac9212bc3a714f5f2729545a18297ee461..b9010691d27fd9aefaf9a8e588631e3d82bcc3ea 100644 --- a/modules/core/docs/authproc_attributealter.txt +++ b/modules/core/docs/authproc_attributealter.txt @@ -90,6 +90,17 @@ Get the domain of the email and put it in a separate attribute: '%replace', ), +Defaulting an attribute to one value (Add it with the default before altering) unless another attribute meets a condition: + + 10 => array ('class' => 'core:AttributeAdd', + 'myAttribute' => 'default-value'), + 11 => array ('class' => 'core:AttributeAlter', + 'subject' => 'entitlement', + 'pattern' => '/faculty/', + 'target' => 'myAttribute', + '%replace', + ), + Remove internal, private values from eduPersonEntitlement: 10 => array( @@ -117,4 +128,4 @@ Set a value to be NULL (which will be sent as a NULL value): 'pattern' => '/NULL/', 'replacement' => null, '%replace', - ), \ No newline at end of file + ), diff --git a/tests/modules/core/lib/Auth/Process/AttributeAlterTest.php b/tests/modules/core/lib/Auth/Process/AttributeAlterTest.php new file mode 100644 index 0000000000000000000000000000000000000000..16f261536e61a9b8cef6684ae20f35a652ec8612 --- /dev/null +++ b/tests/modules/core/lib/Auth/Process/AttributeAlterTest.php @@ -0,0 +1,93 @@ +<?php + +/** + * Test for the core:AttributeAlter filter. + */ +class Test_Core_Auth_Process_AttributeAlter extends PHPUnit_Framework_TestCase +{ + + /** + * Helper function to run the filter with a given configuration. + * + * @param array $config The filter configuration. + * @param array $request The request state. + * @return array The state array after processing. + */ + private static function processFilter(array $config, array $request) + { + $filter = new sspmod_core_Auth_Process_AttributeAlter($config, NULL); + $filter->process($request); + return $request; + } + + /** + * Test the most basic functionality. + */ + public function testBasic() + { + $config = array( + 'subject' => 'test', + 'pattern' => '/wrong/', + 'replacement' => 'right', + ); + + $request = array( + 'Attributes' => array( + 'test' => array('wrong'), + ), + ); + + $result = self::processFilter($config, $request); + $attributes = $result['Attributes']; + $this->assertArrayHasKey('test', $attributes); + $this->assertEquals($attributes['test'], array('right')); + } + + /** + * Test replacing attribute value. + */ + public function testReplaceMatch() + { + $config = array( + 'subject' => 'source', + 'pattern' => '/wrong/', + 'replacement' => 'right', + 'target' => 'test', + '%replace', + ); + $request = array( + 'Attributes' => array( + 'source' => array('wrong'), + 'test' => array('wrong'), + ), + ); + $result = self::processFilter($config, $request); + $attributes = $result['Attributes']; + $this->assertEquals($attributes['test'], array('right')); + } + + /** + * Test replacing attribute values. + */ + public function testReplaceNoMatch() + { + $config = array( + 'subject' => 'test', + 'pattern' => '/doink/', + 'replacement' => 'wrong', + 'target' => 'test', + '%replace', + ); + $request = array( + 'Attributes' => array( + 'source' => array('wrong'), + 'test' => array('right'), + ), + ); + $result = self::processFilter($config, $request); + $attributes = $result['Attributes']; + $this->assertEquals($attributes['test'], array('right')); + } + +} +