MDL-76583 core_external: Migrate external_settings class

This commit is contained in:
Andrew Nicols 2022-12-07 13:14:18 +08:00
parent e460e2e43f
commit ecbb31b160
4 changed files with 244 additions and 197 deletions

View File

@ -0,0 +1,184 @@
<?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 core_external;
/**
* Singleton to handle the external settings.
*
* We use singleton to encapsulate the "logic"
*
* @package core_webservice
* @copyright 2012 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.3
*/
class external_settings {
/** @var object the singleton instance */
public static $instance = null;
/** @var boolean Should the external function return raw text or formatted */
private $raw = false;
/** @var boolean Should the external function filter the text */
private $filter = false;
/** @var boolean Should the external function rewrite plugin file url */
private $fileurl = true;
/** @var string In which file should the urls be rewritten */
private $file = 'webservice/pluginfile.php';
/** @var string The session lang */
private $lang = '';
/** @var string The timezone to use during this WS request */
private $timezone = '';
/**
* Constructor - protected - can not be instanciated
*/
protected function __construct() {
if ((AJAX_SCRIPT == false) && (CLI_SCRIPT == false) && (WS_SERVER == false)) {
// For normal pages, the default should match the default for format_text.
$this->filter = true;
// Use pluginfile.php for web requests.
$this->file = 'pluginfile.php';
}
}
/**
* Return only one instance
*
* @return self
*/
public static function get_instance() {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Set raw
*
* @param boolean $raw
*/
public function set_raw($raw) {
$this->raw = $raw;
}
/**
* Get raw
*
* @return boolean
*/
public function get_raw() {
return $this->raw;
}
/**
* Set filter
*
* @param boolean $filter
*/
public function set_filter($filter) {
$this->filter = $filter;
}
/**
* Get filter
*
* @return boolean
*/
public function get_filter() {
return $this->filter;
}
/**
* Set fileurl
*
* @param bool $fileurl
*/
public function set_fileurl($fileurl) {
$this->fileurl = $fileurl;
}
/**
* Get fileurl
*
* @return bool
*/
public function get_fileurl() {
return $this->fileurl;
}
/**
* Set file
*
* @param string $file
*/
public function set_file($file) {
$this->file = $file;
}
/**
* Get file
*
* @return string
*/
public function get_file() {
return $this->file;
}
/**
* Set lang
*
* @param string $lang
*/
public function set_lang($lang) {
$this->lang = $lang;
}
/**
* Get lang
*
* @return string
*/
public function get_lang() {
return $this->lang;
}
/**
* Set timezone
*
* @param string $timezone
*/
public function set_timezone($timezone) {
$this->timezone = $timezone;
}
/**
* Get timezone
*
* @return string
*/
public function get_timezone() {
return $this->timezone;
}
}

View File

@ -0,0 +1,59 @@
<?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 core_external;
/**
* Unit tests for core_external\external_settings.
*
* @package core_external
* @category test
* @copyright 2022 Andrew Lyons <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @covers \core_external\external_settings
*/
class external_settings_test extends \advanced_testcase {
/**
* Tests for external_settings class.
*/
public function test_external_settings() {
$settings = external_settings::get_instance();
$currentraw = $settings->get_raw();
$currentfilter = $settings->get_filter();
$currentfile = $settings->get_file();
$currentfileurl = $settings->get_fileurl();
$this->assertInstanceOf(external_settings::class, $settings);
// Check apis.
$settings->set_file('plugin.php');
$this->assertEquals('plugin.php', $settings->get_file());
$settings->set_filter(false);
$this->assertFalse($settings->get_filter());
$settings->set_fileurl(false);
$this->assertFalse($settings->get_fileurl());
$settings->set_raw(true);
$this->assertTrue($settings->get_raw());
// Restore original values.
$settings->set_file($currentfile);
$settings->set_filter($currentfilter);
$settings->set_fileurl($currentfileurl);
$settings->set_raw($currentraw);
}
}

View File

@ -36,6 +36,7 @@ class_alias(\core_external\external_function_parameters::class, 'external_functi
class_alias(\core_external\util::class, 'external_util'); class_alias(\core_external\util::class, 'external_util');
class_alias(\core_external\external_files::class, 'external_files'); class_alias(\core_external\external_files::class, 'external_files');
class_alias(\core_external\external_warnings::class, 'external_warnings'); class_alias(\core_external\external_warnings::class, 'external_warnings');
class_alias(\core_external\external_settings::class, 'external_settings');
/** /**
* Generate a token * Generate a token
@ -438,170 +439,3 @@ function external_log_token_request($token) {
\core\task\manager::reschedule_or_queue_adhoc_task($task); \core\task\manager::reschedule_or_queue_adhoc_task($task);
} }
} }
/**
* Singleton to handle the external settings.
*
* We use singleton to encapsulate the "logic"
*
* @package core_webservice
* @copyright 2012 Jerome Mouneyrac
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 2.3
*/
class external_settings {
/** @var object the singleton instance */
public static $instance = null;
/** @var boolean Should the external function return raw text or formatted */
private $raw = false;
/** @var boolean Should the external function filter the text */
private $filter = false;
/** @var boolean Should the external function rewrite plugin file url */
private $fileurl = true;
/** @var string In which file should the urls be rewritten */
private $file = 'webservice/pluginfile.php';
/** @var string The session lang */
private $lang = '';
/** @var string The timezone to use during this WS request */
private $timezone = '';
/**
* Constructor - protected - can not be instanciated
*/
protected function __construct() {
if ((AJAX_SCRIPT == false) && (CLI_SCRIPT == false) && (WS_SERVER == false)) {
// For normal pages, the default should match the default for format_text.
$this->filter = true;
// Use pluginfile.php for web requests.
$this->file = 'pluginfile.php';
}
}
/**
* Return only one instance
*
* @return \external_settings
*/
public static function get_instance() {
if (self::$instance === null) {
self::$instance = new external_settings;
}
return self::$instance;
}
/**
* Set raw
*
* @param boolean $raw
*/
public function set_raw($raw) {
$this->raw = $raw;
}
/**
* Get raw
*
* @return boolean
*/
public function get_raw() {
return $this->raw;
}
/**
* Set filter
*
* @param boolean $filter
*/
public function set_filter($filter) {
$this->filter = $filter;
}
/**
* Get filter
*
* @return boolean
*/
public function get_filter() {
return $this->filter;
}
/**
* Set fileurl
*
* @param boolean $fileurl
*/
public function set_fileurl($fileurl) {
$this->fileurl = $fileurl;
}
/**
* Get fileurl
*
* @return boolean
*/
public function get_fileurl() {
return $this->fileurl;
}
/**
* Set file
*
* @param string $file
*/
public function set_file($file) {
$this->file = $file;
}
/**
* Get file
*
* @return string
*/
public function get_file() {
return $this->file;
}
/**
* Set lang
*
* @param string $lang
*/
public function set_lang($lang) {
$this->lang = $lang;
}
/**
* Get lang
*
* @return string
*/
public function get_lang() {
return $this->lang;
}
/**
* Set timezone
*
* @param string $timezone
*/
public function set_timezone($timezone) {
$this->timezone = $timezone;
}
/**
* Get timezone
*
* @return string
*/
public function get_timezone() {
return $this->timezone;
}
}

View File

@ -43,36 +43,6 @@ class externallib_test extends \advanced_testcase {
} }
} }
/**
* Tests for external_settings class.
*/
public function test_external_settings() {
$settings = \external_settings::get_instance();
$currentraw = $settings->get_raw();
$currentfilter = $settings->get_filter();
$currentfile = $settings->get_file();
$currentfileurl = $settings->get_fileurl();
$this->assertInstanceOf(\external_settings::class, $settings);
// Check apis.
$settings->set_file('plugin.php');
$this->assertEquals('plugin.php', $settings->get_file());
$settings->set_filter(false);
$this->assertFalse($settings->get_filter());
$settings->set_fileurl(false);
$this->assertFalse($settings->get_fileurl());
$settings->set_raw(true);
$this->assertTrue($settings->get_raw());
// Restore original values.
$settings->set_file($currentfile);
$settings->set_filter($currentfilter);
$settings->set_fileurl($currentfileurl);
$settings->set_raw($currentraw);
}
public function test_external_format_text() { public function test_external_format_text() {
$settings = \external_settings::get_instance(); $settings = \external_settings::get_instance();