name: E2E

on:
  push:
  pull_request:

permissions:
  contents: read

jobs:
  browser_tests:
    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, '[skip ci]') && !contains(github.event.head_commit.message, '[ci skip]') && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name)"

    strategy:
      fail-fast: false
      matrix:
        # Use minimum and maximum supported PHP version here
        php: ["8.1", "8.5"]

    name: Linux / PHP ${{ matrix.php }}

    services:
      mailhost:
        image: docker.io/greenmail/standalone
        ports:
          - '3143:3143'
          - '3025:3025'
      browserhost:
        image: docker.io/selenium/standalone-chromium
        volumes:
          - '/dev/shm:/dev/shm'
          - ${{ github.workspace }}:/roundcubemail
        ports:
          - '4444:4444'
        options: '--add-host=host.docker.internal:host-gateway'

    env:
      WEBDRIVER_CONNECT_URL: 'http://localhost:4444'
      SERVER_URL: 'http://host.docker.internal:8000'
      SERVER_BIND: '0.0.0.0:8000'
      BROWSER_DOWNLOADS_DIR: '/roundcubemail/tests/Browser/downloads'
      TESTRUNNER_DOWNLOADS_DIR: './tests/Browser/downloads'
      RC_CONFIG_IMAP_HOST: 'tls://localhost:3143'
      RC_CONFIG_SMTP_HOST: 'localhost:3025'

    steps:
      - name: Checkout code
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Setup PHP
        uses: shivammathur/setup-php@9e72090525849c5e82e596468b86eb55e9cc5401 # v2.32.0
        with:
          php-version: ${{ matrix.php }}
          extensions: dom, curl, fileinfo, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd, ldap, intl, pspell
          tools: composer:v2
          coverage: none

      - name: Setup NPM
        uses: actions/setup-node@cdca7365b2dadb8aad0a33bc7601856ffabcc48e # v4.3.0
        with:
          node-version: '16'

      - name: Setup problem matchers
        run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

      - name: Install Aspell
        run: sudo apt-get -y update && sudo apt-get -y install --no-install-recommends aspell aspell-en aspell-de

      - name: Execute tests
        run: .ci/run_browser_tests.sh

      - name: Upload screenshots as artifacts
        # Upload screenshot if the test suite failed.
        if: failure()
        uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 # v4.6.1
        with:
          name: screenshots-php${{ matrix.php }}
          path: tests/Browser/screenshots

      - name: Upload browser console as artifacts
        # Upload console messages if the test suite failed.
        if: failure()
        uses: actions/upload-artifact@4cec3d8aa04e39d1a68397de0c4cd6fb9dce8ec1 # v4.6.1
        with:
          name: console-php${{ matrix.php }}
          path: tests/Browser/console
