drupalLoginAsAdmin.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 ${
  21. this.drupalSitePath
  22. }`,
  23. ),
  24. );
  25. this.drupalRelativeURL(userLink.toString());
  26. this.drupalUserIsLoggedIn(sessionExists => {
  27. if (!sessionExists) {
  28. throw new Error('Logging in as an admin user failed.');
  29. }
  30. });
  31. });
  32. if (typeof callback === 'function') {
  33. callback.call(self);
  34. }
  35. this.drupalLogout({ silent: true });
  36. return this;
  37. };