/**
  * 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);
 }
 if ($idpmeta->hasValue('UIInfo')) {
     $metaArray['UIInfo'] = $idpmeta->getArray('UIInfo');
 }
 if ($idpmeta->hasValue('DiscoHints')) {
     $metaArray['DiscoHints'] = $idpmeta->getArray('DiscoHints');
 }
 if ($idpmeta->hasValue('RegistrationInfo')) {
     $metaArray['RegistrationInfo'] = $idpmeta->getArray('RegistrationInfo');
 }
 $metaflat = '$metadata[' . var_export($idpentityid, TRUE) . '] = ' . var_export($metaArray, TRUE) . ';';
 $metaBuilder = new SimpleSAML_Metadata_SAMLBuilder($idpentityid);
 $metaBuilder->addSecurityTokenServiceType($metaArray);
 $metaBuilder->addOrganizationInfo($metaArray);
 $technicalContactEmail = $config->getString('technicalcontact_email', NULL);
 if ($technicalContactEmail && $technicalContactEmail !== '*****@*****.**') {
     $metaBuilder->addContact('technical', SimpleSAML_Utils_Config_Metadata::getContact(array('emailAddress' => $technicalContactEmail, 'name' => $config->getString('technicalcontact_name', NULL), 'contactType' => 'technical')));
 }
 $output_xhtml = array_key_exists('output', $_GET) && $_GET['output'] == 'xhtml';
 $metaxml = $metaBuilder->getEntityDescriptorText($output_xhtml);
 if (!$output_xhtml) {
     $metaxml = str_replace("\n", '', $metaxml);
 }
 /* Sign the metadata if enabled. */
 $metaxml = SimpleSAML_Metadata_Signer::sign($metaxml, $idpmeta->toArray(), 'ADFS IdP');
 if ($output_xhtml) {
     $defaultidp = $config->getString('default-adfs-idp', NULL);
     $t = new SimpleSAML_XHTML_Template($config, 'metadata.php', 'admin');
     $t->data['available_certs'] = $availableCerts;
     $t->data['header'] = 'adfs-idp';
     $t->data['metaurl'] = SimpleSAML_Utilities::selfURLNoQuery();
     $t->data['metadata'] = htmlspecialchars($metaxml);
 /**
  * Add contact information.
  *
  * Accepts a contact type, and a contact array that must be previously sanitized.
  *
  * @param string $type The type of contact. Deprecated.
  * @param array $details The details about the contact.
  *
  * @todo Change the signature to remove $type.
  * @todo Remove the capability to pass a name and parse it inside the method.
  *
  * @deprecated This function will change its signature and no longer parse a 'name' element.
  */
 public function addContact($type, $details)
 {
     assert('is_string($type)');
     assert('is_array($details)');
     assert('in_array($type, array("technical", "support", "administrative", "billing", "other"), TRUE)');
     // TODO: remove this check as soon as getContact() is called always before calling this function.
     $details = SimpleSAML_Utils_Config_Metadata::getContact($details);
     $e = new SAML2_XML_md_ContactPerson();
     $e->contactType = $type;
     if (isset($details['company'])) {
         $e->Company = $details['company'];
     }
     if (isset($details['givenName'])) {
         $e->GivenName = $details['givenName'];
     }
     if (isset($details['surName'])) {
         $e->SurName = $details['surName'];
     }
     if (isset($details['emailAddress'])) {
         $eas = $details['emailAddress'];
         if (!is_array($eas)) {
             $eas = array($eas);
         }
         foreach ($eas as $ea) {
             $e->EmailAddress[] = $ea;
         }
     }
     if (isset($details['telephoneNumber'])) {
         $tlfNrs = $details['telephoneNumber'];
         if (!is_array($tlfNrs)) {
             $tlfNrs = array($tlfNrs);
         }
         foreach ($tlfNrs as $tlfNr) {
             $e->TelephoneNumber[] = $tlfNr;
         }
     }
     $this->entityDescriptor->ContactPerson[] = $e;
 }
 }
 if ($idpmeta->hasValue('redirect.validate')) {
     $metaArray['redirect.sign'] = $idpmeta->getBoolean('redirect.validate');
 }
 if ($idpmeta->hasValue('contacts')) {
     $contacts = $idpmeta->getArray('contacts');
     foreach ($contacts as $contact) {
         $metaArray['contacts'][] = SimpleSAML_Utils_Config_Metadata::getContact($contact);
     }
 }
 $technicalContactEmail = $config->getString('technicalcontact_email', FALSE);
 if ($technicalContactEmail && $technicalContactEmail !== '*****@*****.**') {
     $techcontact['emailAddress'] = $technicalContactEmail;
     $techcontact['name'] = $config->getString('technicalcontact_name', NULL);
     $techcontact['contactType'] = 'technical';
     $metaArray['contacts'][] = SimpleSAML_Utils_Config_Metadata::getContact($techcontact);
 }
 $metaBuilder = new SimpleSAML_Metadata_SAMLBuilder($idpentityid);
 $metaBuilder->addMetadataIdP20($metaArray);
 $metaBuilder->addOrganizationInfo($metaArray);
 $metaxml = $metaBuilder->getEntityDescriptorText();
 $metaflat = '$metadata[' . var_export($idpentityid, TRUE) . '] = ' . var_export($metaArray, TRUE) . ';';
 /* Sign the metadata if enabled. */
 $metaxml = SimpleSAML_Metadata_Signer::sign($metaxml, $idpmeta->toArray(), 'SAML 2 IdP');
 if (array_key_exists('output', $_GET) && $_GET['output'] == 'xhtml') {
     $defaultidp = $config->getString('default-saml20-idp', NULL);
     $t = new SimpleSAML_XHTML_Template($config, 'metadata.php', 'admin');
     $t->data['available_certs'] = $availableCerts;
     $t->data['header'] = 'saml20-idp';
     $t->data['metaurl'] = SimpleSAML_Utilities::selfURLNoQuery();
     $t->data['metadata'] = htmlspecialchars($metaxml);