Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infection #1

Merged
merged 17 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions .github/workflows/moodle-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,46 @@ on:
- '**'

jobs:
define-matrix:
runs-on: ubuntu-latest

outputs:
include_list: ${{ steps.include_list.outputs.include_list }}

steps:
- name: Check out repository code
uses: actions/checkout@v4

- name: Define matrix
id: include_list
run: |
# Generates a json array with include entries for all possible combinations of PHP, Moodle branch and database
include_str="["
first_entry=true
for i in $(cat plugin_compatibility.json | jq -c '.[]'); do
moodle=$(echo "$i" | jq -r '.moodle')
for php in $(echo "$i" | jq -r '.PHP[]'); do
for database in pgsql mariadb; do
if [ "$first_entry" = true ]; then
first_entry=false
else
include_str="$include_str,"
fi
next_entry="{\"php\": \"$php\", \"moodle-branch\": \"$moodle\", \"database\": \"$database\", \"experimental\": false}"
include_str="$include_str$next_entry"
done
done
done
include_str="$include_str]"
echo "include_list=$include_str" >> "$GITHUB_OUTPUT"

test:
runs-on: ubuntu-22.04
needs: define-matrix

services:
postgres:
image: postgres:13
image: postgres:16
env:
POSTGRES_USER: 'postgres'
POSTGRES_HOST_AUTH_METHOD: 'trust'
Expand All @@ -38,15 +72,11 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['8.1', '8.2', '8.3']
moodle-branch: ['MOODLE_404_STABLE', 'MOODLE_405_STABLE']
php: ['8.3']
moodle-branch: ['main']
database: [pgsql, mariadb]
experimental: [false]
include:
- php: '8.3'
moodle-branch: 'main'
database: mariadb
experimental: true
experimental: [true]
include: ${{ fromJSON(needs.define-matrix.outputs.include_list) }}

continue-on-error: ${{ matrix.experimental }}

Expand Down
49 changes: 25 additions & 24 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Moodle Plugin CI
name: Create release

on:
release:
Expand All @@ -24,45 +24,46 @@ jobs:
# - release is marked as pre-release on github, then MATURITY_BETA
# - release name contains 'rc', then MATURITY_RC
if [[ "${{ github.ref_name }}" == *"rc"* ]]; then
sed -i "s/^\$plugin->maturity\s=\sMATURITY_STABLE;/\$plugin->maturity = MATURITY_RC;/" version.php
sed -i "s/^\$plugin->maturity\s=\sMATURITY_[A-Z]*;/\$plugin->maturity = MATURITY_RC;/" version.php
elif [[ "${{ github.event_name }}" == "release" && "${{ github.event.release.prerelease }}" == "true" ]]; then
sed -i "s/^\$plugin->maturity\s=\sMATURITY_STABLE;/\$plugin->maturity = MATURITY_BETA;/" version.php
else
sed -i "s/^\$plugin->maturity\s=\sMATURITY_STABLE;/\$plugin->maturity = MATURITY_STABLE;/" version.php
sed -i "s/^\$plugin->maturity\s=\sMATURITY_[A-Z]*;/\$plugin->maturity = MATURITY_BETA;/" version.php
else
sed -i "s/^\$plugin->maturity\s=\sMATURITY_[A-Z]*;/\$plugin->maturity = MATURITY_STABLE;/" version.php
fi

- name: remove files not needed for release
run: |
rm -rf .github tests vendor .gitignore composer.json composer.lock phpunit.xml dev_utils
rm -rf .github tests vendor .gitignore composer.json composer.lock phpunit.xml dev_utils phpunit.xml infection.json5

- name: Create release archives
- name: Create release archive
run: |
tar --exclude='.git' -czf /tmp/moodle-${{ env.PLUGIN_NAME }}-${{ github.ref_name }}.tgz *
zip -x .git -r /tmp/moodle-${{ env.PLUGIN_NAME }}-${{ github.ref_name }}.zip *
FOLDER_NAME=$(echo ${{ env.PLUGIN_NAME }} | cut -d'_' -f2)
mkdir -p /tmp/$FOLDER_NAME
cp -r * /tmp/$FOLDER_NAME
cd /tmp
zip -r moodle-${{ env.PLUGIN_NAME }}-${{ github.ref_name }}.zip $FOLDER_NAME

- name: Upload release archives
- name: Calculate MD5 hash
run: |
cd /tmp
md5sum /tmp/moodle-${{ env.PLUGIN_NAME }}-${{ github.ref_name }}.zip > /tmp/moodle-${{ env.PLUGIN_NAME }}-${{ github.ref_name }}.zip.md5

- name: Upload release archive (ZIP)
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: /tmp/moodle-${{ env.PLUGIN_NAME }}-${{ github.ref_name }}.zip
tag: ${{ github.ref_name }}
- name: Upload release archives
- name: Upload MD5 hash files (ZIP)
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: /tmp/moodle-${{ env.PLUGIN_NAME }}-${{ github.ref_name }}.tgz
file: /tmp/moodle-${{ env.PLUGIN_NAME }}-${{ github.ref_name }}.zip.md5
tag: ${{ github.ref_name }}

- name: Prepare release body (description)
id: prep_body
- name: Upload to package registry
run: |
echo "${{ github.event.release.body }}" > changes.md

