From 282e19798689c922b6defc48ad61c2995d339500 Mon Sep 17 00:00:00 2001
From: Jaime Perez Crespo <jaime.perez@uninett.no>
Date: Wed, 15 Apr 2015 20:44:42 +0200
Subject: [PATCH] Reorganize and clean tests.

---
 tests/{Utils => Metadata}/MetadataTest.php    |  13 +-
 tests/Metadata/SAMLBuilderTest.php            | 137 ++++++++++++++++++
 tests/SimpleSAML/Metadata/SAMLBuilderTest.php | 137 ------------------
 tests/Utils/{Arrays.php => ArraysTest.php}    |  16 +-
 tests/Utils/{Net.php => NetTest.php}          |  11 +-
 tools/phpunit/phpunit.xml                     |   8 +-
 6 files changed, 165 insertions(+), 157 deletions(-)
 rename tests/{Utils => Metadata}/MetadataTest.php (97%)
 create mode 100644 tests/Metadata/SAMLBuilderTest.php
 delete mode 100644 tests/SimpleSAML/Metadata/SAMLBuilderTest.php
 rename tests/Utils/{Arrays.php => ArraysTest.php} (91%)
 rename tests/Utils/{Net.php => NetTest.php} (84%)

diff --git a/tests/Utils/MetadataTest.php b/tests/Metadata/MetadataTest.php
similarity index 97%
rename from tests/Utils/MetadataTest.php
rename to tests/Metadata/MetadataTest.php
index 12eafa862..81e039180 100644
--- a/tests/Utils/MetadataTest.php
+++ b/tests/Metadata/MetadataTest.php
@@ -1,8 +1,9 @@
 <?php
+
+
 /**
- * Class Utils_MetadataTest
+ * Tests related to SAML metadata.
  */
