# This script is used by Travis CI to run automatically Continuous test integration
# from Dolibarr GitHub repository.
# For syntax, see https://docs.travis-ci.com/user/languages/php/

# We use dist: focal = 20.04
os: linux
dist: focal

language: generic

#scan_logs: false

git:
  depth: 1

# Start on every boot
services:
- memcached
#- mysql
- postgresql


addons:
  mariadb: '10.5'


env:
  global:
  # Set to true for very verbose output
  - DEBUG=true

cache:
  directories:
    - ~/.cache


jobs:
  fast_finish: true
  allow_failures:
  - php: '8.4'
  include:
    - stage: PHP min and max
      if: type = push
      php: '7.1'
      env:
      - DB=postgresql
      - TRAVIS_PHP_VERSION=7.1
    - stage: PHP min and max
      if: type = pull_request OR type = push
      php: '8.3'
      env:
      - DB=mysql
      - TRAVIS_PHP_VERSION=8.3
    - stage: PHP 8.4
      if: type = push AND branch = develop
      php: '8.4'
      env:
      - DB=mysql
      - TRAVIS_PHP_VERSION=8.4

notifications:
  email:
    on_success: never # [always|never|change] default: change
    on_failure: never # [always|never|change] default: always
  irc:
    channels:
    - "chat.freenode.net#dolibarr"
    on_success: change
    on_failure: always
    use_notice: true


before_install:
- |
  echo "Remove awful phpenv and install packages"
  sudo rm -fr ~/.phpenv
  sudo rm -fr /root/.phpenv
  sudo add-apt-repository -y ppa:ondrej/php
  sudo apt-get update
  echo TRAVIS_PHP_VERSION=$TRAVIS_PHP_VERSION

- |
  echo Install packages for PHP
  sudo apt-get install -y pgloader memcached
  if [ "$TRAVIS_PHP_VERSION" = '7.1' ]; then
  	sudo apt install unzip apache2 php7.1 php7.1-cli php7.1-curl php7.1-mysql php7.1-pgsql php7.1-gd php7.1-imap php7.1-intl php7.1-ldap php7.1-xml php7.1-mbstring php7.1-xml php7.1-zip libapache2-mod-php7.1
  fi
  if [ "$TRAVIS_PHP_VERSION" = '8.1' ]; then
  	sudo apt install unzip apache2 php8.1 php8.1-cli php8.1-curl php8.1-mysql php8.1-pgsql php8.1-gd php8.1-imap php8.1-intl php8.1-ldap php8.1-xml php8.1-mbstring php8.1-xml php8.1-zip libapache2-mod-php8.1
  fi
  if [ "$TRAVIS_PHP_VERSION" = '8.2' ]; then
  	sudo apt install unzip apache2 php8.2 php8.2-cli php8.2-curl php8.2-mysql php8.2-pgsql php8.2-gd php8.2-imap php8.2-intl php8.2-ldap php8.2-xml php8.2-mbstring php8.2-xml php8.2-zip libapache2-mod-php8.2
  fi
  if [ "$TRAVIS_PHP_VERSION" = '8.3' ]; then
  	sudo apt install unzip apache2 php8.3 php8.3-cli php8.3-curl php8.3-mysql php8.3-pgsql php8.3-gd php8.3-imap php8.3-intl php8.3-ldap php8.3-xml php8.3-mbstring php8.3-xml php8.3-zip libapache2-mod-php8.3
  fi
  if [ "$TRAVIS_PHP_VERSION" = '8.4' ]; then
    sudo apt install unzip apache2 php8.4 php8.4-cli php8.4-curl php8.4-mysql php8.4-pgsql php8.4-gd php8.4-imap php8.4-intl php8.4-ldap php8.4-xml php8.4-mbstring php8.4-xml php8.4-zip libapache2-mod-php8.4
  fi

- |
  echo Install pgsql if run is for pgsql
  if [ "$DB" = 'postgresql' ]; then
    echo "Check pgloader version"
    pgloader --version
    #ps fauxww | grep postgres
    ls /etc/postgresql/13/main/

    sudo sed -i -e '/local.*peer/s/postgres/all/' -e 's/peer\|md5/trust/g' /etc/postgresql/13/main/pg_hba.conf
    sudo cat /etc/postgresql/13/main/pg_hba.conf

    sudo service postgresql restart

    psql postgresql://postgres:postgres@127.0.0.1:5432 -l -A

    psql postgresql://postgres:postgres@127.0.0.1:5432 -c 'create database travis;'
    psql postgresql://postgres:postgres@127.0.0.1:5432 -c "CREATE USER travis WITH ENCRYPTED PASSWORD 'travis';"
    psql postgresql://postgres:postgres@127.0.0.1:5432 -c 'GRANT ALL PRIVILEGES ON DATABASE travis TO travis;'

    psql postgresql://postgres:postgres@127.0.0.1:5432 -l -A
  fi

