MDL-69215 output: Correct font awesome for themes
authorAndrew Nicols <andrew@nicols.co.uk>
Tue, 7 Jul 2020 05:23:41 +0000 (13:23 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Wed, 15 Jul 2020 23:29:22 +0000 (07:29 +0800)
lib/amd/build/icon_system_fontawesome.min.js
lib/amd/build/icon_system_fontawesome.min.js.map
lib/amd/src/icon_system_fontawesome.js
lib/classes/external/output/icon_system/load_fontawesome_map.php [new file with mode: 0644]
lib/classes/output/external.php
lib/db/services.php
lib/tests/external/output/icon_system/load_fontawesome_map_test.php [new file with mode: 0644]
lib/upgrade.txt
version.php

index b446165..47c4244 100644 (file)
Binary files a/lib/amd/build/icon_system_fontawesome.min.js and b/lib/amd/build/icon_system_fontawesome.min.js differ
index 179a881..1bb4e1d 100644 (file)
Binary files a/lib/amd/build/icon_system_fontawesome.min.js.map and b/lib/amd/build/icon_system_fontawesome.min.js.map differ
index b2f8cee..25c749c 100644 (file)
@@ -58,8 +58,10 @@ define(['core/icon_system', 'jquery', 'core/ajax', 'core/mustache', 'core/locals
 
         if (fetchMap === null) {
             fetchMap = Ajax.call([{
-                methodname: 'core_output_load_fontawesome_icon_map',
-                args: []
+                methodname: 'core_output_load_fontawesome_icon_system_map',
+                args: {
+                    themename: M.cfg.theme,
+                },
             }], true, false, false, 0, M.cfg.themerev)[0];
         }
 
diff --git a/lib/classes/external/output/icon_system/load_fontawesome_map.php b/lib/classes/external/output/icon_system/load_fontawesome_map.php
new file mode 100644 (file)
index 0000000..aafaa4d
--- /dev/null
@@ -0,0 +1,98 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * A web service to load the mapping of moodle pix names to fontawesome icon names.
+ *
+ * @package    core
+ * @category   external
+ * @copyright  2020 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\external\output\icon_system;
+
+use external_api;
+use external_function_parameters;
+use external_multiple_structure;
+use external_single_structure;
+use external_value;
+use core\output\icon_system_fontawesome;
+use theme_config;
+
+/**
+ * Web service to load font awesome icon maps.
+ *
+ * @package    core
+ * @category   external
+ * @copyright  2020 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class load_fontawesome_map extends external_api {
+
+    /**
+     * Description of the parameters suitable for the `execute` function.
+     *
+     * @return external_function_parameters
+     */
+    public static function execute_parameters() {
+        return new external_function_parameters([
+            'themename' => new external_value(PARAM_ALPHANUMEXT, 'The theme to fetch the map for'),
+        ]);
+    }
+
+    /**
+     * Return a mapping of icon names to icons.
+     *
+     * @param   string $themename The theme to fetch icons for
+     * @return  array the mapping
+     */
+    public static function execute(string $themename) {
+        [
+            'themename' => $themename,
+        ] = self::validate_parameters(self::execute_parameters(), [
+            'themename' => $themename,
+        ]);
+
+        $theme = theme_config::load($themename);
+        $instance = icon_system_fontawesome::instance($theme->get_icon_system());
+
+        $result = [];
+        foreach ($instance->get_icon_name_map() as $from => $to) {
+            [$component, $pix] = explode(':', $from);
+            $result[] = [
+                'component' => $component,
+                'pix' => $pix,
+                'to' => $to,
+            ];
+        }
+
+        return $result;
+    }
+
+    /**
+     * Description of the return value for the `execute` function.
+     *
+     * @return external_description
+     */
+    public static function execute_returns() {
+        return new external_multiple_structure(new external_single_structure([
+            'component' => new external_value(PARAM_COMPONENT, 'The component for the icon.'),
+            'pix' => new external_value(PARAM_RAW, 'Value to map the icon from.'),
+            'to' => new external_value(PARAM_RAW, 'Value to map the icon to.'),
+        ]));
+    }
+}
index a8f2d6a..f9ee71b 100644 (file)
@@ -34,6 +34,7 @@ use core_component;
 use moodle_exception;
 use context_system;
 use theme_config;
+use core\external\output\icon_system\load_fontawesome_map;
 
 /**
  * This class contains a list of webservice functions related to output.
@@ -202,24 +203,13 @@ class external extends external_api {
     /**
      * Return a mapping of icon names to icons.
      *
+     * @deprecated since Moodle 3.10
      * @return array the mapping
      */
     public static function load_fontawesome_icon_map() {
-        $instance = icon_system::instance(icon_system::FONTAWESOME);
+        global $PAGE;
 
-        $map = $instance->get_icon_name_map();
-
-        $result = [];
-
-        foreach ($map as $from => $to) {
-            list($component, $pix) = explode(':', $from);
-            $one = [];
-            $one['component'] = $component;
-            $one['pix'] = $pix;
-            $one['to'] = $to;
-            $result[] = $one;
-        }
-        return $result;
+        return load_fontawesome_map::execute($PAGE->theme->name);
     }
 
     /**
@@ -228,13 +218,16 @@ class external extends external_api {
      * @return external_description
      */
     public static function load_fontawesome_icon_map_returns() {
-        return new external_multiple_structure(new external_single_structure(
-            array(
-                'component' => new external_value(PARAM_COMPONENT, 'The component for the icon.'),
-                'pix' => new external_value(PARAM_RAW, 'Value to map the icon from.'),
-                'to' => new external_value(PARAM_RAW, 'Value to map the icon to.')
-            )
-        ));
+        return load_fontawesome_map::execute_returns();
     }
-}
 
+    /**
+     * The `load_fontawesome_icon_map` function has been replaced with
+     * @see load_fontawesome_map::execute()
+     *
+     * @return bool
+     */
+    public static function load_fontawesome_icon_map_is_deprecated() {
+        return true;
+    }
+}
index 57038ef..0be2bf7 100644 (file)
@@ -1646,6 +1646,14 @@ $functions = array(
         'loginrequired' => false,
         'ajax' => true,
     ),
