From 2d06c575a95f5d966d79c5525b28bf71174899c4 Mon Sep 17 00:00:00 2001
From: Thijs Kinkhorst <thijs@kinkhorst.com>
Date: Tue, 29 Sep 2015 14:56:49 +0000
Subject: [PATCH] Add more tests.

---
 .../lib/Auth/Process/AttributeAlterTest.php   | 268 +++++++++++++++++-
 1 file changed, 266 insertions(+), 2 deletions(-)

diff --git a/tests/modules/core/lib/Auth/Process/AttributeAlterTest.php b/tests/modules/core/lib/Auth/Process/AttributeAlterTest.php
index 16f261536..47a16e5a4 100644
--- a/tests/modules/core/lib/Auth/Process/AttributeAlterTest.php
+++ b/tests/modules/core/lib/Auth/Process/AttributeAlterTest.php
@@ -33,20 +33,92 @@ class Test_Core_Auth_Process_AttributeAlter extends PHPUnit_Framework_TestCase
 
         $request = array(
             'Attributes' => array(
-                 'test' => array('wrong'),
+                 'test' => array('somethingiswrong'),
              ),
         );
 
         $result = self::processFilter($config, $request);
         $attributes = $result['Attributes'];
         $this->assertArrayHasKey('test', $attributes);
-        $this->assertEquals($attributes['test'], array('right'));
+        $this->assertEquals($attributes['test'], array('somethingisright'));
+    }
+
+    /**
+     * Test the most basic functionality.
+     */
+    public function testWithTarget()
+    {
+        $config = array(
+            'subject' => 'test',
+            'target' => 'test2',
+            'pattern' => '/wrong/',
+            'replacement' => 'right',
+        );
+
+        $request = array(
+            'Attributes' => array(
+                 'something' => array('somethingelse'),
+                 'test' => array('wrong'),
+             ),
+        );
+
+        $result = self::processFilter($config, $request);
+        $attributes = $result['Attributes'];
+        $this->assertArrayHasKey('test2', $attributes);
+        $this->assertEquals($attributes['test'], array('wrong'));
+        $this->assertEquals($attributes['test2'], array('right'));
+    }
+
+    /**
+     * Module is a no op if subject attribute is not present.
+     */
+    public function testNomatch()
+    {
+        $config = array(
+            'subject' => 'test',
+            'pattern' => '/wrong/',
+            'replacement' => 'right',
+        );
+
+        $request = array(
+            'Attributes' => array(
+                 'something' => array('somevalue'),
+                 'somethingelse' => array('someothervalue'),
+             ),
+        );
+
+        $result = self::processFilter($config, $request);
+        $attributes = $result['Attributes'];
+        $this->assertEquals($attributes,
+            array('something' => array('somevalue'),
+            'somethingelse' => array('someothervalue')));
     }
 
     /**
      * Test replacing attribute value.
      */
     public function testReplaceMatch()
