#!/bin/bash
echo ">>> Running code sniffer"
$BITBUCKET_CLONE_DIR/vendor/bin/phpcs --config-set installed_paths $BITBUCKET_CLONE_DIR/vendor/drupal/coder/coder_sniffer
if [ -d $BITBUCKET_CLONE_DIR/web/modules/custom ] ; then
  $BITBUCKET_CLONE_DIR/vendor/bin/phpcs --standard=Drupal $BITBUCKET_CLONE_DIR/web/modules/custom
fi
if [ -d $BITBUCKET_CLONE_DIR/web/themes/custom ] ; then
  $BITBUCKET_CLONE_DIR/vendor/bin/phpcs --standard=Drupal $BITBUCKET_CLONE_DIR/web/themes/custom/*/*.theme
fi

# Allow adding more code sniffing in an ignored file that can be customized.
if [ -f $BITBUCKET_CLONE_DIR/scripts/pipelines/custom_code_sniffing.sh ] ; then
  sh $BITBUCKET_CLONE_DIR/scripts/pipelines/custom_code_sniffing.sh
fi

res=$?
if [ $res -ne 0 ] ; then
   return $res
fi

echo ">>> Running PHPUnit tests"
mkdir $BITBUCKET_CLONE_DIR/web/sites/default/files/simpletest
sudo chmod 777 -R $BITBUCKET_CLONE_DIR/web/sites/default/files/simpletest
sudo chown -R pipelineuser:pipelineuser $BITBUCKET_CLONE_DIR/web/sites/default/files/simpletest
sudo chmod 660 $BITBUCKET_CLONE_DIR/certificates/*.key
sudo chown www-data:www-data $BITBUCKET_CLONE_DIR/certificates/*.key
cp $BITBUCKET_CLONE_DIR/scripts/pipelines/config/phpunit.xml.dist $BITBUCKET_CLONE_DIR/web/core/phpunit.xml
sudo -u www-data -E $BITBUCKET_CLONE_DIR/vendor/bin/phpunit -c $BITBUCKET_CLONE_DIR/web/core/phpunit.xml web/modules/custom/ --group station --debug --verbose
