stages:
  - prepare
  - test
  - sast
  - get_plugin_version

get_plugin_version:
  script:
    - |
      if [ "$PLUGIN_SLUG" == "really-simple-ssl" ]; then
        cp gitlab-ci-free.yml .gitlab-ci-dynamic.yml
      elif [ "$PLUGIN_SLUG" == "really-simple-ssl-pro" ]; then
        cp gitlab-ci-pro.yml .gitlab-ci-dynamic.yml
      else
        echo "Unknown plugin slug"
        exit 1
      fi

# Select what we should cache
cache:
  paths:
    - vendor/

services:
  - mysql

download_zip:
  stage: prepare
  before_script: []
  script:
    # Get main branch in a really-simple-ssl-pro-artifact.zip file
    - 'curl -L -H "PRIVATE-TOKEN: $CI_AUTH_KEY" https://gitlab.com/really-simple-plugins/really-simple-ssl/really-simple-ssl-pro/-/archive/master/really-simple-ssl-pro-master.zip -o really-simple-ssl-pro-artifact.zip'
    - apt-get update -yqq > /dev/null 2>&1
    - apt-get install zip -yqq > /dev/null 2>&1
    - unzip really-simple-ssl-pro-artifact.zip > /dev/null 2>&1
    - mv really-simple-ssl-pro-master really-simple-ssl-pro > /dev/null 2>&1
    - zip -r really-simple-ssl-pro-artifact.zip . > /dev/null 2>&1
  artifacts:
    paths:
      - really-simple-ssl-pro-artifact.zip
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" || $CI_COMMIT_BRANCH == "master"'

variables:
  # Configure mysql service (https://hub.docker.com/_/mysql/)
  MYSQL_DATABASE: wordpress_tests
  MYSQL_ROOT_PASSWORD: mysql
  WP_VERSION: latest
  WP_MULTISITE: "0"
  SAST_EXCLUDED_PATHS: "vendor/,tests,vendor,*.min.js"

# Set up test environment
before_script:
  #  stage: test
  # Install git, the php image doesn't have it installed
  - apt-get update -yqq > /dev/null 2>&1
  - apt-get install git -yqq > /dev/null 2>&1
  # required for "PHP 7.4+
  - apt-get install libonig-dev -yqq > /dev/null 2>&1
  # install the required packages for the running CI tests
  - apt-get -yqqf install vim wget zip unzip subversion default-mysql-client libmcrypt-dev default-libmysqlclient-dev default-mysql-server --fix-missing > /dev/null 2>&1
  # Zip current branch to allow it to be installed using wp-cli
  - zip -r really-simple-ssl-pro.zip . > /dev/null 2>&1
  # Install mysql driver
  - docker-php-ext-install mysqli pdo_mysql mbstring > /dev/null 2>&1
  # Install Xdebug. See https://xdebug.org/docs/compat for which version to install
  - pecl install xdebug-${XDEBUG_VERSION} > /dev/null 2>&1
  # XDEBUG mode has to be set to coverage
  - echo xdebug.mode=coverage > /usr/local/etc/php/conf.d/xdebug.ini > /dev/null 2>&1
  # PHP extensions
  - docker-php-ext-enable mysqli pdo_mysql mbstring xdebug > /dev/null 2>&1
  # Install composer
  - curl -sS https://getcomposer.org/installer | php > /dev/null 2>&1
  # Install project dependencies
  - php composer.phar install --ignore-platform-reqs > /dev/null 2>&1
  - php composer.phar update > /dev/null 2>&1
  # Set up WordPress test environment
  - bash bin/install-wp-tests.sh wordpress_test root mysql mysql $WP_VERSION > /dev/null 2>&1
  # Install WP-CLI and configure a WordPress installation
  - curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar > /dev/null 2>&1
  - chmod +x wp-cli.phar > /dev/null 2>&1
  - mv wp-cli.phar /usr/local/bin/wp > /dev/null 2>&1
  - wp core download --allow-root > /dev/null 2>&1
  - wp core config --dbhost=mysql --dbname=$DB_NAME --dbuser=$DB_USER --dbpass=$DB_PASS --allow-root > /dev/null 2>&1
  - wp config list --allow-root > /dev/null 2>&1
  - wp core install --url=http://localhost --title=Example --admin_user=$ADMIN_USER --admin_password=$ADMIN_PASSWORD --admin_email=$ADMIN_EMAIL --allow-root > /dev/null 2>&1