- name: Discord notification
uses: appleboy/discord-action@master
with:
webhook_id: ${{ secrets.DISCORD_RELEASE_CHANNEL_WEBHOOK_ID }}
webhook_token: ${{ secrets.DISCORD_RELEASE_CHANNEL_WEBHOOK_TOKEN }}
username: GitHub Releases
message: "New release of **${{ github.repository }}**\nVersion: ${{ github.ref_name }} (${{github.event.release.name}})\n<${{ github.event.release.html_url }}>"
file: changes.md
cd /tmp
MD5_CONTENT=$(awk '{print $1 " '${{ github.ref_name }}.zip'"}' /tmp/moodle-${{ env.PLUGIN_NAME }}-${{ github.ref_name }}.zip.md5)
curl --fail-with-body -u upload:${{ secrets.PACKAGE_REGISTRY_UPLOAD_PASSWORD }} -T /tmp/moodle-${{ env.PLUGIN_NAME }}-${{ github.ref_name }}.zip https://packages.projekt-adler.eu/upload/moodle/${{ env.PLUGIN_NAME }}/${{ github.ref_name }}.zip
curl --fail-with-body -u upload:${{ secrets.PACKAGE_REGISTRY_UPLOAD_PASSWORD }} -X PUT --data "$MD5_CONTENT" https://packages.projekt-adler.eu/upload/moodle/${{ env.PLUGIN_NAME }}/${{ github.ref_name }}.zip.md5
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@
Das Local Logging Plugin ermöglicht eine einfache Protokollierung von Ereignissen in Moodle. Es stellt eine Logger-Klasse zur Verfügung, die es Entwicklern ermöglicht, Nachrichten verschiedener Schweregrade zu protokollieren.

## Kompabilität
Folgende Versionen werden unterstützt (mit mariadb und postresql getestet):

| Moodle Branch | PHP Version |
|-------------------------|-------------|
| MOODLE_404_STABLE | 8.1 |
| MOODLE_404_STABLE | 8.2 |
| MOODLE_404_STABLE | 8.3 |
| MOODLE_405_STABLE (LTS) | 8.1 |
| MOODLE_405_STABLE (LTS) | 8.2 |
| MOODLE_405_STABLE (LTS) | 8.3 |

siehe [plugin_compatibility.json](plugin_compatibility.json)

## Funktionsweise

Expand Down
14 changes: 9 additions & 5 deletions classes/hook_listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@

namespace local_logging;

use core\di;
use core\hook\di_configuration;
use local_logging\local\output\log_output;
use local_logging\local\output\log_output_db;
use local_logging\local\output\log_output_mtrace;
use local_logging\local\output\log_output_stdout;

class hook_listener {
public static function inject_dependencies(di_configuration $hook): void {
$hook->add_definition(
id: log_output::class,
definition: function (): log_output {
if (defined('CLI_SCRIPT') || defined('PHPUNIT_TEST')) {
return new log_output_stdout();
} else {
return new log_output_db();
}
return di::get(log_output_mtrace::class);
// if (defined('CLI_SCRIPT') || defined('PHPUNIT_TEST')) {
// return new log_output_stdout();
// return di::get(log_output_mtrace::class);
// } else {
// return di::get(log_output_db::class);
// }
}
);
}
Expand Down
13 changes: 13 additions & 0 deletions classes/local/output/log_output_mtrace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace local_logging\local\output;

use local_logging\local\util;

class log_output_mtrace implements log_output {
public function output(string $message, int $level, string $component, string $title, int $time, ?int $user_id): void {
mtrace(
'[' . date('Y-m-d H:i:s', $time) . '] ' . util::convertLogLevelToString($level) . ' ' . $component . ' - ' . $title . ': ' . $message . PHP_EOL,
);
}
}
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.29.0/resources/schema.json",
"source": {
"directories": [
"."
]
},
"phpUnit": {
"configDir": ".",
"customPath": "./vendor/phpunit/phpunit/phpunit"
},
"initialTestsPhpOptions": "-dxdebug.mode=off -dpcov.enabled=1 -dpcov.directory=.",
"bootstrap": "vendor/autoloadmoodle.php",
"mutators": {
"@default": true,
}
}
18 changes: 18 additions & 0 deletions plugin_compatibility.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"moodle": "MOODLE_404_STABLE",
"PHP": [
"8.1",
"8.2",
"8.3"
]
},
{
"moodle": "MOODLE_405_STABLE",
"PHP": [
"8.1",
"8.2",
"8.3"
]
}
]
3 changes: 2 additions & 1 deletion tests/local/output/log_output_db_test.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace local_logging\local\output;

use core\di;
use local_logging\lib\adler_testcase;
use local_logging\local\output\log_output_db;

defined('MOODLE_INTERNAL') || die();

Expand Down
42 changes: 42 additions & 0 deletions tests/local/output/log_output_mtrace_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace local_logging\local\output;

use local_logging\lib\adler_testcase;
use local_logging\local\util;

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->dirroot . '/local/logging/tests/lib/adler_testcase.php');

class log_output_mtrace_test extends adler_testcase {
public function test_output() {
// Create an instance of log_output_stdout.
$logOutput = new log_output_mtrace();

// Define test data.
$message = 'Test message';
$level = util::LEVEL_TRACE; // Assuming 100 corresponds to TRACE level
$component = 'testcomponent';
$title = 'testtitle';
$time = time();
$user_id = null;

// Start output buffering.
ob_start();

// Call the output method.
$logOutput->output($message, $level, $component, $title, $time, $user_id);

// Get the output and clean the buffer.
$output = ob_get_clean();

// Assert that the output contains the expected components.
$this->assertStringContainsString('[' . date('Y-m-d H:i:s', $time) . ']', $output);
$this->assertStringContainsString('TRACE', $output);
$this->assertStringContainsString($component, $output);
$this->assertStringContainsString($title . ':', $output);
$this->assertStringContainsString($message, $output);
}
}
3 changes: 2 additions & 1 deletion tests/local/output/log_output_stdout_test.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace local_logging\local\output;

use local_logging\lib\adler_testcase;
use local_logging\local\output\log_output_stdout;
use local_logging\local\util;

defined('MOODLE_INTERNAL') || die();
Expand Down
Loading