From 383c61e0b9f1ab8c53c81289428d5c339914eaae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Arenaza?= Date: Fri, 9 Dec 2022 18:42:57 +0100 Subject: [PATCH] Refactor units tests to work in Moodle 3.11 and later. Use filter_multilang as the basis for the refactoring. Obviously this means that this plugin version should only be used in Moodle 3.11 and later. Not because the plugin itself can't run in earlier versions (it should be able to run on anything since Moodle 3.5). But because the unit tests will fail on anything earlier than Moodle 3.11 (as the way unit tests are implemented changed in an incompatible way in that release). This is why we are bumping the plugin version to 2.0.0. To signal the breaking change. Also extract the data used for the actual unit test to a separate file, as the idea is to keep two separate branches for the Moodle 3.10 and earlier versions, and Moodle 3.11 and later versions. But to reuse the unit tests data across the two branches as easily as possible, without having to touch other parts of the code. Fixes: #32 --- tests/actual_test_cases.php | 261 +++++++++++++++++++++++++++++++ tests/filter_test.php | 298 ++++++------------------------------ version.php | 6 +- 3 files changed, 312 insertions(+), 253 deletions(-) create mode 100644 tests/actual_test_cases.php diff --git a/tests/actual_test_cases.php b/tests/actual_test_cases.php new file mode 100644 index 0000000..db5defc --- /dev/null +++ b/tests/actual_test_cases.php @@ -0,0 +1,261 @@ +. + +/** + * Actual test cases for the unit tests. + * + * @package filter_multilang2 + * @category test + * @copyright 2022 onwards Iñaki Arenaza + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Returns the actual test cases for the unit tests. + * + * @return array of test cases + */ +function multilang2_actual_test_cases() { + return [ + [ + 'No multilang tags', + 'No multilang tags', + 'es', + ], + [ + 'Todo el texto está en español', + '{mlang es}Todo el texto está en español{mlang}', + 'es', + ], + [ + 'Todo el texto está en español', + '{MLANG es}Todo el texto está en español{MLANG}', + 'es', + ], + [ + 'Todo el texto está en español', + '{MLANG ES}Todo el texto está en español{MLANG}', + 'es', + ], + [ + 'Todo el texto está en español', + '{MlAnG Es}Todo el texto está en español{MlAnG}', + 'es', + ], + [ + 'Some non-filtered content plus some content in Spanish (mejor dicho, en español)', + 'Some non-filtered content {mlang es}plus some content in Spanish (mejor dicho, en español){mlang}', + 'es', + ], + [ + 'Zerbait euskeraz', + '{mlang es}Algo en español{mlang}{mlang eu}Zerbait euskeraz{mlang}', + 'eu', + ], + [ + 'Non-filtered {begin}EuskerazNon-filtered{end}', + 'Non-filtered {begin}{mlang es}En español{mlang}{mlang eu}Euskeraz{mlang}Non-filtered{end}', + 'eu', + ], + [ + '{mlang}Bad filter syntax{mlang}', + '{mlang}Bad filter syntax{mlang}', + 'es', + ], + [ + '{mlang-es}Bad filter syntax{mlang}', + '{mlang-es}Bad filter syntax{mlang}', + 'es', + ], + [ + '{mlang_es}Bad filter syntax{mlang}', + '{mlang_es}Bad filter syntax{mlang}', + 'es', + ], + [ + '', + '{mlang _es}Este texto está en español{mlang}', + 'es', + ], + [ + '', + '{mlang -es}Este texto está en español{mlang}', + 'es', + ], + [ + '{mlang}Bad filter syntax{mlang}Algo de español', + '{mlang}Bad filter syntax{mlang}{mlang es}Algo de español{mlang}', + 'es', + ], + [ + 'Before {mlang}Bad filter syntax{mlang}Algo de español After', + 'Before {mlang}Bad filter syntax{mlang}{mlang es}Algo de español{mlang} After', + 'es', + ], + [ + 'Before After', + 'Before {mlang non-existent-language}Some content{mlang} After', + 'es', + ], + [ + 'Before Some content After', + 'Before {mlang en_us}Some content{mlang} After', + 'en_us', ['en_us' => 'en'], + ], + [ + 'Before Some content After', + 'Before {mlang en-us}Some content{mlang} After', + 'en_us', ['en_us' => 'en'], + ], + [ + 'Before After', + 'Before {mlang fr}French{mlang}{mlang it}Italian{mlang}{mlang de}Deutsch{mlang} After', + 'en', + ], + [ + 'Before България + Middle България + After', + 'Before {mlang fr}Français{mlang}{mlang it}Italiano{mlang}{mlang de}Deutsch{mlang}{mlang other}България{mlang} + Middle {mlang other}България{mlang}{mlang it}Italiano{mlang}{mlang de}Deutsch{mlang}{mlang fr}Français{mlang} + After', + 'en', + ], + [ + 'no other language declared', + '{mlang de}Deutsch {mlang}no other language declared', + 'en', + ], + [ + 'Other language', + '{mlang other}Other language{mlang}', + 'en', + ], + [ + 'Other language Middle Other language', + '{mlang other}Other language{mlang} Middle {mlang de}Deutsch{mlang}{mlang other}Other language{mlang}', + 'en', + ], + [ + 'Hello! + This text is common for all languages because it is outside of all lang blocks. + Bye!', + '{mlang other}Hello!{mlang}{mlang es,es_mx}¡Hola!{mlang} + This text is common for all languages because it is outside of all lang blocks. + {mlang other}Bye!{mlang}{mlang it}Ciao!{mlang}', + 'fr', + ], + [ + '¡Hola! + This text is common for all languages because it is outside of all lang blocks. + ', + '{mlang other}Hello!{mlang}{mlang es,es_mx}¡Hola!{mlang} + This text is common for all languages because it is outside of all lang blocks. + {mlang other}Bye!{mlang}{mlang it}Ciao!{mlang}', + 'es_mx', ['es_mx' => 'es'] + ], + [ + ' + This text is common for all languages because it is outside of all lang blocks. + Ciao!', + '{mlang other}Hello!{mlang}{mlang es,es_mx}¡Hola!{mlang} + This text is common for all languages because it is outside of all lang blocks. + {mlang other}Bye!{mlang}{mlang it}Ciao!{mlang}', + 'it', + ], + [ + 'Todo el texto está en español', + '{mlang en,fr,es}Todo el texto está en español{mlang}', + 'es', + ], + [ + 'Todo el texto está en español', + '{mlang en , fr,es }Todo el texto está en español{mlang}', + 'es', + ], + [ + 'Todo el texto está en español', + '{mLaNg eN , FR,Es }Todo el texto está en español{mlang}', + 'es', + ], + [ + '{mlang en , , ,,, fr,es,, ,}Bad filter syntax{mlang}', + '{mlang en , , ,,, fr,es,, ,}Bad filter syntax{mlang}', + 'es', + ], + [ + '{mlang en ,}Bad filter syntax{mlang}', + '{mlang en ,}Bad filter syntax{mlang}', + 'es', + ], + [ + '{mlang en, }Bad filter syntax{mlang}', + '{mlang en, }Bad filter syntax{mlang}', + 'es', + ], + [ + '{mlang en , }Bad filter syntax{mlang}', + '{mlang en , }Bad filter syntax{mlang}', + 'es', + ], + [ + '{mlang en, , }Bad filter syntax{mlang}', + '{mlang en, , }Bad filter syntax{mlang}', + 'es', + ], + [ + '{mlang en,,es}Bad filter syntax{mlang}', + '{mlang en,,es}Bad filter syntax{mlang}', + 'es', + ], + [ + '{mlang en ,, es }Bad filter syntax{mlang}', + '{mlang en ,, es }Bad filter syntax{mlang}', + 'es', + ], + [ + '{mlang en , , es}Bad filter syntax{mlang}', + '{mlang en , , es}Bad filter syntax{mlang}', + 'es', + ], + [ + '{mlang en , , es , }Bad filter syntax{mlang}', + '{mlang en , , es , }Bad filter syntax{mlang}', + 'es', + ], + [ + '{mlang en,es,}Bad filter syntax{mlang}', + '{mlang en,es,}Bad filter syntax{mlang}', + 'es', + ], + [ + '{mlang en , es , }Bad filter syntax{mlang}', + '{mlang en , es , }Bad filter syntax{mlang}', + 'es', + ], + [ + '{mlang,es}Bad filter syntax{mlang}', + '{mlang,es}Bad filter syntax{mlang}', + 'es', + ], + [ + '{mlang ,es}Bad filter syntax{mlang}', + '{mlang ,es}Bad filter syntax{mlang}', + 'es', + ], + ]; +} + diff --git a/tests/filter_test.php b/tests/filter_test.php index 42ced2f..2ba2962 100644 --- a/tests/filter_test.php +++ b/tests/filter_test.php @@ -17,19 +17,19 @@ /** * Tests for filter_multilang2. * - * Based on unit tests from filter_text, by Damyon Wise. + * Based on unit tests from filter_multilang, by The Open University * * @package filter_multilang2 * @category test - * @copyright 2014 Damyon Wiese - * @copyright 2016 Iñaki Arenaza & Mondragon Unibertsitatea + * @copyright 2014 onwards Damyon Wiese + * @copyright 2016 onwards Iñaki Arenaza & Mondragon Unibertsitatea + * @copyright 2019 onwards The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -defined('MOODLE_INTERNAL') || die(); +namespace filter_multilang2; -global $CFG; -require_once($CFG->dirroot . '/filter/multilang2/filter.php'); +use filter_multilang2; /** * Unit tests for Multi-Language v2 filter. @@ -39,14 +39,9 @@ * * @package filter_multilang2 * @category test - * @copyright 2014 Damyon Wiese - * @copyright 2016 Iñaki Arenaza & Mondragon Unibertsitatea - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @covers \filter_multilang2 */ -class filter_multilang2_testcase extends advanced_testcase { - - /** @var object The filter plugin object to perform the tests on */ - protected $filter; +class filter_test extends \advanced_testcase { /** * Setup the test framework @@ -55,252 +50,55 @@ class filter_multilang2_testcase extends advanced_testcase { */ protected function setUp():void { parent::setUp(); + $this->resetAfterTest(true); - $this->filter = new filter_multilang2(context_system::instance(), array()); + + // Enable multilang2 filter at top level. + filter_set_global_state('multilang2', TEXTFILTER_ON); } /** - * Perform the actual tests, once the unit test is set up. + * Setup parent language relationship. * - * @return void + * @param string $child the child language, e.g. 'fr_ca'. + * @param string $parent the parent language, e.g. 'fr'. */ - public function test_filter_multilang2() { + protected function setup_parent_language(string $child, string $parent) { global $CFG; - $tests = array( - array ( - 'filterwithlang' => 'es', - 'before' => 'No multilang tags', - 'after' => 'No multilang tags', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang es}Todo el texto está en español{mlang}', - 'after' => 'Todo el texto está en español', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{MLANG es}Todo el texto está en español{MLANG}', - 'after' => 'Todo el texto está en español', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{MLANG ES}Todo el texto está en español{MLANG}', - 'after' => 'Todo el texto está en español', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{MlAnG Es}Todo el texto está en español{MlAnG}', - 'after' => 'Todo el texto está en español', - ), - array ( - 'filterwithlang' => 'es', - 'before' => 'Some non-filtered content {mlang es}plus some content in Spanish (mejor dicho, en español){mlang}', - 'after' => 'Some non-filtered content plus some content in Spanish (mejor dicho, en español)', - ), - array ( - 'filterwithlang' => 'eu', - 'before' => '{mlang es}Algo en español{mlang}{mlang eu}Zerbait euskeraz{mlang}', - 'after' => 'Zerbait euskeraz', - ), - array ( - 'filterwithlang' => 'eu', - 'before' => 'Non-filtered {begin}{mlang es}En español{mlang}{mlang eu}Euskeraz{mlang}Non-filtered{end}', - 'after' => 'Non-filtered {begin}EuskerazNon-filtered{end}', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang}Bad filter syntax{mlang}', - 'after' => '{mlang}Bad filter syntax{mlang}', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang-es}Bad filter syntax{mlang}', - 'after' => '{mlang-es}Bad filter syntax{mlang}', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang_es}Bad filter syntax{mlang}', - 'after' => '{mlang_es}Bad filter syntax{mlang}', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang _es}Este texto está en español{mlang}', - 'after' => '', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang -es}Este texto está en español{mlang}', - 'after' => '', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang}Bad filter syntax{mlang}{mlang es}Algo de español{mlang}', - 'after' => '{mlang}Bad filter syntax{mlang}Algo de español', - ), - array ( - 'filterwithlang' => 'es', - 'before' => 'Before {mlang}Bad filter syntax{mlang}{mlang es}Algo de español{mlang} After', - 'after' => 'Before {mlang}Bad filter syntax{mlang}Algo de español After', - ), - array ( - 'filterwithlang' => 'es', - 'before' => 'Before {mlang non-existent-language}Some content{mlang} After', - 'after' => 'Before After', - ), - array ( - 'filterwithlang' => 'en_us', - 'before' => 'Before {mlang en_us}Some content{mlang} After', - 'after' => 'Before Some content After', - ), - array ( - 'filterwithlang' => 'en_us', - 'before' => 'Before {mlang en-us}Some content{mlang} After', - 'after' => 'Before Some content After', - ), - array ( - 'filterwithlang' => 'en', - 'before' => 'Before {mlang fr}French{mlang}{mlang it}Italian{mlang}{mlang de}Deutsch{mlang} After', - 'after' => 'Before After', - ), - array ( - 'filterwithlang' => 'en', - 'before' => 'Before {mlang fr}Frnçais{mlang}{mlang it}Italiano{mlang}{mlang de}Deutsch{mlang}' . - '{mlang other}България{mlang} Middle {mlang other}България{mlang}' . - '{mlang it}Italiano{mlang}{mlang de}Deutsch{mlang}{mlang fr}Français{mlang} After', - 'after' => 'Before България Middle България After', - ), - array ( - 'filterwithlang' => 'en', - 'before' => '{mlang de}Deutsch {mlang}no other language declared', - 'after' => 'no other language declared', - ), - array ( - 'filterwithlang' => 'en', - 'before' => '{mlang other}Other language{mlang}', - 'after' => 'Other language', - ), - array ( - 'filterwithlang' => 'en', - 'before' => '{mlang other}Other language{mlang} Middle {mlang de}Deutsch{mlang}{mlang other}Other language{mlang}', - 'after' => 'Other language Middle Other language', - ), - array ( - 'filterwithlang' => 'fr', - 'before' => '{mlang other}Hello!{mlang}{mlang es,es_mx}¡Hola!{mlang} - This text is common for all languages because it is outside of all lang blocks. - {mlang other}Bye!{mlang}{mlang it}Ciao!{mlang}', - 'after' => 'Hello! - This text is common for all languages because it is outside of all lang blocks. - Bye!', - ), - array ( - 'filterwithlang' => 'es_mx', - 'before' => '{mlang other}Hello!{mlang}{mlang es,es_mx}¡Hola!{mlang} - This text is common for all languages because it is outside of all lang blocks. - {mlang other}Bye!{mlang}{mlang it}Ciao!{mlang}', - 'after' => '¡Hola! - This text is common for all languages because it is outside of all lang blocks. - ', - ), - array ( - 'filterwithlang' => 'it', - 'before' => '{mlang other}Hello!{mlang}{mlang es,es_mx}¡Hola!{mlang} - This text is common for all languages because it is outside of all lang blocks. - {mlang other}Bye!{mlang}{mlang it}Ciao!{mlang}', - 'after' => ' - This text is common for all languages because it is outside of all lang blocks. - Ciao!', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang en,fr,es}Todo el texto está en español{mlang}', - 'after' => 'Todo el texto está en español', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang en , fr,es }Todo el texto está en español{mlang}', - 'after' => 'Todo el texto está en español', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mLaNg eN , FR,Es }Todo el texto está en español{mlang}', - 'after' => 'Todo el texto está en español', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang en , , ,,, fr,es,, ,}Bad filter syntax{mlang}', - 'after' => '{mlang en , , ,,, fr,es,, ,}Bad filter syntax{mlang}', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang en ,}Bad filter syntax{mlang}', - 'after' => '{mlang en ,}Bad filter syntax{mlang}', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang en, }Bad filter syntax{mlang}', - 'after' => '{mlang en, }Bad filter syntax{mlang}', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang en , }Bad filter syntax{mlang}', - 'after' => '{mlang en , }Bad filter syntax{mlang}', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang en, , }Bad filter syntax{mlang}', - 'after' => '{mlang en, , }Bad filter syntax{mlang}', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang en,,es}Bad filter syntax{mlang}', - 'after' => '{mlang en,,es}Bad filter syntax{mlang}', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang en ,, es }Bad filter syntax{mlang}', - 'after' => '{mlang en ,, es }Bad filter syntax{mlang}', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang en , , es}Bad filter syntax{mlang}', - 'after' => '{mlang en , , es}Bad filter syntax{mlang}', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang en , , es , }Bad filter syntax{mlang}', - 'after' => '{mlang en , , es , }Bad filter syntax{mlang}', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang en,es,}Bad filter syntax{mlang}', - 'after' => '{mlang en,es,}Bad filter syntax{mlang}', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang en , es , }Bad filter syntax{mlang}', - 'after' => '{mlang en , es , }Bad filter syntax{mlang}', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang,es}Bad filter syntax{mlang}', - 'after' => '{mlang,es}Bad filter syntax{mlang}', - ), - array ( - 'filterwithlang' => 'es', - 'before' => '{mlang ,es}Bad filter syntax{mlang}', - 'after' => '{mlang ,es}Bad filter syntax{mlang}', - ), - ); + $langfolder = $CFG->dataroot . '/lang/' . $child; + check_dir_exists($langfolder); + $langconfig = " parent lang. E.g. ['es_co' => 'es', 'es_mx' => 'es']. + */ + public function test_filtering($expectedoutput, $input, $targetlang, $parentlangs = []) { + global $SESSION; + $SESSION->forcelang = $targetlang; - // As we need to switch languages to test the filter, store the current - // language to restore it at the end the tests. - $currlang = $CFG->lang; - foreach ($tests as $test) { - $CFG->lang = $test['filterwithlang']; - $this->assertEquals($test['after'], $this->filter->filter($test['before'])); + foreach ($parentlangs as $child => $parent) { + $this->setup_parent_language($child, $parent); } - $CFG->lang = $currlang; + + $filtered = format_text($input, FORMAT_HTML, array('context' => \context_system::instance())); + $this->assertEquals($expectedoutput, $filtered); } } diff --git a/version.php b/version.php index eba954d..cf59552 100644 --- a/version.php +++ b/version.php @@ -25,8 +25,8 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2020101300; // The current plugin version (Date: YYYYMMDDXX). -$plugin->requires = 2013111800; // Requires this Moodle version. +$plugin->version = 2022120900; // The current plugin version (Date: YYYYMMDDXX). +$plugin->requires = 2021051700; // Requires this Moodle version. $plugin->component = 'filter_multilang2'; // Full name of the plugin (used for diagnostics). -$plugin->release = '1.1.2'; +$plugin->release = '2.0.0'; $plugin->maturity = MATURITY_STABLE;