name: Run distribution tests

on:
  pull_request:
    paths-ignore:
      - '**.md'
      - 'package.json'
      - 'package-lock.json'
  schedule:
    - cron: '0 6 * * *'

env:
  DRUPAL_TESTING_COMPOSER_PROJECT: thunder/thunder-project
  DRUPAL_TESTING_COMPOSER_PROJECT_VERSION: "^5.0@stable"
  DRUPAL_TESTING_CLEANUP: false
  DRUPAL_TESTING_DATABASE_USER: root
  DRUPAL_TESTING_DATABASE_PASSWORD: root
  DRUPAL_TESTING_TEST_DUMP_FILE: site-dump.tar.gz
  DRUPAL_TESTING_DATABASE_ENGINE: mysql
  DRUPAL_TESTING_TEST_CODING_STYLES: false
  DRUPAL_TESTING_TEST_PROFILE: thunder
  DRUPAL_TESTING_TEST_CONFIGURATION: /tmp/test/thunder/install/docroot/profiles/contrib/thunder/
  DRUPAL_TESTING_HTTP_PORT: 8888
  DRUPAL_TESTING_VERBOSE: false
  DRUPAL_TESTING_VERSION: "^2.0"
  DRUPAL_TESTING_TEST_FILTER: ""
  DRUPAL_TESTING_SELENIUM_HOST: 127.0.0.1
  DRUPAL_TESTING_SELENIUM_PORT: 4444
  PHPSTAN_MEMORY_LIMIT: 4G
  DRUPAL_TESTING_PARALLEL_TESTING: false
  MINK_DRIVER_ARGS_WEBDRIVER: '["chrome", {"browserName":"chrome", "goog:chromeOptions":{"w3c": true, "args":["--disable-gpu", "--headless", "--no-sandbox", "--ignore-certificate-errors", "--allow-insecure-localhost", "--headless", "--dns-prefetch-disable", "--disable-dev-shm-usage"]}}, "http://127.0.0.1:4444"]'
  SIMPLETEST_BASE_URL: http://thunder-testing:8888
  BROWSERTEST_OUTPUT_DIRECTORY: /tmp
  SKIP_TEST_CLEANUP: true
  # The following variable set the version that the upgrade test starts with.
  DRUPAL_TESTING_UPGRADE_COMPOSER_PROJECT_VERSION: 4.2.0
  DRUPAL_TESTING_UPGRADE_DRUSH_VERSION: 13.4.0
  DRUPAL_TESTING_UPGRADE_VERSION: 7.5.1