install:
- |
  if [ "$TRAVIS_PHP_VERSION" = '7.1' ]; then
    sudo update-alternatives --set php /usr/bin/php7.1
  fi
  if [ "$TRAVIS_PHP_VERSION" = '8.1' ]; then
    sudo update-alternatives --set php /usr/bin/php8.1
  fi
  if [ "$TRAVIS_PHP_VERSION" = '8.2' ]; then
    sudo update-alternatives --set php /usr/bin/php8.2
  fi
  php -i | head -

- |
  echo "Updating Composer config"
  curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
  HASH=`curl -sS https://composer.github.io/installer.sig`
  echo $HASH
  php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
  sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
  sudo chmod -R a+rwx /usr/local/bin/composer

  #sudo apt install composer
  composer -V
  composer -n config -g vendor-dir htdocs/includes
  echo

- |
  echo "Update Composer version and Install tools - PHP Unit, Parallel Lint, PHP CodeSniffer, PHP Vardump check - for $TRAVIS_PHP_VERSION"
  echo "(composer version 2.5 is bugged and generate phpunit error Exception: Serialization of 'Closure' is not allowed)"
  if [ "$TRAVIS_PHP_VERSION" = '7.1' ] || [ "$TRAVIS_PHP_VERSION" = '7.2' ]; then
    sudo composer self-update 2.2.18
    composer -n require phpunit/phpunit ^7.5 \
                        php-parallel-lint/php-parallel-lint ^1 \
                        php-parallel-lint/php-console-highlighter ^0 \
                        php-parallel-lint/php-var-dump-check ~0.4 \
                        squizlabs/php_codesniffer ^3
  fi
  if [ "$TRAVIS_PHP_VERSION" = '7.3' ] || [ "$TRAVIS_PHP_VERSION" = '7.4' ]; then
    sudo composer self-update 2.2.18
    composer -n require phpunit/phpunit ^7.5 \
                        php-parallel-lint/php-parallel-lint ^1.2 \
                        php-parallel-lint/php-console-highlighter ^0 \
                        php-parallel-lint/php-var-dump-check ~0.4 \
                        squizlabs/php_codesniffer ^3
  fi
  # phpunit 9 is required for php 8
  if [ "$TRAVIS_PHP_VERSION" = '8.0' ] || [ "$TRAVIS_PHP_VERSION" = '8.1' ] || [ "$TRAVIS_PHP_VERSION" = '8.2' ] || [ "$TRAVIS_PHP_VERSION" = '8.3' ] || [ "$TRAVIS_PHP_VERSION" = '8.4' ] || [ "$TRAVIS_PHP_VERSION" = 'nightly' ]; then
      sudo composer self-update 2.4.4
      composer -n require --ignore-platform-reqs phpunit/phpunit ^8 \
                                                 php-parallel-lint/php-parallel-lint ^1.2 \
                                                 php-parallel-lint/php-console-highlighter ^0 \
                                                 php-parallel-lint/php-var-dump-check ~0.4 \
                                                 squizlabs/php_codesniffer ^3
  fi

  # Remove non expected files
  rm -fr /home/travis/build/Dolibarr/dolibarr/htdocs/includes/phpunit/php-code-coverage/src/Report/Html/Renderer/Template/js
  echo

- |
  echo "Adding path of binaries tools installed by composer to the PATH"
  export PATH="$TRAVIS_BUILD_DIR/htdocs/includes/bin:$PATH"
  echo $PATH
  ls $TRAVIS_BUILD_DIR/htdocs/includes
  ls $TRAVIS_BUILD_DIR/htdocs/includes/bin
  echo



