MDL-68571 core_h5p: Return default handler if no it has been defined
authorcescobedo <carlos.escobedo@gmail.com>
Wed, 6 May 2020 17:11:58 +0000 (19:11 +0200)
committercescobedo <carlos.escobedo@gmail.com>
Mon, 11 May 2020 14:46:27 +0000 (16:46 +0200)
admin/settings/h5p.php
h5p/classes/local/library/autoloader.php
h5p/tests/h5p_core_test.php

index 3b9302d..a512853 100644 (file)
@@ -33,7 +33,7 @@ $ADMIN->add('h5p', new admin_externalpage('h5pmanagelibraries', get_string('h5pm
     new moodle_url('/h5p/libraries.php'), ['moodle/site:config', 'moodle/h5p:updatelibraries']));
 
 // H5P settings.
-$defaulth5plib = \core_h5p\local\library\autoloader::get_default_handler();
+$defaulth5plib = \core_h5p\local\library\autoloader::get_default_handler_library();
 if (!empty($defaulth5plib)) {
     // As for now this page only has this setting, it will be hidden if there isn't any H5P libraries handler defined.
     $settings = new admin_settingpage('h5psettings', new lang_string('h5psettings', 'core_h5p'));
index e046c2a..27b5f4e 100644 (file)
@@ -50,10 +50,27 @@ class autoloader {
     }
 
     /**
-     * Returns the default H5P library handler.
+     * Returns the default H5P library handler class.
+     *
      * @return string|null H5P library handler class
      */
     public static function get_default_handler(): ?string {
+        $default = null;
+        $handlers = self::get_all_handlers();
+        if (!empty($handlers)) {
+            // The default handler will be the first value in the list.
+            $default = array_shift($handlers);
+        }
+
+        return $default;
+    }
+
+    /**
+     * Returns the default H5P library handler.
+     *
+     * @return string|null H5P library handler
+     */
+    public static function get_default_handler_library(): ?string {
         $default = null;
         $handlers = self::get_all_handlers();
         if (!empty($handlers)) {
@@ -81,7 +98,7 @@ class autoloader {
             }
         }
 
-        // If no handler has been defined or it doesn't exist, return the default one.
+        // If no handler has been defined, return the default one.
         $defaulthandler = self::get_default_handler();
         if (empty($defaulthandler)) {
             // If there is no default handler, throw an exception.
index a814c44..8d7f088 100644 (file)
@@ -172,4 +172,25 @@ class h5p_core_testcase extends \advanced_testcase {
         $siteuuid2 = $this->core->get_site_uuid();
         $this->assertEquals( $siteuuid, $siteuuid2);
     }
+
+    /**
+     * Test if no handler has been defined.
+     */
+    public function test_get_default_handler() {
+        global $CFG;
+
+        $this->resetAfterTest(true);
+        // Emtpy the h5plibraryhandler setting.
+        $CFG->h5plibraryhandler = '';
+
+        // Get the default habdler library to use in the settings h5p page.
+        // For instance, h5plib_v124.
+        $handlerlib = autoloader::get_default_handler_library();
+        $this->assertNotNull($handlerlib);
+        $this->assertStringNotContainsString($handlerlib, '\local\library\handler');
+        // Get the default handler class.
+        // For instance, \h5plib_v124\local\library\handler.
+        $handlerclass = autoloader::get_default_handler();
+        $this->assertStringEndsWith('\local\library\handler', $handlerclass);
+    }
 }