Merge pull request #3339 from yashodha/CRM-14664
[civicrm-core.git] / tools / scripts / mk-drupal-test-site
index 0624cf8b192ceec252ffc332d797291e30cfc326..d567c371573df67b437afbd623c45ffdd4fd4dad 100755 (executable)
@@ -5,6 +5,9 @@ set -ex
 ## If a site already exists, destroy and recreate it.
 ## Usage: mk-drupal-test-site <domain.name> <db_name> </path/to/drupal> </path/to/civi>
 
+## SOURCE:
+## https://github.com/civicrm/civicrm-core/blob/master/tools/scripts/mk-drupal-test-site
+
 ## Pre-requisites:
 ## - MySQL admin credentials in ~/.my.cnf
 ## - Apache vhost with mod_rewrite, etc
@@ -15,6 +18,34 @@ set -ex
 ## - drush
 ## - (strongly recommended) filesystem with "acl" support
 
+
+function usage() {
+  cat <<EOT
+Usage: mk-drupal-test-site SITE_URL DB_NAME DRUPAL_ROOT [CIVI_ROOT]
+Re/creates a Drupal-based CiviCRM site.
+
+  * SITE_URL: URL of the site. Example: example.org
+  * DB_NAME: MySQL database name. Example: civicrm_test
+  * DRUPAL_ROOT: Root path of the Drupal website. Example: /var/www/
+  * CIVI_ROOT: Root path of the CiviCRM directory. Will be symlinked.
+    You probably want to have a separate version somewhere to avoid
+    cloning a new version of the code base for each CMS.
+    The CiviCRM directory can either contain the main git repositories,
+    or an equivalent of the tar.gz archive.
+    Example: /srv/repositories/civicrm/
+  * SQL_DUMP (optional): instead of a standard blank install, import a
+    specific .sql dump. You can specify two .sql files.
+
+EOT
+
+  exit 99;
+}
+
+[ "$1" = "--help" ] && usage
+[ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] && usage
+[ ! -d "$3" ]                             && echo "ERROR: $3: Drupal root directory not found." && usage
+[ ! -d "$4" ]                             && echo "ERROR: $4: CiviCRM root directory not found." && usage
+
 SITE_URL="$1"
 DB_NAME="$2"
 DB_USER="$DB_NAME"
@@ -23,19 +54,24 @@ DB_HOST=localhost
 DRUPAL_ROOT="$3"
 CIVI_ROOT="$4"
 FACL_USERS="www-data $(whoami)"
+SQL_DUMP="$5"
+SQL_DUMP2="$6"
 
 SITE_KEY=$(makepasswd --chars=16)
 ADMIN_USER="admin"
 ADMIN_PASS=$(makepasswd --chars=12)
 
+# Check if the CiviCRM directory looks OK
 if [ -z "$CIVI_ROOT" -o ! -d "$CIVI_ROOT/bin" ]; then
   echo "Failed to locate civi root: $CIVI_ROOT"
   exit 1
 fi
 
-if [ -z "$DB_NAME" ]; then
-  echo "Missing database name"
-  exit 2
+if [ -n "$SQL_DUMP" ]; then
+  if [ ! -f "$SQL_DUMP" ]; then
+    echo "$SQL_DUMP: Could not find the .sql file. Try using an absolute path to the file."
+    exit 3;
+  fi
 fi
 
 ## Create database
@@ -50,6 +86,9 @@ if [ -d "sites/$SITE_URL" ]; then
   chmod u+w "sites/$SITE_URL"
   rm -rf "sites/$SITE_URL"
 fi
+
+# NB: Avoid sending e-mails for the site installation
+# On hosts without sendmail (ex: demo sites), this causes the installation to fail.
 drush site-install -y \
   --db-url="mysql://${DB_USER}:${DB_PASS}@${DB_HOST}/${DB_NAME}" \
   --account-name="$ADMIN_USER" \
@@ -71,9 +110,9 @@ for SUBDIR in modules files files/civicrm files/civicrm/templates_c ; do
   fi
 done
 
-ln -s "$CIVI_ROOT" "sites/$SITE_URL/modules/"
+ln -s "$CIVI_ROOT" "sites/$SITE_URL/modules/civicrm"
 
-cat "$CIVI_ROOT/templates/CRM/common/civicrm.settings.php.tpl" \
+cat "$CIVI_ROOT/templates/CRM/common/civicrm.settings.php.template" \
   | sed "s;%%baseURL%%;http://${SITE_URL};" \
   | sed "s;%%cms%%;Drupal;" \
   | sed "s;%%CMSdbHost%%;${DB_HOST};" \
@@ -89,18 +128,21 @@ cat "$CIVI_ROOT/templates/CRM/common/civicrm.settings.php.tpl" \
   | sed "s;%%templateCompileDir%%;${DRUPAL_ROOT}/sites/${SITE_URL}/files/civicrm/templates_c;" \
   > "sites/$SITE_URL/civicrm.settings.php"
 
+echo  >> "sites/$SITE_URL/civicrm.settings.php"
+echo "define('CIVICRM_MAIL_LOG', '/dev/null');" >> "sites/$SITE_URL/civicrm.settings.php"
 popd
 
 ## Create CiviCRM config
 cat > "$CIVI_ROOT/bin/setup.conf" << EOF
   SVNROOT="$CIVI_ROOT"
+  CIVISOURCEDIR="$CIVI_ROOT"
   SCHEMA=schema/Schema.xml
   DBNAME="$DB_NAME"
   DBUSER="$DB_USER"
   DBPASS="$DB_PASS"
   DBARGS=""
   PHP5PATH=
-  DBLOAD=
+  DBLOAD="$DBLOAD"
   # DBADD=
 EOF
 
@@ -122,6 +164,8 @@ class CiviSeleniumSettings {
        var \$password = 'demo';
        var \$adminUsername = '${ADMIN_USER}';
        var \$adminPassword = '${ADMIN_PASS}';
+       var \$adminApiKey = 'apikey${ADMIN_PASS}';
+       var \$siteKey = '${SITE_KEY}';
         var \$UFemail = 'noreply@civicrm.org';
        function __construct() {
                \$this->fullSandboxPath = \$this->sandboxURL . \$this->sandboxPATH;
@@ -134,9 +178,22 @@ pushd "$CIVI_ROOT"
 ./bin/setup.sh
 popd
 
-pushd "$DRUPAL_ROOT"
-drush -l "${SITE_URL}" -y pm-enable civicrm
-drush -l "${SITE_URL}" -y pm-enable civicrm_webtest
-drush -l "${SITE_URL}" -y user-create --password=demo --mail='demo@example.com' demo
-drush -l "${SITE_URL}" -y user-add-role civicrm_webtest_user demo
-popd
+if [ -n "$SQL_DUMP" ]; then
+  echo "Importing SQL dump: $SQL_DUMP"
+  mysql $DB_NAME < "$SQL_DUMP"
+  echo "SQL import complete."
+
+  if [ -n "$SQL_DUMP2" -a -f "$SQL_DUMP2" ]; then
+    echo "Importing SQL dump: $SQL_DUMP2"
+    mysql $DB_NAME < "$SQL_DUMP2"
+    echo "SQL import complete."
+  fi
+else
+  pushd "$DRUPAL_ROOT"
+  drush -l "${SITE_URL}" -y pm-enable civicrm
+  drush -l "${SITE_URL}" -y pm-enable civicrm_webtest
+  drush -l "${SITE_URL}" -y user-create --password=demo --mail='demo@example.com' demo
+  drush -l "${SITE_URL}" -y user-add-role civicrm_webtest_user demo
+  popd
+fi
+