jobs:

  build:

    runs-on: ubuntu-24.04

    services:
      mysql:
        image: mariadb:lts
        env:
          MYSQL_ROOT_PASSWORD: root
        ports:
          - 3306:3306

    strategy:
      matrix:
        PHP_VERSION: [ '8.3', '8.4' ]

    env:
      DRUPAL_TESTING_TEST_DEPRECATION: true

    steps:
      - uses: actions/checkout@v3

      - uses: shivammathur/setup-php@v2
        with:
          coverage: none
          php-version: ${{ matrix.PHP_VERSION }}
          extensions: :redis, :pspell, Imagick

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

      - name: Get build environment
        run: |
          composer global config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true --no-plugins
          composer global require thunder/drupal-testing:${DRUPAL_TESTING_VERSION}
          composer global require php-parallel-lint/php-parallel-lint

      - name: Lint
        run: |
          parallel-lint --version
          parallel-lint --no-progress -e php,module,install,inc,profile,theme .

      - name: Prepare the build
        run: test-drupal-project prepare_build

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

      - name: Test for deprecations
        run: test-drupal-project deprecation

      - name: Install drupal
        run: test-drupal-project install

      - name: Zip build
        run: cd /tmp/test; tar cfz build-${{ matrix.PHP_VERSION }}.tgz thunder; mv build-${{ matrix.PHP_VERSION }}.tgz ${GITHUB_WORKSPACE}

      - name: Upload build
        uses: actions/upload-artifact@v4
        with:
          name: build-${{ matrix.PHP_VERSION }}
          path: build-${{ matrix.PHP_VERSION }}.tgz

  test-max:
    needs: build

    runs-on: ubuntu-24.04

    services:
      mysql:
        image: mariadb:lts
        env:
          MYSQL_ROOT_PASSWORD: root
        ports:
          - 3306:3306
      chrome:
        image: seleniarm/standalone-chromium:latest
        options: --add-host=thunder-testing:host-gateway --name chrome
        ports:
          - 4444:4444

    strategy:
      fail-fast: false
      matrix:
        PHP_VERSION: [ '8.3', '8.4' ]
        DRUPAL_TESTING_TEST_SUITE: [ 'kernel', 'functional', 'functional-javascript' ]

    steps:
      - uses: actions/checkout@v3

      - uses: shivammathur/setup-php@v2
        with:
          coverage: none
          php-version: ${{ matrix.PHP_VERSION }}
          extensions: :redis, :sodium, :pspell, Imagick, gd, PDO, mysql, pdo_mysql, mbstring, xmlwriter, pdo, date, dom, filter, hash, json, pcre, session, SimpleXML, SPL, tokenizer, xml, curl

      - name: Add host
        run: echo "127.0.0.1 thunder-testing" | sudo tee -a /etc/hosts

      - name: Prepare fixtures
        run: docker cp ./tests/fixtures chrome:/fixtures

      - name: Get build environment
        run: |
          composer global config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true --no-plugins
          composer global require thunder/drupal-testing:${DRUPAL_TESTING_VERSION}

      - name: Download build
        uses: actions/download-artifact@v4
        with:
          name: build-${{ matrix.PHP_VERSION }}

      - name: Unzip build artifact
        run: mkdir -p /tmp/test; tar xCfz /tmp/test build-${{ matrix.PHP_VERSION }}.tgz thunder; rm -rf build-${{ matrix.PHP_VERSION }}

      - name: Setup Apache
        uses: thunder/apache-shiva-php-action@v1
        with:
          php-version: ${{ matrix.PHP_VERSION }}
          site-directory: /tmp/test/thunder/install/docroot
          http-port: ${{ env.DRUPAL_TESTING_HTTP_PORT }}

      - name: PHP Info
        run: |
          php -m
          php -i

      - name: Run tests
        run: test-drupal-project run_tests
        env:
          DRUPAL_TESTING_TEST_SUITE: ${{ matrix.DRUPAL_TESTING_TEST_SUITE }}

      - name: Upload test output
        uses: actions/upload-artifact@v4
        if: success() || failure()
        with:
          name: text-max-output-${{ matrix.DRUPAL_TESTING_TEST_SUITE }}-${{ matrix.PHP_VERSION }}
          path: /tmp/test/thunder/install/docroot/sites/simpletest/browser_output/

  test-upgrade:

    runs-on: ubuntu-24.04

    services:
      mysql:
        image: mariadb:lts
        env:
          MYSQL_ROOT_PASSWORD: root
        ports:
          - 3306:3306
      chrome:
        image: seleniarm/standalone-chromium:latest
        options: --add-host=thunder-testing:host-gateway --name chrome
        ports:
          - 4444:4444

    strategy:
      matrix:
        PHP_VERSION: [ '8.3', '8.4' ]

    if: ${{ contains(github.event.pull_request.labels.*.name, 'test-upgrade') || github.event_name == 'schedule' }}

    env:
      DRUPAL_TESTING_TEST_GROUP_EXCLUDE: "NoUpdate"
      DRUPAL_TESTING_TEST_GROUP: Thunder

    steps:
      - uses: actions/checkout@v3

      - uses: shivammathur/setup-php@v2
        with:
          coverage: none
          php-version: ${{ matrix.PHP_VERSION }}
          extensions: :redis, :pspell, Imagick, gd, pdo_mysql

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

      - name: Prepare fixtures
        run: docker cp ./tests/fixtures chrome:/fixtures

      - name: Add host
        run: echo "127.0.0.1 thunder-testing" | sudo tee -a /etc/hosts

      - name: Get build environment
        run: |
          composer global config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true --no-plugins
          composer global require thunder/drupal-testing:${DRUPAL_TESTING_VERSION}

      - name: Prepare the old install
        run: test-drupal-project prepare_old_install
        env:
          COMPOSER_ROOT_VERSION: ${{ env.DRUPAL_TESTING_UPGRADE_COMPOSER_PROJECT_VERSION }}

      - name: Fix the old install
        run: |
          composer upgrade league/container
        working-directory: /tmp/test/thunder/install
        env:
          COMPOSER_ROOT_VERSION: ${{ env.DRUPAL_TESTING_UPGRADE_COMPOSER_PROJECT_VERSION }}

      - name: Install drupal
        run: test-drupal-project install
        env:
          COMPOSER_ROOT_VERSION: ${{ env.DRUPAL_TESTING_UPGRADE_COMPOSER_PROJECT_VERSION }}

      - name: Prepare the upgrade
        run: test-drupal-project prepare_upgrade
        env:
          COMPOSER_ROOT_VERSION: ${{ env.DRUPAL_TESTING_UPGRADE_COMPOSER_PROJECT_VERSION }}

      - name: Update the docroot
        run: |
          composer remove thunder/thunder-distribution --no-update
          composer config name "drupal-testing-thunder/thunder-distribution" --working-dir="${GITHUB_WORKSPACE}"
          composer require "drupal-testing-thunder/thunder-distribution:*" --no-update
          composer require "drupal/slick" --no-update
          composer require "drupal/blazy" --no-update
          composer require "drupal/select2" --no-update
          composer require "drupal/core-dev:~11.3.0" --no-update

          composer update
          composer install
          composer exec -- drush updb -y

          cd docroot
          php core/scripts/db-tools.php dump-database-d8-mysql --database-url "${SIMPLETEST_DB}" >"sites/default/database-dump.php"
          tar -czf "${DRUPAL_TESTING_TEST_DUMP_FILE}" --exclude='config_*' --exclude='php' --exclude='styles' --directory='sites/default' files database-dump.php
        working-directory: /tmp/test/thunder/install
        env:
          COMPOSER_ROOT_VERSION: ${{ env.DRUPAL_TESTING_UPGRADE_COMPOSER_PROJECT_VERSION }}

      - name: Setup Apache
        uses: thunder/apache-shiva-php-action@v1
        with:
          php-version: ${{ matrix.PHP_VERSION }}
          site-directory: /tmp/test/thunder/install/docroot
          http-port: ${{ env.DRUPAL_TESTING_HTTP_PORT }}

      - name: Run tests suite
        run: test-drupal-project run_tests
        env:
          COMPOSER_ROOT_VERSION: ${{ env.DRUPAL_TESTING_UPGRADE_COMPOSER_PROJECT_VERSION }}

      - name: Upload test output
        uses: actions/upload-artifact@v4
        if: success() || failure()
        with:
          name: text-upgrade-output-${{ matrix.PHP_VERSION }}
          path: /tmp/test/thunder/install/docroot/sites/simpletest/browser_output/

  test-min:

    runs-on: ubuntu-24.04

    services:
      mysql:
        image: mariadb:lts
        env:
          MYSQL_ROOT_PASSWORD: root
        ports:
          - 3306:3306
      chrome:
        image: seleniarm/standalone-chromium:latest
        options: --add-host=thunder-testing:host-gateway --name chrome
        ports:
          - 4444:4444

    strategy:
      matrix:
        PHP_VERSION: [ '8.3', '8.4' ]

    if: ${{ contains(github.event.pull_request.labels.*.name, 'test-min') || github.event_name == 'schedule' }}

    steps:
      - uses: actions/checkout@v3

      - uses: shivammathur/setup-php@v2
        with:
          coverage: none
          php-version: ${{ matrix.PHP_VERSION }}
          extensions: :redis, :pspell, Imagick, gd, pdo_mysql

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

      - name: Prepare fixtures
        run: docker cp ./tests/fixtures chrome:/fixtures

      - name: Add host
        run: echo "127.0.0.1 thunder-testing" | sudo tee -a /etc/hosts

      - name: Get build environment
        run: |
          composer global config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true --no-plugins
          composer global require thunder/drupal-testing:${DRUPAL_TESTING_VERSION}

      - name: Prepare the build
        run: test-drupal-project prepare_build

      - name: Install suggested dependencies
        run: |
          composer require "league/container:^4.2" "drupal/core-dev:^11.3" "grasmash/yaml-cli:^3.2.1" "drupal/tour:2.0.8" --no-update --no-progress --working-dir=/tmp/test/thunder/install
          composer config allow-plugins.php-http/discovery true --no-plugins --working-dir=/tmp/test/thunder/install

      - name: Build the docroot
        run: test-drupal-project build
        env:
          DRUPAL_TESTING_MIN_BUILD: true

      - name: Setup Apache
        uses: thunder/apache-shiva-php-action@v1
        with:
          php-version: ${{ matrix.PHP_VERSION }}
          site-directory: /tmp/test/thunder/install/docroot
          http-port: ${{ env.DRUPAL_TESTING_HTTP_PORT }}

      - name: Run the tests
        run: test-drupal-project run_tests
        env:
          DRUPAL_TESTING_TEST_GROUP: Thunder

      - name: Upload test output
        uses: actions/upload-artifact@v4
        if: success() || failure()
        with:
          name: text-min-output-${{ matrix.PHP_VERSION }}
          path: /tmp/test/thunder/install/docroot/sites/simpletest/browser_output/

#   Start a debug session.
#   - name: Setup tmate session
#     uses: mxschmitt/action-tmate@v3
