.codeception-base:
  tags:
    - codeception
    - $TESTS_TAG
  image:
    name: gitlab.wpdesk.dev:5050/wpdesk/docker-tests/codeception:13.1.0
    entrypoint: [""]
  variables:
    WP_CLI_CACHE_DIR: /cache/wp-cli
    APACHE_DOCUMENT_ROOT: ${CI_PROJECT_DIR}/tests/wordpress
    SCREEN_WIDTH: 1200
    SCREEN_HEIGHT: 1200
    DEPENDENT_PLUGINS_DIR: ${CI_PROJECT_DIR}/tests/dependent_plugins
  services:
    - name: mysql:5.6
      alias: mysqltests
      command: [--max-allowed-packet=67108864]
    - name: wpdesknet/wordpress:52
      alias: wootests
    - name: selenium/standalone-chrome:latest
      alias: chrome
  artifacts:
    when: always
    expire_in: 1 month
    name: "acceptance logs"
    paths:
      - tests/codeception/tests/_output
    reports:
      junit: tests/codeception/tests/_output/report.xml
  stage: tests
  needs:
    - prepare prefixed vendor
    - prepare translations
    - job: prepare tests
      optional: true
    - job: prepare npm assets
      optional: true
  rules:
    - if: $DISABLE_CODECEPTION
      when: never
    - if: $IS_LIBRARY || $IS_NPM_LIBRARY
      when: never
  allow_failure: false
  interruptible: true
  before_script:
    - composer config gitlab-token.gitlab.wpdesk.dev gitlab-ci-token ${CI_JOB_TOKEN}
    - echo "xdebug.max_nesting_level=2560" >> /usr/local/etc/php/php.ini
    - mkdir -p /cache/wp-cli
    - mkdir -p /project
    - mkdir -p ${APACHE_DOCUMENT_ROOT}
    - mkdir -p ${CI_PROJECT_DIR}/tests/codeception/tests/{_output,acceptance,functional,unit}
    - export DEPENDENT_PLUGINS_DIR=${CI_PROJECT_DIR}/tests/dependent_plugins
    - export MYSQL_IP=$(awk '/^[[:space:]]*($|#)/{next} /mysqltests/{print $1; exit}' /etc/hosts)
    - export WOOTESTS_IP=$(awk '/^[[:space:]]*($|#)/{next} /wootests/{print $1; exit}' /etc/hosts)
    - export TEST_SITE_WP_URL="http://${WOOTESTS_IP}"
    - |
      if [ -f ${CI_PROJECT_DIR}/tests/codeception/bootstrap.sh ]; then
        set -x
        cp -r ${CI_PROJECT_DIR}/. /project
        cd /project
        composer install --no-progress --prefer-dist --no-dev
        rm auth.json
        cd ${CI_PROJECT_DIR}
        set +x
      fi
    - |
      if [ -f ${CI_PROJECT_DIR}/tests/codeception/bootstrap.sh ]; then
        set -x
        cd ${APACHE_DOCUMENT_ROOT}
        wp core download ${WORDPRESS_CLI_PARAMETERS} --allow-root
        wp config create --dbhost=${MYSQL_IP} --dbname=wptest --dbuser=mysql --dbpass=mysql --allow-root
        wp config set WP_DEBUG true --raw --type=constant --allow-root
        cd ${CI_PROJECT_DIR}
        chmod a+x ./tests/codeception/bootstrap.sh
        . ./tests/codeception/bootstrap.sh
        set +x
      fi
    - |
      if [ -f ${CI_PROJECT_DIR}/tests/codeception/wpdesk.yml ]; then
        set -x
        composer prepare-wordpress-for-codeception
        composer prepare-local-codeception-tests
        set +x
      fi
    - chmod -R a+w ${APACHE_DOCUMENT_ROOT}/wp-content/uploads
    - touch ${APACHE_DOCUMENT_ROOT}/wp-content/debug.log
    - chmod a+w ${APACHE_DOCUMENT_ROOT}/wp-content/debug.log
    - echo "End before"
    - cd ${CI_PROJECT_DIR}
  script:
    - vendor/bin/codecept run acceptance --steps --xml --html -f --verbose
  after_script:
    - !reference [.after-codeception, after_script]
    - 'if [[ ${REPORTPORTAL_AUTHORIZATION} && ${REPORTPORTAL_PROJECT} ]]; then curl -X POST "http://195.201.225.204:8080/api/v1/${REPORTPORTAL_PROJECT}/launch/import" -H "accept: */*" -H "Content-Type: multipart/form-data" -H "${REPORTPORTAL_AUTHORIZATION}" -F "file=@tests/codeception/tests/_output/report.xml;type=text/xml;filename=${CI_PROJECT_NAME}-${CI_COMMIT_REF_SLUG}-codeception.xml"; fi'

codeception test:
  extends: .codeception-base
  rules:
    - !reference [.codeception-base, rules]
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
    - if: $CI_COMMIT_BRANCH == "devel"
    - if: $CI_COMMIT_BRANCH =~ /^tests.*/

codeception test manual:
  extends: .codeception-base
  when: manual
  allow_failure: true
  rules:
    - !reference [.codeception-base, rules]
    - if: $CI_COMMIT_TAG
      when: never
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: never
    - if: $CI_COMMIT_BRANCH == "devel"
      when: never

codeception test recorded manual:
  extends: .codeception-base
  when: manual
  allow_failure: true
  script:
    - cp vendor/wpdesk/wp-codeception/configuration/codeception-with-recorder.dist.yml codeception-with-recorder.dist.yml
    - vendor/bin/codecept -c codeception-with-recorder.dist.yml run --steps --xml --html -f --verbose
  after_script:
    - rm -r ${APACHE_DOCUMENT_ROOT}
    - 'if [[ ${REPORTPORTAL_AUTHORIZATION} && ${REPORTPORTAL_PROJECT} ]]; then curl -X POST "http://195.201.225.204:8080/api/v1/${REPORTPORTAL_PROJECT}/launch/import" -H "accept: */*" -H "Content-Type: multipart/form-data" -H "${REPORTPORTAL_AUTHORIZATION}" -F "file=@tests/codeception/tests/_output/report.xml;type=text/xml;filename=${CI_PROJECT_NAME}-${CI_COMMIT_REF_SLUG}-codeception.xml"; fi'
    - 'curl -X POST --data-urlencode "payload={\"text\": \"Testy projektu <${CI_PROJECT_URL}|${CI_PROJECT_NAME}> zostały wykonane. <${CI_PROJECT_URL}/-/jobs/${CI_JOB_ID}/artifacts/browse/tests/codeception/tests/_output/records.html|Zapis testu>\", }" https://hooks.slack.com/services/${SLACK_AUTH}'
