Headless LifterLMS: Building a JavaScript Front End for Your Courses
What a headless LifterLMS setup really involves: which parts the REST API covers, why lessons and completion need custom endpoints, how to…
Read articleIf your business depends on a custom LifterLMS add-on, such as an enrollment rule, a gateway tweak or an integration with your CRM, every LifterLMS, WordPress and PHP update is a chance for it to break quietly. LifterLMS unit testing, run automatically on every change, is how you find out on a pull request instead of from a student.
This guide is for developers who maintain LifterLMS add-ons, in-house or for clients. It shows how to set up the official testing framework that LifterLMS itself uses, write a first meaningful test, run the suite against several versions, and wire it into GitHub Actions. Code and commands were checked against lifterlms/lifterlms-tests 4.5 and LifterLMS 10.3.
If you are still deciding whether your customisation should be a plugin at all, read when to build a custom LifterLMS plugin first.
LifterLMS publishes its test tooling on Packagist so add-on developers can use the same setup as the core team:
llms-env, a Docker-based local environment.The LifterLMS core plugin lists both as development dependencies in its composer.json, which is a good sign they will stay maintained.
composer require --dev lifterlms/lifterlms-tests
Place it in the root of your add-on. The important parts are the bootstrap path and the test directory:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php" backupGlobals="false" colors="true" verbose="true"
convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true">
<testsuites>
<testsuite name="LLMSX Bonus Courses">
<directory suffix=".php">tests/unit-tests</directory>
</testsuite>
</testsuites>
</phpunit>
The bootstrap extends LLMS_Tests_Bootstrap and names your plugin's main file. By default it loads LifterLMS core before your add-on, so your code runs in a realistic environment.
<?php
/**
* Test bootstrap for LLMSX Bonus Courses.
*/
require_once './vendor/lifterlms/lifterlms-tests/bootstrap.php';
class LLMSX_Bonus_Courses_Tests_Bootstrap extends LLMS_Tests_Bootstrap {
public $file = __FILE__;
public $suite_name = 'LLMSX Bonus Courses';
public $plugin_main = 'llmsx-bonus-courses.php';
}
return new LLMSX_Bonus_Courses_Tests_Bootstrap();
The installer downloads WordPress and its test library and creates a test database. It needs a MySQL server you can write to; use a throwaway database, because it is emptied on every run.
# Database name, user, password, host, WordPress version
./vendor/bin/llms-tests install llms_tests root password 127.0.0.1 latest
# Install LifterLMS into the test WordPress
./vendor/bin/llms-tests plugin lifterlms
# Run the suite
./vendor/bin/phpunit
The plugin command also accepts a version, which is how you test against an older LifterLMS release your customers still run. LifterLMS core wraps these commands in Composer scripts called tests-install and tests-run; copying that pattern into your add-on's composer.json saves typing.
Here is a small add-on to test. When a student enrolls in a course that has a bonus course set in its meta, it enrolls them in the bonus course too:
<?php
/**
* Plugin Name: LLMSX Bonus Courses
*/
defined( 'ABSPATH' ) || exit;
add_action( 'llms_user_enrolled_in_course', 'llmsx_enroll_bonus_course', 10, 2 );
function llmsx_enroll_bonus_course( $user_id, $course_id ) {
$bonus_id = absint( get_post_meta( $course_id, '_llmsx_bonus_course', true ) );
if ( $bonus_id && ! llms_is_user_enrolled( $user_id, $bonus_id ) ) {
llms_enroll_student( $user_id, $bonus_id, 'llmsx_bonus_' . $course_id );
}
}
And a test class for it, saved in tests/unit-tests/. Test classes extend LLMS_Unit_Test_Case, which gives you the LifterLMS factories on $this->factory:
<?php
/**
* @group llmsx_bonus
*/
class LLMSX_Test_Bonus_Courses extends LLMS_Unit_Test_Case {
public function test_enrolling_in_main_course_adds_bonus_course() {
$main_id = $this->factory->course->create();
$bonus_id = $this->factory->course->create();
$student_id = $this->factory->student->create();
update_post_meta( $main_id, '_llmsx_bonus_course', $bonus_id );
llms_enroll_student( $student_id, $main_id );
$this->assertTrue( llms_is_user_enrolled( $student_id, $bonus_id ) );
}
public function test_no_bonus_course_when_meta_is_missing() {
$main_id = $this->factory->course->create();
$other_id = $this->factory->course->create();
$student_id = $this->factory->student->create();
llms_enroll_student( $student_id, $main_id );
$this->assertFalse( llms_is_user_enrolled( $student_id, $other_id ) );
}
}
Each test runs inside a database transaction that is rolled back afterwards, so tests do not leak data into each other. Note that the test calls llms_enroll_student(), the same function the checkout uses, so the real llms_user_enrolled_in_course action fires and your hook runs exactly as it would in production.
| Helper | What it is for |
|---|---|
| Factories for course, membership, order, student and instructor | Build realistic data quickly; the course factory can also generate sections, lessons and quizzes |
llms_tests_mock_current_time() and llms_tests_reset_current_time() | Test drip dates, access expiration and anything else that depends on "now" |
mock_http_request() | Short-circuit the next matching wp_remote_request(), essential for testing gateway and CRM integrations without real API calls |
assertOutputContains() and related assertions | Check template and shortcode output |
| WP_Error assertions | Check error codes and messages from your validation code |
mockGetRequest() and mockPostRequest() | Simulate form submissions and query strings |
LLMS_Unit_Test_Exception_Redirect and LLMS_Unit_Test_Exception_Exit | Test code that redirects or exits, such as form handlers |
LLMS_Unit_Test_Util::call_method() | Call private and protected methods when you really need to |
For REST endpoints, build a WP_REST_Request, dispatch it with rest_get_server()->dispatch(), and assert on the status and data. Set the current user with wp_set_current_user() first to test permissions for students, instructors and administrators separately.
You do not need to test LifterLMS itself. Test the behaviour your add-on adds, through the same public functions and hooks a real request would use. Our LifterLMS hooks and filters guide lists the ones add-ons rely on most.
A slow suite stops being run. Mirror your plugin's structure in tests/unit-tests, one test class per class or feature, so failures point straight at the code. Create only the data each test needs: a course with no lessons is enough to test an enrollment hook. Use PHPUnit groups, like the @group llmsx_bonus tag above, to run one area with ./vendor/bin/phpunit --group llmsx_bonus while you work. When you fix a bug, write the failing test first, then the fix, so the same bug cannot return unnoticed after the next refactor.
This workflow runs the suite on every push and pull request, against the oldest and newest PHP versions you support. LifterLMS 10.x requires PHP 7.4 or newer.
name: Tests
on: [ push, pull_request ]
jobs:
phpunit:
runs-on: ubuntu-latest
strategy:
matrix:
php: [ '7.4', '8.3' ]
services:
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: password
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer
- run: sudo apt-get update && sudo apt-get install -y subversion
- run: composer install --no-interaction --prefer-dist
- run: ./vendor/bin/llms-tests install llms_tests root password 127.0.0.1 latest
- run: ./vendor/bin/llms-tests plugin lifterlms
- run: ./vendor/bin/phpunit
Some practical additions once the basics work:
llms-tests plugin lifterlms, so you test the latest release and the oldest one you support.latest to the install command.schedule trigger so a new LifterLMS release is tested even when nobody has pushed code.The subversion step is there because the WordPress test library installer has traditionally used it; drop it if your runner already has it or the installer does not need it.
Yes. lifterlms/lifterlms-tests on Packagist is the framework used for LifterLMS core and add-ons. It builds on PHPUnit and the WordPress test suite.
Version 4.5 of the framework supports PHPUnit 7.5 or 9.5 and PHP 7.3 and newer, with Yoast PHPUnit Polyfills for compatibility.
Use the bundled llms-env tool, which generates a Docker Compose setup, or any local database server you already run.
Yes. Install the add-on into the test WordPress before running the suite, from a private package or a zip you are licensed to use, and load it in your bootstrap before your own plugin. Keep its files out of public repositories.
For checkout flows and complex front-end features, yes, a few. Keep most coverage in fast unit and integration tests and reserve browser tests for the paths that make money.
Add the framework to one add-on this week and write tests for its single most important behaviour, usually the one that grants access or touches payments. Then switch on CI. If you have custom LifterLMS code with no tests and an update you are nervous about, our LifterLMS plugin development team can add a test suite and CI to existing add-ons, or build new ones with tests from day one. Tell us about your add-on.
What a headless LifterLMS setup really involves: which parts the REST API covers, why lessons and completion need custom endpoints, how to…
Read article
How the LifterLMS engagement engine works, how to register a custom engagement trigger that admins can configure, how to fire it reliably, and how to…
Read article
When the built-in LifterLMS reporting is not enough: where the data lives, four ways to build custom reports, and a complete plugin that adds a…
Read articleSkip the trial and error. Get specialist LifterLMS help and ship faster.