diff --git a/lib/_autoload_modules.php b/lib/_autoload_modules.php
index 8484e35b03c0332d8a203fe92084d9b629260afa..db0f5b3f9d018d3aa843bf405a26ac65faef6729 100644
--- a/lib/_autoload_modules.php
+++ b/lib/_autoload_modules.php
@@ -22,9 +22,9 @@ function temporaryLoader($class)
     // handle the upgrade to the latest version of XMLSecLibs using namespaces
     if (strstr($class, 'XMLSec') && !strstr($class, '\\RobRichards\\XMLSecLibs\\')) {
         $new = '\\RobRichards\\XMLSecLibs\\'.$class;
-        if (class_exists($new, false)) {
+        if (class_exists($new, true)) {
             class_alias($new, $class);
-            SimpleSAML\Logger::warning("The class or interface '$original' is now using namespaces, please use '$new'.");
+            SimpleSAML\Logger::warning("The class or interface '$class' is now using namespaces, please use '$new'.");
             return;
         }
     }
diff --git a/tests/lib/AutoloadModulesTest.php b/tests/lib/AutoloadModulesTest.php
new file mode 100644
index 0000000000000000000000000000000000000000..5bc596d2321dc6034735504e82dcd107b9113bf1
--- /dev/null
+++ b/tests/lib/AutoloadModulesTest.php
@@ -0,0 +1,26 @@
+<?php
+
+namespace SimpleSAML\Test;
+
+use PHPUnit\Framework\TestCase;
+
+class AutoloadModulesTest extends TestCase
+{
+    /**
+     * @test
+     * @runInSeparateProcess
+     */
+    public function autoloaderDoesNotRecurseInfinitely()
+    {
+        $this->assertFalse(class_exists('NonExisting\\ClassThatHasNothing\\ToDoWithXMLSec\\Library', true));
+    }
+
+    /**
+     * @test
+     * @runInSeparateProcess
+     */
+    public function autoloaderSubstitutesNamespacedXmlSecClassesWhereNonNamespacedClassWasUsed()
+    {
+        $this->assertTrue(class_exists('XMLSecEnc', true));
+    }
+}