Testing that Moodle plugins correctly post data to remote endpoints requires a local test server to capture requests. This guide shows how to set up a simple PHP endpoint, configure the plugin, and inspect the actual HTTP payloads being sent.

SOP: Testing Moodle Plugins That Transmit Data to a Remote Endpoint

Purpose

To verify that a Moodle plugin correctly sends data (e.g., grades) to a remote web service (typically via HTTP POST) when specific events are triggered, such as grading an assignment.

Prerequisites

  • Moodle instance with the plugin installed and enabled
  • Access to the Moodle server (for grading and logs)
  • PHP CLI available (7.4+)
  • A terminal: Bash/Zsh on Linux/macOS, or PowerShell/CMD on Windows
  • Optional: curl or Postman for manual POSTs

Step 1: Set up a fake local endpoint

Linux / macOS

Create a test directory and a small index.php that prints whatever it receives:

mkdir -p ~/php/wstest
cd ~/php/wstest
<?php
// index.php
ob_start();

echo "=== REQUEST METHOD: " . $_SERVER['REQUEST_METHOD'] . " ===n";

foreach (getallheaders() as $name => $value) {
    echo "$name: $valuen";
}

echo "n";

$input = file_get_contents('php://input');
echo "=== BODY ===n";
echo $input . "n";

$json = json_decode($input, true);
if ($json !== null) {
    echo "n=== PARSED JSON ===n";
    print_r($json);
}

file_put_contents('php://stdout', "=== LOG FROM index.php ===n" . ob_get_contents() . "n");
ob_end_flush();

Start the built-in PHP server and leave the terminal open; incoming requests are printed here:

php -S localhost:8000

Windows

  • Download PHP from https://windows.php.net/ and extract it to C:php, then add C:php to your PATH.
  • Create the folder C:phpwstest and place the same index.php as above inside it.

Start the server from CMD or PowerShell:

cd C:phpwstest
php -S localhost:8000

Step 2: Point Moodle to the local endpoint

  • In the plugin settings, set the endpoint to your local test server: http://localhost:8000
  • Ensure the plugin’s event observer is registered in db/events.php (your plugin may differ):
'callback' => 'local_yourpluginobserver::graded',

Confirm the observer method exists and is correctly namespaced:

public static function graded($eventdata) {
    // your handling code
}

Note: if the plugin makes its outbound call through Moodle’s own curl class, Moodle’s cURL security may block requests to localhost by default. If nothing reaches your test server, see our guide on allowing Moodle cURL requests to localhost for development.

Step 3: Trigger the event

  • Grade an assignment, or perform whatever action triggers the event.
  • Watch the terminal running the test server.

Example terminal output:

=== LOG FROM index.php ===
=== REQUEST METHOD: POST ===
Content-Type: application/json
=== BODY ===
{"grade":"77.00000","gradetype":"point","activityid":"15030","userid":"145404"}
=== PARSED JSON ===
Array
(
    [grade] => 77.00000
    [gradetype] => point
    [activityid] => 15030
    [userid] => 145404
)

Step 4: Debug if nothing appears

  • Confirm the plugin is enabled.
  • Confirm the correct Moodle event is actually triggered by the action.
  • Confirm the callback in db/events.php matches your observer class and method.
  • Add temporary debug logging, e.g. error_log("Triggered observer::graded()");, and check the web server logs.
  • Ensure the outbound request uses CURLOPT_RETURNTRANSFER => true (or the equivalent on Moodle’s curl class) so the call completes.
  • If you use Moodle’s curl class, check whether cURL security is blocking localhost (see Step 2 note).

Success criteria and cleanup

  • The endpoint receives a POST request with the expected keys and values.
  • No errors are logged in Moodle or PHP.
  • When done, press Ctrl+C to stop the test server and revert the endpoint configuration.

Solin specializes in Moodle plugin development and testing.

Contact us