This commit is contained in:
Sara Arjona
2025-03-17 15:46:28 +01:00
23 changed files with 1161 additions and 7 deletions
@@ -0,0 +1,10 @@
issueNumber: MDL-84353
notes:
editor_tiny:
- message: >
New external function `editor_tiny_get_configuration`.
TinyMCE subplugins can provide configuration to the new external
function by implementing the `plugin_with_configuration_for_external`
interface and/or overriding the `is_enabled_for_external` method.
type: improved
+137
View File
@@ -0,0 +1,137 @@
<?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/>.
namespace editor_tiny\external;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
use editor_tiny\manager;
/**
* External function that returns the TinyMCE configuration for a context.
*
* @package editor_tiny
* @copyright 2025 Moodle Pty Ltd
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_configuration extends external_api {
/**
* Describes the parameters of the external function.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([
'contextlevel' => new external_value(PARAM_ALPHA, 'Context level: system, user, coursecat, course, module or block'),
'instanceid' => new external_value(PARAM_INT, 'Instance ID of the context (e.g. course ID)'),
]);
}
/**
* Returns the TinyMCE configuration for a context.
*
* This function serves a similar purpose as \editor_tiny\editor::use_editor but with the following differences:
* - It does not receive editor or file picker options.
* - It only returns information that depends on site settings or permission checks.
*
* @param string $contextlevel Context level: system, user, coursecat, course, module or block.
* @param int $instanceid Instance ID of the context (e.g. course ID).
* @return array
*/
public static function execute(string $contextlevel, int $instanceid): array {
global $PAGE;
$params = self::validate_parameters(self::execute_parameters(), [
'contextlevel' => $contextlevel,
'instanceid' => $instanceid,
]);
$context = self::get_context_from_params($params);
self::validate_context($context);
$siteconfig = get_config('editor_tiny');
$branding = !empty($siteconfig->branding ?? true);
$extendedvalidelements = $siteconfig->extended_valid_elements ?? 'script[*],p[*],i[*]';
$installedlanguages = [];
foreach (get_string_manager()->get_list_of_translations(true) as $lang => $name) {
$installedlanguages[] = ['lang' => $lang, 'name' => $name];
}
$manager = new manager();
$plugins = [];
foreach ($manager->get_plugin_configuration_for_external($context) as $name => $settings) {
$plugin = [
'name' => $name,
'settings' => [],
];
foreach ($settings as $name => $value) {
$plugin['settings'][] = [
'name' => $name,
'value' => $value,
];
}
$plugins[] = $plugin;
}
return [
'contextid' => $context->id,
'branding' => $branding,
'extendedvalidelements' => $extendedvalidelements,
'installedlanguages' => $installedlanguages,
'plugins' => $plugins,
'warnings' => [],
];
}
/**
* Describes the return structure of the external function.
*
* @return external_single_structure
*/
public static function execute_returns(): external_single_structure {
return new external_single_structure([
'contextid' => new external_value(PARAM_INT, 'Context id'),
'branding' => new external_value(PARAM_BOOL, 'Display the TinyMCE logo'),
'extendedvalidelements' => new external_value(PARAM_RAW, 'Extended valid elements'),
'installedlanguages' => new external_multiple_structure(
new external_single_structure([
'lang' => new external_value(PARAM_LANG, 'Language code'),
'name' => new external_value(PARAM_RAW, 'Language name'),
]),
'List of installed languages',
),
'plugins' => new external_multiple_structure(
new external_single_structure([
'name' => new external_value(PARAM_PLUGIN, 'Name of the plugin'),
'settings' => new external_multiple_structure(
new external_single_structure([
'name' => new external_value(PARAM_RAW, 'Name of the setting'),
'value' => new external_value(PARAM_RAW, 'Value of the setting'),
]),
'Settings of the plugin',
),
]),
'Configuration of enabled plugins for the context'),
'warnings' => new external_warnings(),
]);
}
}
+42
View File
@@ -84,6 +84,48 @@ class manager {
return $plugins;
}
/**
* Get the configuration for external functions.
*
* @param context $context The context that the editor is used within.
*/
public function get_plugin_configuration_for_external(context $context): array {
$plugins = [];
$moodleplugins = \core_component::get_plugin_list_with_class('tiny', 'plugininfo');
$enabledplugins = \editor_tiny\plugininfo\tiny::get_enabled_plugins();
foreach ($moodleplugins as $plugin => $classname) {
[, $pluginname] = explode('_', $plugin, 2);
if (!in_array($pluginname, $enabledplugins)) {
// This plugin has been disabled.
continue;
}
if (!is_a($classname, plugin::class, true)) {
// Skip plugins that do not implement the plugin interface.
debugging("Plugin {$plugin} does not implement the plugin interface", DEBUG_DEVELOPER);
continue;
}
$options = ['pluginname' => $pluginname];
if (!$classname::is_enabled_for_external($context, $options)) {
// This plugin has disabled itself for some reason.
continue;
}
// Get the plugin configuration for external functions.
$pluginconfig = [];
if (is_a($classname, plugin_with_configuration_for_external::class, true)) {
$pluginconfig = $classname::get_plugin_configuration_for_external($context);
}
$plugins[$pluginname] = $pluginconfig;
}
return $plugins;
}
/**
* Get a list of the buttons provided by this plugin.
*
+12
View File
@@ -62,6 +62,18 @@ abstract class plugin {
return has_capability($capability, $context);
}
/**
* Whether the plugin is enabled and accessible for external functions.
*
* @param context $context The context that the editor is used within.
* @param array $options Additional options:
* - pluginname: Name of the plugin, without the "tiny_" prefix.
* @return bool
*/
public static function is_enabled_for_external(context $context, array $options): bool {
return static::is_enabled($context, $options, []);
}
/**
* Get the plugin information for the plugin.
*
@@ -0,0 +1,36 @@
<?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/>.
namespace editor_tiny;
use context;
/**
* An interface representing a plugin with configuration for external functions.
*
* @package editor_tiny
* @copyright 2025 Moodle Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
interface plugin_with_configuration_for_external {
/**
* Get the configuration for external functions provided by this plugin.
*
* @param context $context The context that the editor is used within.
* @return array
*/
public static function get_plugin_configuration_for_external(context $context): array;
}
+34
View File
@@ -0,0 +1,34 @@
<?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/>.
/**
* Tiny text editor webservice definitions.
*
* @package editor_tiny
* @copyright 2025 Moodle Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$functions = [
'editor_tiny_get_configuration' => [
'classname' => \editor_tiny\external\get_configuration::class,
'description' => 'Returns the TinyMCE configuration for a context.',
'type' => 'read',
'services' => [MOODLE_OFFICIAL_MOBILE_SERVICE],
],
];
@@ -25,6 +25,7 @@ use editor_tiny\editor;
use editor_tiny\plugin;
use editor_tiny\plugin_with_buttons;
use editor_tiny\plugin_with_configuration;
use editor_tiny\plugin_with_configuration_for_external;
use editor_tiny\plugin_with_menuitems;
/**
@@ -34,7 +35,12 @@ use editor_tiny\plugin_with_menuitems;
* @copyright 2024 Matt Porritt <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class plugininfo extends plugin implements plugin_with_buttons, plugin_with_menuitems, plugin_with_configuration {
class plugininfo extends plugin implements
plugin_with_buttons,
plugin_with_menuitems,
plugin_with_configuration,
plugin_with_configuration_for_external {
/**
* @var array|string[] The possible actions for the plugin.
*/
@@ -53,6 +59,13 @@ class plugininfo extends plugin implements plugin_with_buttons, plugin_with_menu
return in_array(true, self::get_allowed_actions($context, $options));
}
#[\Override]
public static function is_enabled_for_external(context $context, array $options): bool {
// Assume files are allowed. Otherwise, the generate_image action is ignored.
$options['maxfiles'] = 1;
return self::is_enabled($context, $options, []);
}
#[\Override]
public static function get_available_buttons(): array {
return array_map(fn ($action) => "tiny_aiplacement/{$action}", self::$possibleactions);
@@ -82,6 +95,16 @@ class plugininfo extends plugin implements plugin_with_buttons, plugin_with_menu
], $allowedactions);
}
#[\Override]
public static function get_plugin_configuration_for_external(context $context): array {
// Assume files are allowed. Otherwise, the generate_image action is ignored.
$options = ['maxfiles' => 1];
$settings = self::get_plugin_configuration_for_context($context, $options, []);
unset($settings['contextid']);
unset($settings['userid']);
return array_map(fn($value) => is_bool($value) ? ($value ? '1' : '0') : $value, $settings);
}
/**
* Get the allowed actions for the plugin.
*
@@ -0,0 +1,149 @@
<?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/>.
declare(strict_types=1);
namespace tiny_aiplacement;
use advanced_testcase;
/**
* Unit tests for the \tiny_aiplacement\plugininfo class.
*
* @package tiny_aiplacement
* @covers \tiny_aiplacement\plugininfo::is_enabled_for_external
* @covers \tiny_aiplacement\plugininfo::get_plugin_configuration_for_external
* @copyright 2025 Moodle Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class plugininfo_test extends advanced_testcase {
/**
* Basic setup for tests.
*/
public function setUp(): void {
parent::setUp();
$this->resetAfterTest(true);
$aimanager = \core\di::get(\core_ai\manager::class);
$aiprovider = $aimanager->create_provider_instance(
classname: '\aiprovider_openai\provider',
name: 'test_provider',
enabled: true,
config: ['apikey' => 'test_api_key'],
);
$aimanager->set_action_state(
plugin: $aiprovider->provider,
actionbasename: \core_ai\aiactions\generate_text::class::get_basename(),
enabled: 1,
instanceid: $aiprovider->id
);
$aimanager->set_action_state(
plugin: $aiprovider->provider,
actionbasename: \core_ai\aiactions\generate_image::class::get_basename(),
enabled: 1,
instanceid: $aiprovider->id
);
}
/**
* Test the is_enabled_for_external and get_plugin_configuration_for_external methods.
*
* @dataProvider for_external_provider
* @param ?string $role Role name to assign to the user. If null, no role is assigned.
* @param bool $enabled True if the aiplacement_editor must be enabled.
* @param bool $expectedenabled Expected result for is_enabled_for_external.
* @param array $expectedconfiguration Expected result for get_plugin_configuration_for_external.
* @return void
*/
public function test_for_external(?string $role, bool $enabled, bool $expectedenabled, array $expectedconfiguration): void {
global $CFG;
set_config('enabled', (int) $enabled, 'aiplacement_editor');
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$course = $generator->create_course();
$context = \context_course::instance($course->id);
if ($role) {
$generator->enrol_user($user->id, $course->id, $role);
}
$this->setUser($user);
$this->assertEquals($expectedenabled, plugininfo::is_enabled_for_external($context, ['pluginname' => 'aiplacement']));
$this->assertEquals($expectedconfiguration, plugininfo::get_plugin_configuration_for_external($context));
}
/**
* Data provider for test_for_external.
*
* @return array
*/
public static function for_external_provider(): array {
return [
[
'role' => null,
'enabled' => true,
'expectedenabled' => false,
'expectedconfiguration' => [
'policyagreed' => '0',
'generate_text' => '0',
'generate_image' => '0',
],
],
[
'role' => 'guest',
'enabled' => true,
'expectedenabled' => false,
'expectedconfiguration' => [
'policyagreed' => '0',
'generate_text' => '0',
'generate_image' => '0',
],
],
[
'role' => 'student',
'enabled' => true,
'expectedenabled' => true,
'expectedconfiguration' => [
'policyagreed' => '0',
'generate_text' => '1',
'generate_image' => '1',
],
],
[
'role' => 'teacher',
'enabled' => true,
'expectedenabled' => true,
'expectedconfiguration' => [
'policyagreed' => '0',
'generate_text' => '1',
'generate_image' => '1',
],
],
[
'role' => 'teacher',
'enabled' => false,
'expectedenabled' => false,
'expectedconfiguration' => [
'policyagreed' => '0',
'generate_text' => '0',
'generate_image' => '0',
],
],
];
}
}
@@ -30,6 +30,7 @@ use editor_tiny\editor;
use editor_tiny\plugin;
use editor_tiny\plugin_with_buttons;
use editor_tiny\plugin_with_configuration;
use editor_tiny\plugin_with_configuration_for_external;
use editor_tiny\plugin_with_menuitems;
use filter_manager;
@@ -40,7 +41,11 @@ use filter_manager;
* @copyright 2022 Huong Nguyen <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class plugininfo extends plugin implements plugin_with_buttons, plugin_with_menuitems, plugin_with_configuration {
class plugininfo extends plugin implements
plugin_with_buttons,
plugin_with_menuitems,
plugin_with_configuration,
plugin_with_configuration_for_external {
public static function get_available_buttons(): array {
return [
@@ -103,4 +108,14 @@ class plugininfo extends plugin implements plugin_with_buttons, plugin_with_menu
'texdocsurl' => get_docs_url('Using_TeX_Notation'),
];
}
#[\Override]
public static function get_plugin_configuration_for_external(context $context): array {
$settings = self::get_plugin_configuration_for_context($context, [], []);
return [
'texfilter' => $settings['texfilter'] ? '1' : '0',
'libraries' => json_encode($settings['libraries']),
'texdocsurl' => $settings['texdocsurl'],
];
}
}
@@ -0,0 +1,107 @@
<?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/>.
declare(strict_types=1);
namespace tiny_equation;
use advanced_testcase;
/**
* Unit tests for the \tiny_equation\plugininfo class.
*
* @package tiny_equation
* @covers \tiny_equation\plugininfo::get_plugin_configuration_for_external
* @copyright 2025 Moodle Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class plugininfo_test extends advanced_testcase {
/**
* Basic setup for tests.
*/
public function setUp(): void {
parent::setUp();
$this->resetAfterTest(true);
}
/**
* Test the get_plugin_configuration_for_external method.
*
* @dataProvider get_plugin_configuration_for_external_provider
* @param bool $enabled True if the filter must be enabled.
* @param array $expectedconfiguration Expected configuration.
* @return void
*/
public function test_get_plugin_configuration_for_external(bool $enabled, array $expectedconfiguration): void {
global $CFG;
$filtermanager = \core_plugin_manager::resolve_plugininfo_class('filter');
$filtermanager::enable_plugin('mathjaxloader', $enabled ? TEXTFILTER_ON : TEXTFILTER_OFF);
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$context = \context_system::instance();
$this->setUser($user);
$this->assertEquals($expectedconfiguration, plugininfo::get_plugin_configuration_for_external($context));
}
/**
* Data provider for test_get_plugin_configuration_for_external.
*
* @return array
*/
public static function get_plugin_configuration_for_external_provider(): array {
$settings = [
'libraries' => json_encode([
[
'key' => 'group1',
'groupname' => get_string('librarygroup1', 'tiny_equation'),
'elements' => explode("\n", trim(get_config('tiny_equation', 'librarygroup1'))),
'active' => true,
],
[
'key' => 'group2',
'groupname' => get_string('librarygroup2', 'tiny_equation'),
'elements' => explode("\n", trim(get_config('tiny_equation', 'librarygroup2'))),
],
[
'key' => 'group3',
'groupname' => get_string('librarygroup3', 'tiny_equation'),
'elements' => explode("\n", trim(get_config('tiny_equation', 'librarygroup3'))),
],
[
'key' => 'group4',
'groupname' => get_string('librarygroup4', 'tiny_equation'),
'elements' => explode("\n", trim(get_config('tiny_equation', 'librarygroup4'))),
],
]),
'texdocsurl' => get_docs_url('Using_TeX_Notation'),
];
return [
[
'enabled' => true,
'expectedconfiguration' => ['texfilter' => '1', ...$settings],
],
[
'enabled' => false,
'expectedconfiguration' => ['texfilter' => '0', ...$settings],
],
];
}
}
@@ -21,6 +21,7 @@ use editor_tiny\plugin;
use editor_tiny\plugin_with_buttons;
use editor_tiny\plugin_with_menuitems;
use editor_tiny\plugin_with_configuration;
use editor_tiny\plugin_with_configuration_for_external;
/**
* Tiny H5P plugin for Moodle.
@@ -32,7 +33,8 @@ use editor_tiny\plugin_with_configuration;
class plugininfo extends plugin implements
plugin_with_buttons,
plugin_with_menuitems,
plugin_with_configuration {
plugin_with_configuration,
plugin_with_configuration_for_external {
public static function get_available_buttons(): array {
return [
@@ -63,4 +65,13 @@ class plugininfo extends plugin implements
'storeinrepo' => true,
];
}
#[\Override]
public static function get_plugin_configuration_for_external(context $context): array {
$settings = self::get_plugin_configuration_for_context($context, [], []);
return [
'embedallowed' => $settings['permissions']['embed'] ? '1' : '0',
'uploadallowed' => $settings['permissions']['upload'] ? '1' : '0',
];
}
}
@@ -0,0 +1,93 @@
<?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/>.
declare(strict_types=1);
namespace tiny_h5p;
use advanced_testcase;
/**
* Unit tests for the \tiny_h5p\plugininfo class.
*
* @package tiny_h5p
* @covers \tiny_h5p\plugininfo::get_plugin_configuration_for_external
* @copyright 2025 Moodle Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class plugininfo_test extends advanced_testcase {
/**
* Basic setup for tests.
*/
public function setUp(): void {
parent::setUp();
$this->resetAfterTest(true);
}
/**
* Test the get_plugin_configuration_for_external method.
*
* @dataProvider get_plugin_configuration_for_external_provider
* @param ?string $role Role name to assign to the user. If null, no role is assigned.
* @param array $expectedconfiguration Expected configuration.
* @return void
*/
public function test_get_plugin_configuration_for_external(?string $role, array $expectedconfiguration): void {
global $CFG;
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$course = $generator->create_course();
$context = \context_course::instance($course->id);
if ($role) {
$generator->enrol_user($user->id, $course->id, $role);
}
$this->setUser($user);
$this->assertEquals($expectedconfiguration, plugininfo::get_plugin_configuration_for_external($context));
}
/**
* Data provider for test_get_plugin_configuration_for_external.
*
* @return array
*/
public static function get_plugin_configuration_for_external_provider(): array {
return [
[
'role' => null,
'expectedconfiguration' => ['embedallowed' => '0', 'uploadallowed' => '0'],
],
[
'role' => 'guest',
'expectedconfiguration' => ['embedallowed' => '0', 'uploadallowed' => '0'],
],
[
'role' => 'student',
'expectedconfiguration' => ['embedallowed' => '0', 'uploadallowed' => '0'],
],
[
'role' => 'teacher',
'expectedconfiguration' => ['embedallowed' => '0', 'uploadallowed' => '0'],
],
[
'role' => 'editingteacher',
'expectedconfiguration' => ['embedallowed' => '1', 'uploadallowed' => '1'],
],
];
}
}
@@ -51,6 +51,13 @@ class plugininfo extends plugin implements plugin_with_buttons, plugin_with_menu
has_capability('tiny/media:use', $context);
}
#[\Override]
public static function is_enabled_for_external(context $context, array $options): bool {
// Assume files are allowed.
$options['maxfiles'] = 1;
return self::is_enabled($context, $options, []);
}
public static function get_available_buttons(): array {
return [
'tiny_media/tiny_media_image',
@@ -0,0 +1,75 @@
<?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/>.
declare(strict_types=1);
namespace tiny_media;
use advanced_testcase;
/**
* Unit tests for the \tiny_media\plugininfo class.
*
* @package tiny_media
* @covers \tiny_media\plugininfo::is_enabled_for_external
* @copyright 2025 Moodle Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class plugininfo_test extends advanced_testcase {
/**
* Basic setup for tests.
*/
public function setUp(): void {
parent::setUp();
$this->resetAfterTest(true);
}
/**
* Test the is_enabled_for_external method.
*
* @dataProvider is_enabled_for_external_provider
* @param bool $guest True to use guest user.
* @param bool $expectedenabled Expected result.
* @return void
*/
public function test_is_enabled_for_external(bool $guest, bool $expectedenabled): void {
global $CFG;
$generator = $this->getDataGenerator();
if ($guest) {
$this->setGuestUser();
} else {
$user = $generator->create_user();
$this->setUser($user);
}
$context = \context_system::instance();
$this->assertEquals($expectedenabled, plugininfo::is_enabled_for_external($context, ['pluginname' => 'media']));
}
/**
* Data provider for test_is_enabled_for_external.
*
* @return array
*/
public static function is_enabled_for_external_provider(): array {
return [
['guest' => false, 'expectedenabled' => true],
['guest' => true, 'expectedenabled' => false],
];
}
}
@@ -20,6 +20,7 @@ use context;
use editor_tiny\editor;
use editor_tiny\plugin;
use editor_tiny\plugin_with_configuration;
use editor_tiny\plugin_with_configuration_for_external;
use tiny_premium\manager;
/**
@@ -29,7 +30,7 @@ use tiny_premium\manager;
* @copyright 2023 David Woloszyn <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class plugininfo extends plugin implements plugin_with_configuration {
class plugininfo extends plugin implements plugin_with_configuration, plugin_with_configuration_for_external {
#[\Override]
public static function is_enabled(
@@ -68,4 +69,12 @@ class plugininfo extends plugin implements plugin_with_configuration {
'premiumplugins' => implode(',', $allowedplugins),
];
}
#[\Override]
public static function get_plugin_configuration_for_external(context $context): array {
$settings = self::get_plugin_configuration_for_context($context, [], []);
return [
'premiumplugins' => $settings['premiumplugins'],
];
}
}
@@ -33,6 +33,13 @@ $capabilities = [
],
'clonepermissionsfrom' => 'tiny/premium:accesspremium',
],
'tiny/premium:usea11ychecker' => [
'captype' => 'read',
'contextlevel' => CONTEXT_USER,
'archetypes' => [
'user' => CAP_ALLOW,
],
],
'tiny/premium:useadvtable' => [
'captype' => 'read',
'contextlevel' => CONTEXT_USER,
@@ -32,5 +32,6 @@ $functions = [
'type' => 'read',
'capabilities' => '',
'ajax' => true,
'services' => [MOODLE_OFFICIAL_MOBILE_SERVICE],
],
];
@@ -0,0 +1,63 @@
<?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/>.
declare(strict_types=1);
namespace tiny_premium;
use advanced_testcase;
/**
* Unit tests for the \tiny_premium\plugininfo class.
*
* @package tiny_premium
* @covers \tiny_premium\plugininfo::get_plugin_configuration_for_external
* @copyright 2025 Moodle Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class plugininfo_test extends advanced_testcase {
/**
* Basic setup for tests.
*/
public function setUp(): void {
parent::setUp();
$this->resetAfterTest(true);
foreach (\tiny_premium\manager::get_plugins() as $plugin) {
\tiny_premium\manager::set_plugin_config(['enabled' => 1], $plugin);
}
}
/**
* Test the get_plugin_configuration_for_external method.
*
* @return void
*/
public function test_get_plugin_configuration_for_external(): void {
global $CFG;
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$context = \context_system::instance();
$this->setUser($user);
$this->assertEquals(
['premiumplugins' => implode(',', \tiny_premium\manager::get_plugins())],
plugininfo::get_plugin_configuration_for_external($context)
);
}
}
+1 -1
View File
@@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2025021400;
$plugin->version = 2025021401;
$plugin->requires = 2024100100;
$plugin->component = 'tiny_premium';
@@ -21,6 +21,7 @@ use editor_tiny\editor;
use editor_tiny\plugin;
use editor_tiny\plugin_with_buttons;
use editor_tiny\plugin_with_configuration;
use editor_tiny\plugin_with_configuration_for_external;
use editor_tiny\plugin_with_menuitems;
/**
@@ -30,7 +31,11 @@ use editor_tiny\plugin_with_menuitems;
* @copyright 2022 Stevani Andolo <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class plugininfo extends plugin implements plugin_with_buttons, plugin_with_menuitems, plugin_with_configuration {
class plugininfo extends plugin implements
plugin_with_buttons,
plugin_with_menuitems,
plugin_with_configuration,
plugin_with_configuration_for_external {
#[\Override]
public static function is_enabled(
@@ -47,6 +52,13 @@ class plugininfo extends plugin implements plugin_with_buttons, plugin_with_menu
return isloggedin() && !isguestuser() && $canhavefiles && has_capability('tiny/recordrtc:use', $context);
}
#[\Override]
public static function is_enabled_for_external(context $context, array $options): bool {
// Assume files are allowed.
$options['maxfiles'] = 1;
return self::is_enabled($context, $options, []);
}
public static function get_available_buttons(): array {
return [
'tiny_recordrtc/tiny_recordrtc_image',
@@ -136,4 +148,26 @@ class plugininfo extends plugin implements plugin_with_buttons, plugin_with_menu
'pausingAllowed' => $allowedpausing,
];
}
#[\Override]
public static function get_plugin_configuration_for_external(context $context): array {
$settings = self::get_plugin_configuration_for_context($context, [], []);
return [
'videoallowed' => $settings['videoAllowed'] ? '1' : '0',
'audioallowed' => $settings['audioAllowed'] ? '1' : '0',
'screenallowed' => $settings['screenAllowed'] ? '1' : '0',
'pausingallowed' => $settings['pausingAllowed'] ? '1' : '0',
'allowedtypes' => implode(',', $settings['data']['params']['allowedtypes']),
'audiobitrate' => $settings['data']['params']['audiobitrate'],
'videobitrate' => $settings['data']['params']['videobitrate'],
'screenbitrate' => $settings['data']['params']['screenbitrate'],
'audiotimelimit' => $settings['data']['params']['audiotimelimit'],
'videotimelimit' => $settings['data']['params']['videotimelimit'],
'screentimelimit' => $settings['data']['params']['screentimelimit'],
'maxrecsize' => (string) $settings['data']['params']['maxrecsize'],
'videoscreenwidth' => $settings['data']['params']['videoscreenwidth'],
'videoscreenheight' => $settings['data']['params']['videoscreenheight'],
'audiortcformat' => (string) $settings['data']['params']['audiortcformat'],
];
}
}
@@ -0,0 +1,112 @@
<?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/>.
declare(strict_types=1);
namespace tiny_recordrtc;
use advanced_testcase;
/**
* Unit tests for the \tiny_recordrtc\plugininfo class.
*
* @package tiny_recordrtc
* @covers \tiny_recordrtc\plugininfo::is_enabled_for_external
* @covers \tiny_recordrtc\plugininfo::get_plugin_configuration_for_external
* @copyright 2025 Moodle Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class plugininfo_test extends advanced_testcase {
/**
* Basic setup for tests.
*/
public function setUp(): void {
parent::setUp();
$this->resetAfterTest(true);
}
/**
* Test the is_enabled_for_external and get_plugin_configuration_for_external methods.
*
* @dataProvider for_external_provider
* @param bool $guest Use a guest user.
* @param bool $expectedenabled Expected result for is_enabled_for_external.
* @param array $expectedconfiguration Expected result for get_plugin_configuration_for_external.
* @return void
*/
public function test_for_external(bool $guest, bool $expectedenabled, array $expectedconfiguration): void {
global $CFG;
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$context = \context_system::instance();
if ($guest) {
$this->setUser($user);
} else {
$this->setGuestUser();
}
$this->assertEquals($expectedenabled, plugininfo::is_enabled_for_external($context, ['pluginname' => 'recordrtc']));
$this->assertEquals($expectedconfiguration, plugininfo::get_plugin_configuration_for_external($context));
}
/**
* Data provider for test_for_external.
*
* @return array
*/
public static function for_external_provider(): array {
$settings = [
'pausingallowed' => get_config('tiny_recordrtc', 'allowedpausing'),
'allowedtypes' => get_config('tiny_recordrtc', 'allowedtypes'),
'audiobitrate' => get_config('tiny_recordrtc', 'audiobitrate'),
'videobitrate' => get_config('tiny_recordrtc', 'videobitrate'),
'screenbitrate' => get_config('tiny_recordrtc', 'screenbitrate'),
'audiotimelimit' => get_config('tiny_recordrtc', 'audiotimelimit'),
'videotimelimit' => get_config('tiny_recordrtc', 'videotimelimit'),
'screentimelimit' => get_config('tiny_recordrtc', 'screentimelimit'),
'maxrecsize' => (string) get_max_upload_file_size(),
'videoscreenwidth' => explode(',', get_config('tiny_recordrtc', 'screensize'))[0],
'videoscreenheight' => explode(',', get_config('tiny_recordrtc', 'screensize'))[1],
'audiortcformat' => (string) get_config('tiny_recordrtc', 'audiortcformat'),
];
$allowedtypes = explode(',', $settings['allowedtypes']);
return [
[
'guest' => false,
'expectedenabled' => false,
'expectedconfiguration' => [
'videoallowed' => '0',
'audioallowed' => '0',
'screenallowed' => '0',
...$settings,
],
],
[
'guest' => true,
'expectedenabled' => true,
'expectedconfiguration' => [
'videoallowed' => in_array('video', $allowedtypes) ? '1' : '0',
'audioallowed' => in_array('audio', $allowedtypes) ? '1' : '0',
'screenallowed' => in_array('screen', $allowedtypes) ? '1' : '0',
...$settings,
],
],
];
}
}
@@ -0,0 +1,177 @@
<?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/>.
declare(strict_types=1);
namespace editor_tiny\external;
use advanced_testcase;
use core_external\external_api;
use editor_tiny\plugininfo\tiny;
/**
* Unit tests for the editor_tiny\external get_configuration class.
*
* @package editor_tiny
* @covers \editor_tiny\external\get_configuration
* @covers \editor_tiny\manager::get_plugin_configuration_for_external
* @covers \editor_tiny\plugin::is_enabled_for_external
* @copyright 2025 Moodle Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class get_configuration_test extends advanced_testcase {
/**
* Basic setup for tests.
*/
public function setUp(): void {
parent::setUp();
$this->resetAfterTest(true);
// Global editor settings.
set_config('branding', false, 'editor_tiny');
set_config('extended_valid_elements', 'script[*]', 'editor_tiny');
// Editor plugins.
foreach (\editor_tiny\plugininfo\tiny::get_enabled_plugins() as $plugin) {
tiny::enable_plugin($plugin, 0);
}
tiny::enable_plugin('h5p', 1); // Plugin with get_plugin_configuration_for_external.
tiny::enable_plugin('media', 1); // Plugin with is_enabled_for_external.
tiny::enable_plugin('html', 1); // Plugin with no overriden methods.
}
/**
* Test the external function.
*
* @dataProvider execute_provider
* @param string $contextlevel Context level: system, course or module.
* @param ?string $role Role name to assign to use. If null, no role is assigned.
* @return void
*/
public function test_execute(string $contextlevel, ?string $role): void {
global $CFG;
// Setup user and context.
$generator = $this->getDataGenerator();
$user = $generator->create_user();
if ($contextlevel == 'system') {
$context = \core\context\system::instance();
if ($role) {
$generator->role_assign($role, $user->id, $context);
}
} else if ($contextlevel == 'course' || $contextlevel == 'module') {
$course = $generator->create_course();
if ($contextlevel == 'module') {
$module = $generator->create_module('forum', ['course' => $course->id]);
$context = \core\context\module::instance($module->cmid);
} else {
$context = \core\context\course::instance($course->id);
}
if ($role) {
$generator->enrol_user($user->id, $course->id, $role);
} else {
$this->expectException(\core\exception\require_login_exception::class);
}
} else {
throw new \coding_exception("Invalid context level: {$contextlevel}");
}
$this->setUser($user);
// Execute function.
$result = get_configuration::execute($contextlevel, (int) $context->instanceid);
$result = external_api::clean_returnvalue(get_configuration::execute_returns(), $result);
// Check context id.
self::assertEquals($context->id, $result['contextid']);
// Check global settings.
self::assertEquals(get_config('editor_tiny', 'branding'), $result['branding']);
self::assertEquals(get_config('editor_tiny', 'extended_valid_elements'), $result['extendedvalidelements']);
self::assertEquals(self::get_installed_languages(), $result['installedlanguages']);
// Check plugin settings.
$plugins = [];
if (\tiny_h5p\plugininfo::is_enabled($context, ['pluginname' => 'h5p'], [])) {
$settings = [];
foreach (\tiny_h5p\plugininfo::get_plugin_configuration_for_external($context) as $name => $value) {
$settings[] = ['name' => $name, 'value' => $value];
}
$plugins[] = ['name' => 'h5p', 'settings' => $settings];
}
$plugins[] = ['name' => 'html', 'settings' => []];
if (\tiny_media\plugininfo::is_enabled_for_external($context, ['plugginname' => 'media'])) {
$plugins[] = ['name' => 'media', 'settings' => []];
}
self::assertEquals($plugins, $result['plugins']);
}
/**
* Data provider for test_execute.
*
* @return array
*/
public static function execute_provider(): array {
return [
['contextlevel' => 'system', 'role' => null],
['contextlevel' => 'system', 'role' => 'guest'],
['contextlevel' => 'system', 'role' => 'manager'],
['contextlevel' => 'course', 'role' => null],
['contextlevel' => 'course', 'role' => 'guest'],
['contextlevel' => 'course', 'role' => 'student'],
['contextlevel' => 'course', 'role' => 'teacher'],
['contextlevel' => 'course', 'role' => 'editingteacher'],
['contextlevel' => 'module', 'role' => null],
['contextlevel' => 'module', 'role' => 'guest'],
['contextlevel' => 'module', 'role' => 'student'],
['contextlevel' => 'module', 'role' => 'teacher'],
['contextlevel' => 'module', 'role' => 'editingteacher'],
];
}
/**
* Test the external function with an invalid context level.
*/
public function test_execute_invalid_context_level(): void {
$this->expectException(\invalid_parameter_exception::class);
get_configuration::execute('invalid', (int) SITEID);
}
/**
* Test the external function with an invalid instance ID.
*/
public function test_execute_invalid_instance_id(): void {
$this->expectException(\invalid_parameter_exception::class);
get_configuration::execute('course', -1);
}
/**
* Returns the expected list of installed languages returned by the external function.
*
* @return array
*/
private static function get_installed_languages(): array {
$installedlanguages = [];
foreach (get_string_manager()->get_list_of_translations(true) as $lang => $name) {
$installedlanguages[] = ['lang' => $lang, 'name' => $name];
}
return $installedlanguages;
}
}
+1 -1
View File
@@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2024121800; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2024121801; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2024100100;
$plugin->component = 'editor_tiny'; // Full name of the plugin (used for diagnostics).