From 489f6f268445eeeac496804e77bfcbbd4896b8cb Mon Sep 17 00:00:00 2001 From: Ferran Recio Date: Tue, 25 Aug 2020 11:06:14 +0200 Subject: [PATCH] MDL-69583 tool_customlang: add customization import --- admin/tool/customlang/classes/form/import.php | 68 + .../customlang/classes/local/importer.php | 268 ++++ .../classes/local/mlang/langstring.php | 177 +++ .../classes/local/mlang/logstatus.php | 92 ++ .../classes/local/mlang/phpparser.php | 260 ++++ admin/tool/customlang/import.php | 74 ++ admin/tool/customlang/index.php | 5 + .../customlang/lang/en/tool_customlang.php | 12 + .../tests/behat/import_files.feature | 48 + .../tests/behat/import_mode.feature | 81 ++ .../customlang/tests/fixtures/customlang.zip | Bin 0 -> 2329 bytes .../tests/fixtures/mod_fakecomponent.php | 29 + .../tool/customlang/tests/fixtures/moodle.php | 28 + .../tests/fixtures/tool_customlang.php | 28 + .../tests/local/mlang/langstring_test.php | 1090 +++++++++++++++++ .../tests/local/mlang/phpparser_test.php | 207 ++++ admin/tool/customlang/version.php | 2 +- 17 files changed, 2468 insertions(+), 1 deletion(-) create mode 100644 admin/tool/customlang/classes/form/import.php create mode 100644 admin/tool/customlang/classes/local/importer.php create mode 100644 admin/tool/customlang/classes/local/mlang/langstring.php create mode 100644 admin/tool/customlang/classes/local/mlang/logstatus.php create mode 100644 admin/tool/customlang/classes/local/mlang/phpparser.php create mode 100644 admin/tool/customlang/import.php create mode 100644 admin/tool/customlang/tests/behat/import_files.feature create mode 100644 admin/tool/customlang/tests/behat/import_mode.feature create mode 100644 admin/tool/customlang/tests/fixtures/customlang.zip create mode 100644 admin/tool/customlang/tests/fixtures/mod_fakecomponent.php create mode 100644 admin/tool/customlang/tests/fixtures/moodle.php create mode 100644 admin/tool/customlang/tests/fixtures/tool_customlang.php create mode 100644 admin/tool/customlang/tests/local/mlang/langstring_test.php create mode 100644 admin/tool/customlang/tests/local/mlang/phpparser_test.php diff --git a/admin/tool/customlang/classes/form/import.php b/admin/tool/customlang/classes/form/import.php new file mode 100644 index 00000000000..3d4d0685ba8 --- /dev/null +++ b/admin/tool/customlang/classes/form/import.php @@ -0,0 +1,68 @@ +. + +/** + * Upload a zip of custom lang php files. + * + * @package tool_customlang + * @copyright 2020 Ferran Recio + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_customlang\form; + +use tool_customlang\local\importer; + +/** + * Upload a zip/php of custom lang php files. + * + * @copyright 2020 Ferran Recio + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class import extends \moodleform { + + /** + * Form definition. + */ + public function definition() { + $mform = $this->_form; + $mform->addElement('header', 'settingsheader', get_string('import', 'tool_customlang')); + + $mform->addElement('hidden', 'lng'); + $mform->setType('lng', PARAM_LANG); + $mform->setDefault('lng', $this->_customdata['lng']); + + $filemanageroptions = array( + 'accepted_types' => array('.php', '.zip'), + 'maxbytes' => 0, + 'maxfiles' => 1, + 'subdirs' => 0 + ); + + $mform->addElement('filepicker', 'pack', get_string('langpack', 'tool_customlang'), + null, $filemanageroptions); + $mform->addRule('pack', null, 'required'); + + $modes = [ + importer::IMPORTALL => get_string('import_all', 'tool_customlang'), + importer::IMPORTUPDATE => get_string('import_update', 'tool_customlang'), + importer::IMPORTNEW => get_string('import_new', 'tool_customlang'), + ]; + $mform->addElement('select', 'importmode', get_string('import_mode', 'tool_customlang'), $modes); + + $mform->addElement('submit', 'importcustomstrings', get_string('importfile', 'tool_customlang')); + } +} diff --git a/admin/tool/customlang/classes/local/importer.php b/admin/tool/customlang/classes/local/importer.php new file mode 100644 index 00000000000..955ec3fdac0 --- /dev/null +++ b/admin/tool/customlang/classes/local/importer.php @@ -0,0 +1,268 @@ +. + +/** + * Custom lang importer. + * + * @package tool_customlang + * @copyright 2020 Ferran Recio + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_customlang\local; + +use tool_customlang\local\mlang\phpparser; +use tool_customlang\local\mlang\logstatus; +use tool_customlang\local\mlang\langstring; +use core\output\notification; +use stored_file; +use coding_exception; +use moodle_exception; +use core_component; +use stdClass; + +/** + * Class containing tha custom lang importer + * + * @package tool_customlang + * @copyright 2020 Ferran Recio + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class importer { + + /** @var int imports will only create new customizations */ + public const IMPORTNEW = 1; + /** @var int imports will only update the current customizations */ + public const IMPORTUPDATE = 2; + /** @var int imports all strings */ + public const IMPORTALL = 3; + + /** + * @var string the language name + */ + protected $lng; + + /** + * @var int the importation mode (new, update, all) + */ + protected $importmode; + + /** + * @var string request folder path + */ + private $folder; + + /** + * @var array import log messages + */ + private $log; + + /** + * Constructor for the importer class. + * + * @param string $lng the current language to import. + * @param int $importmode the import method (IMPORTALL, IMPORTNEW, IMPORTUPDATE). + */ + public function __construct(string $lng, int $importmode = self::IMPORTALL) { + $this->lng = $lng; + $this->importmode = $importmode; + $this->log = []; + } + + /** + * Returns the last parse log. + * + * @return logstatus[] mlang logstatus with the messages + */ + public function get_log(): array { + return $this->log; + } + + /** + * Import customlang files. + * + * @param stored_file[] $files array of files to import + */ + public function import(array $files): void { + // Create a temporal folder to store the files. + $this->folder = make_request_directory(false); + + $langfiles = $this->deploy_files($files); + + $this->process_files($langfiles); + } + + /** + * Deploy all files into a request folder. + * + * @param stored_file[] $files array of files to deploy + * @return string[] of file paths + */ + private function deploy_files(array $files): array { + $result = []; + // Desploy all files. + foreach ($files as $file) { + if ($file->get_mimetype() == 'application/zip') { + $result = array_merge($result, $this->unzip_file($file)); + } else { + $path = $this->folder.'/'.$file->get_filename(); + $file->copy_content_to($path); + $result = array_merge($result, [$path]); + } + } + return $result; + } + + /** + * Unzip a file into the request folder. + * + * @param stored_file $file the zip file to unzip + * @return string[] of zip content paths + */ + private function unzip_file(stored_file $file): array { + $fp = get_file_packer('application/zip'); + $zipcontents = $fp->extract_to_pathname($file, $this->folder); + if (!$zipcontents) { + throw new moodle_exception("Error Unzipping file", 1); + } + $result = []; + foreach ($zipcontents as $contentname => $success) { + if ($success) { + $result[] = $this->folder.'/'.$contentname; + } + } + return $result; + } + + /** + * Import strings from a list of langfiles. + * + * @param string[] $langfiles an array with file paths + */ + private function process_files(array $langfiles): void { + $parser = phpparser::get_instance(); + foreach ($langfiles as $filepath) { + $component = $this->component_from_filepath($filepath); + if ($component) { + $strings = $parser->parse(file_get_contents($filepath)); + $this->import_strings($strings, $component); + } + } + } + + /** + * Try to get the component from a filepath. + * + * @param string $filepath the filepath + * @return stdCalss|null the DB record of that component + */ + private function component_from_filepath(string $filepath) { + global $DB; + + // Get component from filename. + $pathparts = pathinfo($filepath); + if (empty($pathparts['filename'])) { + throw new coding_exception("Cannot get filename from $filepath", 1); + } + $filename = $pathparts['filename']; + + $normalized = core_component::normalize_component($filename); + if (count($normalized) == 1 || empty($normalized[1])) { + $componentname = $normalized[0]; + } else { + $componentname = implode('_', $normalized); + } + + $result = $DB->get_record('tool_customlang_components', ['name' => $componentname]); + + if (!$result) { + $this->log[] = new logstatus('notice_missingcomponent', notification::NOTIFY_ERROR, null, $componentname); + return null; + } + return $result; + } + + /** + * Import an array of strings into the customlang tables. + * + * @param langstring[] $strings the langstring to set + * @param stdClass $component the target component + */ + private function import_strings(array $strings, stdClass $component): void { + global $DB; + + foreach ($strings as $newstring) { + // Check current DB entry. + $customlang = $DB->get_record('tool_customlang', [ + 'componentid' => $component->id, + 'stringid' => $newstring->id, + 'lang' => $this->lng, + ]); + if (!$customlang) { + $customlang = null; + } + + if ($this->can_save_string($customlang, $newstring, $component)) { + $customlang->local = $newstring->text; + $customlang->timecustomized = $newstring->timemodified; + $customlang->outdated = 0; + $customlang->modified = 1; + $DB->update_record('tool_customlang', $customlang); + } + } + } + + /** + * Determine if a specific string can be saved based on the current importmode. + * + * @param stdClass $customlang customlang original record + * @param langstring $newstring the new strign to store + * @param stdClass $component the component target + * @return bool if the string can be stored + */ + private function can_save_string(?stdClass $customlang, langstring $newstring, stdClass $component): bool { + $result = false; + $message = 'notice_success'; + if (empty($customlang)) { + $message = 'notice_inexitentstring'; + $this->log[] = new logstatus($message, notification::NOTIFY_ERROR, null, $component->name, $newstring); + return $result; + } + + switch ($this->importmode) { + case self::IMPORTNEW: + $result = empty($customlang->local); + $warningmessage = 'notice_ignoreupdate'; + break; + case self::IMPORTUPDATE: + $result = !empty($customlang->local); + $warningmessage = 'notice_ignorenew'; + break; + case self::IMPORTALL: + $result = true; + break; + } + if ($result) { + $errorlevel = notification::NOTIFY_SUCCESS; + } else { + $errorlevel = notification::NOTIFY_ERROR; + $message = $warningmessage; + } + $this->log[] = new logstatus($message, $errorlevel, null, $component->name, $newstring); + + return $result; + } +} diff --git a/admin/tool/customlang/classes/local/mlang/langstring.php b/admin/tool/customlang/classes/local/mlang/langstring.php new file mode 100644 index 00000000000..f380cd6bf06 --- /dev/null +++ b/admin/tool/customlang/classes/local/mlang/langstring.php @@ -0,0 +1,177 @@ +. + +/** + * Language string based on David Mudrak langstring from local_amos. + * + * @package tool_customlang + * @copyright 2020 Ferran Recio + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_customlang\local\mlang; + +use moodle_exception; +use stdclass; + +/** + * Class containing a lang string cleaned. + * + * @package tool_customlang + * @copyright 2020 Ferran Recio + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Represents a single string + */ +class langstring { + + /** @var string identifier */ + public $id = null; + + /** @var string */ + public $text = ''; + + /** @var int the time stamp when this string was saved */ + public $timemodified = null; + + /** @var bool is deleted */ + public $deleted = false; + + /** @var stdclass extra information about the string */ + public $extra = null; + + /** + * Class constructor. + * + * @param string $id string identifier + * @param string $text string text + * @param int $timemodified + * @param int $deleted + * @param stdclass $extra + */ + public function __construct(string $id, string $text = '', int $timemodified = null, + int $deleted = 0, stdclass $extra = null) { + + if (is_null($timemodified)) { + $timemodified = time(); + } + $this->id = $id; + $this->text = $text; + $this->timemodified = $timemodified; + $this->deleted = $deleted; + $this->extra = $extra; + } + + /** + * Given a string text, returns it being formatted properly for storing in AMOS repository. + * + * Note: This method is taken directly from local_amos as it is highly tested and robust. + * The Moodle 1.x part is keep on puspose to make it easier the copy paste from both codes. + * This could change in the future when AMOS stop suporting the 1.x langstrings. + * + * We need to know for what branch the string should be prepared due to internal changes in + * format required by get_string() + * - for get_string() in Moodle 1.6 - 1.9 use $format == 1 + * - for get_string() in Moodle 2.0 and higher use $format == 2 + * + * Typical usages of this methods: + * $t = langstring::fix_syntax($t); // sanity new translations of 2.x strings + * $t = langstring::fix_syntax($t, 1); // sanity legacy 1.x strings + * $t = langstring::fix_syntax($t, 2, 1); // convert format of 1.x strings into 2.x + * + * Backward converting 2.x format into 1.x is not supported + * + * @param string $text string text to be fixed + * @param int $format target get_string() format version + * @param int $from which format version does the text come from, defaults to the same as $format + * @return string + */ + public static function fix_syntax(string $text, int $format = 2, ?int $from = null): string { + if (is_null($from)) { + $from = $format; + } + + // Common filter. + $clean = trim($text); + $search = [ + // Remove \r if it is part of \r\n. + '/\r(?=\n)/', + + // Control characters to be replaced with \n + // LINE TABULATION, FORM FEED, CARRIAGE RETURN, END OF TRANSMISSION BLOCK, + // END OF MEDIUM, SUBSTITUTE, BREAK PERMITTED HERE, NEXT LINE, START OF STRING, + // STRING TERMINATOR and Unicode character categorys Zl and Zp. + '/[\x{0B}-\r\x{17}\x{19}\x{1A}\x{82}\x{85}\x{98}\x{9C}\p{Zl}\p{Zp}]/u', + + // Control characters to be removed + // NULL, ENQUIRY, ACKNOWLEDGE, BELL, SHIFT {OUT,IN}, DATA LINK ESCAPE, + // DEVICE CONTROL {ONE,TWO,THREE,FOUR}, NEGATIVE ACKNOWLEDGE, SYNCHRONOUS IDLE, ESCAPE, + // DELETE, PADDING CHARACTER, HIGH OCTET PRESET, NO BREAK HERE, INDEX, + // {START,END} OF SELECTED AREA, CHARACTER TABULATION {SET,WITH JUSTIFICATION}, + // LINE TABULATION SET, PARTIAL LINE {FORWARD,BACKWARD}, REVERSE LINE FEED, + // SINGLE SHIFT {TWO,THREE}, DEVICE CONTROL STRING, PRIVATE USE {ONE,TWO}, + // SET TRANSMIT STATE, MESSAGE WAITING, {START,END} OF GUARDED AREA, + // {SINGLE {GRAPHIC,} CHARACTER,CONTROL SEQUENCE} INTRODUCER, OPERATING SYSTEM COMMAND, + // PRIVACY MESSAGE, APPLICATION PROGRAM COMMAND, ZERO WIDTH {,NO-BREAK} SPACE, + // REPLACEMENT CHARACTER. + '/[\0\x{05}-\x{07}\x{0E}-\x{16}\x{1B}\x{7F}\x{80}\x{81}\x{83}\x{84}\x{86}-\x{93}\x{95}-\x{97}\x{99}-\x{9B}\x{9D}-\x{9F}\x{200B}\x{FEFF}\x{FFFD}]++/u', + + // Remove trailing whitespace at the end of lines in a multiline string. + '/[ \t]+(?=\n)/', + ]; + $replace = [ + '', + "\n", + '', + '', + ]; + $clean = preg_replace($search, $replace, $clean); + + if (($format === 2) && ($from === 2)) { + // Sanity translations of 2.x strings. + $clean = preg_replace("/\n{3,}/", "\n\n\n", $clean); // Collapse runs of blank lines. + + } else if (($format === 2) && ($from === 1)) { + // Convert 1.x string into 2.x format. + $clean = preg_replace("/\n{3,}/", "\n\n\n", $clean); // Collapse runs of blank lines. + $clean = preg_replace('/%+/', '%', $clean); // Collapse % characters. + $clean = str_replace('\$', '@@@___XXX_ESCAPED_DOLLAR__@@@', $clean); // Remember for later. + $clean = str_replace("\\", '', $clean); // Delete all slashes. + $clean = preg_replace('/(^|[^{])\$a\b(\->[a-zA-Z0-9_]+)?/', '\\1{$a\\2}', $clean); // Wrap placeholders. + $clean = str_replace('@@@___XXX_ESCAPED_DOLLAR__@@@', '$', $clean); + $clean = str_replace('$', '$', $clean); + + } else if (($format === 1) && ($from === 1)) { + // Sanity legacy 1.x strings. + $clean = preg_replace("/\n{3,}/", "\n\n", $clean); // Collapse runs of blank lines. + $clean = str_replace('\$', '@@@___XXX_ESCAPED_DOLLAR__@@@', $clean); + $clean = str_replace("\\", '', $clean); // Delete all slashes. + $clean = str_replace('$', '\$', $clean); // Escape all embedded variables. + // Unescape placeholders: only $a and $a->something are allowed. All other $variables are left escaped. + $clean = preg_replace('/\\\\\$a\b(\->[a-zA-Z0-9_]+)?/', '$a\\1', $clean); // Unescape placeholders. + $clean = str_replace('@@@___XXX_ESCAPED_DOLLAR__@@@', '\$', $clean); + $clean = str_replace('"', "\\\"", $clean); // Add slashes for ". + $clean = preg_replace('/%+/', '%', $clean); // Collapse % characters. + $clean = str_replace('%', '%%', $clean); // Duplicate %. + + } else { + throw new moodle_exception('Unknown get_string() format version'); + } + return $clean; + } +} diff --git a/admin/tool/customlang/classes/local/mlang/logstatus.php b/admin/tool/customlang/classes/local/mlang/logstatus.php new file mode 100644 index 00000000000..eca3a987f46 --- /dev/null +++ b/admin/tool/customlang/classes/local/mlang/logstatus.php @@ -0,0 +1,92 @@ +. + +/** + * Language string based on David Mudrak langstring from local_amos. + * + * @package tool_customlang + * @copyright 2020 Ferran Recio + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_customlang\local\mlang; + +use moodle_exception; +use stdclass; + +/** + * Class containing a lang string cleaned. + * + * @package tool_customlang + * @copyright 2020 Ferran Recio + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Represents a single string + */ +class logstatus { + + /** @var langstring the current string */ + public $langstring = null; + + /** @var string the component */ + public $component = null; + + /** @var string the string ID */ + public $stringid = null; + + /** @var string the original filename */ + public $filename = null; + + /** @var int the error level */ + public $errorlevel = null; + + /** @var string the message identifier */ + private $message; + + /** + * Class creator. + * + * @param string $message the message identifier to display + * @param string $errorlevel the notice level + * @param string|null $filename the filename of this log + * @param string|null $component the component of this log + * @param langstring|null $langstring the langstring of this log + */ + public function __construct(string $message, string $errorlevel, ?string $filename = null, + ?string $component = null, ?langstring $langstring = null) { + + $this->filename = $filename; + $this->component = $component; + $this->langstring = $langstring; + $this->message = $message; + $this->errorlevel = $errorlevel; + + if ($langstring) { + $this->stringid = $langstring->id; + } + } + + /** + * Get the log message. + * + * @return string the log message. + */ + public function get_message(): string { + return get_string($this->message, 'tool_customlang', $this); + } +} diff --git a/admin/tool/customlang/classes/local/mlang/phpparser.php b/admin/tool/customlang/classes/local/mlang/phpparser.php new file mode 100644 index 00000000000..c4c54070307 --- /dev/null +++ b/admin/tool/customlang/classes/local/mlang/phpparser.php @@ -0,0 +1,260 @@ +. + +/** + * Mlang PHP based on David Mudrak phpparser for local_amos. + * + * @package tool_customlang + * @copyright 2020 Ferran Recio + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_customlang\local\mlang; + +use coding_exception; +use moodle_exception; + +/** + * Parser of Moodle strings defined as associative array. + * + * Moodle core just includes this file format directly as normal PHP code. However + * for security reasons, we must not do this for files uploaded by anonymous users. + * This parser reconstructs the associative $string array without actually including + * the file. + */ +class phpparser { + + /** @var holds the singleton instance of self */ + private static $instance = null; + + /** + * Prevents direct creation of object + */ + private function __construct() { + } + + /** + * Prevent from cloning the instance + */ + public function __clone() { + throw new coding_exception('Cloning os singleton is not allowed'); + } + + /** + * Get the singleton instance fo this class + * + * @return phpparser singleton instance of phpparser + */ + public static function get_instance(): phpparser { + if (is_null(self::$instance)) { + self::$instance = new phpparser(); + } + return self::$instance; + } + + /** + * Parses the given data in Moodle PHP string format + * + * Note: This method is adapted from local_amos as it is highly tested and robust. + * The priority is keeping it similar to the original one to make it easier to mantain. + * + * @param string $data definition of the associative array + * @param int $format the data format on the input, defaults to the one used since 2.0 + * @return langstring[] array of langstrings of this file + */ + public function parse(string $data, int $format = 2): array { + $result = []; + $strings = $this->extract_strings($data); + foreach ($strings as $id => $text) { + $cleaned = clean_param($id, PARAM_STRINGID); + if ($cleaned !== $id) { + continue; + } + $text = langstring::fix_syntax($text, 2, $format); + $result[] = new langstring($id, $text); + } + return $result; + } + + /** + * Low level parsing method + * + * Note: This method is adapted from local_amos as it is highly tested and robust. + * The priority is keeping it similar to the original one to make it easier to mantain. + * + * @param string $data + * @return string[] the data strings + */ + protected function extract_strings(string $data): array { + + $strings = []; // To be returned. + + if (empty($data)) { + return $strings; + } + + // Tokenize data - we expect valid PHP code. + $tokens = token_get_all($data); + + // Get rid of all non-relevant tokens. + $rubbish = [T_WHITESPACE, T_INLINE_HTML, T_COMMENT, T_DOC_COMMENT, T_OPEN_TAG, T_CLOSE_TAG]; + foreach ($tokens as $i => $token) { + if (is_array($token)) { + if (in_array($token[0], $rubbish)) { + unset($tokens[$i]); + } + } + } + + $id = null; + $text = null; + $line = 0; + $expect = 'STRING_VAR'; // The first expected token is '$string'. + + // Iterate over tokens and look for valid $string array assignment patterns. + foreach ($tokens as $token) { + $foundtype = null; + $founddata = null; + if (is_array($token)) { + $foundtype = $token[0]; + $founddata = $token[1]; + if (!empty($token[2])) { + $line = $token[2]; + } + + } else { + $foundtype = 'char'; + $founddata = $token; + } + + if ($expect == 'STRING_VAR') { + if ($foundtype === T_VARIABLE and $founddata === '$string') { + $expect = 'LEFT_BRACKET'; + continue; + } else { + // Allow other code at the global level. + continue; + } + } + + if ($expect == 'LEFT_BRACKET') { + if ($foundtype === 'char' and $founddata === '[') { + $expect = 'STRING_ID'; + continue; + } else { + throw new moodle_exception('Parsing error. Expected character [ at line '.$line); + } + } + + if ($expect == 'STRING_ID') { + if ($foundtype === T_CONSTANT_ENCAPSED_STRING) { + $id = $this->decapsulate($founddata); + $expect = 'RIGHT_BRACKET'; + continue; + } else { + throw new moodle_exception('Parsing error. Expected T_CONSTANT_ENCAPSED_STRING array key at line '.$line); + } + } + + if ($expect == 'RIGHT_BRACKET') { + if ($foundtype === 'char' and $founddata === ']') { + $expect = 'ASSIGNMENT'; + continue; + } else { + throw new moodle_exception('Parsing error. Expected character ] at line '.$line); + } + } + + if ($expect == 'ASSIGNMENT') { + if ($foundtype === 'char' and $founddata === '=') { + $expect = 'STRING_TEXT'; + continue; + } else { + throw new moodle_exception('Parsing error. Expected character = at line '.$line); + } + } + + if ($expect == 'STRING_TEXT') { + if ($foundtype === T_CONSTANT_ENCAPSED_STRING) { + $text = $this->decapsulate($founddata); + $expect = 'SEMICOLON'; + continue; + } else { + throw new moodle_exception( + 'Parsing error. Expected T_CONSTANT_ENCAPSED_STRING array item value at line '.$line + ); + } + } + + if ($expect == 'SEMICOLON') { + if (is_null($id) or is_null($text)) { + throw new moodle_exception('Parsing error. NULL string id or value at line '.$line); + } + if ($foundtype === 'char' and $founddata === ';') { + if (!empty($id)) { + $strings[$id] = $text; + } + $id = null; + $text = null; + $expect = 'STRING_VAR'; + continue; + } else { + throw new moodle_exception('Parsing error. Expected character ; at line '.$line); + } + } + + } + + return $strings; + } + + /** + * Given one T_CONSTANT_ENCAPSED_STRING, return its value without quotes + * + * Also processes escaped quotes inside the text. + * + * Note: This method is taken directly from local_amos as it is highly tested and robust. + * + * @param string $text value obtained by token_get_all() + * @return string value without quotes + */ + protected function decapsulate(string $text): string { + + if (strlen($text) < 2) { + throw new moodle_exception('Parsing error. Expected T_CONSTANT_ENCAPSED_STRING in decapsulate()'); + } + + if (substr($text, 0, 1) == "'" and substr($text, -1) == "'") { + // Single quoted string. + $text = trim($text, "'"); + $text = str_replace("\'", "'", $text); + $text = str_replace('\\\\', '\\', $text); + return $text; + + } else if (substr($text, 0, 1) == '"' and substr($text, -1) == '"') { + // Double quoted string. + $text = trim($text, '"'); + $text = str_replace('\"', '"', $text); + $text = str_replace('\\\\', '\\', $text); + return $text; + + } else { + throw new moodle_exception( + 'Parsing error. Unexpected quotation in T_CONSTANT_ENCAPSED_STRING in decapsulate(): '.$text + ); + } + } +} diff --git a/admin/tool/customlang/import.php b/admin/tool/customlang/import.php new file mode 100644 index 00000000000..628a9f88655 --- /dev/null +++ b/admin/tool/customlang/import.php @@ -0,0 +1,74 @@ +. + +/** + * Import custom lang files. + * + * @package tool_customlang + * @subpackage customlang + * @copyright 2020 Ferran Recio + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +use tool_customlang\form\import; +use tool_customlang\local\importer; + +require(__DIR__ . '/../../../config.php'); +require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/customlang/locallib.php'); +require_once($CFG->libdir.'/adminlib.php'); + +require_login(SITEID, false); +require_capability('tool/customlang:edit', context_system::instance()); + +$lng = required_param('lng', PARAM_LANG); + +admin_externalpage_setup('toolcustomlang', '', null, + new moodle_url('/admin/tool/customlang/import.php', ['lng' => $lng])); + +$output = $PAGE->get_renderer('tool_customlang'); + +$form = new import(null, ['lng' => $lng]); +if ($data = $form->get_data()) { + require_sesskey(); + + // Get the file from the users draft area. + $usercontext = context_user::instance($USER->id); + $fs = get_file_storage(); + $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data->pack, 'id', + false); + + // Send files to the importer. + $importer = new importer($data->lng, $data->importmode); + $importer->import($files); + + echo $output->header(); + + // Display logs. + $log = $importer->get_log(); + foreach ($log as $message) { + echo $output->notification($message->get_message(), $message->errorlevel); + } + + // Show continue button. + echo $output->continue_button(new moodle_url('index.php', array('lng' => $lng))); + +} else { + echo $output->header(); + + $form->display(); +} + +echo $OUTPUT->footer(); diff --git a/admin/tool/customlang/index.php b/admin/tool/customlang/index.php index d796e8cdcc2..e57aa4428cb 100644 --- a/admin/tool/customlang/index.php +++ b/admin/tool/customlang/index.php @@ -132,6 +132,11 @@ if (has_capability('tool/customlang:edit', context_system::instance())) { 'method' => 'post', ); } + $menu['import'] = array( + 'title' => get_string('import', 'tool_customlang'), + 'url' => new moodle_url($PAGE->url, ['action' => 'checkout', 'lng' => $lng, 'next' => 'import']), + 'method' => 'post', + ); } if (has_capability('tool/customlang:export', context_system::instance())) { $langdir = tool_customlang_utils::get_localpack_location($lng); diff --git a/admin/tool/customlang/lang/en/tool_customlang.php b/admin/tool/customlang/lang/en/tool_customlang.php index 8ac0c2afd74..513b2f7877d 100644 --- a/admin/tool/customlang/lang/en/tool_customlang.php +++ b/admin/tool/customlang/lang/en/tool_customlang.php @@ -55,12 +55,24 @@ $string['headingcomponent'] = 'Component'; $string['headinglocal'] = 'Local customisation'; $string['headingstandard'] = 'Standard text'; $string['headingstringid'] = 'String'; +$string['import'] = 'Import custom strings'; +$string['import_mode'] = 'Import mode'; +$string['import_new'] = 'Create only strings without local customisation'; +$string['import_update'] = 'Update only strings with local customisation'; +$string['import_all'] = 'Create or update all strings from the component(s)'; +$string['importfile'] = 'Import file'; +$string['langpack'] = 'Language component(s)'; $string['markinguptodate'] = 'Marking the customisation as up-to-date'; $string['markinguptodate_help'] = 'The customised translation may get outdated if either the English original or the master translation has modified since the string was customised on your site. Review the customised translation. If you find it up-to-date, click the checkbox. Edit it otherwise.'; $string['markuptodate'] = 'mark as up-to-date'; $string['modifiedno'] = 'There are no modified strings to save.'; $string['modifiednum'] = 'There are {$a} modified strings. Do you wish to save these changes to your local language pack?'; $string['nolocallang'] = 'No local strings found.'; +$string['notice_ignorenew'] = 'Ignoring string {$a->component}/{$a->stringid} because it is not customised.'; +$string['notice_ignoreupdate'] = 'Ignoring string {$a->component}/{$a->stringid} because it is already defined.'; +$string['notice_inexitentstring'] = 'String {$a->component}/{$a->stringid} not found.'; +$string['notice_missingcomponent'] = 'Missing component {$a->component}.'; +$string['notice_success'] = 'String {$a->component}/{$a->stringid} updated successfully.'; $string['nostringsfound'] = 'No strings found, please modify the filter settings'; $string['placeholder'] = 'Placeholders'; $string['placeholder_help'] = 'Placeholders are special statements like `{$a}` or `{$a->something}` within the string. They are replaced with a value when the string is actually printed. diff --git a/admin/tool/customlang/tests/behat/import_files.feature b/admin/tool/customlang/tests/behat/import_files.feature new file mode 100644 index 00000000000..20df1c0f675 --- /dev/null +++ b/admin/tool/customlang/tests/behat/import_files.feature @@ -0,0 +1,48 @@ +@tool @tool_customlang @_file_upload +Feature: Within a moodle instance, an administrator should be able to import modified langstrings. + In order to import modified langstrings in the adminsettings from one to another instance, + As an admin + I need to be able to import the zips and php files of the language customisation of a language. + + Background: + Given I log in as "admin" + And I navigate to "Language > Language customisation" in site administration + And I set the field "lng" to "en" + And I click on "Import custom strings" "button" + And I press "Continue" + + @javascript + Scenario: Import a PHP file to add a new core lang customization + When I upload "admin/tool/customlang/tests/fixtures/tool_customlang.php" file to "Language component(s)" filemanager + And I press "Import file" + Then I should see "String tool_customlang/pluginname updated successfully." + And I should see "String tool_customlang/nonexistentinvetedstring not found." + And I click on "Continue" "button" + And I should see "There are 1 modified strings." + And I click on "Save strings to language pack" "button" + And I click on "Continue" "button" + And I should see "An amazing import feature" in the "page-header" "region" + + @javascript + Scenario: Try to import a PHP file from a non existent component + When I upload "admin/tool/customlang/tests/fixtures/mod_fakecomponent.php" file to "Language component(s)" filemanager + And I press "Import file" + Then I should see "Missing component mod_fakecomponent." + + @javascript + Scenario: Import a zip file with some PHP files in it. + When I upload "admin/tool/customlang/tests/fixtures/customlang.zip" file to "Language component(s)" filemanager + And I press "Import file" + Then I should see "String core/administrationsite updated successfully." + And I should see "String core/language updated successfully." + And I should see "String core/nonexistentinvetedstring not found." + And I should see "String tool_customlang/pluginname updated successfully." + And I should see "String tool_customlang/nonexistentinvetedstring not found." + And I should see "Missing component mod_fakecomponent." + And I click on "Continue" "button" + And I should see "There are 3 modified strings." + And I click on "Save strings to language pack" "button" + And I click on "Continue" "button" + And I should see "Uploaded custom string" in the "page-header" "region" + And I should see "Another Uploaded string" in the "page-header" "region" + And I should see "An amazing import feature" in the "page-header" "region" diff --git a/admin/tool/customlang/tests/behat/import_mode.feature b/admin/tool/customlang/tests/behat/import_mode.feature new file mode 100644 index 00000000000..2347cf24e9d --- /dev/null +++ b/admin/tool/customlang/tests/behat/import_mode.feature @@ -0,0 +1,81 @@ +@tool @tool_customlang @_file_upload +Feature: Within a moodle instance, an administrator should be able to import langstrings with several modes. + In order to import modified langstrings in the adminsettings from one to another instance, + As an admin + I need to be able to import only some language customisation strings depending on some conditions. + + Background: + # Add one customization. + Given I log in as "admin" + And I navigate to "Language > Language customisation" in site administration + And I set the field "lng" to "en" + And I press "Open language pack for editing" + And I press "Continue" + And I set the field "Show strings of these components" to "moodle.php" + And I set the field "String identifier" to "administrationsite" + And I press "Show strings" + And I set the field "core/administrationsite" to "Custom string example" + And I press "Save changes to the language pack" + And I should see "There are 1 modified strings." + And I click on "Continue" "button" + And I should see "Custom string example" in the "page-header" "region" + + @javascript + Scenario: Update only customized strings + When I set the field "lng" to "en" + And I click on "Import custom strings" "button" + And I press "Continue" + And I upload "admin/tool/customlang/tests/fixtures/moodle.php" file to "Language component(s)" filemanager + And I set the field "Import mode" to "Update only strings with local customisation" + And I press "Import file" + Then I should see "String core/administrationsite updated successfully." + And I should see "Ignoring string core/language because it is not customised." + And I should see "String core/nonexistentinvetedstring not found." + And I click on "Continue" "button" + And I should see "There are 1 modified strings." + And I should not see "Uploaded custom string" in the "page-header" "region" + And I click on "Save strings to language pack" "button" + And I click on "Continue" "button" + And I should not see "Custom string example" in the "page-header" "region" + And I should see "Uploaded custom string" in the "page-header" "region" + And I should not see "Another Uploaded string" in the "page-header" "region" + + @javascript + Scenario: Create only new strings + When I set the field "lng" to "en" + And I click on "Import custom strings" "button" + And I press "Continue" + And I upload "admin/tool/customlang/tests/fixtures/moodle.php" file to "Language component(s)" filemanager + And I set the field "Import mode" to "Create only strings without local customisation" + And I press "Import file" + Then I should see "Ignoring string core/administrationsite because it is already defined." + And I should see "String core/language updated successfully." + And I should see "String core/nonexistentinvetedstring not found." + And I click on "Continue" "button" + And I should see "There are 1 modified strings." + And I should not see "Uploaded custom string" in the "page-header" "region" + And I click on "Save strings to language pack" "button" + And I click on "Continue" "button" + And I should see "Custom string example" in the "page-header" "region" + And I should not see "Uploaded custom string" in the "page-header" "region" + And I should see "Another Uploaded string" in the "page-header" "region" + + @javascript + Scenario: Import all strings + When I set the field "lng" to "en" + And I click on "Import custom strings" "button" + And I press "Continue" + And I upload "admin/tool/customlang/tests/fixtures/moodle.php" file to "Language component(s)" filemanager + And I set the field "Import mode" to "Create or update all strings from the component(s)" + And I press "Import file" + Then I should see "String core/administrationsite updated successfully." + And I should see "String core/language updated successfully." + And I should see "String core/nonexistentinvetedstring not found." + And I click on "Continue" "button" + And I should see "There are 2 modified strings." + And I should not see "Uploaded custom string" in the "page-header" "region" + And I click on "Save strings to language pack" "button" + And I click on "Continue" "button" + And I should not see "Custom string example" in the "page-header" "region" + And I should see "Uploaded custom string" in the "page-header" "region" + And I should see "Another Uploaded string" in the "page-header" "region" diff --git a/admin/tool/customlang/tests/fixtures/customlang.zip b/admin/tool/customlang/tests/fixtures/customlang.zip new file mode 100644 index 0000000000000000000000000000000000000000..9f61c26e2fc154a750b22162b535df634cb35df5 GIT binary patch literal 2329 zcmWIWW@Zs#-~htAiHd;?Q1FeJfkBi(fgv|PB|a@NJ2g2!w;(?+HLpakAfq5OgqML` zN!~9WhD$5B85mh!0QG>0)?nZMTLuDqpNF4WD70bG$;ABA-<0nj%3GqdKwyhgXz0v} zNfD)QPLwV1`2EWIj~Ps}vEtOWC-BUHz`(K@o{3hpd#p%~hVkysTmgugR?L zPftW&tP7Um-63^wVdk3kZ}|I9>)I@GI;^s0_g)d!C5s$V1tluKm~V6A`yF@OSnS^6 zv=0p2zK70mu3gs9bdOi&LWI@fw@Z&HSg_{qIWfcX{hfw9+kB@oe}$R5gASYb#{Yh- z5VAh%^|Y#$o;|y)xik~|FGT8CTYgPpI_y2ke(g5PBd2Be9ElXu|9rRSz`AQ+y4Y>J zJC{vunzB;)@D%HbANT#Ssjg7+`lISOxyJol;>0JXf2J{ahq4*2d933vw=Hwd!K&G7 z!)I4=Y;ery-h05lE&AKkxGCAPIw=e&YXU2xLcB`iztis;(#loIqCrXB z^A+#@UVffg?!|X;)AylCFBh$vn)p_#vTTpv!-Ch(Z>*nPvPj`U1XpW~O;z05eak+} zpER1Ee%eCT_rcexV*QU#>Toob+c5yGP1+-P>i9zcxuSgT_a8F%Y+HM!=kk)Sb5?f` zNZQYeojKj@+>aT@9zOon{(ahlEr;SmxTXrfY7p7Sc$xdl|7VHeOP)SG+mD5bb0UR-_@^f zcYJiynBQhs5@sP&+s@9U$d6g>k{VlKN)4Ialb4cC(mm0e=UD6y6F;EeV}HD z-wNiSZ)`sL7yi#|51ly8U;e3wbkoEW`JiO!D^W?Ve-(%JWoOT(WA9c6>AMGKZrb(x~X zI%(5uCHebHQx3O_t<_`NIp^V-cOU)*$6lNF{P%<@cO1^KGX2e7rn^E$%qw+EG4s-` zU+2CLe_OStTyGMh_4wyW z!wYfB##a)&S-N)_y-V9(5ta9lQ#DI1;qDt&f5k_dUd;zQ7T-2hP1}%PDUzVz_6kdULyWg&Z3 zv8tyPEt$DRUFcq%eYgBPPN5pk#RfMg*34)>S+wouff`X~hSv&yhuU*Rj%>Jh?Df?& zGqxKGCb#xIkUx9&_Rj!j<1Ibh#T)l5o+uThZlnP$I=5(kV$MvSu6kQjmmHhWqyp5ar`@H-6<_la%w4cw}xNm>V^D`W0*2Z5+in--gGVPk6jM}=3 zxpE!HzwhBG)-!zlU2OH$WRu@=dXppf@GQE=5Erj0c|+dE_I&kYA*Js1d!tNPr%wvK z@taM4fw)J4=x5&W<*7gT)u)TdKlQ!2x5^{?IT+ODyWh33aFrl xfhCQLfsV?}C$`u|Hy7DkpqvH+TN. + +/** + * Local language pack. + * + * @copyright 2020 Ferran Recio + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + + +$string['administrationsite'] = 'Uploaded custom string'; +$string['language'] = 'Another Uploaded string'; +$string['nonexistentinvetedstring'] = 'This should not be imported'; diff --git a/admin/tool/customlang/tests/fixtures/moodle.php b/admin/tool/customlang/tests/fixtures/moodle.php new file mode 100644 index 00000000000..b1f1866b288 --- /dev/null +++ b/admin/tool/customlang/tests/fixtures/moodle.php @@ -0,0 +1,28 @@ +. + +/** + * Local language pack. + * + * @copyright 2020 Ferran Recio + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$string['administrationsite'] = 'Uploaded custom string'; +$string['language'] = 'Another Uploaded string'; +$string['nonexistentinvetedstring'] = 'This should not be imported'; diff --git a/admin/tool/customlang/tests/fixtures/tool_customlang.php b/admin/tool/customlang/tests/fixtures/tool_customlang.php new file mode 100644 index 00000000000..73559cb8f70 --- /dev/null +++ b/admin/tool/customlang/tests/fixtures/tool_customlang.php @@ -0,0 +1,28 @@ +. + +/** + * Local language pack from http://localhost/m/MDL-69583 + * + * @copyright 2020 Ferran Recio + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + + +$string['pluginname'] = 'An amazing import feature'; +$string['nonexistentinvetedstring'] = 'This string should not be imported'; diff --git a/admin/tool/customlang/tests/local/mlang/langstring_test.php b/admin/tool/customlang/tests/local/mlang/langstring_test.php new file mode 100644 index 00000000000..df5d99b3237 --- /dev/null +++ b/admin/tool/customlang/tests/local/mlang/langstring_test.php @@ -0,0 +1,1090 @@ +. + +/** + * mlang langstring tests. + * + * Based on local_amos mlang_langstring tests. + * + * @package tool_customlang + * @copyright 2020 Ferran Recio + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_customlang\local\mlang; + +use advanced_testcase; +use moodle_exception; + +/** + * Langstring tests. + * + * @package tool_customlang + * @copyright 2020 Ferran Recio + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class langstring_testcase extends advanced_testcase { + + /** + * Sanity 1.x string + * - all variables but $a placeholders must be escaped because the string is eval'ed + * - all ' and " must be escaped + * - all single % must be converted into %% for backwards compatibility + * + * @dataProvider fix_syntax_data + * @param string $text the text to test + * @param int $version the lang package version (1 or 2) + * @param int|null $fromversion the version to convert (null for none) + * @param string $expected the expected result + * + */ + public function test_fix_syntax(string $text, int $version, ?int $fromversion, string $expected): void { + $this->assertEquals(langstring::fix_syntax($text, $version, $fromversion), $expected); + } + + /** + * Data provider for the test_parse. + * + * @return array + */ + public function fix_syntax_data() : array { + return [ + // Syntax sanity v1 strings. + [ + 'No change', 1, null, + 'No change' + ], + [ + 'Completed 100% of work', 1, null, + 'Completed 100%% of work' + ], + [ + 'Completed 100%% of work', 1, null, + 'Completed 100%% of work' + ], + [ + "Windows\r\nsucks", 1, null, + "Windows\nsucks" + ], + [ + "Linux\nsucks", 1, null, + "Linux\nsucks" + ], + [ + "Mac\rsucks", 1, null, + "Mac\nsucks" + ], + [ + "LINE TABULATION\x0Bnewline", 1, null, + "LINE TABULATION\nnewline" + ], + [ + "FORM FEED\x0Cnewline", 1, null, + "FORM FEED\nnewline" + ], + [ + "END OF TRANSMISSION BLOCK\x17newline", 1, null, + "END OF TRANSMISSION BLOCK\nnewline" + ], + [ + "END OF MEDIUM\x19newline", 1, null, + "END OF MEDIUM\nnewline" + ], + [ + "SUBSTITUTE\x1Anewline", 1, null, + "SUBSTITUTE\nnewline" + ], + [ + "BREAK PERMITTED HERE\xC2\x82newline", 1, null, + "BREAK PERMITTED HERE\nnewline" + ], + [ + "NEXT LINE\xC2\x85newline", 1, null, + "NEXT LINE\nnewline" + ], + [ + "START OF STRING\xC2\x98newline", 1, null, + "START OF STRING\nnewline" + ], + [ + "STRING TERMINATOR\xC2\x9Cnewline", 1, null, + "STRING TERMINATOR\nnewline" + ], + [ + "Unicode Zl\xE2\x80\xA8newline", 1, null, + "Unicode Zl\nnewline" + ], + [ + "Unicode Zp\xE2\x80\xA9newline", 1, null, + "Unicode Zp\nnewline" + ], + [ + "Empty\n\n\n\n\n\nlines", 1, null, + "Empty\n\nlines" + ], + [ + "Trailing \n whitespace \t \nat \nmultilines ", 1, null, + "Trailing\n whitespace\nat\nmultilines" + ], + [ + 'Escape $variable names', 1, null, + 'Escape \$variable names' + ], + [ + 'Escape $alike names', 1, null, + 'Escape \$alike names' + ], + [ + 'String $a placeholder', 1, null, + 'String $a placeholder' + ], + [ + 'Escaped \$a', 1, null, + 'Escaped \$a' + ], + [ + 'Wrapped {$a}', 1, null, + 'Wrapped {$a}' + ], + [ + 'Trailing $a', 1, null, + 'Trailing $a' + ], + [ + '$a leading', 1, null, + '$a leading' + ], + [ + 'Hit $a-times', 1, null, + 'Hit $a-times' + ], // This is placeholder. + [ + 'This is $a_book', 1, null, + 'This is \$a_book' + ], // This is not a place holder. + [ + 'Bye $a, ttyl', 1, null, + 'Bye $a, ttyl' + ], + [ + 'Object $a->foo placeholder', 1, null, + 'Object $a->foo placeholder' + ], + [ + 'Trailing $a->bar', 1, null, + 'Trailing $a->bar' + ], + [ + 'AMOS', 1, null, + 'AMOS' + ], + [ + 'AMOS', 1, null, + 'AMOS' + ], + [ + 'AMOS', 1, null, + 'AMOS' + ], + [ + "'Murder!', she wrote", 1, null, + "'Murder!', she wrote" + ], // Will be escaped by var_export(). + [ + "\t Trim Hunter \t\t", 1, null, + 'Trim Hunter' + ], + [ + 'Delete role "$a->role"?', 1, null, + 'Delete role \"$a->role\"?' + ], + [ + 'Delete role \"$a->role\"?', 1, null, + 'Delete role \"$a->role\"?' + ], + [ + "Delete ASCII\0 NULL control character", 1, null, + 'Delete ASCII NULL control character' + ], + [ + "Delete ASCII\x05 ENQUIRY control character", 1, null, + 'Delete ASCII ENQUIRY control character' + ], + [ + "Delete ASCII\x06 ACKNOWLEDGE control character", 1, null, + 'Delete ASCII ACKNOWLEDGE control character' + ], + [ + "Delete ASCII\x07 BELL control character", 1, null, + 'Delete ASCII BELL control character' + ], + [ + "Delete ASCII\x0E SHIFT OUT control character", 1, null, + 'Delete ASCII SHIFT OUT control character' + ], + [ + "Delete ASCII\x0F SHIFT IN control character", 1, null, + 'Delete ASCII SHIFT IN control character' + ], + [ + "Delete ASCII\x10 DATA LINK ESCAPE control character", 1, null, + 'Delete ASCII DATA LINK ESCAPE control character' + ], + [ + "Delete ASCII\x11 DEVICE CONTROL ONE control character", 1, null, + 'Delete ASCII DEVICE CONTROL ONE control character' + ], + [ + "Delete ASCII\x12 DEVICE CONTROL TWO control character", 1, null, + 'Delete ASCII DEVICE CONTROL TWO control character' + ], + [ + "Delete ASCII\x13 DEVICE CONTROL THREE control character", 1, null, + 'Delete ASCII DEVICE CONTROL THREE control character' + ], + [ + "Delete ASCII\x14 DEVICE CONTROL FOUR control character", 1, null, + 'Delete ASCII DEVICE CONTROL FOUR control character' + ], + [ + "Delete ASCII\x15 NEGATIVE ACKNOWLEDGE control character", 1, null, + 'Delete ASCII NEGATIVE ACKNOWLEDGE control character' + ], + [ + "Delete ASCII\x16 SYNCHRONOUS IDLE control character", 1, null, + 'Delete ASCII SYNCHRONOUS IDLE control character' + ], + [ + "Delete ASCII\x1B ESCAPE control character", 1, null, + 'Delete ASCII ESCAPE control character' + ], + [ + "Delete ASCII\x7F DELETE control character", 1, null, + 'Delete ASCII DELETE control character' + ], + [ + "Delete ISO 8859\xC2\x80 PADDING CHARACTER control character", 1, null, + 'Delete ISO 8859 PADDING CHARACTER control character' + ], + [ + "Delete ISO 8859\xC2\x81 HIGH OCTET PRESET control character", 1, null, + 'Delete ISO 8859 HIGH OCTET PRESET control character' + ], + [ + "Delete ISO 8859\xC2\x83 NO BREAK HERE control character", 1, null, + 'Delete ISO 8859 NO BREAK HERE control character' + ], + [ + "Delete ISO 8859\xC2\x84 INDEX control character", 1, null, + 'Delete ISO 8859 INDEX control character' + ], + [ + "Delete ISO 8859\xC2\x86 START OF SELECTED AREA control character", 1, null, + 'Delete ISO 8859 START OF SELECTED AREA control character' + ], + [ + "Delete ISO 8859\xC2\x87 END OF SELECTED AREA control character", 1, null, + 'Delete ISO 8859 END OF SELECTED AREA control character' + ], + [ + "Delete ISO 8859\xC2\x88 CHARACTER TABULATION SET control character", 1, null, + 'Delete ISO 8859 CHARACTER TABULATION SET control character' + ], + [ + "Delete ISO 8859\xC2\x89 CHARACTER TABULATION WITH JUSTIFICATION control character", 1, null, + 'Delete ISO 8859 CHARACTER TABULATION WITH JUSTIFICATION control character' + ], + [ + "Delete ISO 8859\xC2\x8A LINE TABULATION SET control character", 1, null, + 'Delete ISO 8859 LINE TABULATION SET control character' + ], + [ + "Delete ISO 8859\xC2\x8B PARTIAL LINE FORWARD control character", 1, null, + 'Delete ISO 8859 PARTIAL LINE FORWARD control character' + ], + [ + "Delete ISO 8859\xC2\x8C PARTIAL LINE BACKWARD control character", 1, null, + 'Delete ISO 8859 PARTIAL LINE BACKWARD control character' + ], + [ + "Delete ISO 8859\xC2\x8D REVERSE LINE FEED control character", 1, null, + 'Delete ISO 8859 REVERSE LINE FEED control character' + ], + [ + "Delete ISO 8859\xC2\x8E SINGLE SHIFT TWO control character", 1, null, + 'Delete ISO 8859 SINGLE SHIFT TWO control character' + ], + [ + "Delete ISO 8859\xC2\x8F SINGLE SHIFT THREE control character", 1, null, + 'Delete ISO 8859 SINGLE SHIFT THREE control character' + ], + [ + "Delete ISO 8859\xC2\x90 DEVICE CONTROL STRING control character", 1, null, + 'Delete ISO 8859 DEVICE CONTROL STRING control character' + ], + [ + "Delete ISO 8859\xC2\x91 PRIVATE USE ONE control character", 1, null, + 'Delete ISO 8859 PRIVATE USE ONE control character' + ], + [ + "Delete ISO 8859\xC2\x92 PRIVATE USE TWO control character", 1, null, + 'Delete ISO 8859 PRIVATE USE TWO control character' + ], + [ + "Delete ISO 8859\xC2\x93 SET TRANSMIT STATE control character", 1, null, + 'Delete ISO 8859 SET TRANSMIT STATE control character' + ], + [ + "Delete ISO 8859\xC2\x95 MESSAGE WAITING control character", 1, null, + 'Delete ISO 8859 MESSAGE WAITING control character' + ], + [ + "Delete ISO 8859\xC2\x96 START OF GUARDED AREA control character", 1, null, + 'Delete ISO 8859 START OF GUARDED AREA control character' + ], + [ + "Delete ISO 8859\xC2\x97 END OF GUARDED AREA control character", 1, null, + 'Delete ISO 8859 END OF GUARDED AREA control character' + ], + [ + "Delete ISO 8859\xC2\x99 SINGLE GRAPHIC CHARACTER INTRODUCER control character", 1, null, + 'Delete ISO 8859 SINGLE GRAPHIC CHARACTER INTRODUCER control character' + ], + [ + "Delete ISO 8859\xC2\x9A SINGLE CHARACTER INTRODUCER control character", 1, null, + 'Delete ISO 8859 SINGLE CHARACTER INTRODUCER control character' + ], + [ + "Delete ISO 8859\xC2\x9B CONTROL SEQUENCE INTRODUCER control character", 1, null, + 'Delete ISO 8859 CONTROL SEQUENCE INTRODUCER control character' + ], + [ + "Delete ISO 8859\xC2\x9D OPERATING SYSTEM COMMAND control character", 1, null, + 'Delete ISO 8859 OPERATING SYSTEM COMMAND control character' + ], + [ + "Delete ISO 8859\xC2\x9E PRIVACY MESSAGE control character", 1, null, + 'Delete ISO 8859 PRIVACY MESSAGE control character' + ], + [ + "Delete ISO 8859\xC2\x9F APPLICATION PROGRAM COMMAND control character", 1, null, + 'Delete ISO 8859 APPLICATION PROGRAM COMMAND control character' + ], + [ + "Delete Unicode\xE2\x80\x8B ZERO WIDTH SPACE control character", 1, null, + 'Delete Unicode ZERO WIDTH SPACE control character' + ], + [ + "Delete Unicode\xEF\xBB\xBF ZERO WIDTH NO-BREAK SPACE control character", 1, null, + 'Delete Unicode ZERO WIDTH NO-BREAK SPACE control character' + ], + [ + "Delete Unicode\xEF\xBF\xBD REPLACEMENT CHARACTER control character", 1, null, + 'Delete Unicode REPLACEMENT CHARACTER control character' + ], + // Syntax sanity v2 strings. + [ + 'No change', 2, null, + 'No change' + ], + [ + 'Completed 100% of work', 2, null, + 'Completed 100% of work' + ], + [ + '%%%% HEADER %%%%', 2, null, + '%%%% HEADER %%%%' + ], // Was not possible before. + [ + "Windows\r\nsucks", 2, null, + "Windows\nsucks" + ], + [ + "Linux\nsucks", 2, null, + "Linux\nsucks" + ], + [ + "Mac\rsucks", 2, null, + "Mac\nsucks" + ], + [ + "LINE TABULATION\x0Bnewline", 2, null, + "LINE TABULATION\nnewline" + ], + [ + "FORM FEED\x0Cnewline", 2, null, + "FORM FEED\nnewline" + ], + [ + "END OF TRANSMISSION BLOCK\x17newline", 2, null, + "END OF TRANSMISSION BLOCK\nnewline" + ], + [ + "END OF MEDIUM\x19newline", 2, null, + "END OF MEDIUM\nnewline" + ], + [ + "SUBSTITUTE\x1Anewline", 2, null, + "SUBSTITUTE\nnewline" + ], + [ + "BREAK PERMITTED HERE\xC2\x82newline", 2, null, + "BREAK PERMITTED HERE\nnewline" + ], + [ + "NEXT LINE\xC2\x85newline", 2, null, + "NEXT LINE\nnewline" + ], + [ + "START OF STRING\xC2\x98newline", 2, null, + "START OF STRING\nnewline" + ], + [ + "STRING TERMINATOR\xC2\x9Cnewline", 2, null, + "STRING TERMINATOR\nnewline" + ], + [ + "Unicode Zl\xE2\x80\xA8newline", 2, null, + "Unicode Zl\nnewline" + ], + [ + "Unicode Zp\xE2\x80\xA9newline", 2, null, + "Unicode Zp\nnewline" + ], + [ + "Empty\n\n\n\n\n\nlines", 2, null, + "Empty\n\n\nlines" + ], // Now allows up to two empty lines. + [ + "Trailing \n whitespace\t\nat \nmultilines ", 2, null, + "Trailing\n whitespace\nat\nmultilines" + ], + [ + 'Do not escape $variable names', 2, null, + 'Do not escape $variable names' + ], + [ + 'Do not escape $alike names', 2, null, + 'Do not escape $alike names' + ], + [ + 'Not $a placeholder', 2, null, + 'Not $a placeholder' + ], + [ + 'String {$a} placeholder', 2, null, + 'String {$a} placeholder' + ], + [ + 'Trailing {$a}', 2, null, + 'Trailing {$a}' + ], + [ + '{$a} leading', 2, null, + '{$a} leading' + ], + [ + 'Trailing $a', 2, null, + 'Trailing $a' + ], + [ + '$a leading', 2, null, + '$a leading' + ], + [ + 'Not $a->foo placeholder', 2, null, + 'Not $a->foo placeholder' + ], + [ + 'Object {$a->foo} placeholder', 2, null, + 'Object {$a->foo} placeholder' + ], + [ + 'Trailing $a->bar', 2, null, + 'Trailing $a->bar' + ], + [ + 'Invalid $a-> placeholder', 2, null, + 'Invalid $a-> placeholder' + ], + [ + 'AMOS', 2, null, + 'AMOS' + ], + [ + "'Murder!', she wrote", 2, null, + "'Murder!', she wrote" + ], // Will be escaped by var_export(). + [ + "\t Trim Hunter \t\t", 2, null, + 'Trim Hunter' + ], + [ + 'Delete role "$a->role"?', 2, null, + 'Delete role "$a->role"?' + ], + [ + 'Delete role \"$a->role\"?', 2, null, + 'Delete role \"$a->role\"?' + ], + [ + "Delete ASCII\0 NULL control character", 2, null, + 'Delete ASCII NULL control character' + ], + [ + "Delete ASCII\x05 ENQUIRY control character", 2, null, + 'Delete ASCII ENQUIRY control character' + ], + [ + "Delete ASCII\x06 ACKNOWLEDGE control character", 2, null, + 'Delete ASCII ACKNOWLEDGE control character' + ], + [ + "Delete ASCII\x07 BELL control character", 2, null, + 'Delete ASCII BELL control character' + ], + [ + "Delete ASCII\x0E SHIFT OUT control character", 2, null, + 'Delete ASCII SHIFT OUT control character' + ], + [ + "Delete ASCII\x0F SHIFT IN control character", 2, null, + 'Delete ASCII SHIFT IN control character' + ], + [ + "Delete ASCII\x10 DATA LINK ESCAPE control character", 2, null, + 'Delete ASCII DATA LINK ESCAPE control character' + ], + [ + "Delete ASCII\x11 DEVICE CONTROL ONE control character", 2, null, + 'Delete ASCII DEVICE CONTROL ONE control character' + ], + [ + "Delete ASCII\x12 DEVICE CONTROL TWO control character", 2, null, + 'Delete ASCII DEVICE CONTROL TWO control character' + ], + [ + "Delete ASCII\x13 DEVICE CONTROL THREE control character", 2, null, + 'Delete ASCII DEVICE CONTROL THREE control character' + ], + [ + "Delete ASCII\x14 DEVICE CONTROL FOUR control character", 2, null, + 'Delete ASCII DEVICE CONTROL FOUR control character' + ], + [ + "Delete ASCII\x15 NEGATIVE ACKNOWLEDGE control character", 2, null, + 'Delete ASCII NEGATIVE ACKNOWLEDGE control character' + ], + [ + "Delete ASCII\x16 SYNCHRONOUS IDLE control character", 2, null, + 'Delete ASCII SYNCHRONOUS IDLE control character' + ], + [ + "Delete ASCII\x1B ESCAPE control character", 2, null, + 'Delete ASCII ESCAPE control character' + ], + [ + "Delete ASCII\x7F DELETE control character", 2, null, + 'Delete ASCII DELETE control character' + ], + [ + "Delete ISO 8859\xC2\x80 PADDING CHARACTER control character", 2, null, + 'Delete ISO 8859 PADDING CHARACTER control character' + ], + [ + "Delete ISO 8859\xC2\x81 HIGH OCTET PRESET control character", 2, null, + 'Delete ISO 8859 HIGH OCTET PRESET control character' + ], + [ + "Delete ISO 8859\xC2\x83 NO BREAK HERE control character", 2, null, + 'Delete ISO 8859 NO BREAK HERE control character' + ], + [ + "Delete ISO 8859\xC2\x84 INDEX control character", 2, null, + 'Delete ISO 8859 INDEX control character' + ], + [ + "Delete ISO 8859\xC2\x86 START OF SELECTED AREA control character", 2, null, + 'Delete ISO 8859 START OF SELECTED AREA control character' + ], + [ + "Delete ISO 8859\xC2\x87 END OF SELECTED AREA control character", 2, null, + 'Delete ISO 8859 END OF SELECTED AREA control character' + ], + [ + "Delete ISO 8859\xC2\x88 CHARACTER TABULATION SET control character", 2, null, + 'Delete ISO 8859 CHARACTER TABULATION SET control character' + ], + [ + "Delete ISO 8859\xC2\x89 CHARACTER TABULATION WITH JUSTIFICATION control character", 2, null, + 'Delete ISO 8859 CHARACTER TABULATION WITH JUSTIFICATION control character' + ], + [ + "Delete ISO 8859\xC2\x8A LINE TABULATION SET control character", 2, null, + 'Delete ISO 8859 LINE TABULATION SET control character' + ], + [ + "Delete ISO 8859\xC2\x8B PARTIAL LINE FORWARD control character", 2, null, + 'Delete ISO 8859 PARTIAL LINE FORWARD control character' + ], + [ + "Delete ISO 8859\xC2\x8C PARTIAL LINE BACKWARD control character", 2, null, + 'Delete ISO 8859 PARTIAL LINE BACKWARD control character' + ], + [ + "Delete ISO 8859\xC2\x8D REVERSE LINE FEED control character", 2, null, + 'Delete ISO 8859 REVERSE LINE FEED control character' + ], + [ + "Delete ISO 8859\xC2\x8E SINGLE SHIFT TWO control character", 2, null, + 'Delete ISO 8859 SINGLE SHIFT TWO control character' + ], + [ + "Delete ISO 8859\xC2\x8F SINGLE SHIFT THREE control character", 2, null, + 'Delete ISO 8859 SINGLE SHIFT THREE control character' + ], + [ + "Delete ISO 8859\xC2\x90 DEVICE CONTROL STRING control character", 2, null, + 'Delete ISO 8859 DEVICE CONTROL STRING control character' + ], + [ + "Delete ISO 8859\xC2\x91 PRIVATE USE ONE control character", 2, null, + 'Delete ISO 8859 PRIVATE USE ONE control character' + ], + [ + "Delete ISO 8859\xC2\x92 PRIVATE USE TWO control character", 2, null, + 'Delete ISO 8859 PRIVATE USE TWO control character' + ], + [ + "Delete ISO 8859\xC2\x93 SET TRANSMIT STATE control character", 2, null, + 'Delete ISO 8859 SET TRANSMIT STATE control character' + ], + [ + "Delete ISO 8859\xC2\x95 MESSAGE WAITING control character", 2, null, + 'Delete ISO 8859 MESSAGE WAITING control character' + ], + [ + "Delete ISO 8859\xC2\x96 START OF GUARDED AREA control character", 2, null, + 'Delete ISO 8859 START OF GUARDED AREA control character' + ], + [ + "Delete ISO 8859\xC2\x97 END OF GUARDED AREA control character", 2, null, + 'Delete ISO 8859 END OF GUARDED AREA control character' + ], + [ + "Delete ISO 8859\xC2\x99 SINGLE GRAPHIC CHARACTER INTRODUCER control character", 2, null, + 'Delete ISO 8859 SINGLE GRAPHIC CHARACTER INTRODUCER control character' + ], + [ + "Delete ISO 8859\xC2\x9A SINGLE CHARACTER INTRODUCER control character", 2, null, + 'Delete ISO 8859 SINGLE CHARACTER INTRODUCER control character' + ], + [ + "Delete ISO 8859\xC2\x9B CONTROL SEQUENCE INTRODUCER control character", 2, null, + 'Delete ISO 8859 CONTROL SEQUENCE INTRODUCER control character' + ], + [ + "Delete ISO 8859\xC2\x9D OPERATING SYSTEM COMMAND control character", 2, null, + 'Delete ISO 8859 OPERATING SYSTEM COMMAND control character' + ], + [ + "Delete ISO 8859\xC2\x9E PRIVACY MESSAGE control character", 2, null, + 'Delete ISO 8859 PRIVACY MESSAGE control character' + ], + [ + "Delete ISO 8859\xC2\x9F APPLICATION PROGRAM COMMAND control character", 2, null, + 'Delete ISO 8859 APPLICATION PROGRAM COMMAND control character' + ], + [ + "Delete Unicode\xE2\x80\x8B ZERO WIDTH SPACE control character", 2, null, + 'Delete Unicode ZERO WIDTH SPACE control character' + ], + [ + "Delete Unicode\xEF\xBB\xBF ZERO WIDTH NO-BREAK SPACE control character", 2, null, + 'Delete Unicode ZERO WIDTH NO-BREAK SPACE control character' + ], + [ + "Delete Unicode\xEF\xBF\xBD REPLACEMENT CHARACTER control character", 2, null, + 'Delete Unicode REPLACEMENT CHARACTER control character' + ], + // Conterting from v1 to v2. + [ + 'No change', 2, 1, + 'No change' + ], + [ + 'Completed 100% of work', 2, 1, + 'Completed 100% of work' + ], + [ + 'Completed 100%% of work', 2, 1, + 'Completed 100% of work' + ], + [ + "Windows\r\nsucks", 2, 1, + "Windows\nsucks" + ], + [ + "Linux\nsucks", 2, 1, + "Linux\nsucks" + ], + [ + "Mac\rsucks", 2, 1, + "Mac\nsucks" + ], + [ + "LINE TABULATION\x0Bnewline", 2, 1, + "LINE TABULATION\nnewline" + ], + [ + "FORM FEED\x0Cnewline", 2, 1, + "FORM FEED\nnewline" + ], + [ + "END OF TRANSMISSION BLOCK\x17newline", 2, 1, + "END OF TRANSMISSION BLOCK\nnewline" + ], + [ + "END OF MEDIUM\x19newline", 2, 1, + "END OF MEDIUM\nnewline" + ], + [ + "SUBSTITUTE\x1Anewline", 2, 1, + "SUBSTITUTE\nnewline" + ], + [ + "BREAK PERMITTED HERE\xC2\x82newline", 2, 1, + "BREAK PERMITTED HERE\nnewline" + ], + [ + "NEXT LINE\xC2\x85newline", 2, 1, + "NEXT LINE\nnewline" + ], + [ + "START OF STRING\xC2\x98newline", 2, 1, + "START OF STRING\nnewline" + ], + [ + "STRING TERMINATOR\xC2\x9Cnewline", 2, 1, + "STRING TERMINATOR\nnewline" + ], + [ + "Unicode Zl\xE2\x80\xA8newline", 2, 1, + "Unicode Zl\nnewline" + ], + [ + "Unicode Zp\xE2\x80\xA9newline", 2, 1, + "Unicode Zp\nnewline" + ], + [ + "Empty\n\n\n\n\n\nlines", 2, 1, + "Empty\n\n\nlines" + ], + [ + "Trailing \n whitespace\t\nat \nmultilines ", 2, 1, + "Trailing\n whitespace\nat\nmultilines" + ], + [ + 'Do not escape $variable names', 2, 1, + 'Do not escape $variable names' + ], + [ + 'Do not escape \$variable names', 2, 1, + 'Do not escape $variable names' + ], + [ + 'Do not escape $alike names', 2, 1, + 'Do not escape $alike names' + ], + [ + 'Do not escape \$alike names', 2, 1, + 'Do not escape $alike names' + ], + [ + 'Do not escape \$a names', 2, 1, + 'Do not escape $a names' + ], + [ + 'String $a placeholder', 2, 1, + 'String {$a} placeholder' + ], + [ + 'String {$a} placeholder', 2, 1, + 'String {$a} placeholder' + ], + [ + 'Trailing $a', 2, 1, + 'Trailing {$a}' + ], + [ + '$a leading', 2, 1, + '{$a} leading' + ], + [ + '$a', 2, 1, + '{$a}' + ], + [ + '$a->single', 2, 1, + '{$a->single}' + ], + [ + 'Trailing $a->foobar', 2, 1, + 'Trailing {$a->foobar}' + ], + [ + 'Trailing {$a}', 2, 1, + 'Trailing {$a}' + ], + [ + 'Hit $a-times', 2, 1, + 'Hit {$a}-times' + ], + [ + 'This is $a_book', 2, 1, + 'This is $a_book' + ], + [ + 'Object $a->foo placeholder', 2, 1, + 'Object {$a->foo} placeholder' + ], + [ + 'Object {$a->foo} placeholder', 2, 1, + 'Object {$a->foo} placeholder' + ], + [ + 'Trailing $a->bar', 2, 1, + 'Trailing {$a->bar}' + ], + [ + 'Trailing {$a->bar}', 2, 1, + 'Trailing {$a->bar}' + ], + [ + 'Invalid $a-> placeholder', 2, 1, + 'Invalid {$a}-> placeholder' + ], // Weird but BC. + [ + 'AMOS', 2, 1, + 'AMOS' + ], + [ + "'Murder!', she wrote", 2, 1, + "'Murder!', she wrote" + ], // Will be escaped by var_export(). + [ + "\'Murder!\', she wrote", 2, 1, + "'Murder!', she wrote" + ], // Will be escaped by var_export(). + [ + "\t Trim Hunter \t\t", 2, 1, + 'Trim Hunter' + ], + [ + 'Delete role "$a->role"?', 2, 1, + 'Delete role "{$a->role}"?' + ], + [ + 'Delete role \"$a->role\"?', 2, 1, + 'Delete role "{$a->role}"?' + ], + [ + 'See $CFG->foo', 2, 1, + 'See $CFG->foo' + ], + [ + "Delete ASCII\0 NULL control character", 2, 1, + 'Delete ASCII NULL control character' + ], + [ + "Delete ASCII\x05 ENQUIRY control character", 2, 1, + 'Delete ASCII ENQUIRY control character' + ], + [ + "Delete ASCII\x06 ACKNOWLEDGE control character", 2, 1, + 'Delete ASCII ACKNOWLEDGE control character' + ], + [ + "Delete ASCII\x07 BELL control character", 2, 1, + 'Delete ASCII BELL control character' + ], + [ + "Delete ASCII\x0E SHIFT OUT control character", 2, 1, + 'Delete ASCII SHIFT OUT control character' + ], + [ + "Delete ASCII\x0F SHIFT IN control character", 2, 1, + 'Delete ASCII SHIFT IN control character' + ], + [ + "Delete ASCII\x10 DATA LINK ESCAPE control character", 2, 1, + 'Delete ASCII DATA LINK ESCAPE control character' + ], + [ + "Delete ASCII\x11 DEVICE CONTROL ONE control character", 2, 1, + 'Delete ASCII DEVICE CONTROL ONE control character' + ], + [ + "Delete ASCII\x12 DEVICE CONTROL TWO control character", 2, 1, + 'Delete ASCII DEVICE CONTROL TWO control character' + ], + [ + "Delete ASCII\x13 DEVICE CONTROL THREE control character", 2, 1, + 'Delete ASCII DEVICE CONTROL THREE control character' + ], + [ + "Delete ASCII\x14 DEVICE CONTROL FOUR control character", 2, 1, + 'Delete ASCII DEVICE CONTROL FOUR control character' + ], + [ + "Delete ASCII\x15 NEGATIVE ACKNOWLEDGE control character", 2, 1, + 'Delete ASCII NEGATIVE ACKNOWLEDGE control character' + ], + [ + "Delete ASCII\x16 SYNCHRONOUS IDLE control character", 2, 1, + 'Delete ASCII SYNCHRONOUS IDLE control character' + ], + [ + "Delete ASCII\x1B ESCAPE control character", 2, 1, + 'Delete ASCII ESCAPE control character' + ], + [ + "Delete ASCII\x7F DELETE control character", 2, 1, + 'Delete ASCII DELETE control character' + ], + [ + "Delete ISO 8859\xC2\x80 PADDING CHARACTER control character", 2, 1, + 'Delete ISO 8859 PADDING CHARACTER control character' + ], + [ + "Delete ISO 8859\xC2\x81 HIGH OCTET PRESET control character", 2, 1, + 'Delete ISO 8859 HIGH OCTET PRESET control character' + ], + [ + "Delete ISO 8859\xC2\x83 NO BREAK HERE control character", 2, 1, + 'Delete ISO 8859 NO BREAK HERE control character' + ], + [ + "Delete ISO 8859\xC2\x84 INDEX control character", 2, 1, + 'Delete ISO 8859 INDEX control character' + ], + [ + "Delete ISO 8859\xC2\x86 START OF SELECTED AREA control character", 2, 1, + 'Delete ISO 8859 START OF SELECTED AREA control character' + ], + [ + "Delete ISO 8859\xC2\x87 END OF SELECTED AREA control character", 2, 1, + 'Delete ISO 8859 END OF SELECTED AREA control character' + ], + [ + "Delete ISO 8859\xC2\x88 CHARACTER TABULATION SET control character", 2, 1, + 'Delete ISO 8859 CHARACTER TABULATION SET control character' + ], + [ + "Delete ISO 8859\xC2\x89 CHARACTER TABULATION WITH JUSTIFICATION control character", 2, 1, + 'Delete ISO 8859 CHARACTER TABULATION WITH JUSTIFICATION control character' + ], + [ + "Delete ISO 8859\xC2\x8A LINE TABULATION SET control character", 2, 1, + 'Delete ISO 8859 LINE TABULATION SET control character' + ], + [ + "Delete ISO 8859\xC2\x8B PARTIAL LINE FORWARD control character", 2, 1, + 'Delete ISO 8859 PARTIAL LINE FORWARD control character' + ], + [ + "Delete ISO 8859\xC2\x8C PARTIAL LINE BACKWARD control character", 2, 1, + 'Delete ISO 8859 PARTIAL LINE BACKWARD control character' + ], + [ + "Delete ISO 8859\xC2\x8D REVERSE LINE FEED control character", 2, 1, + 'Delete ISO 8859 REVERSE LINE FEED control character' + ], + [ + "Delete ISO 8859\xC2\x8E SINGLE SHIFT TWO control character", 2, 1, + 'Delete ISO 8859 SINGLE SHIFT TWO control character' + ], + [ + "Delete ISO 8859\xC2\x8F SINGLE SHIFT THREE control character", 2, 1, + 'Delete ISO 8859 SINGLE SHIFT THREE control character' + ], + [ + "Delete ISO 8859\xC2\x90 DEVICE CONTROL STRING control character", 2, 1, + 'Delete ISO 8859 DEVICE CONTROL STRING control character' + ], + [ + "Delete ISO 8859\xC2\x91 PRIVATE USE ONE control character", 2, 1, + 'Delete ISO 8859 PRIVATE USE ONE control character' + ], + [ + "Delete ISO 8859\xC2\x92 PRIVATE USE TWO control character", 2, 1, + 'Delete ISO 8859 PRIVATE USE TWO control character' + ], + [ + "Delete ISO 8859\xC2\x93 SET TRANSMIT STATE control character", 2, 1, + 'Delete ISO 8859 SET TRANSMIT STATE control character' + ], + [ + "Delete ISO 8859\xC2\x95 MESSAGE WAITING control character", 2, 1, + 'Delete ISO 8859 MESSAGE WAITING control character' + ], + [ + "Delete ISO 8859\xC2\x96 START OF GUARDED AREA control character", 2, 1, + 'Delete ISO 8859 START OF GUARDED AREA control character' + ], + [ + "Delete ISO 8859\xC2\x97 END OF GUARDED AREA control character", 2, 1, + 'Delete ISO 8859 END OF GUARDED AREA control character' + ], + [ + "Delete ISO 8859\xC2\x99 SINGLE GRAPHIC CHARACTER INTRODUCER control character", 2, 1, + 'Delete ISO 8859 SINGLE GRAPHIC CHARACTER INTRODUCER control character' + ], + [ + "Delete ISO 8859\xC2\x9A SINGLE CHARACTER INTRODUCER control character", 2, 1, + 'Delete ISO 8859 SINGLE CHARACTER INTRODUCER control character' + ], + [ + "Delete ISO 8859\xC2\x9B CONTROL SEQUENCE INTRODUCER control character", 2, 1, + 'Delete ISO 8859 CONTROL SEQUENCE INTRODUCER control character' + ], + [ + "Delete ISO 8859\xC2\x9D OPERATING SYSTEM COMMAND control character", 2, 1, + 'Delete ISO 8859 OPERATING SYSTEM COMMAND control character' + ], + [ + "Delete ISO 8859\xC2\x9E PRIVACY MESSAGE control character", 2, 1, + 'Delete ISO 8859 PRIVACY MESSAGE control character' + ], + [ + "Delete ISO 8859\xC2\x9F APPLICATION PROGRAM COMMAND control character", 2, 1, + 'Delete ISO 8859 APPLICATION PROGRAM COMMAND control character' + ], + [ + "Delete Unicode\xE2\x80\x8B ZERO WIDTH SPACE control character", 2, 1, + 'Delete Unicode ZERO WIDTH SPACE control character' + ], + [ + "Delete Unicode\xEF\xBB\xBF ZERO WIDTH NO-BREAK SPACE control character", 2, 1, + 'Delete Unicode ZERO WIDTH NO-BREAK SPACE control character' + ], + [ + "Delete Unicode\xEF\xBF\xBD REPLACEMENT CHARACTER control character", 2, 1, + 'Delete Unicode REPLACEMENT CHARACTER control character' + ], + ]; + } +} diff --git a/admin/tool/customlang/tests/local/mlang/phpparser_test.php b/admin/tool/customlang/tests/local/mlang/phpparser_test.php new file mode 100644 index 00000000000..3dcdd934d5c --- /dev/null +++ b/admin/tool/customlang/tests/local/mlang/phpparser_test.php @@ -0,0 +1,207 @@ +. + +/** + * PHP lang parser test. + * + * @package tool_customlang + * @copyright 2015 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace tool_customlang\local\mlang; + +use advanced_testcase; +use moodle_exception; + +/** + * PHP lang parser test class. + * + * @package tool_customlang + * @copyright 2015 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class phpparser_testcase extends advanced_testcase { + + + /** + * Test get instance static method. + * + */ + public function test_get_instance(): void { + + $instance = phpparser::get_instance(); + + $this->assertInstanceOf('tool_customlang\local\mlang\phpparser', $instance); + $this->assertEquals($instance, phpparser::get_instance()); + } + + /** + * Test get instance parse method. + * + * @dataProvider parse_provider + * @param string $phpcode PHP code to test + * @param array $expected Expected result + * @param bool $exception if an exception is expected + */ + public function test_parse(string $phpcode, array $expected, bool $exception): void { + + $instance = phpparser::get_instance(); + + if ($exception) { + $this->expectException(moodle_exception::class); + } + + $strings = $instance->parse($phpcode); + + $this->assertEquals(count($expected), count($strings)); + foreach ($strings as $key => $langstring) { + $this->assertEquals($expected[$key][0], $langstring->id); + $this->assertEquals($expected[$key][1], $langstring->text); + } + } + + /** + * Data provider for the test_parse. + * + * @return array + */ + public function parse_provider() : array { + return [ + 'Invalid PHP code' => [ + 'No PHP code', [], false + ], + 'No PHP open tag' => [ + "\$string['example'] = 'text';\n", [], false + ], + 'One string code' => [ + " [ + " [ + " [ + " [ + " [ + " [ + " [ + " [ + " [ + " [ + " [ + " [ + " [ + " [ + " [ + " [ + " [ + "version = 2021052501; +$plugin->version = 2021052502; $plugin->requires = 2021052500; $plugin->component = 'tool_customlang'; // Full name of the plugin (used for diagnostics)