stages:
  - setup
  - test
  - sast

cache:
  paths:
    - vendor/

services:
  - mysql

before_script:
  - apt-get update -yqq > /dev/null 2>&1
  - apt-get install git -yqq > /dev/null 2>&1
  - apt-get install libonig-dev -yqq > /dev/null 2>&1
  - apt-get -yqqf install wget zip unzip subversion default-mysql-client libmcrypt-dev default-libmysqlclient-dev default-mysql-server --fix-missing > /dev/null 2>&1
  - docker-php-ext-install mysqli pdo_mysql mbstring > /dev/null 2>&1
  - pecl install xdebug-${XDEBUG_VERSION} > /dev/null 2>&1
  - echo xdebug.mode=coverage > /usr/local/etc/php/conf.d/xdebug.ini 2>&1
  - docker-php-ext-enable mysqli pdo_mysql mbstring xdebug > /dev/null 2>&1
  - curl -sS https://getcomposer.org/installer | php > /dev/null 2>&1
  - php composer.phar install --ignore-platform-reqs > /dev/null 2>&1
  - php composer.phar update > /dev/null 2>&1
  - bash bin/install-wp-tests.sh wordpress_test root mysql mysql $WP_VERSION > /dev/null 2>&1
  - 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
  - zip -r really-simple-ssl.zip . > /dev/null 2>&1
  - wp plugin install really-simple-ssl.zip --allow-root --force
  - wp plugin activate really-simple-ssl --allow-root
  - wp plugin deactivate really-simple-ssl --allow-root
  - wp plugin uninstall really-simple-ssl --allow-root
  - wp plugin install really-simple-ssl --allow-root
  - wp plugin activate really-simple-ssl --allow-root
  - wp plugin install really-simple-ssl.zip --allow-root --force
  - wp plugin deactivate really-simple-ssl --allow-root
  - wp plugin uninstall really-simple-ssl --allow-root
variables:
  MYSQL_DATABASE: wordpress_tests
  MYSQL_ROOT_PASSWORD: mysql
  WP_VERSION: latest
  WP_MULTISITE: '0'
  XDEBUG_VERSION: 3.1.6
  SAST_EXCLUDED_PATHS: "vendor/,tests,vendor,*.min.js"
  CI_JOB_LOG_LEVEL: ERROR

test:php7.4:
  stage: test
  image: php:7.4
  variables:
    XDEBUG_VERSION: 3.1.6
  script:
    - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clove --colors=never
  rules:
    - if: "$CI_PIPELINE_SOURCE == 'merge_request_event'"
    - if: "$CI_COMMIT_BRANCH == 'master'"
test:php7.4:multisite:
  stage: test
  image: php:7.4
  variables:
    WP_MULTISITE: '1'
    XDEBUG_VERSION: 3.1.6
  script:
    - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clove --colors=never
  rules:
    - if: "$CI_PIPELINE_SOURCE == 'merge_request_event'"
    - if: "$CI_COMMIT_BRANCH == 'master'"
test:php8.2:
  stage: test
  image: php:8.2
  variables:
    XDEBUG_VERSION: 3.2.1
  script:
    - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clove --colors=never
  rules:
    - if: "$CI_PIPELINE_SOURCE == 'merge_request_event'"
    - if: "$CI_COMMIT_BRANCH == 'master'"
test:php8.2:multisite:
  stage: test
  image: php:8.2
  variables:
    WP_MULTISITE: '1'
    XDEBUG_VERSION: 3.2.1
  script:
    - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clove --colors=never
  rules:
    - if: "$CI_PIPELINE_SOURCE == 'merge_request_event'"
    - if: "$CI_COMMIT_BRANCH == 'master'"
test:php8.2:wordpress5.9:
  stage: test
  image: php:8.2
  variables:
    XDEBUG_VERSION: 3.2.1
    WP_VERSION: '5.9'
  script:
    - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clove --colors=never
  rules:
    - if: "$CI_PIPELINE_SOURCE == 'merge_request_event'"
    - if: "$CI_COMMIT_BRANCH == 'master'"
test:php8.2:wordpress5.9:multisite:
  stage: test
  image: php:8.2
  variables:
    WP_MULTISITE: '1'
    XDEBUG_VERSION: 3.2.1
    WP_VERSION: '5.9'
  script:
    - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clove --colors=never
  rules:
    - if: "$CI_PIPELINE_SOURCE == 'merge_request_event'"
    - if: "$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