drupalInstall.js 2.1 KB

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