name: Run module tests

on:
  pull_request:
    branches:
      - 8.x-[1-9]+.x
    paths-ignore:
      - '**.md'

  schedule:
    - cron:  '0 6 * * *'

env:
   DRUPAL_TESTING_TEST_CODING_STYLES: false
   DRUPAL_TESTING_DATABASE_USER: root
   DRUPAL_TESTING_DATABASE_PASSWORD: root
   DRUPAL_TESTING_DATABASE_ENGINE: mysql

jobs:
  build:

    runs-on: ubuntu-20.04

    strategy:
      matrix:
        DRUPAL_TESTING_DRUPAL_VERSION: ['~8.9.0', '~9.1.0']

    steps:
    - uses: actions/checkout@v1

    - uses: actions/setup-node@v1
      with:
        node-version: '12.x'

    - uses: shivammathur/setup-php@v2
      with:
        coverage: none
        php-version: '7.4'
        extensions: Imagick, gd, pdo_mysql

    - name: Start MySql service
      run: |
        sudo /etc/init.d/mysql start
        mysql -uroot -proot -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';"

    - name: Cache composer dependencies
      uses: actions/cache@v1
      with:
        path: ~/.composer/cache
        key: ${{ runner.os }}-composer-cache-${{ hashFiles('**/composer.json') }}
        restore-keys: ${{ runner.os }}-composer-cache-

    - name: Cache NPM dependencies
      id: npm-cache
      uses: actions/cache@v1
      with:
        path: ~/.npm
        key: ${{ runner.os }}-npm-cache
        restore-keys: ${{ runner.os }}-npm-cache

    - name: Get build environment
      run: composer global require thunder/drupal-testing

    - name: Prepare the build
      run: test-drupal-project prepare_build
      env:
        DRUPAL_TESTING_DRUPAL_VERSION: ${{ matrix.DRUPAL_TESTING_DRUPAL_VERSION }}

    - name: Add select2 library to the composer dependencies
      run: composer require npm-asset/select2:^4.0.7 --no-update
      working-directory: /tmp/test/select2/install

    - name: Build the docroot
      run: test-drupal-project build

    - name: Test for deprecations
      run: test-drupal-project deprecation
      if: ${{ matrix.DRUPAL_TESTING_DRUPAL_VERSION != '~9.1.0' }}

    - name: Install drupal
      run: test-drupal-project install
      env:
        DRUPAL_TESTING_TEST_DEPRECATION: false

    - name: Start webserver and run the tests
      run: test-drupal-project
