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>
9 ## https://github.com/civicrm/civicrm-core/blob/master/tools/scripts/mk-drupal-test-site
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)
19 ## - (strongly recommended) filesystem with "acl" support
24 Usage: mk-drupal-test-site SITE_URL DB_NAME DRUPAL_ROOT [CIVI_ROOT]
25 Re/creates a Drupal-based CiviCRM site.
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.
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
52 DB_PASS
=$
(makepasswd
--chars=12)
56 FACL_USERS
="www-data $(whoami)"
60 SITE_KEY
=$
(makepasswd
--chars=16)
62 ADMIN_PASS
=$
(makepasswd
--chars=12)
64 # Check if the CiviCRM directory looks OK
65 if [ -z "$CIVI_ROOT" -o ! -d "$CIVI_ROOT/bin" ]; then
66 echo "Failed to locate civi root: $CIVI_ROOT"
70 if [ -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."
78 echo "DROP DATABASE IF EXISTS $DB_NAME" | mysql
79 echo "CREATE DATABASE $DB_NAME" | mysql
80 echo "GRANT ALL ON ${DB_NAME}.* TO '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_PASS}'" | mysql
81 echo "GRANT SUPER ON *.* TO '${DB_USER}'@'localhost'" | mysql
85 if [ -d "sites/$SITE_URL" ]; then
86 chmod u
+w
"sites/$SITE_URL"
87 rm -rf "sites/$SITE_URL"
90 # NB: Avoid sending e-mails for the site installation
91 # On hosts without sendmail (ex: demo sites), this causes the installation to fail.
92 drush site-install
-y \
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"
97 chmod u
+w
"sites/$SITE_URL"
99 ## Allow shell and WWW users to both manipulate "files" directory
100 if 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
106 ## Create Drupal-CiviCRM dirs and config
107 for 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}"
113 ln -s "$CIVI_ROOT" "sites
/$SITE_URL/modules
/civicrm
"
115 cat "$CIVI_ROOT/templates
/CRM
/common
/civicrm.settings.php.template
" \
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
"
131 echo >> "sites
/$SITE_URL/civicrm.settings.php
"
132 echo "define
('CIVICRM_MAIL_LOG', '/dev/null');" >> "sites
/$SITE_URL/civicrm.settings.php
"
135 ## Create CiviCRM config
136 cat > "$CIVI_ROOT/bin
/setup.conf
" << EOF
138 CIVISOURCEDIR="$CIVI_ROOT"
139 SCHEMA=schema/Schema.xml
149 cat > "$CIVI_ROOT/tests
/phpunit
/CiviTest
/civicrm.settings.
local.php
" << EOF
151 define('CIVICRM_DSN', "mysql
://${DB_USER}:${DB_PASS}@${DB_HOST}/${DB_NAME}");
152 define('CIVICRM_TEMPLATE_COMPILEDIR', '${DRUPAL_ROOT}/sites/${SITE_URL}/files/civicrm/templates_c');
153 define('DONT_DOCUMENT_TEST_CONFIG', TRUE);
156 cat > "$CIVI_ROOT/tests
/phpunit
/CiviTest
/CiviSeleniumSettings.php
" << EOF
158 class 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}';
167 var \$adminApiKey = 'apikey${ADMIN_PASS}';
168 var \$siteKey = '${SITE_KEY}';
169 var \$UFemail = 'noreply@civicrm.org';
170 function __construct() {
171 \$this->fullSandboxPath = \$this->sandboxURL . \$this->sandboxPATH;
181 if [ -n "$SQL_DUMP" ]; then
182 echo "Importing SQL dump
: $SQL_DUMP"
183 mysql $DB_NAME < "$SQL_DUMP"
184 echo "SQL import complete.
"
186 if [ -n "$SQL_DUMP2" -a -f "$SQL_DUMP2" ]; then
187 echo "Importing SQL dump
: $SQL_DUMP2"
188 mysql $DB_NAME < "$SQL_DUMP2"
189 echo "SQL import complete.
"
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