before_script:
  - |
    echo Start before_script
    echo Current dir is `pwd`
    echo Home dir is `echo ~`
    echo TRAVIS_BUILD_DIR is $TRAVIS_BUILD_DIR

  - |
    echo "Versions information"
    echo
    # Check PHP
    echo "PHP version"
    php -i | head -
    # Check Parallel-lint version
    echo "Parallel-lint version"
    which parallel-lint
    parallel-lint -V
    # Check PHP CodeSniffer version
    echo "PHPCS version"
    which phpcs
    phpcs --version | head -
    phpcs -i | head -
    # Check PHP Vardump check version
    echo "PHP Vardump check version"
    which var_dump_check
    var_dump_check --version
    # Check PHPUnit version
    echo "PHPUnit version"
    which phpunit
    phpunit --version | head -
    # Check Apache version
    echo "Apache version"
    apache2 -v | head -
    # Check Database
    echo "Database version"
    mysql --version | head -
    psql --version

  - |
    echo "Setting up database"
    if [ "$DB" = 'mysql' ] || [ "$DB" = 'mariadb' ] || [ "$DB" = 'postgresql' ]; then
      echo "MySQL stop"
      sudo systemctl stop mariadb.service
      echo "MySQL restart without pass"
      #sudo mysqld_safe --skip-grant-tables --socket=/tmp/aaa
      sudo mysqld_safe --skip-grant-tables --socket=/tmp/aaa &
      sleep 3
      sudo ps fauxww
      echo "MySQL set root password"
      sudo mysql -u root -h 127.0.0.1 -e "FLUSH PRIVILEGES; CREATE DATABASE IF NOT EXISTS travis CHARACTER SET = 'utf8'; ALTER USER 'root'@'localhost' IDENTIFIED BY 'password'; CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY 'password'; CREATE USER 'travis'@'127.0.0.1' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON travis.* TO root@127.0.0.1; GRANT ALL PRIVILEGES ON travis.* TO travis@127.0.0.1; FLUSH PRIVILEGES;"
      echo "MySQL grant"
      sudo mysql -u root -h 127.0.0.1 -ppassword -e 'FLUSH PRIVILEGES; GRANT ALL PRIVILEGES ON travis.* TO travis@127.0.0.1; FLUSH PRIVILEGES;'
      echo "MySQL list current users"
      sudo mysql -u root -h 127.0.0.1 -ppassword -e 'use mysql; select * from user;'
      echo "List pid file"
      sudo mysql -u root -h 127.0.0.1 -ppassword -e "show variables like '%pid%';"

      #sudo kill `cat /var/lib/mysqld/mysqld.pid`
      #sudo systemctl start mariadb

      echo "MySQL grant"
      sudo mysql -u root -h 127.0.0.1 -ppassword -e 'GRANT ALL PRIVILEGES ON travis.* TO travis@127.0.0.1;'
      echo "MySQL flush"
      sudo mysql -u root -h 127.0.0.1 -ppassword -e 'FLUSH PRIVILEGES;'

      echo "MySQL load sql"
      sudo mysql -u root -h 127.0.0.1 -ppassword -D travis < dev/initdemo/mysqldump_dolibarr_3.5.0.sql
    fi
    if [ "$DB" = 'postgresql' ]; then
      echo "PGSQL"
      sudo mkdir -p /tmp/pgloader
      sudo chmod -R a+rwx /tmp/pgloader/
      sudo pgloader 'mysql://root:password@127.0.0.1/travis' 'postgresql://postgres:postgres@127.0.0.1:5432/travis'
      echo 'ALTER SEQUENCE llx_accountingaccount_rowid_seq RENAME TO llx_accounting_account_rowid_seq' | psql 'postgresql://postgres:postgres@127.0.0.1:5432/travis'
      echo 'ALTER SEQUENCE llx_accounting_account_rowid_seq RESTART WITH 1000001;' | psql 'postgresql://postgres:postgres@127.0.0.1:5432/travis'

      #echo '\d llx_adherent' | psql 'postgresql://postgres:postgres@127.0.0.1:5432/travis'
      #echo '\d llx_c_country' | psql 'postgresql://postgres:postgres@127.0.0.1:5432/travis'

      # Create pgsql compatibility functions
      psql 'postgresql://postgres:postgres@127.0.0.1:5432/travis' < htdocs/install/pgsql/functions/functions.sql
    fi
    echo

  - |
    export CONF_FILE=htdocs/conf/conf.php
    echo "Setting up Dolibarr '$CONF_FILE'"
    echo '<?php' > $CONF_FILE
    echo 'error_reporting(E_ALL);' >> $CONF_FILE
    echo '$'dolibarr_main_url_root=\'http://127.0.0.1\'';' >> $CONF_FILE
    echo '$'dolibarr_main_document_root=\'$TRAVIS_BUILD_DIR/htdocs\'';' >> $CONF_FILE
    echo '$'dolibarr_main_data_root=\'$TRAVIS_BUILD_DIR/documents\'';' >> $CONF_FILE
    echo '$'dolibarr_main_db_host=\'127.0.0.1\'';' >> $CONF_FILE
    echo '$'dolibarr_main_db_name=\'travis\'';' >> $CONF_FILE
    echo '$'dolibarr_main_instance_unique_id=\'travis1234567890\'';' >> $CONF_FILE
    if [ "$DB" = 'mysql' ] || [ "$DB" = 'mariadb' ]; then
      echo '$'dolibarr_main_db_type=\'mysqli\'';' >> $CONF_FILE
      echo '$'dolibarr_main_db_port=3306';' >> $CONF_FILE
      echo '$'dolibarr_main_db_user=\'root\'';' >> $CONF_FILE
      echo '$'dolibarr_main_db_pass=\'password\'';' >> $CONF_FILE
    fi
    if [ "$DB" = 'postgresql' ]; then
      echo '$'dolibarr_main_db_type=\'pgsql\'';' >> $CONF_FILE
      echo '$'dolibarr_main_db_port=5432';' >> $CONF_FILE
      echo '$'dolibarr_main_db_user=\'postgres\'';' >> $CONF_FILE
      echo '$'dolibarr_main_db_pass=\'postgres\'';' >> $CONF_FILE
    fi
    echo '$'dolibarr_main_authentication=\'dolibarr\'';' >> $CONF_FILE
    cat $CONF_FILE
    echo

  - |
    echo "Create documents directory and set permissions"
    # Add admin/temp subdirectory needed for unit tests
    mkdir -p $TRAVIS_BUILD_DIR/documents/admin/temp
    sudo chmod -R a+rwx $TRAVIS_BUILD_DIR/documents
    echo "***** First line of dolibarr.log" > $TRAVIS_BUILD_DIR/documents/dolibarr.log
    echo


  - |
    echo "Setting up Apache"
    sudo sed -i -e "s,www-data,travis,g" /etc/apache2/envvars
    sudo cp -f build/travis-ci/apache.conf /etc/apache2/sites-available/000-default.conf
    sudo sed -e "s?%TRAVIS_BUILD_DIR%?$(pwd)?g" --in-place /etc/apache2/sites-available/000-default.conf
    sudo cat /etc/apache2/sites-available/000-default.conf
    sudo service apache2 restart


