version: 2.1
orbs:
  browser-tools: circleci/browser-tools@1.1
aliases:
  - &composer-cache
      v4-composer-cache
jobs:
  build:
    docker:
      - image: circleci/php:7.3-apache-stretch-node-browsers
        auth:
          username: mydockerhub-user
          password: $DOCKERHUB_PASSWORD
      - image: circleci/mysql:8.0.4
        auth:
          username: mydockerhub-user
          password: $DOCKERHUB_PASSWORD
        entrypoint: ['docker-entrypoint.sh', '--default-authentication-plugin=mysql_native_password']
        environment:
          MYSQL_ROOT_PASSWORD: rootpw
          MYSQL_DATABASE: test_varbase900xxc
          MYSQL_USER: user
          MYSQL_PASSWORD: passw0rd
    working_directory: /var/www/html/varbase/docroot
    steps:
      - checkout:
          path: /var/www/html/varbase
      ## Setup Apache to point varbase.test at /var/www/html/varbase/docroot
      - run:
          name: Setup Apache to point varbase.test at /var/www/html/varbase/docroot
          command: |
            sudo cp /var/www/html/varbase/.circleci/env/varbase.test.conf /etc/apache2/sites-available/varbase.test.conf
            sudo a2ensite varbase.test
            sudo service apache2 restart
            echo 127.0.0.1 varbase.test | sudo tee -a /etc/hosts

      ## Install Usefull must have utils for debugging with SSH into this box.
      - run:
          name: Install Usefull must have utils for debugging with SSH into this box
          command: |
            sudo apt -qq update && sudo apt -qqy upgrade
            sudo apt -yqq install vim links lynx git diffutils htop curl wget p7zip-full unzip zip sed gcc g++ make libfreetype6-dev libjpeg62-turbo-dev libpng-dev xvfb
      ## Install PHP extra.
      - run:
          name: Install PHP extra
          command: |
            sudo apt update
            sudo apt install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev
            sudo -E docker-php-ext-install zip
            sudo -E docker-php-ext-configure pdo_mysql --with-pdo-mysql=mysqlnd
            sudo -E docker-php-ext-install mbstring bcmath pdo_mysql
            sudo -E docker-php-ext-install gd
            sudo service apache2 restart

      ## Change PHP Configurations.
      - run:
          name: PHP Configurations
          command: |
            echo -e "memory_limit = -1" | sudo tee /usr/local/etc/php/php.ini > /dev/null
            sudo rm /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
            sudo service apache2 restart
      - restore_cache:
          keys:
            - *composer-cache

      ## Waiting for MySQL to be ready.
      - run:
          name: Waiting for MySQL to be ready
          command: |
            for i in `seq 1 10`;
            do
              nc -z 127.0.0.1 3306 && echo Success && exit 0
              echo -n .
              sleep 1
            done
            echo Failed waiting for MySQL && exit 1

      ## Create test Varbase MySQL database and drop old one if exists.
      - run:
          name: Create test Varbase MySQL database and drop old one if exists
          command: |
            sudo apt install mysql-client
            mysql -h 127.0.0.1 -uuser -ppassw0rd -e "DROP DATABASE IF EXISTS test_varbase900xxc;CREATE DATABASE test_varbase900xxc;"
      - browser-tools/install-browser-tools

      ## List tool version.
      - run:
          name: List tool version
          command: |
            php --version
            node --version
            java --version
            google-chrome --version
            chromedriver --version
            mysql --version

      ## Configure and run the virtual display.
      - run:
          name: Configure and run the virtual display
          command: |
            export DISPLAY=:99
            xvfb :99 -ac -screen 0 1366x768x24 &>/dev/null &
            sleep 3

      ## Run selenium standalone server.
      - run:
          name: Download Selenium
          command: if [[ ! -f /home/circleci/selenium-server-standalone-2.53.1.jar ]]; then wget -O /home/circleci/selenium-server-standalone-2.53.1.jar http://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.1.jar; fi
      - run:
          name: Start Selenium at the 4445 port
          command: |
            cd /home/circleci/
            java -jar selenium-server-standalone-2.53.1.jar -port 4445 > /dev/null 2>&1 &
          background: true

      ## Update the composer
      - run:
          name: Update the composer
          command: |
            sudo composer self-update --2
            composer --version

      ## Build Varbase 9.0.x with the composer.
      - run:
          name: Build Varbase 9.0.x with the composer
          command: |
            cd /var/www/html/varbase
            composer install --no-interaction -vvv
            git clone --branch 9.0.x https://github.com/vardot/varbase.git /var/www/html/varbase/docroot/profiles/varbase
      - save_cache:
          key: *composer-cache
          paths:
            - ~/.composer/cache

      ## Update PATH to point at the composer bin in the project.
      - run:
          name: Update PATH to point at the composer bin in the project
          command: |
            echo 'export PATH=/var/www/html/varbase/bin:$PATH' >> $BASH_ENV
            source $BASH_ENV

      ## Install Varbase 9.0.x with drush.
      - run:
          name: Install Varbase with drush
          no_output_timeout: 30m
          command: |
            cd /var/www/html/varbase/docroot/
            drush site-install varbase --yes --site-name='Varbase Test' --account-name=webmaster --account-pass=dD.123123ddd --account-mail=webmaster@vardot.com --db-url="mysql://user:passw0rd@127.0.0.1/test_varbase900xxc" varbase_multilingual_configuration.enable_multilingual=true varbase_extra_components.vmi=true varbase_extra_components.varbase_heroslider_media=true varbase_extra_components.varbase_carousels=true varbase_extra_components.varbase_search=true varbase_extra_components.varbase_blog=true varbase_extra_components.varbase_landing=true varbase_extra_components.varbase_auth=true
            drush pm-enable varbase_development --yes
            drush pm-enable varbase_styleguide --yes
            drush pm-enable varbase_landing --yes
            drush pm-enable varbase_media_instagram --yes
            drush pm-enable varbase_media_twitter --yes
            drush pm-enable varbase_api --yes
            drush pm-enable social_auth_google --yes
            drush pm-enable social_auth_facebook --yes
            drush pm-enable social_auth_linkedin --yes
            drush pm-enable varbase_content_planner --yes
            drush config-set system.performance css.preprocess 0 --yes
            drush config-set system.performance js.preprocess 0 --yes
            drush config-set system.logging error_level all --yes
            drush cr

      ## Automated Functional Acceptance Testing.
      - run:
          name: Automated Functional Acceptance Testing
          no_output_timeout: 30m
          command: |
            cd /var/www/html/varbase/docroot/profiles/varbase
            behat --strict --no-snippets -f pretty -o std tests/features/varbase/step1-init-tests
            behat --strict --no-snippets -f pretty -o std tests/features/varbase/step2-apply-tests