-
 class Utils_MetadataTest extends PHPUnit_Framework_TestCase
 {
 
@@ -45,7 +46,7 @@ class Utils_MetadataTest extends PHPUnit_Framework_TestCase
         // test basic name parsing
         $contact = array(
             'contactType' => 'technical',
-            'name' => 'John Doe'
+            'name'        => 'John Doe'
         );
         $parsed = SimpleSAML_Utils_Config_Metadata::getContact($contact);
         $this->assertArrayNotHasKey('name', $parsed);
@@ -57,7 +58,7 @@ class Utils_MetadataTest extends PHPUnit_Framework_TestCase
         // test comma-separated names
         $contact = array(
             'contactType' => 'technical',
-            'name' => 'Doe, John'
+            'name'        => 'Doe, John'
         );
         $parsed = SimpleSAML_Utils_Config_Metadata::getContact($contact);
         $this->assertArrayHasKey('givenName', $parsed);
@@ -68,7 +69,7 @@ class Utils_MetadataTest extends PHPUnit_Framework_TestCase
         // test long names
         $contact = array(
             'contactType' => 'technical',
-            'name' => 'John Fitzgerald Doe Smith'
+            'name'        => 'John Fitzgerald Doe Smith'
         );
         $parsed = SimpleSAML_Utils_Config_Metadata::getContact($contact);
         $this->assertArrayNotHasKey('name', $parsed);
@@ -79,7 +80,7 @@ class Utils_MetadataTest extends PHPUnit_Framework_TestCase
         // test comma-separated long names
         $contact = array(
             'contactType' => 'technical',
-            'name' => 'Doe Smith, John Fitzgerald'
+            'name'        => 'Doe Smith, John Fitzgerald'
         );
         $parsed = SimpleSAML_Utils_Config_Metadata::getContact($contact);
         $this->assertArrayNotHasKey('name', $parsed);
diff --git a/tests/Metadata/SAMLBuilderTest.php b/tests/Metadata/SAMLBuilderTest.php
new file mode 100644
index 000000000..48caa5eaf
--- /dev/null
+++ b/tests/Metadata/SAMLBuilderTest.php
@@ -0,0 +1,137 @@
+<?php
+
+
+/**
+ * Class SimpleSAML_Metadata_SAMLBuilderTest
+ */
+class SimpleSAML_Metadata_SAMLBuilderTest extends PHPUnit_Framework_TestCase
+{
+
+    /**
+     * Test the requeste attributes are valued correctly.
+     */
+    public function testAttributes()
+    {
+        $entityId = 'https://entity.example.com/id';
+
+        //  test SP20 array parsing, no friendly name
+        $set = 'saml20-sp-remote';
+        $metadata = array(
+            'entityid'     => $entityId,
+            'name'         => array('en' => 'Test SP'),
+            'metadata-set' => $set,
+            'attributes'   => array(
+                'urn:oid:1.3.6.1.4.1.5923.1.1.1.10',
+                'urn:oid:1.3.6.1.4.1.5923.1.1.1.6',
+                'urn:oid:0.9.2342.19200300.100.1.3',
+                'urn:oid:2.5.4.3',
+            ),
+        );
+
+        $samlBuilder = new SimpleSAML_Metadata_SAMLBuilder($entityId);
+        $samlBuilder->addMetadata($set, $metadata);
+
+        $spDesc = $samlBuilder->getEntityDescriptor();
+        $acs = $spDesc->getElementsByTagName("AttributeConsumingService");
+        $this->assertEquals(1, $acs->length);
+        $attributes = $acs->item(0)->getElementsByTagName("RequestedAttribute");
+        $this->assertEquals(4, $attributes->length);
+        for ($c = 0; $c < $attributes->length; $c++) {
+            $curAttribute = $attributes->item($c);
+            $this->assertTrue($curAttribute->hasAttribute("Name"));
+            $this->assertFalse($curAttribute->hasAttribute("FriendlyName"));
+            $this->assertEquals($metadata['attributes'][$c], $curAttribute->getAttribute("Name"));
+        }
+
+        // test SP20 array parsing, no friendly name
+        $set = 'saml20-sp-remote';
+        $metadata = array(
+            'entityid'     => $entityId,
+            'name'         => array('en' => 'Test SP'),
+            'metadata-set' => $set,
+            'attributes'   => array(
+                'eduPersonTargetedID'    => 'urn:oid:1.3.6.1.4.1.5923.1.1.1.10',
+                'eduPersonPrincipalName' => 'urn:oid:1.3.6.1.4.1.5923.1.1.1.6',
+                'eduPersonOrgDN'         => 'urn:oid:0.9.2342.19200300.100.1.3',
+                'cn'                     => 'urn:oid:2.5.4.3',
+            ),
+        );
+
+        $samlBuilder = new SimpleSAML_Metadata_SAMLBuilder($entityId);
+        $samlBuilder->addMetadata($set, $metadata);
+
+        $spDesc = $samlBuilder->getEntityDescriptor();
+        $acs = $spDesc->getElementsByTagName("AttributeConsumingService");
+        $this->assertEquals(1, $acs->length);
+        $attributes = $acs->item(0)->getElementsByTagName("RequestedAttribute");
+        $this->assertEquals(4, $attributes->length);
+        $keys = array_keys($metadata['attributes']);
+        for ($c = 0; $c < $attributes->length; $c++) {
+            $curAttribute = $attributes->item($c);
+            $this->assertTrue($curAttribute->hasAttribute("Name"));
+            $this->assertTrue($curAttribute->hasAttribute("FriendlyName"));
+            $this->assertEquals($metadata['attributes'][$keys[$c]], $curAttribute->getAttribute("Name"));
+            $this->assertEquals($keys[$c], $curAttribute->getAttribute("FriendlyName"));
+        }
+
+        //  test SP13 array parsing, no friendly name
+        $set = 'shib13-sp-remote';
+        $metadata = array(
+            'entityid'     => $entityId,
+            'name'         => array('en' => 'Test SP'),
+            'metadata-set' => $set,
+            'attributes'   => array(
+                'urn:oid:1.3.6.1.4.1.5923.1.1.1.10',
+                'urn:oid:1.3.6.1.4.1.5923.1.1.1.6',
+                'urn:oid:0.9.2342.19200300.100.1.3',
+                'urn:oid:2.5.4.3',
+            ),
+        );
+
+        $samlBuilder = new SimpleSAML_Metadata_SAMLBuilder($entityId);
+        $samlBuilder->addMetadata($set, $metadata);
+
+        $spDesc = $samlBuilder->getEntityDescriptor();
+        $acs = $spDesc->getElementsByTagName("AttributeConsumingService");
+        $this->assertEquals(1, $acs->length);
+        $attributes = $acs->item(0)->getElementsByTagName("RequestedAttribute");
+        $this->assertEquals(4, $attributes->length);
+        for ($c = 0; $c < $attributes->length; $c++) {
+            $curAttribute = $attributes->item($c);
+            $this->assertTrue($curAttribute->hasAttribute("Name"));
+            $this->assertFalse($curAttribute->hasAttribute("FriendlyName"));
+            $this->assertEquals($metadata['attributes'][$c], $curAttribute->getAttribute("Name"));
+        }
+
+        // test SP20 array parsing, no friendly name
+        $set = 'shib13-sp-remote';
+        $metadata = array(
+            'entityid'     => $entityId,
+            'name'         => array('en' => 'Test SP'),
+            'metadata-set' => $set,
+            'attributes'   => array(
+                'eduPersonTargetedID'    => 'urn:oid:1.3.6.1.4.1.5923.1.1.1.10',
+                'eduPersonPrincipalName' => 'urn:oid:1.3.6.1.4.1.5923.1.1.1.6',
+                'eduPersonOrgDN'         => 'urn:oid:0.9.2342.19200300.100.1.3',
+                'cn'                     => 'urn:oid:2.5.4.3',
+            ),
+        );
+
+        $samlBuilder = new SimpleSAML_Metadata_SAMLBuilder($entityId);
+        $samlBuilder->addMetadata($set, $metadata);
+
+        $spDesc = $samlBuilder->getEntityDescriptor();
+        $acs = $spDesc->getElementsByTagName("AttributeConsumingService");
+        $this->assertEquals(1, $acs->length);
+        $attributes = $acs->item(0)->getElementsByTagName("RequestedAttribute");
+        $this->assertEquals(4, $attributes->length);
+        $keys = array_keys($metadata['attributes']);
+        for ($c = 0; $c < $attributes->length; $c++) {
+            $curAttribute = $attributes->item($c);
+            $this->assertTrue($curAttribute->hasAttribute("Name"));
+            $this->assertTrue($curAttribute->hasAttribute("FriendlyName"));
+            $this->assertEquals($metadata['attributes'][$keys[$c]], $curAttribute->getAttribute("Name"));
+            $this->assertEquals($keys[$c], $curAttribute->getAttribute("FriendlyName"));
+        }
+    }
+}
diff --git a/tests/SimpleSAML/Metadata/SAMLBuilderTest.php b/tests/SimpleSAML/Metadata/SAMLBuilderTest.php
deleted file mode 100644
index 277a3b60f..000000000
--- a/tests/SimpleSAML/Metadata/SAMLBuilderTest.php
+++ /dev/null
@@ -1,137 +0,0 @@
-<?php
-/**
- * Class SimpleSAML_Metadata_SAMLBuilderTest
- */
-
-class SimpleSAML_Metadata_SAMLBuilderTest extends PHPUnit_Framework_TestCase
-{
-
-    /**
-     * Test the requeste attributes are valued correctly.
-     */
-    public function testAttributes()
-    {
-	$entityId = 'https://entity.examle.com/id';
-
-	//  test SP20 array parsing, no friendly name
-	$set = 'saml20-sp-remote';
-	$metadata = array(
-		'entityid' => $entityId,
-		'name' => array('en' => 'Test SP'),
-		'metadata-set' => $set,
-		'attributes' => array(
-			'urn:oid:1.3.6.1.4.1.5923.1.1.1.10',
-			'urn:oid:1.3.6.1.4.1.5923.1.1.1.6',
-			'urn:oid:0.9.2342.19200300.100.1.3',
-			'urn:oid:2.5.4.3',
-		),
-	);
-
-	$samlBuilder = new SimpleSAML_Metadata_SAMLBuilder($entityId);
-	$samlBuilder->addMetadata($set, $metadata);
-
-	$spDesc = $samlBuilder->getEntityDescriptor();
-	$acs = $spDesc->getElementsByTagName("AttributeConsumingService");
-	$this->assertEquals(1, $acs->length);
-	$attributes = $acs->item(0)->getElementsByTagName("RequestedAttribute");
-	$this->assertEquals(4, $attributes->length);
-	for ($c = 0; $c < $attributes->length; $c++) {
-		$curAttribute = $attributes->item($c);
-		$this->assertTrue($curAttribute->hasAttribute("Name"));
-		$this->assertFalse($curAttribute->hasAttribute("FriendlyName"));
-		$this->assertEquals($metadata['attributes'][$c], $curAttribute->getAttribute("Name"));
-	}
-
-	// test SP20 array parsing, no friendly name
-	$set = 'saml20-sp-remote';
-	$metadata = array(
-		'entityid' => $entityId,
-		'name' => array('en' => 'Test SP'),
-		'metadata-set' => $set,
-		'attributes' => array(
-			'eduPersonTargetedID' => 'urn:oid:1.3.6.1.4.1.5923.1.1.1.10',
-			'eduPersonPrincipalName' => 'urn:oid:1.3.6.1.4.1.5923.1.1.1.6',
-			'eduPersonOrgDN' => 'urn:oid:0.9.2342.19200300.100.1.3',
-			'cn' => 'urn:oid:2.5.4.3',
-		),
-	);
-
-	$samlBuilder = new SimpleSAML_Metadata_SAMLBuilder($entityId);
-	$samlBuilder->addMetadata($set, $metadata);
-
-	$spDesc = $samlBuilder->getEntityDescriptor();
-	$acs = $spDesc->getElementsByTagName("AttributeConsumingService");
-	$this->assertEquals(1, $acs->length);
-	$attributes = $acs->item(0)->getElementsByTagName("RequestedAttribute");
-	$this->assertEquals(4, $attributes->length);
-	$keys = array_keys($metadata['attributes']);
-	for ($c = 0; $c < $attributes->length; $c++) {
-		$curAttribute = $attributes->item($c);
-		$this->assertTrue($curAttribute->hasAttribute("Name"));
-		$this->assertTrue($curAttribute->hasAttribute("FriendlyName"));
-		$this->assertEquals($metadata['attributes'][$keys[$c]], $curAttribute->getAttribute("Name"));
-		$this->assertEquals($keys[$c], $curAttribute->getAttribute("FriendlyName"));
-	}
-
-	//  test SP13 array parsing, no friendly name
-	$set = 'shib13-sp-remote';
-	$metadata = array(
-		'entityid' => $entityId,
-		'name' => array('en' => 'Test SP'),
-		'metadata-set' => $set,
-		'attributes' => array(
-			'urn:oid:1.3.6.1.4.1.5923.1.1.1.10',
-			'urn:oid:1.3.6.1.4.1.5923.1.1.1.6',
-			'urn:oid:0.9.2342.19200300.100.1.3',
-			'urn:oid:2.5.4.3',
-		),
-	);
-
-	$samlBuilder = new SimpleSAML_Metadata_SAMLBuilder($entityId);
-	$samlBuilder->addMetadata($set, $metadata);
-
-	$spDesc = $samlBuilder->getEntityDescriptor();
-	$acs = $spDesc->getElementsByTagName("AttributeConsumingService");
-	$this->assertEquals(1, $acs->length);
-	$attributes = $acs->item(0)->getElementsByTagName("RequestedAttribute");
-	$this->assertEquals(4, $attributes->length);
-	for ($c = 0; $c < $attributes->length; $c++) {
-		$curAttribute = $attributes->item($c);
-		$this->assertTrue($curAttribute->hasAttribute("Name"));
-		$this->assertFalse($curAttribute->hasAttribute("FriendlyName"));
-		$this->assertEquals($metadata['attributes'][$c], $curAttribute->getAttribute("Name"));
-	}
-
-	// test SP20 array parsing, no friendly name
-	$set = 'shib13-sp-remote';
-	$metadata = array(
-		'entityid' => $entityId,
-		'name' => array('en' => 'Test SP'),
-		'metadata-set' => $set,
-		'attributes' => array(
-			'eduPersonTargetedID' => 'urn:oid:1.3.6.1.4.1.5923.1.1.1.10',
-			'eduPersonPrincipalName' => 'urn:oid:1.3.6.1.4.1.5923.1.1.1.6',
-			'eduPersonOrgDN' => 'urn:oid:0.9.2342.19200300.100.1.3',
-			'cn' => 'urn:oid:2.5.4.3',
-		),
-	);
-
-	$samlBuilder = new SimpleSAML_Metadata_SAMLBuilder($entityId);
-	$samlBuilder->addMetadata($set, $metadata);
-
-	$spDesc = $samlBuilder->getEntityDescriptor();
-	$acs = $spDesc->getElementsByTagName("AttributeConsumingService");
-	$this->assertEquals(1, $acs->length);
-	$attributes = $acs->item(0)->getElementsByTagName("RequestedAttribute");
-	$this->assertEquals(4, $attributes->length);
-	$keys = array_keys($metadata['attributes']);
-	for ($c = 0; $c < $attributes->length; $c++) {
-		$curAttribute = $attributes->item($c);
-		$this->assertTrue($curAttribute->hasAttribute("Name"));
-		$this->assertTrue($curAttribute->hasAttribute("FriendlyName"));
-		$this->assertEquals($metadata['attributes'][$keys[$c]], $curAttribute->getAttribute("Name"));
-		$this->assertEquals($keys[$c], $curAttribute->getAttribute("FriendlyName"));
-	}
-    }
-
-}
diff --git a/tests/Utils/Arrays.php b/tests/Utils/ArraysTest.php
similarity index 91%
rename from tests/Utils/Arrays.php
rename to tests/Utils/ArraysTest.php
index f2627fffc..1d4ed91d2 100644
--- a/tests/Utils/Arrays.php
+++ b/tests/Utils/ArraysTest.php
@@ -2,9 +2,9 @@
 
 
 /**
- * Class Utils_Arrays
+ * Tests for SimpleSAML_Utils_Arrays.
  */
-class Utils_Arrays extends PHPUnit_Framework_TestCase
+class Utils_ArraysTest extends PHPUnit_Framework_TestCase
 {
 
     /**
@@ -12,7 +12,8 @@ class Utils_Arrays extends PHPUnit_Framework_TestCase
      *
      * @expectedException SimpleSAML_Error_Exception
      */
-    public function testNormalizeAttributesArrayBadInput() {
+    public function testNormalizeAttributesArrayBadInput()
+    {
         SimpleSAML_Utils_Arrays::normalizeAttributesArray('string');
     }
 
@@ -21,7 +22,8 @@ class Utils_Arrays extends PHPUnit_Framework_TestCase
      *
      * @expectedException SimpleSAML_Error_Exception
      */
-    public function testNormalizeAttributesArrayBadKeys() {
+    public function testNormalizeAttributesArrayBadKeys()
+    {
         SimpleSAML_Utils_Arrays::normalizeAttributesArray(array('attr1' => 'value1', 1 => 'value2'));
     }
 
@@ -30,14 +32,16 @@ class Utils_Arrays extends PHPUnit_Framework_TestCase
      *
      * @expectedException SimpleSAML_Error_Exception
      */
-    public function testNormalizeAttributesArrayBadValues() {
+    public function testNormalizeAttributesArrayBadValues()
+    {
         SimpleSAML_Utils_Arrays::normalizeAttributesArray(array('attr1' => 'value1', 'attr2' => 0));
     }
 
     /**
      * Test the normalizeAttributesArray() function.
      */
-    public function testNormalizeAttributesArray() {
+    public function testNormalizeAttributesArray()
+    {
         $attributes = array(
             'key1' => 'value1',
             'key2' => array('value2', 'value3'),
diff --git a/tests/Utils/Net.php b/tests/Utils/NetTest.php
similarity index 84%
rename from tests/Utils/Net.php
rename to tests/Utils/NetTest.php
index 2d480f741..9d1b47e20 100644
--- a/tests/Utils/Net.php
+++ b/tests/Utils/NetTest.php
@@ -1,9 +1,10 @@
 <?php
+
+
 /**
- * Class Utils_Net
+ * Tests for SimpleSAML_Utils_Test.
  */
-
-class Utils_Net extends PHPUnit_Framework_TestCase
+class Utils_Net_Test extends PHPUnit_Framework_TestCase
 {
 
 
@@ -19,8 +20,8 @@ class Utils_Net extends PHPUnit_Framework_TestCase
         $this->assertFalse(SimpleSAML_Utils_Net::ipCIDRcheck('127.0.0.256/24', '127.0.0.1'));
 
         // check wrong IP
-        $this->assertTrue(SimpleSAML_Utils_Net::ipCIDRcheck('127.0.0.0/24', '127.0.0'));
-        $this->assertTrue(SimpleSAML_Utils_Net::ipCIDRcheck('127.0.0.0/24', '127.0.0.*'));
+        $this->assertFalse(SimpleSAML_Utils_Net::ipCIDRcheck('127.0.0.0/24', '127.0.0'));
+        $this->assertFalse(SimpleSAML_Utils_Net::ipCIDRcheck('127.0.0.0/24', '127.0.0.*'));
 
         // check limits for standard classes
         $this->assertTrue(SimpleSAML_Utils_Net::ipCIDRcheck('127.0.0.0/24', '127.0.0.0'));
diff --git a/tools/phpunit/phpunit.xml b/tools/phpunit/phpunit.xml
index edabe127c..d0d82a9c6 100644
--- a/tools/phpunit/phpunit.xml
+++ b/tools/phpunit/phpunit.xml
@@ -1,5 +1,4 @@
 <?xml version="1.0" encoding="UTF-8"?>
-
 <phpunit backupGlobals="false"
          backupStaticAttributes="false"
          colors="true"
@@ -11,8 +10,11 @@
          syntaxCheck="false"
          bootstrap="./../../vendor/autoload.php">
     <testsuites>
-        <testsuite name="Test Suite">
-            <directory>./../../tests</directory>
+        <testsuite name="Utils">
+            <directory>./../../tests/Utils/</directory>
+        </testsuite>
+        <testsuite name="Metadata">
+            <directory>./../../tests/Metadata/</directory>
         </testsuite>
     </testsuites>
     <filter>
-- 
GitLab