OpenFlyers SSO WordPress plugin 1.2.0

Read the member name and email from a custom report (profile_report_id).
This commit is contained in:
Polyedre
2026-11-18 20:30:16 +01:00
parent c5f6232509
commit 6b54fd524b
6 changed files with 363 additions and 42 deletions

View File

@@ -1,9 +1,4 @@
<?php
// Command-line only: this script rewrites settings and accounts, it must never run from a web request.
if (PHP_SAPI !== 'cli') {
exit;
}
/**
* Smoke test for the WordPress plugin protocol client, run outside WordPress.
* Checks the HTTP signature generation and the response verification (Digest + Signature)
@@ -180,5 +175,54 @@ check('an HTML certificate refusal is reported as a failure', $result === false)
check('it is recognized as a certificate problem', $client->getLastErrorCode() === 'ssl_certificate_error');
check('the reason reaches the error message', strpos($client->getLastError(), 'SSL certificate error') !== false);
// --- member report (name and e-mail) ------------------------------------------
// Body exactly as the OpenFlyers resource server returns a custom report: the CSV text, JSON encoded.
$reportBody = '"first_name;last_name;email\nÉlodie;D\'Artagnan;elodie.test@example.org\n"';
$reportResp = "HTTP/1.1 200 OK\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n" . $reportBody;
$csv = $parse->invoke($client, $reportResp, $config['resource_uri'], false);
check('a report response is decoded to its CSV text', is_string($csv) && strpos($csv, 'first_name;') === 0);
$row = OF_SSO_Client::parseReportCsv($csv);
check('the report row is keyed by column name', $row === array('first_name' => 'Élodie', 'last_name' => "D'Artagnan", 'email' => 'elodie.test@example.org'));
check('a report without data row gives an empty row', OF_SSO_Client::parseReportCsv("first_name;last_name;email\n") === array());
check('an empty report gives an empty row', OF_SSO_Client::parseReportCsv("\n") === array());
check('a row that does not match the header is ignored', OF_SSO_Client::parseReportCsv("first_name;last_name\nA;B;C\n") === array());
/**
* Client whose report call is answered locally, to check the mapping to WordPress fields.
*/
class OF_SSO_Report_Stub_Client extends OF_SSO_Client
{
public $answer;
public $lastCall;
public function getReport(array $token, $reportId, $resourceType = 'report', array $replacementList = array())
{
$this->lastCall = array($reportId, $resourceType, $replacementList);
return $this->answer;
}
}
$stub = new OF_SSO_Report_Stub_Client($config);
$stub->answer = $csv;
$profile = $stub->getMemberProfile(array('access_token' => 'abc'), 12, 24);
check('the report is called without any member identifier', $stub->lastCall === array(12, 'report', array()));
check('the profile maps to WordPress fields', $profile === array(
'first_name' => 'Élodie',
'last_name' => "D'Artagnan",
'user_email' => 'elodie.test@example.org',
'display_name' => "Élodie D'Artagnan",
));
$stub->answer = "first_name;last_name;email\nJean;MARTIN;\n";
$profile = $stub->getMemberProfile(array('access_token' => 'abc'), 12, 25);
check('an empty e-mail is left out', $profile === array('first_name' => 'Jean', 'last_name' => 'MARTIN', 'display_name' => 'Jean MARTIN'));
$stub->answer = "first_name;last_name;email\n";
check('a member missing from the report is a failure', $stub->getMemberProfile(array('access_token' => 'abc'), 12, 26) === false);
$stub->answer = false;
check('a refused report call is a failure', $stub->getMemberProfile(array('access_token' => 'abc'), 12, 24) === false);
echo $fail === 0 ? "\nAll checks passed.\n" : "\n$fail check(s) failed.\n";
exit($fail === 0 ? 0 : 1);

View File

