name: Moodle Plugin CI

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-22.04

    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_USER: 'postgres'
          POSTGRES_HOST_AUTH_METHOD: 'trust'
        ports:
          - 5432:5432
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 3
      mariadb:
        image: mariadb:10.6
        env:
          MYSQL_USER: 'root'
          MYSQL_ALLOW_EMPTY_PASSWORD: "true"
          MYSQL_CHARACTER_SET_SERVER: "utf8mb4"
          MYSQL_COLLATION_SERVER: "utf8mb4_unicode_ci"

        ports:
          - 3306:3306
        options: --health-cmd="mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 3

    strategy:
      fail-fast: false
      matrix: # I don't know why, but mariadb is much slower, so mostly use pgsql.
        # We use a mix of SBCL and GCL.
        include:
          - php: '8.4'
            moodle-branch: 'main'
            database: 'pgsql'
            maxima: 'GCL'
            moodle-app: true
          - php: '8.4'
            moodle-branch: 'MOODLE_501_STABLE'
            database: 'pgsql'
            maxima: 'GCL'
            moodle-app: true
          - php: '8.2'
            moodle-branch: 'MOODLE_500_STABLE'
            database: 'pgsql'
            maxima: 'SBCL'
            moodle-app: true
          # Edinburgh is planning to run the setup below for 2025-26.
          - php: '8.2'
            moodle-branch: 'MOODLE_405_STABLE'
            database: 'mariadb'
            maxima: 'GCL'
            moodle-app: true
          - php: '7.4'
            moodle-branch: 'MOODLE_401_STABLE'
            database: 'pgsql'
            maxima: 'GCL'
            moodle-app: true

    steps:
      - name: Install Maxima (${{ matrix.maxima }})
        run: |
          maxima="${{ (matrix.maxima == 'SBCL' && 'sbcl') || 'gcl' }}"
          wget http://mirrors.kernel.org/ubuntu/pool/main/r/readline/libreadline7_7.0-3_amd64.deb \
               https://sourceforge.net/projects/maxima/files/Maxima-Linux/5.42.2-Linux/maxima-common_5.42.2-1_all.deb \
               https://sourceforge.net/projects/maxima/files/Maxima-Linux/5.42.2-Linux/maxima-${maxima}_5.42.2-1_amd64.deb

          sudo locale-gen en_AU.UTF-8
          sudo apt-get install libtinfo5
          sudo dpkg -i libreadline7_7.0-3_amd64.deb maxima-common_5.42.2-1_all.deb maxima-${maxima}_5.42.2-1_amd64.deb

          echo "LANG=en_AU.UTF-8" >> $GITHUB_ENV
          echo "adjust_external_format();" | maxima
          echo "diff(x^2,x);" | maxima
          echo "build_info();" | maxima

      - name: Check out repository code
        uses: actions/checkout@v3
        with:
          path: plugin

      - name: Setup PHP ${{ matrix.php }}
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: ${{ matrix.extensions }}
          ini-values: max_input_vars=5000
          coverage: none

      - name: Validate composer.json and composer.lock
        if: ${{ fromJSON(matrix.php) >= 8.2 }}
        run: |
          cd plugin/api
          composer validate

      - name: Cache Composer packages
        id: composer-cache
        if: ${{ fromJSON(matrix.php) >= 8.2 }}
        uses: actions/cache@v3
        with:
          path: vendor
          key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-php-

      - name: Install Dependencies
        if: ${{ fromJSON(matrix.php) >= 8.2 }}
        run: |
          cd plugin/api
          composer install

      - name: Initialise moodle-plugin-ci
        run: |
          composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^4
          echo $(cd ci/bin; pwd) >> $GITHUB_PATH
          echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH
          sudo locale-gen en_AU.UTF-8
          echo "NVM_DIR=$HOME/.nvm" >> $GITHUB_ENV

      - name: Install moodle-plugin-ci
        run: |
          moodle-plugin-ci add-plugin maths/moodle-qbehaviour_dfexplicitvaildate
          moodle-plugin-ci add-plugin maths/moodle-qbehaviour_dfcbmexplicitvaildate
          moodle-plugin-ci add-plugin maths/moodle-qbehaviour_adaptivemultipart

          moodle-plugin-ci install --plugin ./plugin --db-host=127.0.0.1

          moodle-plugin-ci add-config 'define("QTYPE_STACK_TEST_CONFIG_MAXIMAVERSION",       "5.42.2");'
          moodle-plugin-ci add-config 'define("QTYPE_STACK_TEST_CONFIG_MAXIMACOMMAND",       "maxima");'
          moodle-plugin-ci add-config 'define("QTYPE_STACK_TEST_CONFIG_MAXIMACOMMANDOPT",    "timeout --kill-after=10s 10s ${{ github.workspace }}/maxima_opt_auto -eval '\''(cl-user::run)'\''");'
          moodle-plugin-ci add-config 'define("QTYPE_STACK_TEST_CONFIG_MAXIMACOMMANDSERVER", "http://pool.home:8080/MaximaPool/MaximaPool");'
          moodle-plugin-ci add-config 'define("QTYPE_STACK_TEST_CONFIG_CASTIMEOUT",          "100");'
          moodle-plugin-ci add-config 'define("QTYPE_STACK_TEST_CONFIG_MAXIMALIBRARIES",     "stats, distrib, descriptive, simplex");'
          moodle-plugin-ci add-config 'define("QTYPE_STACK_TEST_CONFIG_CASPREPARSE", "true");'
          moodle-plugin-ci add-config 'define("QTYPE_STACK_TEST_CONFIG_PLATFORM",            "linux-optimised");'
          moodle-plugin-ci add-config 'define("QTYPE_STACK_TEST_CONFIG_CASRESULTSCACHE",     "db");'
          moodle-plugin-ci add-config 'define("QTYPE_STACK_TEST_CONFIG_PLOTCOMMAND",         "");'
          moodle-plugin-ci add-config 'define("QTYPE_STACK_TEST_CONFIG_CASDEBUGGING",        "0");'

          #cat ${{ github.workspace }}/moodle/config.php

          cp ${{ github.workspace }}/moodledata/phpu_moodledata/stack/maxima_opt_auto ${{ github.workspace }}/maxima_opt_auto
          # Try a command on the command line.
          # echo "1+1; quit();" | timeout --kill-after=100s 100s ${{ github.workspace }}/maxima_opt_auto -eval '(cl-user::run)' 


        env:
          DB: ${{ matrix.database }}
          MOODLE_BRANCH: ${{ matrix.moodle-branch }}
          MOODLE_APP: ${{ matrix.moodle-app }}

      - name: PHP Lint
        if: ${{ always() }}
        run: moodle-plugin-ci phplint

      - name: PHP Copy/Paste Detector
        continue-on-error: true # This step will show errors but will not fail.
        if: ${{ always() }}
        run: moodle-plugin-ci phpcpd

      - name: PHP Mess Detector
        continue-on-error: true # This step will show errors but will not fail.
        if: ${{ always() }}
        run: moodle-plugin-ci phpmd

      - name: Moodle Code Checker
        continue-on-error: true # Currently fails. We really ought to get this passing.
        if: ${{ always() }}
        run: moodle-plugin-ci codechecker --max-warnings 0

      - name: Moodle PHPDoc Checker
        continue-on-error: true # Currently fails. We really ought to get this passing.
        if: ${{ always() }}
        run: moodle-plugin-ci phpdoc

      - name: Validating
        if: ${{ always() }}
        run: moodle-plugin-ci validate

      - name: Check upgrade savepoints
        if: ${{ always() }}
        run: moodle-plugin-ci savepoints

      - name: Mustache Lint
        if: ${{ always() }}
        run: moodle-plugin-ci mustache

      - name: Grunt
        if: ${{ matrix.moodle-branch == 'MOODLE_500_STABLE' }}
        run: moodle-plugin-ci grunt

      - name: PHPUnit tests
        if: ${{ always() }}
        run: moodle-plugin-ci phpunit

      - name: Behat features
        if: ${{ always() }}
        run: moodle-plugin-ci behat --profile chrome --auto-rerun 6

