Testing Moodle Plugins That Transmit Data to a Remote Endpoint
How to verify that a Moodle plugin sends the correct HTTP payloads to a remote endpoint, using a local PHP listener to capture and inspect requests.
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 Moodle server (for grading & logs)
- PHP CLI available (7.4+)
- A terminal:
- Linux/macOS: Bash or Zsh
- Windows: PowerShell or CMD
- Optional: curl or Postman for manual POSTs
- Step-by-Step Procedure
- Step 1: Set Up a Fake Local Endpoint
- UNIX-like systems (Linux/macOS)
- Create a test directory:
mkdir-p ~/php/wstest cd~/php/wstest- Create a file called
index.php:<?php ob_start();echo"=== REQUEST METHOD: ".$_SERVER['REQUEST_METHOD'] ." ===\n";foreach(getallheaders()as$name=>$value) {echo"$name:$value\n";}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 PHP test server:
php -S localhost:8000 - Keep this terminal open — you'll see incoming requests printed here.
- 🪟 Windows (for testers)
- Install PHP
- Download from https://windows.php.net/
- Extract to
C:\php - Add
C:\phpto your PATH - Create a test script
- Create folder:
C:\php\wstest - Create file:
index.php(same contents as above) - Start server in CMD or PowerShell:
cdC:\php\wstest 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 your plugin's event observer is correctly registered. Example (your plugin may differ):
'callback'=>'\local_yourplugin\observer::graded' - Confirm that your observer method exists and is properly namespaced. Example method signature:
publicstaticfunctiongraded($eventdata) { // your handling code}- Step 3: Trigger the Event
- Grade an assignment or perform any action that triggers the event
- Watch your terminal window
Example terminal output:
=== LOG FROM index.php ====== REQUEST METHOD: POST ===Content-Type: application/json=== BODY ==={"Cijfer":"77.00000","Cijfertype":"point","OpdrachtID":"15030","GebruikerID":"145404"}=== PARSED JSON ===Array([Cijfer] => 77.00000[Cijfertype] => point[OpdrachtID] => 15030[GebruikerID] => 145404)- Step 4: Debug If Nothing Appears
- Check that:
- The plugin is enabled
- The correct Moodle event is triggered
- The callback matches your observer class and method
- Add debug logging with: Example:
error_log("Triggered observer::graded()"); - Check web server logs
- Ensure your
curlcall includes: Example:CURLOPT_RETURNTRANSFER =>true - Success Criteria
- Endpoint receives a POST request
- Data includes expected keys and values
- No errors are logged in Moodle or PHP logs
- Cleanup
- Press
Ctrl+Cto stop the test server - Revert endpoint configuration if needed
Solin specializes in Moodle plugin development and testing.
Contact us