Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions lib/SimpleSAML/XHTML/IdPDisco.php
Original file line number Diff line number Diff line change
Expand Up @@ -599,13 +599,30 @@ public function handleRequest(): void
$language = $translator->getLanguage()->getLanguage();
$tryLanguages = [0 => $language, 1 => $defaultLanguage, 2 => $fallbackLanguage];

$t->data['idplist'] = $this->getPreferredTranslations($idpList, $tryLanguages);
$t->data['preferredidp'] = $preferredIdP;
$t->data['return'] = $this->returnURL;
$t->data['returnIDParam'] = $this->returnIdParam;
$t->data['entityID'] = $this->spEntityId;
$t->data['urlpattern'] = htmlspecialchars(Utils\HTTP::getSelfURLNoQuery());
$t->data['rememberenabled'] = $this->config->getBoolean('idpdisco.enableremember', false);
$t->show();
}


/**
* @param array $idpList
* @return array
*/
private function getPreferredTranslations(array $idpList, array $tryLanguages): array
Copy link
Member

@jaimeperez jaimeperez Nov 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is quite misleading. What this method is doing is returning a list of IdPs, sorted by name, where their names are translated based on the preferred translation for the given request. I don't think getPreferredTranslations() correctly describes that, but instead it sounds like it's just returning the preferred translations for some given IdPs.

I would actually suggest refactoring this code. There's a lot of repetition in there, and testing is quite hard, so we could benefit from routing to feed requests and get responses out of it.

I'm going to merge @m0ark's commit into 1.18, and we can keep working on this meanwhile.

{
$newlist = [];
foreach ($idpList as $entityid => $data) {
$newlist[$entityid]['entityid'] = $entityid;
foreach ($tryLanguages as $lang) {
if ($name = $this->getEntityDisplayName($data, $lang)) {
$newlist[$entityid]['name'] = $name;
continue;
break 1;
}
}
if (empty($newlist[$entityid]['name'])) {
Expand All @@ -614,14 +631,15 @@ public function handleRequest(): void
foreach ($tryLanguages as $lang) {
if (!empty($data['description'][$lang])) {
$newlist[$entityid]['description'] = $data['description'][$lang];
continue;
break 1;
}
}
if (!empty($data['icon'])) {
$newlist[$entityid]['icon'] = $data['icon'];
$newlist[$entityid]['iconurl'] = Utils\HTTP::resolveURL($data['icon']);
}
}

usort(
$newlist,
/**
Expand All @@ -634,14 +652,7 @@ function (array $idpentry1, array $idpentry2) {
}
);

$t->data['idplist'] = $newlist;
$t->data['preferredidp'] = $preferredIdP;
$t->data['return'] = $this->returnURL;
$t->data['returnIDParam'] = $this->returnIdParam;
$t->data['entityID'] = $this->spEntityId;
$t->data['urlpattern'] = htmlspecialchars(Utils\HTTP::getSelfURLNoQuery());
$t->data['rememberenabled'] = $this->config->getBoolean('idpdisco.enableremember', false);
$t->send();
return $newlist;
}


Expand Down
86 changes: 86 additions & 0 deletions tests/lib/SimpleSAML/XHTML/IdPDiscoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace SimpleSAML\Test;

use PHPUnit\Framework\TestCase;
use SimpleSAML\Configuration;
use SimpleSAML\Metadata\MetaDataStorageHandler;
use SimpleSAML\XHTML\IdPDisco;

/**
* Tests for the IdPDisco class.
*
* For the full copyright and license information, please view the LICENSE file that was
* distributed with this source code.
*
* @author Tim van Dijen <tvdijen@gmail.com>
* @package simplesamlphp/simplesamlphp
*/
class IdPDiscoTest extends TestCase
{
/** @var \SimpleSAML\Metadata\MetaDataStorageHandler */
private $mhandler;

/** @var array */
private $config = [
'language' => [
'priorities' => [
'nl' => ['nl', 'en', 'de'],
'es' => ['es', 'fr', 'cz'],
],
],
'language.default' => 'nl',

'metadata.sources' => [
[
'type' => 'xml',
'file' => 'vendor/simplesamlphp/simplesamlphp-test-framework/metadata/xml/unsigned-metadata.xml'
]
],
];


/**
* @return void
*/
public function setUp(): void
{
$config = Configuration::loadFromArray($this->config);

Configuration::setPreloadedConfig($config, 'config.php');

$this->mhandler = MetaDataStorageHandler::getMetadataHandler();
}


/**
*/
public function testPreferredTranslation(): void
{
$disco = new IdPDisco(['saml20-idp-remote'], 'unittest');

$reflection = new \ReflectionClass(get_class($disco));
$method = $reflection->getMethod('getPreferredTranslations');
$method->setAccessible(true);

$idpList = $method->invokeArgs($disco, $idpList);

// Assert that result comes with the right translation based on default languages and available translations
}


/**
*/
public function testIdPSortingOrder(): void
{
$disco = new IdPDisco(['saml20-idp-remote'], 'unittest');

$reflection = new \ReflectionClass(get_class($disco));
$method = $reflection->getMethod('getPreferredTranslations');
$method->setAccessible(true);

$idpList = $method->invokeArgs($disco, $idpList);

// Assert that result comes alphabetically ordered, whatever language or translation is used
}
}