version: 2.1
defaults: &defaults
  docker:
    - image: cimg/base:2022.09
      auth:
        username: mydockerhub-user
        password: $DOCKERHUB_PASSWORD
    - image: cimg/mysql:8.0
      auth:
        username: mydockerhub-user
        password: $DOCKERHUB_PASSWORD
      entrypoint: 
        [
          "docker-entrypoint.sh",
          "--default-authentication-plugin=mysql_native_password",
        ]
      environment:
        MYSQL_ROOT_PASSWORD: rootpw
        MYSQL_ROOT_HOST: "%"
        MYSQL_DATABASE: test_varbase
        MYSQL_USER: user
        MYSQL_PASSWORD: passw0rd
  resource_class: large
commands:
  varbase-build:
    steps:
      - checkout
      - run:
          name: Usefull must have utils
          command: |
            sudo apt -qq update
            sudo apt -qqy install docker-buildx-plugin
            sudo apt -qqy upgrade
            sudo apt -yqq install vim links lynx git diffutils htop curl wget p7zip-full unzip zip sed gcc g++
            sudo apt -yqq install make libfreetype6-dev libaio1 libaio-dev libonig-dev libpng-dev mysql-client default-mysql-client xvfb
            sudo apt -yqq install gtk2-engines-pixbuf xserver-xorg-core xfonts-cyrillic xfonts-100dpi xfonts-75dpi xfonts-base xfonts-scalable
            sudo apt -yqq install libxpm4 libgtk2.0-0 imagemagick dbus-x11 x11-apps libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3
            sudo apt -yqq install libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0
            sudo apt -yqq install libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6
            sudo apt -yqq install libxrandr2 libxrender1 libxss1 libxtst6 libnss3 libu2f-udev libgbm1 libvulkan1 fonts-liberation libappindicator3-1
            sudo apt -yqq install libatk-bridge2.0-0 xdg-utils gnupg2 apt-transport-https ca-certificates
      - run:
          name: Setup Apache2 and PHP8.3
          command: |
            sudo add-apt-repository ppa:ondrej/php -y
            sudo apt update
            sudo apt upgrade
            sudo apt -yqq install apache2 php8.3 libapache2-mod-php8.3 php8.3-mbstring php8.3-mysql php8.3-xml php8.3-cgi php8.3-cli php8.3-common php8.3-curl php8.3-gd php8.3-imap php8.3-intl php8.3-pspell php8.3-sqlite3 php8.3-tidy php8.3-opcache php8.3-yaml php8.3-bz2 php8.3-readline php8.3-xmlrpc php8.3-enchant php8.3-xsl php8.3-zip php8.3-bcmath php-pear
            sudo a2enmod php8.3
            sudo a2enmod headers rewrite env mime expires ssl
            sudo update-alternatives --set php /usr/bin/php8.3
            echo -e "memory_limit = -1" | sudo tee /etc/php/8.3/apache2/php.ini > /dev/null
            echo -e "max_execution_time = 1200" | sudo tee /etc/php/8.3/apache2/php.ini > /dev/null
            echo -e "error_reporting = E_ALL" | sudo tee /etc/php/8.3/apache2/php.ini > /dev/null
            echo -e "max_input_time = 180" | sudo tee /etc/php/8.3/apache2/php.ini > /dev/null
            echo -e "max_input_vars = 10000" | sudo tee /etc/php/8.3/apache2/php.ini > /dev/null
            echo -e "post_max_size = 64M" | sudo tee /etc/php/8.3/apache2/php.ini > /dev/null
            echo -e "upload_max_filesize = 32M" | sudo tee /etc/php/8.3/apache2/php.ini > /dev/null
            echo -e "max_file_uploads = 40" | sudo tee /etc/php/8.3/apache2/php.ini > /dev/null
            sudo service apache2 restart
      - 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
      - run:
          name: Start D-Bus
          command: sudo service dbus start
      - run:
          name: Configure and run the virtual display.
          command: |
            export DISPLAY=:99
            Xvfb -ac :99 -screen 0 1600x1200x24 > /dev/null 2>&1
          background: true
      - run:
          name: Install/Update Google Chrome browser
          command: |
            sudo apt install libu2f-udev
            sudo apt-get install libgbm1 libvulkan1 libnss3 libgconf-2-4 libxss1 fonts-liberation libappindicator3-1 libatk-bridge2.0-0 xdg-utils libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev libnss3 libxss1 libasound2 libxtst6 xauth
            wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
            sudo dpkg -i google-chrome-stable_current_amd64.deb
            sudo apt-get install -f
            rm google-chrome-stable_current_amd64.deb
            sudo chmod +x /usr/bin/google-chrome
            /usr/bin/google-chrome --version
            google-chrome --version
      - run:
          name: Install/Update Chrome Driver
          command: |
            CHROME_VERSION=$(google-chrome --product-version);
            echo $CHROME_VERSION;
            wget https://storage.googleapis.com/chrome-for-testing-public/$CHROME_VERSION/linux64/chromedriver-linux64.zip
            unzip chromedriver-linux64.zip
            sudo chmod +x chromedriver-linux64/chromedriver
            sudo mv -f chromedriver-linux64/chromedriver /usr/bin/
            rm -rf chromedriver-linux64.zip chromedriver-linux64
            chromedriver --version
      - run:
          name: Install Java
          command: |
            sudo apt update
            sudo apt install -y openjdk-11-jre openjdk-11-jre-headless openjdk-11-jdk openjdk-11-jdk-headless
      - run:
          name: Run Selenium Standalone server
          command: |
            export DISPLAY=:99
            if [[ ! -f /home/circleci/selenium-server-4.4.0.jar ]]; then wget -O /home/circleci/selenium-server-4.4.0.jar https://github.com/SeleniumHQ/selenium/releases/download/selenium-4.4.0/selenium-server-4.4.0.jar; fi
            cd /home/circleci/
            java -jar selenium-server-4.4.0.jar standalone --port 4445
          background: true
      - run:
          name: Install composer ~2
          command: |
            sudo curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin/ --filename=composer --2;
            composer config -g github-oauth.github.com $GITHUB_TOKEN
      - run:
          name: Install Node.js and Yarn 4.9.3
          command: |
            curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
            sudo apt-get install -y nodejs
            rm -rf ~/.yarn
            mkdir -p ~/.yarn/releases
            curl -L https://repo.yarnpkg.com/4.9.3/packages/yarnpkg-cli/bin/yarn.js -o ~/.yarn/releases/yarn-4.9.3.cjs
            chmod +x ~/.yarn/releases/yarn-4.9.3.cjs
            echo "yarnPath: .yarn/releases/yarn-4.9.3.cjs" > ~/.yarnrc.yml
            sudo ln -sf ~/.yarn/releases/yarn-4.9.3.cjs /usr/local/bin/yarn
      - run:
          name: List tools versions
          command: |
            php --version
            java -version
            google-chrome --version
            chromedriver --version
            mysql --version
            composer --version
            node --version
            yarn --version
            php -m
            php -i | grep "Loaded Configuration File"
            php --ini
      - run:
          name: Build Varbase with the composer
          command: |
            if [ -d "/var/www/html/test" ]; then
              sudo rm -rf /var/www/html/test -vvv
            fi
            cd /var/www/
            sudo chmod 775 -R .
            sudo chown www-data:circleci -R .
            mkdir /var/www/html/test
            cd /var/www/html/test/
            sudo chmod 775 .
            sudo chown www-data:circleci .
            git clone --branch '10.1.x' https://github.com/vardot/varbase-project.git /var/www/html/test/varbase
            if [ -f "/var/www/html/test/varbase/composer.lock" ]; then
              rm /var/www/html/test/varbase/composer.lock
            fi
            cd /var/www/html/test/varbase
            composer require drupal/drupal-extension:~5 --no-update
            composer require webship/behat-html-formatter:~1.0 --no-update
            composer require drevops/behat-screenshot:~1.0 --no-update
            sudo chmod 775 -R .
            sudo chown www-data:circleci -R .
            composer install --no-interaction -vvv
            sudo chmod 775 -R .
            sudo chown www-data:circleci -R .
            composer drupal-libraries-sync
            cp /var/www/html/test/varbase/docroot/sites/default/default.settings.php /var/www/html/test/varbase/docroot/sites/default/settings.php
            echo "\$databases['default']['default'] = [
              'database' => 'test_varbase',
              'username' => 'root',
              'password' => 'rootpw',
              'host' => '127.0.0.1',
              'port' => '3306',
              'driver' => 'mysql',
              'prefix' => '',
              'collation' => 'utf8mb4_general_ci',
            ];" >> /var/www/html/test/varbase/docroot/sites/default/settings.php
            mkdir /var/www/html/test/varbase/config
            mkdir /var/www/html/test/varbase/config/sync
            echo "\$settings['config_sync_directory'] = '../config/sync';" >> /var/www/html/test/varbase/docroot/sites/default/settings.php
            cd /var/www/html/test/varbase
            sudo chmod 775 -R .
            sudo chown www-data:circleci -R .
      - run:
          name: Point varbase.test at /var/www/html/test/varbase/docroot
          command: |
            sudo a2enmod headers rewrite env mime expires ssl
            sudo cp /home/circleci/project/.circleci/env/varbase.test.conf /etc/apache2/sites-available/varbase.test.conf
            sudo a2ensite varbase.test
            echo 127.0.0.1 varbase.test | sudo tee -a /etc/hosts
            sudo service apache2 restart
      - run:
          name: Install Varbase with drush
          command: |
            mysql -h 127.0.0.1 -uroot -prootpw --execute="DROP DATABASE IF EXISTS test_varbase;" -vvv
            mysql -h 127.0.0.1 -uroot -prootpw --execute="CREATE DATABASE test_varbase;" -vvv
            cd /var/www/html/test/varbase/docroot
            ../bin/drush site-install varbase --yes --account-name="webmaster" --account-pass="dD.123123ddd" --account-mail="webmaster@vardot.com" --db-url="mysql://root:rootpw@127.0.0.1/test_varbase" --locale="en" varbase_multilingual_configuration.enable_multilingual=true varbase_extra_components.varbase_demo=true varbase_extra_components.editoria11y=true varbase_extra_components.vmi=true varbase_extra_components.varbase_heroslider=true varbase_extra_components.varbase_carousels=true varbase_extra_components.varbase_search=true varbase_extra_components.varbase_blog=true varbase_extra_components.varbase_auth=true varbase_extra_components.varbase_ai=true varbase_extra_components.varbase_api=true install_configure_form.enable_update_status_emails=NULL -vvv
            ../bin/drush pm:enable varbase_development --yes
            ../bin/drush pm:enable varbase_landing --yes
            ../bin/drush pm:enable varbase_content_planner --yes
            ../bin/drush pm:enable varbase_media_instagram --yes
            ../bin/drush pm:enable varbase_media_twitter --yes
            ../bin/drush pm:enable social_auth_facebook --yes
            ../bin/drush pm:enable social_auth_linkedin --yes
            ../bin/drush cache:rebuild
      - run:
          name: Add testing users.
          command: |
            cd /var/www/html/test/varbase/docroot/profiles/contrib/varbase/scripts
            bash add-testing-users.sh
      - run:
          name: Import and Update local RTL language
          shell: /bin/bash
          command: |
            cd /var/www/html/test/varbase/docroot
            ../bin/drush locale-import ar --autocreate-language profiles/contrib/varbase/translations/ar.po
            ../bin/drush locale-update --langcodes=ar
            ../bin/drush cr
      - run:
          name: Uninstall Antibot module to let the selenium bot work 
          shell: /bin/bash
          command: |
            cd /var/www/html/test/varbase/docroot
            ../bin/drush pm:uninstall antibot --yes
            ../bin/drush config:set system.performance css.preprocess 0 --yes
            ../bin/drush config:set system.performance js.preprocess 0 --yes
            ../bin/drush config:set system.logging error_level all --yes
            ../bin/drush cache:rebuild