+    {
+        $config = array(
+            'subject' => 'source',
+            'pattern' => '/wrong/',
+            'replacement' => 'right',
+            '%replace',
+        );
+        $request = array(
+            'Attributes' => array(
+                'source' => array('wrongthing'),
+            ),
+        );
+        $result = self::processFilter($config, $request);
+        $attributes = $result['Attributes'];
+        $this->assertEquals($attributes['source'], array('right'));
+    }
+
+    /**
+     * Test replacing attribute value.
+     */
+    public function testReplaceMatchWithTarget()
     {
         $config = array(
             'subject' => 'source',
@@ -89,5 +161,197 @@ class Test_Core_Auth_Process_AttributeAlter extends PHPUnit_Framework_TestCase
         $this->assertEquals($attributes['test'], array('right'));
     }
 
+    /**
+     * Test removing attribute values.
+     * Note that removing a value does not renumber the attributes array.
+     * Also ensure unrelated attributes are not touched.
+     */
+    public function testRemoveMatch()
+    {
+        $config = array(
+            'subject' => 'eduPersonAffiliation',
+            'pattern' => '/^emper/',
+            '%remove',
+        );
+        $request = array(
+            'Attributes' => array(
+                'displayName' => array('emperor kuzco'),
+                'eduPersonAffiliation' => array('member', 'emperor', 'staff'),
+            ),
+        );
+        $result = self::processFilter($config, $request);
+        $attributes = $result['Attributes'];
+        $this->assertEquals($attributes['displayName'], array('emperor kuzco'));
+        $this->assertEquals($attributes['eduPersonAffiliation'], array(0 => 'member', 2 => 'staff'));
+    }
+
+    /**
+     * Test removing attribute values, resulting in an empty attribute.
+     */
+    public function testRemoveMatchAll()
+    {
+        $config = array(
+            'subject' => 'eduPersonAffiliation',
+            'pattern' => '/^emper/',
+            '%remove',
+        );
+        $request = array(
+            'Attributes' => array(
+                'displayName' => array('emperor kuzco'),
+                'eduPersonAffiliation' => array('emperess', 'emperor'),
+            ),
+        );
+        $result = self::processFilter($config, $request);
+        $attributes = $result['Attributes'];
+        $this->assertArrayNotHasKey('eduPersonAffiliation', $attributes);
+    }
+
+    /**
+     * Test for exception with illegal config.
+     *
+     * @expectedException Exception
+     */
+    public function testWrongConfig()
+    {
+        $config = array(
+            'subject' => 'eduPersonAffiliation',
+            'pattern' => '/^emper/',
+            '%dwiw',
+        );
+        $request = array(
+            'Attributes' => array(
+                'eduPersonAffiliation' => array('emperess', 'emperor'),
+            ),
+        );
+        $result = self::processFilter($config, $request);
+    }
+
+    /**
+     * Test for exception with illegal config.
+     *
+     * @expectedException Exception
+     */
+    public function testIncompleteConfig()
+    {
+        $config = array(
+            'subject' => 'eduPersonAffiliation',
+        );
+        $request = array(
+            'Attributes' => array(
+                'eduPersonAffiliation' => array('emperess', 'emperor'),
+            ),
+        );
+        $result = self::processFilter($config, $request);
+    }
+
+    /**
+     * Test for exception with illegal config.
+     *
+     * @expectedException Exception
+     */
+    public function testIncompleteConfig2()
+    {
+        $config = array(
+            'subject' => 'test',
+            'pattern' => '/wrong/',
+        );
+
+        $request = array(
+            'Attributes' => array(
+                 'test' => array('somethingiswrong'),
+             ),
+        );
+
+        $request = array(
+            'Attributes' => array(
+                'eduPersonAffiliation' => array('emperess', 'emperor'),
+            ),
+        );
+        $result = self::processFilter($config, $request);
+    }
+
+    /**
+     * Test for exception with illegal config.
+     *
+     * @expectedException Exception
+     */
+    public function testIncompleteConfig3()
+    {
+        $config = array(
+            'subject' => 'test',
+            'pattern' => '/wrong/',
+            '%replace',
+            '%remove',
+        );
+
+        $request = array(
+            'Attributes' => array(
+                 'test' => array('somethingiswrong'),
+             ),
+        );
+
+        $request = array(
+            'Attributes' => array(
+                'eduPersonAffiliation' => array('emperess', 'emperor'),
+            ),
+        );
+        $result = self::processFilter($config, $request);
+    }
+
+    /**
+     * Test for exception with illegal config.
+     *
+     * @expectedException Exception
+     */
+    public function testIncompleteConfig4()
+    {
+        $config = array(
+            'subject' => 'test',
+            'pattern' => '/wrong/',
+            'target' => 'test2',
+            '%remove',
+        );
+
+        $request = array(
+            'Attributes' => array(
+                 'test' => array('somethingiswrong'),
+             ),
+        );
+
+        $request = array(
+            'Attributes' => array(
+                'eduPersonAffiliation' => array('emperess', 'emperor'),
+            ),
+        );
+        $result = self::processFilter($config, $request);
+    }
+
+
+    /**
+     * Test for exception with illegal config.
+     *
+     * @expectedException Exception
+     */
+    public function testIncompleteConfig5()
+    {
+        $config = array(
+            'subject' => 'test',
+            'pattern' => '/wrong/',
+            'replacement' => null,
+        );
+
+        $request = array(
+            'Attributes' => array(
+                 'test' => array('somethingiswrong'),
+             ),
+        );
+
+        $request = array(
+            'Attributes' => array(
+                'eduPersonAffiliation' => array('emperess', 'emperor'),
+            ),
+        );
+        $result = self::processFilter($config, $request);
+    }
 }
 
-- 
GitLab