Newer
Older
#!/bin/bash
set -ex
## Setup a Drupal site with CiviCRM configured for unit-testing.
## 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
## - DNS or /etc/hosts entries for "url"
## - Drupal source tree
## - CiviCRM source tree (outside the drupal root)
## - makepasswd
## - 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"
DB_PASS=$(makepasswd --chars=12)
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 [ -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
echo "DROP DATABASE IF EXISTS $DB_NAME" | mysql
echo "CREATE DATABASE $DB_NAME" | mysql
echo "GRANT ALL ON ${DB_NAME}.* TO '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_PASS}'" | mysql
echo "GRANT SUPER ON *.* TO '${DB_USER}'@'localhost'" | mysql
## Create Drupal site
pushd "$DRUPAL_ROOT"
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.
php -d sendmail_path=`which true` `which drush` site-install -y \
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
--db-url="mysql://${DB_USER}:${DB_PASS}@${DB_HOST}/${DB_NAME}" \
--account-name="$ADMIN_USER" \
--account-pass="$ADMIN_PASS" \
--sites-subdir="$SITE_URL"
chmod u+w "sites/$SITE_URL"
## Allow shell and WWW users to both manipulate "files" directory
if which setfacl; then
for FACL_USER in $FACL_USERS ; do
find "$DRUPAL_ROOT/sites/${SITE_URL}/files" -type d | xargs setfacl -m u:${FACL_USER}:rwx -m d:u:${FACL_USER}:rwx
done
fi
## Create Drupal-CiviCRM dirs and config
for SUBDIR in modules files files/civicrm files/civicrm/templates_c ; do
if [ ! -d "sites/${SITE_URL}/${SUBDIR}" ]; then
mkdir "sites/${SITE_URL}/${SUBDIR}"
fi
done
ln -s "$CIVI_ROOT" "sites/$SITE_URL/modules/"
cat "$CIVI_ROOT/templates/CRM/common/civicrm.settings.php.tpl" \
| sed "s;%%baseURL%%;http://${SITE_URL};" \
| sed "s;%%cms%%;Drupal;" \
| sed "s;%%CMSdbHost%%;${DB_HOST};" \
| sed "s;%%CMSdbName%%;${DB_NAME};" \
| sed "s;%%CMSdbPass%%;${DB_PASS};" \
| sed "s;%%CMSdbUser%%;${DB_USER};" \
| sed "s;%%crmRoot%%;${DRUPAL_ROOT}/sites/${SITE_URL}/modules/civicrm;" \
| sed "s;%%dbHost%%;${DB_HOST};" \
| sed "s;%%dbName%%;${DB_NAME};" \
| sed "s;%%dbPass%%;${DB_PASS};" \
| sed "s;%%dbUser%%;${DB_USER};" \
| sed "s;%%siteKey%%;${SITE_KEY};" \
| 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"
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
popd
## Create CiviCRM config
cat > "$CIVI_ROOT/bin/setup.conf" << EOF
SVNROOT="$CIVI_ROOT"
SCHEMA=schema/Schema.xml
DBNAME="$DB_NAME"
DBUSER="$DB_USER"
DBPASS="$DB_PASS"
DBARGS=""
PHP5PATH=
DBLOAD=
# DBADD=
EOF
cat > "$CIVI_ROOT/tests/phpunit/CiviTest/civicrm.settings.local.php" << EOF
<?php
define('CIVICRM_DSN', "mysql://${DB_USER}:${DB_PASS}@${DB_HOST}/${DB_NAME}");
define('CIVICRM_TEMPLATE_COMPILEDIR', '${DRUPAL_ROOT}/sites/${SITE_URL}/files/civicrm/templates_c');
define('DONT_DOCUMENT_TEST_CONFIG', TRUE);
EOF
cat > "$CIVI_ROOT/tests/phpunit/CiviTest/CiviSeleniumSettings.php" << EOF
<?php
class CiviSeleniumSettings {
var \$publicSandbox = false;
var \$browser = '*firefox';
var \$sandboxURL = 'http://${SITE_URL}';
var \$sandboxPATH = '';
var \$username = 'demo';
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;
}
}
EOF
pushd "$CIVI_ROOT"
./bin/setup.sh
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