drupalCreateUser.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * Logs into Drupal as the given user.
  3. *
  4. * @param {object} settings
  5. * Settings object
  6. * @param {string} settings.name
  7. * The user name.
  8. * @param {string} settings.password
  9. * The user password.
  10. * @param {array} [settings.permissions=[]]
  11. * The list of permissions granted for the user.
  12. * @param {function} callback
  13. * A callback which will be called when creating the user is finished.
  14. * @return {object}
  15. * The drupalCreateUser command.
  16. */
  17. exports.command = function drupalCreateUser(
  18. { name, password, permissions = [] },
  19. callback,
  20. ) {
  21. const self = this;
  22. let role;
  23. this.perform((client, done) => {
  24. if (permissions) {
  25. client.drupalCreateRole({ permissions, name: null }, newRole => {
  26. role = newRole;
  27. done();
  28. });
  29. } else {
  30. done();
  31. }
  32. }).drupalLoginAsAdmin(() => {
  33. this.drupalRelativeURL('/admin/people/create')
  34. .setValue('input[name="name"]', name)
  35. .setValue('input[name="pass[pass1]"]', password)
  36. .setValue('input[name="pass[pass2]"]', password)
  37. .perform((client, done) => {
  38. if (role) {
  39. client.click(`input[name="roles[${role}]`, () => {
  40. done();
  41. });
  42. } else {
  43. done();
  44. }
  45. })
  46. .submitForm('#user-register-form')
  47. .assert.containsText(
  48. '[data-drupal-messages]',
  49. 'Created a new user account',
  50. `User "${name}" was created successfully.`,
  51. );
  52. });
  53. if (typeof callback === 'function') {
  54. callback.call(self);
  55. }
  56. return this;
  57. };