commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / tools / scripts / mk-drupal-test-site
1 #!/bin/bash
2 set -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
8 ## SOURCE:
9 ## https://github.com/civicrm/civicrm-core/blob/master/tools/scripts/mk-drupal-test-site
10
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
21
22 function usage() {
23 cat <<EOT
24 Usage: mk-drupal-test-site SITE_URL DB_NAME DRUPAL_ROOT [CIVI_ROOT]
25 Re/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
39 EOT
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
49 SITE_URL="$1"
50 DB_NAME="$2"
51 DB_USER="$DB_NAME"
52 DB_PASS=$(makepasswd --chars=12)
53 DB_HOST=localhost
54 DRUPAL_ROOT="$3"
55 CIVI_ROOT="$4"
56 FACL_USERS="www-data $(whoami)"
57 SQL_DUMP="$5"
58 SQL_DUMP2="$6"
59
60 SITE_KEY=$(makepasswd --chars=16)
61 ADMIN_USER="admin"
62 ADMIN_PASS=$(makepasswd --chars=12)
63
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"
67 exit 1
68 fi
69
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."
73 exit 3;
74 fi
75 fi
76
77 ## Create database
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
82
83 ## Create Drupal site
84 pushd "$DRUPAL_ROOT"
85 if [ -d "sites/$SITE_URL" ]; then
86 chmod u+w "sites/$SITE_URL"
87 rm -rf "sites/$SITE_URL"
88 fi
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.
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"
98
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
103 done
104 fi
105
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}"
110 fi
111 done
112
113 ln -s "$CIVI_ROOT" "sites/$SITE_URL/modules/civicrm"
114
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"
130
131 echo >> "sites/$SITE_URL/civicrm.settings.php"
132 echo "define('CIVICRM_MAIL_LOG', '/dev/null');" >> "sites/$SITE_URL/civicrm.settings.php"
133 popd
134
135 ## Create CiviCRM config
136 cat > "$CIVI_ROOT/bin/setup.conf" << EOF
137 SVNROOT="$CIVI_ROOT"
138 CIVISOURCEDIR="$CIVI_ROOT"
139 SCHEMA=schema/Schema.xml
140 DBNAME="$DB_NAME"
141 DBUSER="$DB_USER"
142 DBPASS="$DB_PASS"
143 DBARGS=""
144 PHP5PATH=
145 DBLOAD="$DBLOAD"
146 # DBADD=
147 EOF
148
149 cat > "$CIVI_ROOT/tests/phpunit/CiviTest/civicrm.settings.local.php" << EOF
150 <?php
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);
154 EOF
155
156 cat > "$CIVI_ROOT/tests/phpunit/CiviTest/CiviSeleniumSettings.php" << EOF
157 <?php
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;
172 }
173 }
174
175 EOF
176
177 pushd "$CIVI_ROOT"
178 ./bin/setup.sh
179 popd
180
181 if [ -n "$SQL_DUMP" ]; then
182 echo "Importing SQL dump: $SQL_DUMP"
183 mysql $DB_NAME < "$SQL_DUMP"
184 echo "SQL import complete."
185
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."
190 fi
191 else
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
198 fi
199