Files
thalim-plugin-hal-importer/tests/run-tests.php

103 lines
4.6 KiB
PHP

<?php
/**
* Tests du plugin THALIM HAL Importer.
*
* Exécution (environnement Docker de dev) :
* docker exec wordpress php /var/www/html/wp-content/plugins/thalim-hal-importer/tests/run-tests.php
*/
if (PHP_SAPI !== 'cli') {
exit("CLI only\n");
}
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_HOST'] ?? 'localhost';
$_SERVER['REQUEST_URI'] = $_SERVER['REQUEST_URI'] ?? '/';
require dirname(__DIR__, 4) . '/wp-load.php';
$failures = 0;
$count = 0;
function check(string $name, $actual, $expected): void {
global $failures, $count;
$count++;
if ($actual === $expected) {
echo " ok $name\n";
} else {
$failures++;
echo " FAIL $name\n attendu: " . var_export($expected, true)
. "\n obtenu : " . var_export($actual, true) . "\n";
}
}
$importer = new Thalim_HAL_Importer_Logic();
echo "== parse_hal_date (méthode privée, via Reflection) ==\n";
$parse = new ReflectionMethod(Thalim_HAL_Importer_Logic::class, 'parse_hal_date');
$parse->setAccessible(true);
$d = fn(string $raw): string => $parse->invoke($importer, $raw);
check('date complète', $d('2022-06-15'), '2022-06-15');
check('datetime ISO', $d('2022-06-15T10:30:00Z'), '2022-06-15');
check('année-mois', $d('2022-06'), '2022-06-01');
check('année seule', $d('2022'), '2022-01-01'); // strtotime("2022") = heure, pas année
check('chaîne vide', $d(''), '');
check('espaces', $d(' 2021 '), '2021-01-01');
check('invalide', $d('not-a-date'), '');
echo "== get_category_id (résolution par slug) ==\n";
check('ART → articles (16)', $importer->get_category_id('ART'), 16);
check('OUV → ouvrages (15)', $importer->get_category_id('OUV'), 15);
check('THESE → soutenances (14)', $importer->get_category_id('THESE'), 14);
check('SON → medias (19)', $importer->get_category_id('SON'), 19);
check('REPORT → publications (4)',$importer->get_category_id('REPORT'), 4);
check('type inconnu → null', $importer->get_category_id('XYZ'), null);
echo "== Thalim_HAL_Pods_Storage (résolution par nom) ==\n";
check('pod post = 8', Thalim_HAL_Pods_Storage::pod_id(), 8);
check('champ categorie = 16', Thalim_HAL_Pods_Storage::field_id('categorie'), 16);
check('champ membres = 178', Thalim_HAL_Pods_Storage::field_id('membres'), 178);
check('champ etiquettes = 652',Thalim_HAL_Pods_Storage::field_id('etiquettes'), 652);
check('champ axes = 270', Thalim_HAL_Pods_Storage::field_id('axes_thematiques'), 270);
check('champ inconnu = 0', Thalim_HAL_Pods_Storage::field_id('champ_bidon'), 0);
echo "== Thalim_HAL_Forms_Cache::forms_for_user (lecture cache, sans réseau) ==\n";
// Dégradation gracieuse : cache absent -> forme unique.
check('cache absent -> forme unique',
Thalim_HAL_Forms_Cache::forms_for_user(999999999, 'xavier-garnier'),
['xavier-garnier']);
check('idHAL vide -> tableau vide',
Thalim_HAL_Forms_Cache::forms_for_user(999999999, ''),
[]);
// Cache présent et à jour, sur un vrai user (métas écrites puis restaurées).
$real = get_users(['meta_key' => 'identifiant_hal', 'meta_compare' => 'EXISTS',
'fields' => ['ID'], 'number' => 1]);
if (!empty($real)) {
$ruid = (int) $real[0]->ID;
$idhal = strtolower(trim((string) get_user_meta($ruid, 'identifiant_hal', true)));
$bak_forms = get_user_meta($ruid, Thalim_HAL_Forms_Cache::META_FORMS, true);
$bak_src = get_user_meta($ruid, Thalim_HAL_Forms_Cache::META_SOURCE, true);
update_user_meta($ruid, Thalim_HAL_Forms_Cache::META_FORMS,
wp_json_encode([$idhal, $idhal . '-alt']));
update_user_meta($ruid, Thalim_HAL_Forms_Cache::META_SOURCE, $idhal);
$got = Thalim_HAL_Forms_Cache::forms_for_user($ruid, $idhal); sort($got);
$exp = [$idhal, $idhal . '-alt']; sort($exp);
check('cache présent -> formes alternatives incluses', $got, $exp);
// Cache périmé (source != idHAL courant) -> forme unique.
update_user_meta($ruid, Thalim_HAL_Forms_Cache::META_SOURCE, 'obsolete-source');
check('cache périmé -> forme unique',
Thalim_HAL_Forms_Cache::forms_for_user($ruid, $idhal), [$idhal]);
// Restauration de l'état initial.
if ($bak_src === '') { delete_user_meta($ruid, Thalim_HAL_Forms_Cache::META_SOURCE); }
else { update_user_meta($ruid, Thalim_HAL_Forms_Cache::META_SOURCE, $bak_src); }
if ($bak_forms === '') { delete_user_meta($ruid, Thalim_HAL_Forms_Cache::META_FORMS); }
else { update_user_meta($ruid, Thalim_HAL_Forms_Cache::META_FORMS, $bak_forms); }
}
echo "\n$count tests, $failures échec(s)\n";
exit($failures ? 1 : 0);