Skip to content
Snippets Groups Projects
Commit 38b5cbce authored by Jaime Perez's avatar Jaime Perez
Browse files

Adding the first phpunit to SSP. Hooray!

parent a8c989f7
No related branches found
No related tags found
No related merge requests found
<?php
/**
* Class Utils_MetadataTest
*/
class Utils_MetadataTest extends PHPUnit_Framework_TestCase
{
/**
* Test contact configuration parsing and sanitizing.
*/
public function testGetContact()
{
// test missing type
$contact = array(
'name' => 'John Doe'
);
try {
$parsed = SimpleSAML_Utils_Config_Metadata::getContact($contact);
} catch (InvalidArgumentException $e) {
$this->assertStringStartsWith('"contactType" is mandatory and must be one of ', $e->getMessage());
}
// test invalid type
$contact = array(
'contactType' => 'invalid'
);
try {
$parsed = SimpleSAML_Utils_Config_Metadata::getContact($contact);
} catch (InvalidArgumentException $e) {
$this->assertStringStartsWith('"contactType" is mandatory and must be one of ', $e->getMessage());
}
// test all valid contact types
foreach (SimpleSAML_Utils_Config_Metadata::$VALID_CONTACT_TYPES as $type) {
$contact = array(
'contactType' => $type
);
$parsed = SimpleSAML_Utils_Config_Metadata::getContact($contact);
$this->assertArrayHasKey('contactType', $parsed);
$this->assertArrayNotHasKey('givenName', $parsed);
$this->assertArrayNotHasKey('surName', $parsed);
}
// test basic name parsing
$contact = array(
'contactType' => 'technical',
'name' => 'John Doe'
);
$parsed = SimpleSAML_Utils_Config_Metadata::getContact($contact);
$this->assertArrayNotHasKey('name', $parsed);
$this->assertArrayHasKey('givenName', $parsed);
$this->assertArrayHasKey('surName', $parsed);
$this->assertEquals('John', $parsed['givenName']);
$this->assertEquals('Doe', $parsed['surName']);
// test comma-separated names
$contact = array(
'contactType' => 'technical',
'name' => 'Doe, John'
);
$parsed = SimpleSAML_Utils_Config_Metadata::getContact($contact);
$this->assertArrayHasKey('givenName', $parsed);
$this->assertArrayHasKey('surName', $parsed);
$this->assertEquals('John', $parsed['givenName']);
$this->assertEquals('Doe', $parsed['surName']);
// test long names
$contact = array(
'contactType' => 'technical',
'name' => 'John Fitzgerald Doe Smith'
);
$parsed = SimpleSAML_Utils_Config_Metadata::getContact($contact);
$this->assertArrayNotHasKey('name', $parsed);
$this->assertArrayHasKey('givenName', $parsed);
$this->assertArrayNotHasKey('surName', $parsed);
$this->assertEquals('John Fitzgerald Doe Smith', $parsed['givenName']);
// test comma-separated long names
$contact = array(
'contactType' => 'technical',
'name' => 'Doe Smith, John Fitzgerald'
);
$parsed = SimpleSAML_Utils_Config_Metadata::getContact($contact);
$this->assertArrayNotHasKey('name', $parsed);
$this->assertArrayHasKey('givenName', $parsed);
$this->assertArrayHasKey('surName', $parsed);
$this->assertEquals('John Fitzgerald', $parsed['givenName']);
$this->assertEquals('Doe Smith', $parsed['surName']);
// test givenName
$contact = array(
'contactType' => 'technical',
);
$invalid_types = array(0, array(0), 0.1, true, false);
foreach ($invalid_types as $type) {
$contact['givenName'] = $type;
try {
SimpleSAML_Utils_Config_Metadata::getContact($contact);
} catch (InvalidArgumentException $e) {
$this->assertEquals('"givenName" must be a string and cannot be empty.', $e->getMessage());
}
}
// test surName
$contact = array(
'contactType' => 'technical',
);
$invalid_types = array(0, array(0), 0.1, true, false);
foreach ($invalid_types as $type) {
$contact['surName'] = $type;
try {
SimpleSAML_Utils_Config_Metadata::getContact($contact);
} catch (InvalidArgumentException $e) {
$this->assertEquals('"surName" must be a string and cannot be empty.', $e->getMessage());
}
}
// test company
$contact = array(
'contactType' => 'technical',
);
$invalid_types = array(0, array(0), 0.1, true, false);
foreach ($invalid_types as $type) {
$contact['company'] = $type;
try {
SimpleSAML_Utils_Config_Metadata::getContact($contact);
} catch (InvalidArgumentException $e) {
$this->assertEquals('"company" must be a string and cannot be empty.', $e->getMessage());
}
}
// test emailAddress
$contact = array(
'contactType' => 'technical',
);
$invalid_types = array(0, 0.1, true, false, array());
foreach ($invalid_types as $type) {
$contact['emailAddress'] = $type;
try {
SimpleSAML_Utils_Config_Metadata::getContact($contact);
} catch (InvalidArgumentException $e) {
$this->assertEquals(
'"emailAddress" must be a string or an array and cannot be empty.',
$e->getMessage()
);
}
}
$invalid_types = array(array("string", true), array("string", 0));
foreach ($invalid_types as $type) {
$contact['emailAddress'] = $type;
try {
SimpleSAML_Utils_Config_Metadata::getContact($contact);
} catch (InvalidArgumentException $e) {
$this->assertEquals(
'Email addresses must be a string and cannot be empty.',
$e->getMessage()
);
}
}
// test telephoneNumber
$contact = array(
'contactType' => 'technical',
);
$invalid_types = array(0, 0.1, true, false, array());
foreach ($invalid_types as $type) {
$contact['telephoneNumber'] = $type;
try {
SimpleSAML_Utils_Config_Metadata::getContact($contact);
} catch (InvalidArgumentException $e) {
$this->assertEquals(
'"telephoneNumber" must be a string or an array and cannot be empty.',
$e->getMessage()
);
}
}
$invalid_types = array(array("string", true), array("string", 0));
foreach ($invalid_types as $type) {
$contact['telephoneNumber'] = $type;
try {
SimpleSAML_Utils_Config_Metadata::getContact($contact);
} catch (InvalidArgumentException $e) {
$this->assertEquals('Telephone numbers must be a string and cannot be empty.', $e->getMessage());
}
}
// test completeness
$contact = array();
foreach (SimpleSAML_Utils_Config_Metadata::$VALID_CONTACT_OPTIONS as $option) {
$contact[$option] = 'string';
}
$contact['contactType'] = 'technical';
$contact['name'] = 'to_be_removed';
$parsed = SimpleSAML_Utils_Config_Metadata::getContact($contact);
foreach (array_keys($parsed) as $key) {
$this->assertEquals($parsed[$key], $contact[$key]);
}
$this->assertArrayNotHasKey('name', $parsed);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment