Merge pull request #7253 from jitendrapurohit/CRM-17520
[civicrm-core.git] / tools / scripts / mk-drupal-test-site
CommitLineData
6a488035
TO
1#!/bin/bash
2set -ex
3
4## Setup a Drupal site with CiviCRM configured for unit-testing.
5## If a site already exists, destroy and recreate it.
6## Usage: mk-drupal-test-site <domain.name> <db_name> </path/to/drupal> </path/to/civi>
7
2367822f
ML
8## SOURCE:
9## https://github.com/civicrm/civicrm-core/blob/master/tools/scripts/mk-drupal-test-site
10
6a488035
TO
11## Pre-requisites:
12## - MySQL admin credentials in ~/.my.cnf
13## - Apache vhost with mod_rewrite, etc
14## - DNS or /etc/hosts entries for "url"
15## - Drupal source tree
16## - CiviCRM source tree (outside the drupal root)
17## - makepasswd
18## - drush
19## - (strongly recommended) filesystem with "acl" support
20
2367822f
ML
21
22function usage() {
23 cat <<EOT
24Usage: mk-drupal-test-site SITE_URL DB_NAME DRUPAL_ROOT [CIVI_ROOT]
25Re/creates a Drupal-based CiviCRM site.
26
27 * SITE_URL: URL of the site. Example: example.org
28 * DB_NAME: MySQL database name. Example: civicrm_test
29 * DRUPAL_ROOT: Root path of the Drupal website. Example: /var/www/
30 * CIVI_ROOT: Root path of the CiviCRM directory. Will be symlinked.
31 You probably want to have a separate version somewhere to avoid
32 cloning a new version of the code base for each CMS.
33 The CiviCRM directory can either contain the main git repositories,
34 or an equivalent of the tar.gz archive.
35 Example: /srv/repositories/civicrm/
36 * SQL_DUMP (optional): instead of a standard blank install, import a
37 specific .sql dump. You can specify two .sql files.
38
39EOT
40
41 exit 99;
42}
43
44[ "$1" = "--help" ] && usage
45[ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] && usage
46[ ! -d "$3" ] && echo "ERROR: $3: Drupal root directory not found." && usage
47[ ! -d "$4" ] && echo "ERROR: $4: CiviCRM root directory not found." && usage
48
6a488035
TO
49SITE_URL="$1"
50DB_NAME="$2"
51DB_USER="$DB_NAME"
52DB_PASS=$(makepasswd --chars=12)
53DB_HOST=localhost
54DRUPAL_ROOT="$3"
55CIVI_ROOT="$4"
56FACL_USERS="www-data $(whoami)"
2367822f
ML
57SQL_DUMP="$5"
58SQL_DUMP2="$6"
6a488035
TO
59
60SITE_KEY=$(makepasswd --chars=16)
61ADMIN_USER="admin"
62ADMIN_PASS=$(makepasswd --chars=12)
63
2367822f 64# Check if the CiviCRM directory looks OK
6a488035
TO
65if [ -z "$CIVI_ROOT" -o ! -d "$CIVI_ROOT/bin" ]; then
66 echo "Failed to locate civi root: $CIVI_ROOT"
67 exit 1
68fi
69
2367822f
ML
70if [ -n "$SQL_DUMP" ]; then
71 if [ ! -f "$SQL_DUMP" ]; then
72 echo "$SQL_DUMP: Could not find the .sql file. Try using an absolute path to the file."
73 exit 3;
74 fi
6a488035
TO
75fi
76
77## Create database
78echo "DROP DATABASE IF EXISTS $DB_NAME" | mysql
79echo "CREATE DATABASE $DB_NAME" | mysql
80echo "GRANT ALL ON ${DB_NAME}.* TO '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_PASS}'" | mysql
81echo "GRANT SUPER ON *.* TO '${DB_USER}'@'localhost'" | mysql
82
83## Create Drupal site
84pushd "$DRUPAL_ROOT"
85if [ -d "sites/$SITE_URL" ]; then
86 chmod u+w "sites/$SITE_URL"
87 rm -rf "sites/$SITE_URL"
88fi
4aaa5419
ML
89
90# NB: Avoid sending e-mails for the site installation
91# On hosts without sendmail (ex: demo sites), this causes the installation to fail.
cc6b2f39 92drush site-install -y \
6a488035
TO
93 --db-url="mysql://${DB_USER}:${DB_PASS}@${DB_HOST}/${DB_NAME}" \
94 --account-name="$ADMIN_USER" \
95 --account-pass="$ADMIN_PASS" \
96 --sites-subdir="$SITE_URL"
97chmod u+w "sites/$SITE_URL"
98
99## Allow shell and WWW users to both manipulate "files" directory
100if which setfacl; then
101 for FACL_USER in $FACL_USERS ; do
102 find "$DRUPAL_ROOT/sites/${SITE_URL}/files" -type d | xargs setfacl -m u:${FACL_USER}:rwx -m d:u:${FACL_USER}:rwx
103 done
104fi
105
106## Create Drupal-CiviCRM dirs and config
107for SUBDIR in modules files files/civicrm files/civicrm/templates_c ; do
108 if [ ! -d "sites/${SITE_URL}/${SUBDIR}" ]; then
109 mkdir "sites/${SITE_URL}/${SUBDIR}"
110 fi
111done
112
e690678a 113ln -s "$CIVI_ROOT" "sites/$SITE_URL/modules/civicrm"
6a488035 114
befcb21d 115cat "$CIVI_ROOT/templates/CRM/common/civicrm.settings.php.template" \
6a488035
TO
116 | sed "s;%%baseURL%%;http://${SITE_URL};" \
117 | sed "s;%%cms%%;Drupal;" \
118 | sed "s;%%CMSdbHost%%;${DB_HOST};" \
119 | sed "s;%%CMSdbName%%;${DB_NAME};" \
120 | sed "s;%%CMSdbPass%%;${DB_PASS};" \
121 | sed "s;%%CMSdbUser%%;${DB_USER};" \
122 | sed "s;%%crmRoot%%;${DRUPAL_ROOT}/sites/${SITE_URL}/modules/civicrm;" \
123 | sed "s;%%dbHost%%;${DB_HOST};" \
124 | sed "s;%%dbName%%;${DB_NAME};" \
125 | sed "s;%%dbPass%%;${DB_PASS};" \
126 | sed "s;%%dbUser%%;${DB_USER};" \
127 | sed "s;%%siteKey%%;${SITE_KEY};" \
128 | sed "s;%%templateCompileDir%%;${DRUPAL_ROOT}/sites/${SITE_URL}/files/civicrm/templates_c;" \
129 > "sites/$SITE_URL/civicrm.settings.php"
130
dfd90a3a
TO
131echo >> "sites/$SITE_URL/civicrm.settings.php"
132echo "define('CIVICRM_MAIL_LOG', '/dev/null');" >> "sites/$SITE_URL/civicrm.settings.php"
6a488035
TO
133popd
134
135## Create CiviCRM config
136cat > "$CIVI_ROOT/bin/setup.conf" << EOF
137 SVNROOT="$CIVI_ROOT"
7f41f9a8 138 CIVISOURCEDIR="$CIVI_ROOT"
6a488035
TO
139 SCHEMA=schema/Schema.xml
140 DBNAME="$DB_NAME"
141 DBUSER="$DB_USER"
142 DBPASS="$DB_PASS"
143 DBARGS=""
144 PHP5PATH=
549e9267 145 DBLOAD="$DBLOAD"
6a488035
TO
146 # DBADD=
147EOF
148
149cat > "$CIVI_ROOT/tests/phpunit/CiviTest/civicrm.settings.local.php" << EOF
150<?php
151define('CIVICRM_DSN', "mysql://${DB_USER}:${DB_PASS}@${DB_HOST}/${DB_NAME}");
152define('CIVICRM_TEMPLATE_COMPILEDIR', '${DRUPAL_ROOT}/sites/${SITE_URL}/files/civicrm/templates_c');
153define('DONT_DOCUMENT_TEST_CONFIG', TRUE);
154EOF
155
156cat > "$CIVI_ROOT/tests/phpunit/CiviTest/CiviSeleniumSettings.php" << EOF
157<?php
158class CiviSeleniumSettings {
159 var \$publicSandbox = false;
160 var \$browser = '*firefox';
161 var \$sandboxURL = 'http://${SITE_URL}';
162 var \$sandboxPATH = '';
163 var \$username = 'demo';
164 var \$password = 'demo';
165 var \$adminUsername = '${ADMIN_USER}';
166 var \$adminPassword = '${ADMIN_PASS}';
dc13c338
TO
167 var \$adminApiKey = 'apikey${ADMIN_PASS}';
168 var \$siteKey = '${SITE_KEY}';
6a488035
TO
169 var \$UFemail = 'noreply@civicrm.org';
170 function __construct() {
171 \$this->fullSandboxPath = \$this->sandboxURL . \$this->sandboxPATH;
172 }
173}
174
175EOF
176
177pushd "$CIVI_ROOT"
178./bin/setup.sh
179popd
180
2367822f
ML
181if [ -n "$SQL_DUMP" ]; then
182 echo "Importing SQL dump: $SQL_DUMP"
8eab7a73 183 mysql $DB_NAME < "$SQL_DUMP"
2367822f
ML
184 echo "SQL import complete."
185
186 if [ -n "$SQL_DUMP2" -a -f "$SQL_DUMP2" ]; then
187 echo "Importing SQL dump: $SQL_DUMP2"
8eab7a73 188 mysql $DB_NAME < "$SQL_DUMP2"
2367822f
ML
189 echo "SQL import complete."
190 fi
191else
192 pushd "$DRUPAL_ROOT"
193 drush -l "${SITE_URL}" -y pm-enable civicrm
194 drush -l "${SITE_URL}" -y pm-enable civicrm_webtest
195 drush -l "${SITE_URL}" -y user-create --password=demo --mail='demo@example.com' demo
196 drush -l "${SITE_URL}" -y user-add-role civicrm_webtest_user demo
197 popd
198fi
199