From 3a6e150716ddc2aa72faf20cd36f3cf92e04fdfc Mon Sep 17 00:00:00 2001
From: Thijs Kinkhorst <thijs@kinkhorst.com>
Date: Tue, 26 May 2015 15:09:25 +0000
Subject: [PATCH] Add tests for ScopeFromAttribute filter.

---
 .../Auth/Process/ScopeFromAttributeTest.php   | 122 ++++++++++++++++++
 1 file changed, 122 insertions(+)
 create mode 100644 tests/modules/core/lib/Auth/Process/ScopeFromAttributeTest.php

diff --git a/tests/modules/core/lib/Auth/Process/ScopeFromAttributeTest.php b/tests/modules/core/lib/Auth/Process/ScopeFromAttributeTest.php
new file mode 100644
index 000000000..955d908c1
--- /dev/null
+++ b/tests/modules/core/lib/Auth/Process/ScopeFromAttributeTest.php
@@ -0,0 +1,122 @@
+<?php
+
+/**
+ * Test for the core:ScopeFromAttribute filter.
+ */
+class Test_Core_Auth_Process_ScopeFromAttribute 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_ScopeFromAttribute($config, NULL);
+        $filter->process($request);
+        return $request;
+    }
+
+    /*
+     * Test the most basic functionality.
+     */
+    public function testBasic()
+    {
+        $config = array(
+            'sourceAttribute' => 'eduPersonPrincipalName',
+            'targetAttribute' => 'scope',
+        );
+        $request = array(
+            'Attributes' => array(
+                'eduPersonPrincipalName' => array('jdoe@example.com'),
+            )
+        );
+        $result = self::processFilter($config, $request);
+        $attributes = $result['Attributes'];
+        $this->assertArrayHasKey('scope', $attributes);
+        $this->assertEquals($attributes['scope'], array('example.com'));
+    }
+
+    /*
+     * If scope already set, module must not overwrite.
+     */
+    public function testNoOverwrite()
+    {
+        $config = array(
+            'sourceAttribute' => 'eduPersonPrincipalName',
+            'targetAttribute' => 'scope',
+        );
+        $request = array(
+            'Attributes' => array(
+                'eduPersonPrincipalName' => array('jdoe@example.com'),
+                'scope' => array('example.edu')
+            )
+        );
+        $result = self::processFilter($config, $request);
+        $attributes = $result['Attributes'];
+        $this->assertEquals($attributes['scope'], array('example.edu'));
+    }
+
+    /*
+     * If source attribute not set, nothing happens
+     */
+    public function testNoSourceAttribute()
+    {
+        $config = array(
+            'sourceAttribute' => 'eduPersonPrincipalName',
+            'targetAttribute' => 'scope',
+        );
+        $request = array(
+            'Attributes' => array(
+                'mail' => array('j.doe@example.edu', 'john@example.org'),
+                'scope' => array('example.edu')
+            )
+        );
+        $result = self::processFilter($config, $request);
+        $this->assertEquals($request['Attributes'], $result['Attributes']);
+    }
+
+    /*
+     * When multiple @ signs in attribute, should use last one.
+     */
+    public function testMultiAt()
+    {
+        $config = array(
+            'sourceAttribute' => 'eduPersonPrincipalName',
+            'targetAttribute' => 'scope',
+        );
+        $request = array(
+            'Attributes' => array(
+                'eduPersonPrincipalName' => array('john@doe@example.com'),
+            )
+        );
+        $result = self::processFilter($config, $request);
+        $attributes = $result['Attributes'];
+        $this->assertEquals($attributes['scope'], array('example.com'));
+    }
+
+    /*
+     * When the source attribute doesn't have a scope, a warning is emitted
+     * NOTE: currently disabled: this triggers a warning and a warning
+     * wants to start a session which we cannot do in phpunit. How to fix?
+     */
+/*    public function testNoAt()
+    {
+        $config = array(
+            'sourceAttribute' => 'eduPersonPrincipalName',
+            'targetAttribute' => 'scope',
+        );
+        $request = array(
+            'Attributes' => array(
+                'eduPersonPrincipalName' => array('johndoe'),
+            )
+        );
+        $result = self::processFilter($config, $request);
+        $attributes = $result['Attributes'];
+
+        $this->assertArrayNotHasKey('scope', $attributes);
+    } */
+}
-- 
GitLab