script:
  - |
    echo "Checking webserver availability by a wget -O - --debug http://127.0.0.1"
    # Ensure we stop on error with set -e
    set +e
    # The wget should return a page with line '<meta name="generator" content="Dolibarr installer">
    wget -O - --debug http://127.0.0.1 > test.html
    head -n 200 test.html
    sudo cat /var/log/apache2/travis_error_log
    set +e
    echo

  - |
    echo "Checking PHP syntax errors (only 1 version to not overload travis and avoid duplicate tests)"
    # Ensure we catch errors
    set -e
    # Exclusions are defined in the ruleset.xml file
    if [ "$TRAVIS_PHP_VERSION" = "8.1" ]; then
      parallel-lint -e php --exclude dev/tools/test/namespacemig --exclude htdocs/includes/composer --exclude htdocs/includes/myclabs --exclude htdocs/includes/phpspec --exclude dev/initdata/dbf/includes \
        --exclude htdocs/includes/sabre --exclude htdocs/includes/phpoffice/PhpSpreadsheet --exclude htdocs/includes/sebastian \
        --exclude htdocs/includes/squizlabs/php_codesniffer --exclude htdocs/includes/jakub-onderka --exclude htdocs/includes/php-parallel-lint --exclude htdocs/includes/symfony \
        --exclude htdocs/includes/mike42/escpos-php/example --exclude htdocs/includes/maximebf \
        --exclude htdocs/includes/phpunit/ --exclude htdocs/includes/tecnickcom/tcpdf/include/barcodes --exclude htdocs/includes/webmozart --exclude htdocs/includes/webklex --blame .
    fi
    if [ "$TRAVIS_PHP_VERSION" = "8.2" ]; then
      parallel-lint -e php --exclude dev/tools/test/namespacemig --exclude htdocs/includes/composer --exclude htdocs/includes/myclabs --exclude htdocs/includes/phpspec --exclude dev/initdata/dbf/includes \
        --exclude htdocs/includes/sabre --exclude htdocs/includes/phpoffice/PhpSpreadsheet --exclude htdocs/includes/sebastian \
        --exclude htdocs/includes/squizlabs/php_codesniffer --exclude htdocs/includes/jakub-onderka --exclude htdocs/includes/php-parallel-lint --exclude htdocs/includes/symfony \
        --exclude htdocs/includes/mike42/escpos-php/example --exclude htdocs/includes/maximebf \
        --exclude htdocs/includes/phpunit/ --exclude htdocs/includes/tecnickcom/tcpdf/include/barcodes --exclude htdocs/includes/webmozart --exclude htdocs/includes/webklex --blame .
    fi
    if [ "$TRAVIS_PHP_VERSION" = "8.3" ]; then
      parallel-lint -e php --exclude dev/tools/test/namespacemig --exclude htdocs/includes/composer --exclude htdocs/includes/myclabs --exclude htdocs/includes/phpspec --exclude dev/initdata/dbf/includes \
        --exclude htdocs/includes/sabre --exclude htdocs/includes/phpoffice/PhpSpreadsheet --exclude htdocs/includes/sebastian \
        --exclude htdocs/includes/squizlabs/php_codesniffer --exclude htdocs/includes/jakub-onderka --exclude htdocs/includes/php-parallel-lint --exclude htdocs/includes/symfony \
        --exclude htdocs/includes/mike42/escpos-php/example --exclude htdocs/includes/maximebf \
        --exclude htdocs/includes/phpunit/ --exclude htdocs/includes/tecnickcom/tcpdf/include/barcodes --exclude htdocs/includes/webmozart --exclude htdocs/includes/webklex --blame .
    fi
    if [ "$TRAVIS_PHP_VERSION" = "8.4" ]; then
      parallel-lint -e php --exclude dev/tools/test/namespacemig --exclude htdocs/includes/composer --exclude htdocs/includes/myclabs --exclude htdocs/includes/phpspec --exclude dev/initdata/dbf/includes \
        --exclude htdocs/includes/sabre --exclude htdocs/includes/phpoffice/PhpSpreadsheet --exclude htdocs/includes/sebastian \
        --exclude htdocs/includes/squizlabs/php_codesniffer --exclude htdocs/includes/jakub-onderka --exclude htdocs/includes/php-parallel-lint --exclude htdocs/includes/symfony \
        --exclude htdocs/includes/mike42/escpos-php/example --exclude htdocs/includes/maximebf \
        --exclude htdocs/includes/phpunit/ --exclude htdocs/includes/tecnickcom/tcpdf/include/barcodes --exclude htdocs/includes/webmozart --exclude htdocs/includes/webklex --blame .
    fi
    set +e
    echo

  - |
    echo "Checking coding style (only for 1 version to not overload travis and avoid duplicate tests)"
    # Ensure we catch errors
    set -e
    CACHE_OPT="--cache=${HOME}/.cache/dolibarr-phpcs-${TRAVIS_PHP_VERSION}.cache"
    # Exclusions are defined in the ruleset.xml file
    #if [ "$TRAVIS_PULL_REQUEST" = "false" ] && [ "$TRAVIS_PHP_VERSION" = "8.1" ]; then
    if [ "$TRAVIS_PHP_VERSION" = "8.1" ]; then
      phpcs -s -p -d memory_limit=-1 --extensions=php --colors --tab-width=4 --standard=dev/setup/codesniffer/ruleset.xml --encoding=utf-8 --severity=1 ${CACHE_OPT} --runtime-set ignore_warnings_on_exit true .;
    fi
    if [ "$TRAVIS_PHP_VERSION" = "8.2" ]; then
      phpcs -s -p -d memory_limit=-1 --extensions=php --colors --tab-width=4 --standard=dev/setup/codesniffer/ruleset.xml --encoding=utf-8 --severity=1 ${CACHE_OPT} --runtime-set ignore_warnings_on_exit true .;
    fi
    if [ "$TRAVIS_PHP_VERSION" = "8.3" ]; then
      phpcs -s -p -d memory_limit=-1 --extensions=php --colors --tab-width=4 --standard=dev/setup/codesniffer/ruleset.xml --encoding=utf-8 --severity=1 ${CACHE_OPT} --runtime-set ignore_warnings_on_exit true .;
    fi
    if [ "$TRAVIS_PHP_VERSION" = "8.4" ]; then
      phpcs -s -p -d memory_limit=-1 --extensions=php --colors --tab-width=4 --standard=dev/setup/codesniffer/ruleset.xml --encoding=utf-8 --severity=1 ${CACHE_OPT} --runtime-set ignore_warnings_on_exit true .;
    fi
    set +e
    echo

  - |
    echo "Checking missing debug (only for 1 version to not overload travis and avoid duplicate tests)"
    # Ensure we catch errors
    set -e
    # Exclusions are defined in the ruleset.xml file
    #if [ "$TRAVIS_PULL_REQUEST" = "false" ] && [ "$TRAVIS_PHP_VERSION" = "8.1" ]; then
    if [ "$TRAVIS_PHP_VERSION" = "8.1" ]; then
       var-dump-check --extensions php --tracy --exclude htdocs/includes --exclude test/ --exclude htdocs/public/test/ --exclude htdocs/core/lib/functions.lib.php .
    fi
    if [ "$TRAVIS_PHP_VERSION" = "8.2" ]; then
       var-dump-check --extensions php --tracy --exclude htdocs/includes --exclude test/ --exclude htdocs/public/test/ --exclude htdocs/core/lib/functions.lib.php .
    fi
    if [ "$TRAVIS_PHP_VERSION" = "8.3" ]; then
       var-dump-check --extensions php --tracy --exclude htdocs/includes --exclude test/ --exclude htdocs/public/test/ --exclude htdocs/core/lib/functions.lib.php .
    fi
    if [ "$TRAVIS_PHP_VERSION" = "8.4" ]; then
       var-dump-check --extensions php --tracy --exclude htdocs/includes --exclude test/ --exclude htdocs/public/test/ --exclude htdocs/core/lib/functions.lib.php .
    fi
    set +e
    echo

  - |
    export INSTALL_FORCED_FILE=htdocs/install/install.forced.php
    echo "Setting up Dolibarr $INSTALL_FORCED_FILE to test installation"
    # Ensure we catch errors
    set +e
    echo '<?php ' > $INSTALL_FORCED_FILE
    echo 'error_reporting(E_ALL);' >> $INSTALL_FORCED_FILE
    echo '$'force_install_noedit=2';' >> $INSTALL_FORCED_FILE
    if [ "$DB" = 'mysql' ] || [ "$DB" = 'mariadb' ]; then
      echo '$'force_install_type=\'mysqli\'';' >> $INSTALL_FORCED_FILE
      echo '$'force_install_port=3306';' >> $INSTALL_FORCED_FILE
    fi
    if [ "$DB" = 'postgresql' ]; then
      echo '$'force_install_type=\'pgsql\'';' >> $INSTALL_FORCED_FILE
      echo '$'force_install_port=5432';' >> $INSTALL_FORCED_FILE
    fi
    echo '$'force_install_dbserver=\'127.0.0.1\'';' >> $INSTALL_FORCED_FILE
    echo '$'force_install_database=\'travis\'';' >> $INSTALL_FORCED_FILE
    echo '$'force_install_databaselogin=\'travis\'';' >> $INSTALL_FORCED_FILE
    echo '$'force_install_databasepass=\'\'';' >> $INSTALL_FORCED_FILE
    echo '$'force_install_prefix=\'llx_\'';' >> $INSTALL_FORCED_FILE
    echo '$'force_install_createdatabase=false';' >> $INSTALL_FORCED_FILE
    echo '$'force_install_createuser=false';' >> $INSTALL_FORCED_FILE
    echo '$'force_install_mainforcehttps=false';' >> $INSTALL_FORCED_FILE
    echo '$'force_install_main_data_root=\'$TRAVIS_BUILD_DIR/htdocs\'';' >> $INSTALL_FORCED_FILE
    #cat $INSTALL_FORCED_FILE
    echo

  - |
    echo "Upgrading Dolibarr"
    # Ensure we catch errors with -e. Set this to +e if you want to go to the end to see log files.
    # Note: We keep +e because with pgsql, one of upgrade process fails even if migration seems ok, so
    # I disable stop on error to be able to continue.
    set +e
    cd htdocs/install
    php upgrade.php 3.5.0 3.6.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade350360.log
    php upgrade2.php 3.5.0 3.6.0 > $TRAVIS_BUILD_DIR/upgrade350360-2.log
    php step5.php 3.5.0 3.6.0 > $TRAVIS_BUILD_DIR/upgrade350360-3.log
    php upgrade.php 3.6.0 3.7.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade360370.log
    php upgrade2.php 3.6.0 3.7.0 > $TRAVIS_BUILD_DIR/upgrade360370-2.log
    php step5.php 3.6.0 3.7.0 > $TRAVIS_BUILD_DIR/upgrade360370-3.log
    php upgrade.php 3.7.0 3.8.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade370380.log
    php upgrade2.php 3.7.0 3.8.0 > $TRAVIS_BUILD_DIR/upgrade370380-2.log
    php step5.php 3.7.0 3.8.0 > $TRAVIS_BUILD_DIR/upgrade370380-3.log
    php upgrade.php 3.8.0 3.9.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade380390.log
    php upgrade2.php 3.8.0 3.9.0 > $TRAVIS_BUILD_DIR/upgrade380390-2.log
    php step5.php 3.8.0 3.9.0 > $TRAVIS_BUILD_DIR/upgrade380390-3.log
    php upgrade.php 3.9.0 4.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade390400.log
    php upgrade2.php 3.9.0 4.0.0 > $TRAVIS_BUILD_DIR/upgrade390400-2.log
    php step5.php 3.9.0 4.0.0 > $TRAVIS_BUILD_DIR/upgrade390400-3.log
    php upgrade.php 4.0.0 5.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade400500.log
    php upgrade2.php 4.0.0 5.0.0 > $TRAVIS_BUILD_DIR/upgrade400500-2.log
    php step5.php 4.0.0 5.0.0 > $TRAVIS_BUILD_DIR/upgrade400500-3.log
    php upgrade.php 5.0.0 6.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade500600.log
    php upgrade2.php 5.0.0 6.0.0 > $TRAVIS_BUILD_DIR/upgrade500600-2.log
    php step5.php 5.0.0 6.0.0 > $TRAVIS_BUILD_DIR/upgrade500600-3.log
    php upgrade.php 6.0.0 7.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade600700.log
    php upgrade2.php 6.0.0 7.0.0 > $TRAVIS_BUILD_DIR/upgrade600700-2.log
    php step5.php 6.0.0 7.0.0 > $TRAVIS_BUILD_DIR/upgrade600700-3.log
    php upgrade.php 7.0.0 8.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade700800.log
    php upgrade2.php 7.0.0 8.0.0 > $TRAVIS_BUILD_DIR/upgrade700800-2.log
    php step5.php 7.0.0 8.0.0 > $TRAVIS_BUILD_DIR/upgrade700800-3.log
    php upgrade.php 8.0.0 9.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade800900.log
    php upgrade2.php 8.0.0 9.0.0 > $TRAVIS_BUILD_DIR/upgrade800900-2.log
    php step5.php 8.0.0 9.0.0 > $TRAVIS_BUILD_DIR/upgrade800900-3.log
    php upgrade.php 9.0.0 10.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade9001000.log
    php upgrade2.php 9.0.0 10.0.0 > $TRAVIS_BUILD_DIR/upgrade9001000-2.log
    php step5.php 9.0.0 10.0.0 > $TRAVIS_BUILD_DIR/upgrade9001000-3.log
    php upgrade.php 10.0.0 11.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade10001100.log
    php upgrade2.php 10.0.0 11.0.0 > $TRAVIS_BUILD_DIR/upgrade10001100-2.log
    php step5.php 10.0.0 11.0.0 > $TRAVIS_BUILD_DIR/upgrade10001100-3.log
    php upgrade.php 11.0.0 12.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade11001200.log
    php upgrade2.php 11.0.0 12.0.0 > $TRAVIS_BUILD_DIR/upgrade11001200-2.log
    php step5.php 11.0.0 12.0.0 > $TRAVIS_BUILD_DIR/upgrade11001200-3.log
    php upgrade.php 12.0.0 13.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade12001300.log
    php upgrade2.php 12.0.0 13.0.0 > $TRAVIS_BUILD_DIR/upgrade12001300-2.log
    php step5.php 12.0.0 13.0.0 > $TRAVIS_BUILD_DIR/upgrade12001300-3.log
    php upgrade.php 13.0.0 14.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade13001400.log
    php upgrade2.php 13.0.0 14.0.0 > $TRAVIS_BUILD_DIR/upgrade13001400-2.log
    php step5.php 13.0.0 14.0.0 > $TRAVIS_BUILD_DIR/upgrade13001400-3.log
    php upgrade.php 14.0.0 15.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade14001500.log
    php upgrade2.php 14.0.0 15.0.0 > $TRAVIS_BUILD_DIR/upgrade14001500-2.log
    php step5.php 14.0.0 15.0.0 > $TRAVIS_BUILD_DIR/upgrade14001500-3.log
    php upgrade.php 15.0.0 16.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade15001600.log
    php upgrade2.php 15.0.0 16.0.0 > $TRAVIS_BUILD_DIR/upgrade15001600-2.log
    php step5.php 15.0.0 16.0.0 > $TRAVIS_BUILD_DIR/upgrade15001600-3.log
    php upgrade.php 16.0.0 17.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade16001700.log
    php upgrade2.php 16.0.0 17.0.0 > $TRAVIS_BUILD_DIR/upgrade16001700-2.log
    php step5.php 16.0.0 17.0.0 > $TRAVIS_BUILD_DIR/upgrade16001700-3.log
    php upgrade.php 17.0.0 18.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade17001800.log
    php upgrade2.php 17.0.0 18.0.0 > $TRAVIS_BUILD_DIR/upgrade17001800-2.log
    php step5.php 17.0.0 18.0.0 > $TRAVIS_BUILD_DIR/upgrade17001800-3.log
    php upgrade.php 18.0.0 19.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade18001900.log || cat $TRAVIS_BUILD_DIR/upgrade18001900.log
    php upgrade2.php 18.0.0 19.0.0 > $TRAVIS_BUILD_DIR/upgrade18001900-2.log || cat $TRAVIS_BUILD_DIR/upgrade18001900-2.log
    php step5.php 18.0.0 19.0.0 > $TRAVIS_BUILD_DIR/upgrade18001900-3.log || cat $TRAVIS_BUILD_DIR/upgrade18001900-3.log
    php upgrade.php 19.0.0 20.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade19002000.log || cat $TRAVIS_BUILD_DIR/upgrade19002000.log
    php upgrade2.php 19.0.0 20.0.0 > $TRAVIS_BUILD_DIR/upgrade19002000-2.log || cat $TRAVIS_BUILD_DIR/upgrade19002000-2.log
    php step5.php 19.0.0 20.0.0 > $TRAVIS_BUILD_DIR/upgrade19002000-3.log || cat $TRAVIS_BUILD_DIR/upgrade19002000-3.log
    php upgrade.php 20.0.0 21.0.0 ignoredbversion > $TRAVIS_BUILD_DIR/upgrade20002100.log || cat $TRAVIS_BUILD_DIR/upgrade20002100.log
    php upgrade2.php 20.0.0 21.0.0 > $TRAVIS_BUILD_DIR/upgrade20002100-2.log || cat $TRAVIS_BUILD_DIR/upgrade20002100-2.log
    php step5.php 20.0.0 21.0.0 > $TRAVIS_BUILD_DIR/upgrade20002100-3.log || cat $TRAVIS_BUILD_DIR/upgrade20002100-3.log
    set +e
    echo

  - |
    echo Show some debug info like table content or log
    #echo '\d llx_adherent' | psql 'postgresql://postgres:postgres@127.0.0.1:5432/travis'
    cat $TRAVIS_BUILD_DIR/upgrade18001900.log
    cat $TRAVIS_BUILD_DIR/upgrade19002000.log
    echo

  - |
    echo Result of migration scripts
    ls -alrt $TRAVIS_BUILD_DIR/

    echo Show content of last file
    cat $TRAVIS_BUILD_DIR/"$(ls -rt $TRAVIS_BUILD_DIR/ | tail -n1)"

  - |
    echo "Enabling new modules"
    # Enable modules not enabled into original dump
    set -e
    php upgrade2.php 0.0.0 0.0.0 MAIN_MODULE_API,MAIN_MODULE_ProductBatch,MAIN_MODULE_SupplierProposal,MAIN_MODULE_STRIPE,MAIN_MODULE_ExpenseReport > $TRAVIS_BUILD_DIR/enablemodule.log
    php upgrade2.php 0.0.0 0.0.0 MAIN_MODULE_WEBSITE,MAIN_MODULE_TICKET,MAIN_MODULE_ACCOUNTING,MAIN_MODULE_MRP >> $TRAVIS_BUILD_DIR/enablemodule.log
    php upgrade2.php 0.0.0 0.0.0 MAIN_MODULE_RECEPTION,MAIN_MODULE_RECRUITMENT >> $TRAVIS_BUILD_DIR/enablemodule.log
    php upgrade2.php 0.0.0 0.0.0 MAIN_MODULE_KnowledgeManagement,MAIN_MODULE_EventOrganization,MAIN_MODULE_PARTNERSHIP >> $TRAVIS_BUILD_DIR/enablemodule.log
    php upgrade2.php 0.0.0 0.0.0 MAIN_MODULE_EmailCollector >> $TRAVIS_BUILD_DIR/enablemodule.log
    echo $?
    cd -
    set +e
    echo
    #cat /tmp/dolibarr_install.log
    cat $TRAVIS_BUILD_DIR/enablemodule.log
    echo

  - |
    echo "Unit testing"
    # Execute phpunit, check its exit status and that the phpunit output shows a test summary
    phpunit -d memory_limit=-1 -c test/phpunit/phpunittest.xml test/phpunit/AllTests.php | tee /dev/tty | grep -qE "(OK .*[0-9]+ tests.*[0-9]+ assertions|Tests: [0-9]+)" ; phpunitresult=$((PIPESTATUS[0]?PIPESTATUS[0]:PIPESTATUS[2]))
    echo "Phpunit return code = $phpunitresult"
    # Comment next line if you want to go to the end to see the dolibarr.log file.
    [ $phpunitresult == 0 ] || exit $phpunitresult
    echo