@@ -1,9 +1,4 @@
<?php
// Command-line only: this script rewrites settings and accounts, it must never run from a web request.
if (PHP_SAPI !== 'cli') {
exit;
}
/**
* Functional test of the plugin inside a real WordPress installation.
*
@@ -99,7 +94,7 @@ require_once ABSPATH . 'wp-admin/includes/template.php';
require_once ABSPATH . 'wp-admin/includes/screen.php';
// Remove anything left by a previous run so the script is repeatable.
foreach (array('pilote.test', 'of-7777', 'of-8888') as $login) {
foreach (array('pilote.test', 'of-7777', 'of-8888', 'of-9191', 'of-9292', 'of-9393') as $login) {
$stale = get_user_by('login', $login);
if ($stale) {
wp_delete_user($stale->ID);
@@ -336,6 +331,68 @@ check('the profile filter fills the e-mail', $created->user_email === 'nouveau@e
check('the profile filter fills the display name', $created->display_name === 'Nouveau Membre', $created->display_name);
remove_all_filters('of_sso_user_profile');
// --- member report (name and e-mail) ------------------------------------------
/**
* Client whose report call is answered locally, the way the OpenFlyers resource server answers: the
* row is the one of the token's user ($connectedPersonId), here carried by the test token.
*/
class OF_SSO_Report_Test_Client extends OF_SSO_Client
{
public $answers = array();
public $replacementLists = array();
public function getReport(array $token, $reportId, $resourceType = 'report', array $replacementList = array())
{
$this->replacementLists[] = $replacementList;
$memberId = isset($token['test_user_id']) ? $token['test_user_id'] : 0;
return isset($this->answers[$memberId]) ? $this->answers[$memberId] : false;
}
}
$settings['profile_report_id'] = '12';
update_option('of_sso_settings', OF_SSO_Admin::sanitize($settings));
check('the report number is stored as an integer', of_sso_get_settings()['profile_report_id'] === 12);
$redirects = array();
$_GET = array('of_sso' => 'login');
capture(array('OF_SSO_Auth', 'route'));
parse_str((string) parse_url((string) end($redirects), PHP_URL_QUERY), $query);
check('the custom reports scope is requested once a report is set', $query['scope'] === 'default.login reports.readonly', $query['scope']);
$_GET = array();
$reportClient = new OF_SSO_Report_Test_Client(array());
$reportClient->answers = array(
9191 => "first_name;last_name;email\nÉlodie;D'Artagnan;elodie@example.test\n",
4242 => "first_name;last_name;email\nPaul;PILOTE;paul.pilote@example.test\n",
9292 => "first_name;last_name;email\nJean;MARTIN;paul.pilote@example.test\n",
);
$result = $resolve->invoke(null, 9191, array('test_user_id' => 9191), $reportClient);
$created = get_user_by('id', $result->ID);
check('a new member gets the name from the report', $created->first_name === 'Élodie' && $created->last_name === "D'Artagnan");
check('a new member is displayed under that name', $created->display_name === "Élodie D'Artagnan", $created->display_name);
check('a new member gets the e-mail from the report', $created->user_email === 'elodie@example.test', $created->user_email);
check('the login name stays technical', $created->user_login === 'of-9191');
$result = $resolve->invoke(null, 4242, array('test_user_id' => 4242), $reportClient);
$updated = get_user_by('id', $linkedId);
check('a linked member is updated at the next login', $updated->display_name === 'Paul PILOTE' && $updated->user_email === 'paul.pilote@example.test', $updated->display_name . ' ' . $updated->user_email);
$result = $resolve->invoke(null, 9292, array('test_user_id' => 9292), $reportClient);
check('an e-mail used by another account does not block the login', $result instanceof WP_User, is_wp_error($result) ? $result->get_error_message() : '');
$created = get_user_by('id', $result->ID);
check('that e-mail is not copied to the new account', $created->user_email === '', $created->user_email);
check('the name is still filled', $created->display_name === 'Jean MARTIN', $created->display_name);
$result = $resolve->invoke(null, 9393, array('test_user_id' => 9393), $reportClient);
check('an unreadable report does not block the login', $result instanceof WP_User, is_wp_error($result) ? $result->get_error_message() : '');
check('the member then keeps the technical name', get_user_by('id', $result->ID)->display_name === 'of-9393');
check('no member identifier is ever sent to the report', $reportClient->replacementLists === array(array(), array(), array(), array()), wp_json_encode($reportClient->replacementLists));
$settings['profile_report_id'] = '';
update_option('of_sso_settings', OF_SSO_Admin::sanitize($settings));
check('an empty report number disables the report', of_sso_get_settings()['profile_report_id'] === 0);
// --- shortcodes -------------------------------------------------------------
check('the login shortcode is registered', shortcode_exists('openflyers_login'));
check('the member shortcode is registered', shortcode_exists('openflyers_membre'));
@@ -366,6 +423,7 @@ check('the settings page renders', strlen($page) > 1000, strlen($page) . ' bytes
check('it shows the callback URL to declare', strpos($page, esc_html(of_sso_get_redirect_uri())) !== false);
check('it exposes every certificate field', substr_count($page, 'of_sso_settings[') >= 15);
check('it offers the certificate folder field', strpos($page, 'name="of_sso_settings[cert_dir]"') !== false);
check('it offers the member report field', strpos($page, 'name="of_sso_settings[profile_report_id]"') !== false);
check('it offers the connection test button', strpos($page, 'name="of_sso_test_connection"') !== false);
check('the stored secret is never printed back', strpos($page, 'value="secret"') === false);