Skip to content

Commit

Permalink
infection
Browse files Browse the repository at this point in the history
  • Loading branch information
rdebleu committed Jun 4, 2024
1 parent 45a22f6 commit 0152e5c
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 22 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/infection.yml
Original file line number Diff line number Diff line change
@@ -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 '<?php' > 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
17 changes: 17 additions & 0 deletions .infection.json5
Original file line number Diff line number Diff line change
@@ -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"
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 20 additions & 19 deletions classes/condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 0 additions & 2 deletions classes/frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')];
Expand All @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions tests/condition_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 0152e5c

Please sign in to comment.