# We test PHP 7.4
test:php7.4:
  image: php:7.4
  variables:
    XDEBUG_VERSION: "3.1.6"
  script:
    - &deploy_plugin
      - wp plugin install really-simple-ssl --allow-root
      - wp plugin activate really-simple-ssl --allow-root
      - wp plugin install really-simple-ssl-pro.zip --allow-root --force
      - wp plugin activate really-simple-ssl-pro --allow-root
      - wp plugin deactivate really-simple-ssl-pro --allow-root
      - wp plugin uninstall really-simple-ssl-pro --allow-root
      # New branch install, activate, deactivate and uninstall completed. Rename .zip to -new
      - mv really-simple-ssl-pro.zip really-simple-ssl-pro-new.zip
      # Rename -artifact to plugin name
      - mv really-simple-ssl-pro-artifact.zip really-simple-ssl-pro.zip
      # Install artifact (main)
      - wp plugin install really-simple-ssl-pro.zip --allow-root --force
      - wp plugin activate really-simple-ssl-pro --allow-root
      # Rename back to -artifact for later usage
      - mv really-simple-ssl-pro.zip really-simple-ssl-pro-artifact.zip
      # Rename -new back to plugin to test update from artifact (main)
      - mv really-simple-ssl-pro-new.zip really-simple-ssl-pro.zip
      - wp plugin install really-simple-ssl-pro.zip --allow-root --force
      - wp plugin deactivate really-simple-ssl-pro --allow-root
      - wp plugin uninstall really-simple-ssl-pro --allow-root
      # Run tests
      - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clove --colors=never
    - *deploy_plugin
  needs:
    - download_zip
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" || $CI_COMMIT_BRANCH == "master"'

# We test PHP7.4 with multisite
test:php7.4:multisite:
  variables:
    WP_MULTISITE: "1"
    XDEBUG_VERSION: "3.1.6"
  image: php:7.4
  script:
    - *deploy_plugin
  needs:
    - download_zip
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" || $CI_COMMIT_BRANCH == "master"'

# We test PHP8.0
test:php8.0:
  image: php:8.0
  variables:
    XDEBUG_VERSION: "3.1.6"
  script:
    - *deploy_plugin
  needs:
    - download_zip
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" || $CI_COMMIT_BRANCH == "master"'

# We test PHP8 with multisite
test:php8.0:multisite:
  variables:
    WP_MULTISITE: "1"
    XDEBUG_VERSION: "3.2.1"
  image: php:8.0
  script:
    - *deploy_plugin
  needs:
    - download_zip
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" || $CI_COMMIT_BRANCH == "master"'

# We test PHP8.1
test:php8.1:
  image: php:8.1
  variables:
    XDEBUG_VERSION: "3.1.6"
  script:
    - *deploy_plugin
  needs:
    - download_zip
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" || $CI_COMMIT_BRANCH == "master"'

# We test PHP8.1 with multisite
test:php8.1:multisite:
  variables:
    WP_MULTISITE: "1"
    XDEBUG_VERSION: "3.2.1"
  image: php:8.1
  script:
    - *deploy_plugin
  needs:
    - download_zip
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" || $CI_COMMIT_BRANCH == "master"'

# We test PHP8.2
test:php8.2:
  image: php:8.2
  variables:
    XDEBUG_VERSION: "3.2.1"
  script:
    - *deploy_plugin
  needs:
    - download_zip
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" || $CI_COMMIT_BRANCH == "master"'

# We test PHP8.1 with multisite
test:php8.2:multisite:
  variables:
    WP_MULTISITE: "1"
    XDEBUG_VERSION: "3.2.1"
  image: php:8.2
  script:
    - *deploy_plugin
  needs:
    - download_zip
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" || $CI_COMMIT_BRANCH == "master"'

sast:
  stage: sast
  before_script: []
  script:
    - phpcs --ignore=vendor/* --standard=phpcs.xml .
    - echo "Running SAST..."

include:
  - template: Security/SAST.gitlab-ci.yml