Module 8: Enterprise & CI/CD

Managing Moodle at Scale: Upgrades, Deployment, and Testing

1. Continuous Integration / Continuous Delivery (CI/CD)

In an enterprise environment, you never edit code on the server. Use a pipeline.

Typical Pipeline Steps

  1. Linting: PHPCS with Moodle Standard (moodle-cs).
  2. Unit Tests: PHPUnit tests for backend logic.
  3. Behat Tests: BDD tests for UI interactions.
  4. Build: Compile SCSS, minify JS (Grunt).
  5. Deploy: Push code to servers (Git pull or artifact deployment).
  6. Post-Deploy: Run admin/cli/upgrade.php and admin/cli/purge_caches.php.

2. Moodle Upgrades

Upgrading Moodle is a critical process. Always backup DB and Dataroot first.

# 1. Put site in maintenance mode
php admin/cli/maintenance.php --enable

# 2. Backup Database
mysqldump -u user -p moodle > moodle_backup.sql

# 3. Update Code (Git)
git fetch
git checkout MOODLE_403_STABLE

# 4. Run Upgrade Script
php admin/cli/upgrade.php --non-interactive

# 5. Disable Maintenance
php admin/cli/maintenance.php --disable

3. High Availability Architecture

For 500k+ users, a single server won't suffice.

4. Automated Testing

Enterprise quality assurance relies on automated tests, not manual clicking.

PHPUnit (Unit & Integration Tests)

Tests PHP classes and logic. Requires a separate test database.

# Initialize Test DB
php admin/tool/phpunit/cli/init.php

# Run all tests in a plugin
vendor/bin/phpunit --group=local_myplugin

Data Generators

Don't manually insert records in tests. Use the Data Generator to create courses, users, and enrolments instantly.

$generator = $this->getDataGenerator();
$course = $generator->create_course();
$user = $generator->create_user();
$generator->enrol_user($user->id, $course->id, 'student');
$module = $generator->create_module('forum', ['course' => $course->id]);

Behat (Acceptance Tests)

Tests the UI by simulating a real browser. Written in Gherkin syntax (Given/When/Then).

Feature: User login
  In order to access the course
  As a student
  I need to be able to log in

  Scenario: Login with valid credentials
    Given I am on the homepage
    When I follow "Log in"
    And I set the following fields to these values:
      | Username | student1 |
      | Password | Moodle@123 |
    And I press "Log in"
    Then I should see "My courses"
# Run Behat
vendor/bin/behat --tags=@local_myplugin

5. Docker for Development

Stop using XAMPP/MAMP. Use moodle-docker for a consistent environment.

# 1. Clone moodle-docker
git clone https://github.com/moodlehq/moodle-docker.git

# 2. Configure environment
export MOODLE_DOCKER_WWWROOT=/path/to/moodle
export MOODLE_DOCKER_DB=pgsql

# 3. Start containers
bin/moodle-docker-compose up -d

# 4. Run tests inside container
bin/moodle-docker-compose exec webserver php admin/tool/phpunit/cli/init.php