drupalInstall.js 1.7 KB

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