jobs:
  varbase-testing-01-website-base-requirements:
    <<: *defaults
    steps:
      - varbase-build
      - run:
          name: Automated Functional Acceptance Testing group - 01 website base requirements
          no_output_timeout: 30m
          command: |
            cd /var/www/html/test/varbase/docroot/profiles/contrib/varbase
            ../../../../bin/behat tests/features/varbase/01-website-base-requirements/
  varbase-testing-02-user-management:
    <<: *defaults
    steps:
      - varbase-build
      - run:
          name: Automated Functional Acceptance Testing group - 02 user management
          no_output_timeout: 30m
          command: |
            cd /var/www/html/test/varbase/docroot/profiles/contrib/varbase
            ../../../../bin/behat tests/features/varbase/02-user-management/
  varbase-testing-03-admin-management:
    <<: *defaults
    steps:
      - varbase-build
      - run:
          name: Automated Functional Acceptance Testing group - 03 admin management
          no_output_timeout: 30m
          command: |
            cd /var/www/html/test/varbase/docroot/profiles/contrib/varbase
            ../../../../bin/behat tests/features/varbase/03-admin-management/
  varbase-testing-04-content-structure:
    <<: *defaults
    steps:
      - varbase-build
      - run:
          name: Automated Functional Acceptance Testing group - 04 content structure
          no_output_timeout: 30m
          command: |
            cd /var/www/html/test/varbase/docroot/profiles/contrib/varbase
            ../../../../bin/behat tests/features/varbase/04-content-structure/
  varbase-testing-05-content-management:
    <<: *defaults
    steps:
      - varbase-build
      - run:
          name: Automated Functional Acceptance Testing group - 05 content-management
          no_output_timeout: 30m
          command: |
            cd /var/www/html/test/varbase/docroot/profiles/contrib/varbase
            ../../../../bin/behat tests/features/varbase/05-content-management/

workflows:
  version: 2
  varbase-workflow:
    jobs:
      - varbase-testing-01-website-base-requirements:
          filters:
            tags:
              only: /^10.1.*/
            branches:
              only: /^10.1.x/
      - varbase-testing-02-user-management:
          filters:
            tags:
              only: /^10.1.*/
            branches:
              only: /^10.1.x/
      - varbase-testing-03-admin-management:
          filters:
            tags:
              only: /^10.1.*/
            branches:
              only: /^10.1.x/
      - varbase-testing-04-content-structure:
          filters:
            tags:
              only: /^10.1.*/
            branches:
              only: /^10.1.x/
      - varbase-testing-05-content-management:
          filters:
            tags:
              only: /^10.1.*/
            branches:
              only: /^10.1.x/