after_script:
  - |
    echo "After script - Output last lines of dolibarr.log"
    ls $TRAVIS_BUILD_DIR/documents
    #cat $TRAVIS_BUILD_DIR/documents/dolibarr.log
    sudo tail -n 50 $TRAVIS_BUILD_DIR/documents/dolibarr.log
    echo "After script - Output last lines of apache error.log"
    sudo ls /var/log/apache2
    sudo tail -n 200 /var/log/apache2/travis_error_log

after_success:
  - |
    echo Success

after_failure:
  - |
    echo Failure detected, so we show samples of log to help diagnose
    # This part of code is executed only if the command that fails are enclosed with set +e
    # Show upgrade log files
    #for ficlog in `ls $TRAVIS_BUILD_DIR/*.log`
    #do
      #echo "Debugging information for file $ficlog"
      #cat $ficlog
    #done
    # Show Apache log file
    echo "Debugging information for file apache error.log"
    sudo tail -n 200 /var/log/apache2/travis_error_log
    if [ "$DEBUG" = true ]; then
      # Dolibarr log file
      echo "Debugging information for file dolibarr.log (latest 50 lines)"
      tail -n 200 $TRAVIS_BUILD_DIR/documents/dolibarr.log
      # Database log file
      echo "Debugging information for file mysql error.log"
      sudo tail -n 200 /var/log/mysql/error.log
      # TODO: PostgreSQL log file
      echo
    fi
