drupalLoginAsAdmin.js 1013 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { execSync } from 'child_process';
  2. import { URL } from 'url';
  3. import { commandAsWebserver } from '../globals';
  4. /**
  5. * Logs in as the admin user.
  6. *
  7. * @param {function} callback
  8. * A callback which will allow running commands as an administrator.
  9. * @return {object}
  10. * The drupalLoginAsAdmin command.
  11. */
  12. exports.command = function drupalLoginAsAdmin(callback) {
  13. const self = this;
  14. this.drupalUserIsLoggedIn(sessionExists => {
  15. if (sessionExists) {
  16. this.drupalLogout();
  17. }
  18. const userLink = execSync(
  19. commandAsWebserver(
  20. `php ./scripts/test-site.php user-login 1 --site-path ${this.globals.drupalSitePath}`,
  21. ),
  22. );
  23. this.drupalRelativeURL(userLink.toString());
  24. this.drupalUserIsLoggedIn(sessionExists => {
  25. if (!sessionExists) {
  26. throw new Error('Logging in as an admin user failed.');
  27. }
  28. });
  29. });
  30. if (typeof callback === 'function') {
  31. callback.call(self);
  32. }
  33. this.drupalLogout({ silent: true });
  34. return this;
  35. };