From 9047384a6932d94416c55cf85bd483a11086a468 Mon Sep 17 00:00:00 2001
From: Jeff Krug <jk90@shire.icl.gtri.org>
Date: Fri, 25 Sep 2015 11:10:36 -0400
Subject: [PATCH] Updated documentation for attribute alter to include an
 example of setting an attribute to a default value and then executing a test
 to change it.

Added some very basic tests for the AttributeAlter module.
---
 modules/core/docs/authproc_attributealter.txt | 13 ++-
 .../lib/Auth/Process/AttributeAlterTest.php   | 93 +++++++++++++++++++
 2 files changed, 105 insertions(+), 1 deletion(-)
 create mode 100644 tests/modules/core/lib/Auth/Process/AttributeAlterTest.php

diff --git a/modules/core/docs/authproc_attributealter.txt b/modules/core/docs/authproc_attributealter.txt
index e8eec7ac9..b9010691d 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 000000000..16f261536
--- /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'));
+    }
+
+}
+
-- 
GitLab