c5a4b4655195649fffb66de7bbda91b1a61c49ae
[yaffs-website] / web / core / tests / Drupal / Nightwatch / Commands / drupalInstall.js
1 import { execSync } from 'child_process';
2 import { URL } from 'url';
3 import { commandAsWebserver } from '../globals';
4
5 /**
6  * Installs a Drupal test site.
7  *
8  * @param {oject} [settings={}]
9  *   Settings object
10  * @param {string} [settings.setupFile='']
11  *   Setup file used by TestSiteApplicationTest
12  * @param {function} callback
13  *   A callback which will be called, when the installation is finished.
14  * @return {object}
15  *   The 'browser' object.
16  */
17 exports.command = function drupalInstall({ setupFile = '' } = {}, callback) {
18   const self = this;
19
20   try {
21     setupFile = setupFile ? `--setup-file "${setupFile}"` : '';
22     const dbOption =
23       process.env.DRUPAL_TEST_DB_URL.length > 0
24         ? `--db-url ${process.env.DRUPAL_TEST_DB_URL}`
25         : '';
26     const install = execSync(
27       commandAsWebserver(
28         `php ./scripts/test-site.php install ${setupFile} --base-url ${
29           process.env.DRUPAL_TEST_BASE_URL
30         } ${dbOption} --json`,
31       ),
32     );
33     const installData = JSON.parse(install.toString());
34     this.drupalDbPrefix = installData.db_prefix;
35     this.drupalSitePath = installData.site_path;
36     const url = new URL(process.env.DRUPAL_TEST_BASE_URL);
37     this.url(process.env.DRUPAL_TEST_BASE_URL).setCookie({
38       name: 'SIMPLETEST_USER_AGENT',
39       // Colons need to be URL encoded to be valid.
40       value: encodeURIComponent(installData.user_agent),
41       path: url.pathname,
42       domain: url.host,
43     });
44   } catch (error) {
45     this.assert.fail(error);
46   }
47
48   // Nightwatch doesn't like it when no actions are added in a command file.
49   // https://github.com/nightwatchjs/nightwatch/issues/1792
50   this.pause(1);
51
52   if (typeof callback === 'function') {
53     callback.call(self);
54   }
55
56   return this;
57 };