From 69b3ac39aabe45729751e45538861e810130812a Mon Sep 17 00:00:00 2001 From: Kristian Date: Fri, 8 May 2026 17:59:17 +1000 Subject: [PATCH] Fix saml2 vendor autoload polluting core moodle tests SimpleSAML 2.4 bundles Nyholm/psr7; once loaded, Slim's PSR-17 discovery picks it over Guzzle and breaks core router tests in the same process. Pin the factory list to Guzzle in setuplib.php and from every test file via a shared tests/psr17_factory_pin.php include. --- classes/auth.php | 2 +- setuplib.php | 6 ++++++ tests/auth_test.php | 4 ++++ tests/autoloader_test.php | 7 +++++- tests/form_regenerate_test.php | 4 ++++ tests/generator_test.php | 4 ++++ tests/group_rule_test.php | 4 ++++ tests/locallib_test.php | 1 + tests/metadata_fetcher_test.php | 4 ++++ tests/metadata_parser_test.php | 4 ++++ tests/metadata_refresh_test.php | 4 ++++ tests/metadata_writer_test.php | 4 ++++ tests/psr17_factory_pin.php | 34 ++++++++++++++++++++++++++++++ tests/redis_store_test.php | 4 ++++ tests/saml2_sitedata_test.php | 7 +++++- tests/setting_idpmetadata_test.php | 7 +++++- tests/ssl_algorithm_test.php | 4 ++++ tests/user_extractor_test.php | 4 ++++ tests/user_fields_test.php | 4 ++++ 19 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 tests/psr17_factory_pin.php diff --git a/classes/auth.php b/classes/auth.php index c56958399..d4a694358 100644 --- a/classes/auth.php +++ b/classes/auth.php @@ -1289,7 +1289,7 @@ public function get_ssp_version() { // perform the full initialization. For better performance // we only make sure \SimpleSAML\Configuration is accessible // through _autoload.php. - require_once(__DIR__ . '/../vendor/autoload.php'); + require_once(__DIR__ . '/../setuplib.php'); $config = new \SimpleSAML\Configuration([], ''); return $config->getVersion(); } diff --git a/setuplib.php b/setuplib.php index b45665910..9c24394dd 100644 --- a/setuplib.php +++ b/setuplib.php @@ -28,6 +28,12 @@ require_once(__DIR__ . '/vendor/autoload.php'); +if (class_exists(\Slim\Factory\Psr17\Psr17FactoryProvider::class)) { + \Slim\Factory\Psr17\Psr17FactoryProvider::setFactories([ + \Slim\Factory\Psr17\GuzzlePsr17Factory::class, + ]); +} + global $CFG; require_once("{$CFG->dirroot}/auth/saml2/auth.php"); diff --git a/tests/auth_test.php b/tests/auth_test.php index c0804d2a5..3c2ef0da2 100644 --- a/tests/auth_test.php +++ b/tests/auth_test.php @@ -16,6 +16,10 @@ namespace auth_saml2; +defined('MOODLE_INTERNAL') || die(); + +require_once(__DIR__ . '/psr17_factory_pin.php'); + /** * Unit tests for auth class. * diff --git a/tests/autoloader_test.php b/tests/autoloader_test.php index 64cec7e98..fe5dbb87f 100644 --- a/tests/autoloader_test.php +++ b/tests/autoloader_test.php @@ -18,7 +18,7 @@ defined('MOODLE_INTERNAL') || die(); -require_once(__DIR__ . '/../vendor/autoload.php'); +require_once(__DIR__ . '/psr17_factory_pin.php'); /** * Test Saml2 Autoloader @@ -29,6 +29,11 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ final class autoloader_test extends \advanced_testcase { + public static function setUpBeforeClass(): void { + parent::setUpBeforeClass(); + require_once(__DIR__ . '/../vendor/autoload.php'); + } + public function test_it_loads_classes(): void { $classes = [ \Psr\Log\LoggerInterface::class, diff --git a/tests/form_regenerate_test.php b/tests/form_regenerate_test.php index 6ae317cac..c76c381b2 100644 --- a/tests/form_regenerate_test.php +++ b/tests/form_regenerate_test.php @@ -18,6 +18,10 @@ use auth_saml2\form\regenerate; +defined('MOODLE_INTERNAL') || die(); + +require_once(__DIR__ . '/psr17_factory_pin.php'); + /** * auth_saml2 form submission unit tests * @package auth_saml2 diff --git a/tests/generator_test.php b/tests/generator_test.php index 7812b2b80..323f9924f 100644 --- a/tests/generator_test.php +++ b/tests/generator_test.php @@ -16,6 +16,10 @@ namespace auth_saml2; +defined('MOODLE_INTERNAL') || die(); + +require_once(__DIR__ . '/psr17_factory_pin.php'); + /** * Unit tests for test data generator. * diff --git a/tests/group_rule_test.php b/tests/group_rule_test.php index aff22d523..934fc5af7 100644 --- a/tests/group_rule_test.php +++ b/tests/group_rule_test.php @@ -16,6 +16,10 @@ namespace auth_saml2; +defined('MOODLE_INTERNAL') || die(); + +require_once(__DIR__ . '/psr17_factory_pin.php'); + /** * Testcase class for group_rule class. * diff --git a/tests/locallib_test.php b/tests/locallib_test.php index 50f74cbfa..b4d9fcf16 100644 --- a/tests/locallib_test.php +++ b/tests/locallib_test.php @@ -20,6 +20,7 @@ defined('MOODLE_INTERNAL') || die(); require_once(__DIR__ . '/../locallib.php'); +require_once(__DIR__ . '/psr17_factory_pin.php'); /** * Tests for SAML diff --git a/tests/metadata_fetcher_test.php b/tests/metadata_fetcher_test.php index dac14986c..c15ab5688 100644 --- a/tests/metadata_fetcher_test.php +++ b/tests/metadata_fetcher_test.php @@ -16,6 +16,10 @@ namespace auth_saml2; +defined('MOODLE_INTERNAL') || die(); + +require_once(__DIR__ . '/psr17_factory_pin.php'); + /** * Testcase class for metadata_fetcher class. * diff --git a/tests/metadata_parser_test.php b/tests/metadata_parser_test.php index ab5c4b71e..4c953977a 100644 --- a/tests/metadata_parser_test.php +++ b/tests/metadata_parser_test.php @@ -16,6 +16,10 @@ namespace auth_saml2; +defined('MOODLE_INTERNAL') || die(); + +require_once(__DIR__ . '/psr17_factory_pin.php'); + /** * Testcase class for metadata_parser class. * diff --git a/tests/metadata_refresh_test.php b/tests/metadata_refresh_test.php index 818409a98..5fad83c77 100644 --- a/tests/metadata_refresh_test.php +++ b/tests/metadata_refresh_test.php @@ -18,6 +18,10 @@ use auth_saml2\task\metadata_refresh; +defined('MOODLE_INTERNAL') || die(); + +require_once(__DIR__ . '/psr17_factory_pin.php'); + /** * Testcase class for metadata_refresh task class. * diff --git a/tests/metadata_writer_test.php b/tests/metadata_writer_test.php index 0d5f26473..adfb4112f 100644 --- a/tests/metadata_writer_test.php +++ b/tests/metadata_writer_test.php @@ -16,6 +16,10 @@ namespace auth_saml2; +defined('MOODLE_INTERNAL') || die(); + +require_once(__DIR__ . '/psr17_factory_pin.php'); + /** * Testcase class for metadata_writer class. * diff --git a/tests/psr17_factory_pin.php b/tests/psr17_factory_pin.php new file mode 100644 index 000000000..7998c7b6d --- /dev/null +++ b/tests/psr17_factory_pin.php @@ -0,0 +1,34 @@ +. + +/** + * Pin Slim PSR-17 factory discovery to Guzzle for tests. + * + * SimpleSAML 2.4 bundles Nyholm/psr7; once loaded, Slim's PSR-17 discovery + * picks it over Guzzle and breaks core router tests in the same process. + * + * @package auth_saml2 + * @copyright 2026 Catalyst IT Australia {@link http://www.catalyst-au.net} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +if (class_exists(\Slim\Factory\Psr17\Psr17FactoryProvider::class)) { + \Slim\Factory\Psr17\Psr17FactoryProvider::setFactories([ + \Slim\Factory\Psr17\GuzzlePsr17Factory::class, + ]); +} diff --git a/tests/redis_store_test.php b/tests/redis_store_test.php index c5aa5edc5..24fdefb1c 100644 --- a/tests/redis_store_test.php +++ b/tests/redis_store_test.php @@ -16,6 +16,10 @@ namespace auth_saml2; +defined('MOODLE_INTERNAL') || die(); + +require_once(__DIR__ . '/psr17_factory_pin.php'); + /** * Testcase class for auth/saml2 Redis store. * diff --git a/tests/saml2_sitedata_test.php b/tests/saml2_sitedata_test.php index 0d2e4d82c..c43e51058 100644 --- a/tests/saml2_sitedata_test.php +++ b/tests/saml2_sitedata_test.php @@ -18,7 +18,7 @@ defined('MOODLE_INTERNAL') || die(); -require_once(__DIR__ . '/../vendor/autoload.php'); +require_once(__DIR__ . '/psr17_factory_pin.php'); /** * Saml2 site data test. @@ -29,6 +29,11 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ final class saml2_sitedata_test extends \advanced_testcase { + public static function setUpBeforeClass(): void { + parent::setUpBeforeClass(); + require_once(__DIR__ . '/../vendor/autoload.php'); + } + public function test_it_creates_the_directory_if_it_does_not_exist(): void { global $CFG; diff --git a/tests/setting_idpmetadata_test.php b/tests/setting_idpmetadata_test.php index ddc7b9ada..c3eb36e98 100644 --- a/tests/setting_idpmetadata_test.php +++ b/tests/setting_idpmetadata_test.php @@ -20,7 +20,7 @@ defined('MOODLE_INTERNAL') || die(); -require_once(__DIR__ . '/../vendor/autoload.php'); +require_once(__DIR__ . '/psr17_factory_pin.php'); /** * Test setting idp Metadata. @@ -34,6 +34,11 @@ final class setting_idpmetadata_test extends \advanced_testcase { /** @var setting_idpmetadata */ private static $config; + public static function setUpBeforeClass(): void { + parent::setUpBeforeClass(); + require_once(__DIR__ . '/../vendor/autoload.php'); + } + protected function setUp(): void { parent::setUp(); self::$config = new setting_idpmetadata(); diff --git a/tests/ssl_algorithm_test.php b/tests/ssl_algorithm_test.php index 7a2da4e3f..587a7da4c 100644 --- a/tests/ssl_algorithm_test.php +++ b/tests/ssl_algorithm_test.php @@ -16,6 +16,10 @@ namespace auth_saml2; +defined('MOODLE_INTERNAL') || die(); + +require_once(__DIR__ . '/psr17_factory_pin.php'); + /** * Test Saml2 SSL Algorithms. * diff --git a/tests/user_extractor_test.php b/tests/user_extractor_test.php index c2fa795b2..606caef5e 100644 --- a/tests/user_extractor_test.php +++ b/tests/user_extractor_test.php @@ -16,6 +16,10 @@ namespace auth_saml2; +defined('MOODLE_INTERNAL') || die(); + +require_once(__DIR__ . '/psr17_factory_pin.php'); + /** * Test for user extractor class. * diff --git a/tests/user_fields_test.php b/tests/user_fields_test.php index 6b1b44947..4d26195dd 100644 --- a/tests/user_fields_test.php +++ b/tests/user_fields_test.php @@ -16,6 +16,10 @@ namespace auth_saml2; +defined('MOODLE_INTERNAL') || die(); + +require_once(__DIR__ . '/psr17_factory_pin.php'); + /** * Test for user fields class. *