include:
  - project: $_GITLAB_TEMPLATES_REPO
    ref: $_GITLAB_TEMPLATES_REF
    file:
      - '/includes/include.drupalci.main.yml'
      - '/includes/include.drupalci.variables.yml'
      - '/includes/include.drupalci.workflows.yml'

################################################################################
# Remove Canvas from all jobs except the one that tests it
# This is necessary because Canvas is only supported on Drupal core >=11.2.2.
# If we included Canvas in all jobs, it would be installed on all core versions, and
# would cause test failures on unsupported core versions.
################################################################################
.composer-base:
  before_script:
    - |
      # Remove drupal/canvas from all other jobs.
      cd $CI_PROJECT_DIR
      # Only keep drupal/canvas if we are on Drupal core >= 11.2.2
      # and also on Drupal Core: 11.x-dev / 12.x-dev etc.
      if [ "$DRUPAL_CORE" = "^9.5" ] \
        || ( [ "$(printf '%s\n' "$DRUPAL_CORE" "11.2.1" | sort -V | head -n1)" = "$DRUPAL_CORE" ] \
        && [[ ! "$DRUPAL_CORE" =~ ^([1][1-9]|[2-9][0-9])\.x-dev$ ]] ); then
        echo "Remove 'drupal/canvas' from composer.json; Supported on Drupal Core >=11.2.2."
        jq 'del(.["require-dev"]["drupal/canvas"])' composer.json > composer.tmp.json && mv composer.tmp.json composer.json
      fi

###############################################################################
# Remove acquia_dam_canvas tests from all jobs having Drupal core < 11.2.2.
# These tests are not compatible with other core versions.
###############################################################################
.testing-job-base:
  before_script:
    - |
      if [ "$DRUPAL_CORE" = "^9.5" ] \
        || ( [ "$(printf '%s\n' "$DRUPAL_CORE" "11.2.1" | sort -V | head -n1)" = "$DRUPAL_CORE" ] \
        && [[ ! "$DRUPAL_CORE" =~ ^([1][1-9]|[2-9][0-9])\.x-dev$ ]] ); then
        echo "Remove all acquia_dam_canvas tests; they are not compatible with other core version."
        grep -rl '@group acquia_dam_canvas' $CI_PROJECT_DIR/tests | xargs rm -f
      fi

################
# Create separate job for Drupal 9 testing.
################
composer-drupal-core-9:
  extends: .composer-base
  allow_failure: true
  variables:
    DRUPAL_CORE: ^9.5
    PHP_VERSION: "8.1"
    _TARGET_PHP_IMAGE_VARIANT: 'apache'

phpunit-drupal-core-9:
  needs:
    - composer-drupal-core-9
  extends: .phpunit-base
  # @todo set allow_failure to false post security fix releases for twig.
  allow_failure: true
  stage: test
  parallel:
    matrix:
      - _PHPUNIT_TESTGROUPS:
        - acquia_dam_unit
        - acquia_dam_kernel
        - acquia_dam_functional_javascript
        - acquia_dam_functional_javascript_integration
        - acquiadam_asset_import
        - acquia_dam_integration_links
  variables:
    _TARGET_PHP_IMAGE_VARIANT: "apache"

################
# Drupal Current, previous and Next version testing.
################
variables:
  OPT_IN_TEST_CURRENT: 1
  OPT_IN_TEST_PREVIOUS_MAJOR: 1
  OPT_IN_TEST_PREVIOUS_MINOR: 1
  OPT_IN_TEST_NEXT_MINOR: 1
  OPT_IN_TEST_NEXT_MAJOR: 1

# composer-lint: {allow_failure: false}
# eslint: {allow_failure: true}
# cspell: {allow_failure: true}
# phpcs: {allow_failure: false}
#phpstan: {allow_failure: true}
#phpstan (next minor): {allow_failure: true}
#phpstan (next major): {allow_failure: true}
#phpstan (max PHP version): {allow_failure: true}
#phpunit (next minor): {allow_failure: true}
#phpunit (next major): {allow_failure: true}
#phpunit (max PHP version): {allow_failure: true}
phpunit:
  extends: .phpunit-base
  allow_failure: false
  stage: test
  parallel:
    matrix:
      - _PHPUNIT_TESTGROUPS:
        - acquia_dam_unit
        - acquia_dam_kernel
        - acquia_dam_functional_javascript
        - acquia_dam_functional_javascript_integration
        - acquia_dam_functional_javascript_media_library_integration
        - acquia_dam_functional_javascript_media_library
        - acquiadam_asset_import
        - acquia_dam_integration_links
