From 0152e5c12e5ecd0ecfac0143cd5884c352f08969 Mon Sep 17 00:00:00 2001 From: Renaat Debleu Date: Tue, 4 Jun 2024 22:57:54 +0000 Subject: [PATCH] infection --- .github/workflows/infection.yml | 56 +++++++++++++++++++++++++++++++++ .infection.json5 | 17 ++++++++++ README.md | 3 +- classes/condition.php | 39 ++++++++++++----------- classes/frontend.php | 2 -- tests/condition_test.php | 3 ++ 6 files changed, 98 insertions(+), 22 deletions(-) create mode 100644 .github/workflows/infection.yml create mode 100644 .infection.json5 diff --git a/.github/workflows/infection.yml b/.github/workflows/infection.yml new file mode 100644 index 0000000..644e515 --- /dev/null +++ b/.github/workflows/infection.yml @@ -0,0 +1,56 @@ +name: Tests + +on: [push, pull_request] + +jobs: + ci: + name: ci + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + moodle-branch: ['main'] + php: ['8.3'] + database: ['mysqli'] + + steps: + - name: checkout plugin + uses: actions/checkout@v4 + with: + path: this-plugin + + - name: setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + ini-values: max_input_vars=5000 + tools: infection, phpunit + coverage: pcov + + - name: composer + run: | + composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci + echo $(cd ci/bin; pwd) >> $GITHUB_PATH + echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH + sudo locale-gen en_AU.UTF-8 + sudo systemctl start mysql.service + + - name: install Moodle + run: moodle-plugin-ci install --db-user=root --db-pass=root --db-host=127.0.0.1 --plugin this-plugin + env: + DB: ${{ matrix.database }} + MOODLE_BRANCH: ${{ matrix.moodle-branch }} + + - name: autoload + working-directory: moodle + run: | + echo ' vendor/autoloadmoodle.php + echo 'define("PHPUNIT_UTIL", true);' >> vendor/autoloadmoodle.php + echo 'require(__DIR__ . "/../lib/phpunit/bootstrap.php");' >> vendor/autoloadmoodle.php + echo 'require("autoload.php");' >> vendor/autoloadmoodle.php + + - name: infection + if: ${{ !cancelled() }} + run: infection -s --only-covered --configuration=availability/condition/language/.infection.json5 + working-directory: moodle diff --git a/.infection.json5 b/.infection.json5 new file mode 100644 index 0000000..d0d5700 --- /dev/null +++ b/.infection.json5 @@ -0,0 +1,17 @@ +{ + "$schema": "https://raw.githubusercontent.com/infection/infection/0.27.0/resources/schema.json", + "source": { + "directories": [ + ".", + ], + }, + "mutators": { + "@default": true, + }, + "phpUnit": { + "configDir": ".", + "customPath": "./vendor/bin/phpunit" + }, + "initialTestsPhpOptions": "-dxdebug.mode=off -dpcov.enabled=1 -dpcov.directory=.", + "bootstrap": "./vendor/autoloadmoodle.php" +} diff --git a/README.md b/README.md index 6f4c184..793af7d 100644 --- a/README.md +++ b/README.md @@ -68,10 +68,11 @@ This plugin is maintained for the latest major releases of Moodle. [![Build Status](https://github.com/ewallah/moodle-availability_language/workflows/Tests/badge.svg)](https://github.com/ewallah/moodle-availability_language/actions) [![Coverage Status](https://coveralls.io/repos/github/ewallah/moodle-availability_language/badge.svg?branch=main)](https://coveralls.io/github/ewallah/moodle-availability_language?branch=main) +![Mutation score](https://badgen.net/badge/Mutation%20Score%20Indicator/96) ## Copyright -2023 eWallah.net +eWallah.net This program 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 diff --git a/classes/condition.php b/classes/condition.php index 092e609..e9d9ef8 100644 --- a/classes/condition.php +++ b/classes/condition.php @@ -93,35 +93,36 @@ public static function get_json($languageid = '') { * @return bool True if available */ public function is_available($not, \core_availability\info $info, $grabthelot, $userid) { - global $CFG, $DB, $USER; + global $CFG, $USER; // If course has forced language. $course = $info->get_course(); $allow = false; - if (isset($course->lang) && $course->lang == $this->languageid) { + if (isset($course->lang) && $course->lang === $this->languageid) { $allow = true; - } - if ($userid == $USER->id) { - // Checking the language of the currently logged in user, so do not - // default to the account language, because the session language - // or the language of the current course may be different. - $language = current_language(); } else { - if (is_null($userid)) { - // Fall back to site language or English. - $language = $CFG->lang ?? 'en'; + if ($userid === $USER->id) { + // Checking the language of the currently logged in user, so do not + // default to the account language, because the session language + // or the language of the current course may be different. + $language = current_language(); } else { - // Checking access for someone else than the logged in user, so - // use the preferred language of that user account. - // This language is never empty as there is a not-null constraint. - $language = $DB->get_field('user', 'lang', ['id' => $userid]); + if (is_null($userid)) { + // Fall back to site language or English. + $language = $CFG->lang; + } else { + // Checking access for someone else than the logged in user, so + // use the preferred language of that user account. + // This language is never empty as there is a not-null constraint. + $language = \core_user::get_user($userid)->lang; + } + } + if ($language === $this->languageid) { + $allow = true; } - } - if ($language == $this->languageid) { - $allow = true; } if ($not) { - $allow = !$allow; + return !($allow); } return $allow; } diff --git a/classes/frontend.php b/classes/frontend.php index 3293a78..c2c30df 100644 --- a/classes/frontend.php +++ b/classes/frontend.php @@ -43,7 +43,6 @@ class frontend extends \core_availability\frontend { * @param cm_info $cm Course-module currently being edited (null if none) * @param section_info $section Section currently being edited (null if none) * @return array Array of parameters for the JavaScript function - * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ protected function get_javascript_init_params($course, \cm_info $cm = null, \section_info $section = null) { return [self::convert_associative_array_for_js(get_string_manager()->get_list_of_translations(), 'id', 'name')]; @@ -59,7 +58,6 @@ protected function get_javascript_init_params($course, \cm_info $cm = null, \sec * @param cm_info $cm Course-module currently being edited (null if none) * @param section_info $section Section currently being edited (null if none) * @return bool True if available - * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ protected function allow_add($course, \cm_info $cm = null, \section_info $section = null) { // If forced course language. diff --git a/tests/condition_test.php b/tests/condition_test.php index 7e575f7..14042e8 100755 --- a/tests/condition_test.php +++ b/tests/condition_test.php @@ -44,6 +44,7 @@ final class condition_test extends \advanced_testcase { public function setUp(): void { // Load the mock info class so that it can be used. global $CFG; + parent::setUp(); require_once($CFG->dirroot . '/availability/tests/fixtures/mock_info.php'); } @@ -83,6 +84,8 @@ public function test_in_tree(): void { $this->assertFalse($tree1->check_available(true, $info1, true, null)->is_available()); $this->assertFalse($tree1->check_available(false, $info1, true, $user1)->is_available()); $this->assertTrue($tree2->check_available(false, $info1, true, $user1)->is_available()); + $this->assertTrue($tree1->check_available(true, $info1, true, $user1)->is_available()); + $this->assertFalse($tree2->check_available(true, $info1, true, $user1)->is_available()); $this->assertTrue($tree1->check_available(false, $info1, true, $user2)->is_available()); $this->assertFalse($tree2->check_available(false, $info1, true, $user2)->is_available()); $this->assertFalse($tree1->check_available(false, $info2, true, $user1)->is_available());