+    'core_output_load_fontawesome_icon_system_map' => array(
+        'classname' => 'core\external\output\icon_system\load_fontawesome_map',
+        'methodname' => 'execute',
+        'description' => 'Load the mapping of moodle pix names to fontawesome icon names',
+        'type' => 'read',
+        'loginrequired' => false,
+        'ajax' => true,
+    ),
     // Question related functions.
     'core_question_update_flag' => array(
         'classname'     => 'core_question_external',
diff --git a/lib/tests/external/output/icon_system/load_fontawesome_map_test.php b/lib/tests/external/output/icon_system/load_fontawesome_map_test.php
new file mode 100644 (file)
index 0000000..752ac6c
--- /dev/null
@@ -0,0 +1,98 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * External functions test for record_feedback_action.
+ *
+ * @package    core
+ * @category   test
+ * @copyright  2020 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+namespace core\external\output\icon_system;
+
+use externallib_advanced_testcase;
+
+defined('MOODLE_INTERNAL') || die();
+
+global $CFG;
+
+require_once($CFG->dirroot . '/webservice/tests/helpers.php');
+
+/**
+ * Class record_userfeedback_action_testcase
+ *
+ * @copyright  2020 Andrew Nicols <andrew@nicols.co.uk>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @coversDefaultClass core\external\output\icon_system\load_fontawesome_map
+ */
+class load_fontawesome_map_testcase extends externallib_advanced_testcase {
+
+    /**
+     * Perform setup before these tests are run.
+     */
+    public static function setUpBeforeClass(): void {
+        global $CFG;
+
+        // In normal operation the external_api classes will have been loaded by the caller.
+        // The load_fontawesome_map class should not need to supplement our lack of autoloading of these classes.
+        require_once($CFG->libdir . '/externallib.php');
+    }
+
+    /**
+     * Ensure that a valid theme which uses fontawesome returns a map.
+     *
+     * @covers ::execute_parameters
+     * @covers ::execute
+     * @covers ::execute_returns
+     * @dataProvider valid_fontawesome_theme_provider
+     * @param   string $themename
+     */
+    public function test_execute(string $themename): void {
+        $result = load_fontawesome_map::execute($themename);
+        $this->assertIsArray($result);
+
+        foreach ($result as $value) {
+            $this->assertArrayHasKey('component', $value);
+            $this->assertArrayHasKey('pix', $value);
+            $this->assertArrayHasKey('to', $value);
+        }
+    }
+
+    /**
+     * Ensure that an invalid theme cannot be loaded.
+     */
+    public function test_execute_invalid_themename(): void {
+        $result = load_fontawesome_map::execute('invalidtheme');
+        $this->assertDebuggingCalled(
+            'This page should be using theme invalidtheme which cannot be initialised. Falling back to the site theme boost'
+        );
+        $this->assertIsArray($result);
+    }
+
+    /**
+     * Data provider for valid themes to use with the execute function.
+     *
+     * @return  array
+     */
+    public function valid_fontawesome_theme_provider(): array {
+        return [
+            'Boost theme' => ['boost'],
+            'Classic theme (extends boost)' => ['classic'],
+        ];
+    }
+}
index aa64123..76e3577 100644 (file)
@@ -29,6 +29,8 @@ information provided here is intended especially for developers.
     - course_in_list (now: core_course_list_element)
     - coursecat (now: core_course_category)
 * The form element 'htmleditor', which was deprecated in 3.6, has been removed.
+* The `core_output_load_fontawesome_icon_map` web service has been deprecated and replaced by
+  `core_output_load_fontawesome_icon_system_map` which takes the name of the theme to generate the icon system map for.
 
 === 3.9 ===
 * Following function has been deprecated, please use \core\task\manager::run_from_cli().
index 7805185..3d5bc97 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2020071100.00;              // YYYYMMDD      = weekly release date of this DEV branch.
+$version  = 2020071500.00;              // YYYYMMDD      = weekly release date of this DEV branch.
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.
 $release  = '4.0dev (Build: 20200711)'; // Human-friendly version name