drupalCreateRole.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Creates role with given permissions.
  3. *
  4. * @param {object} settings
  5. * Settings object
  6. * @param {array} settings.permissions
  7. * The list of roles granted for the user.
  8. * @param {string} [settings.name=null]
  9. * The role name.
  10. * @param {function} callback
  11. * A callback which will be called, when creating the role is finished.
  12. * @return {object}
  13. * The drupalCreateRole command.
  14. */
  15. exports.command = function drupalCreateRole(
  16. { permissions, name = null },
  17. callback,
  18. ) {
  19. const self = this;
  20. const roleName =
  21. name ||
  22. Math.random()
  23. .toString(36)
  24. .substring(2, 15);
  25. let machineName;
  26. this.drupalLoginAsAdmin(() => {
  27. this.drupalRelativeURL('/admin/people/roles/add')
  28. .setValue('input[name="label"]', roleName)
  29. // Wait for the machine name to appear so that it can be used later to
  30. // select the permissions from the permission page.
  31. .expect.element('.user-role-form .machine-name-value')
  32. .to.be.visible.before(2000);
  33. this.perform(done => {
  34. this.getText('.user-role-form .machine-name-value', element => {
  35. machineName = element.value;
  36. done();
  37. });
  38. })
  39. .submitForm('#user-role-form')
  40. .drupalRelativeURL('/admin/people/permissions')
  41. .perform((client, done) => {
  42. Promise.all(
  43. permissions.map(
  44. permission =>
  45. new Promise(resolve => {
  46. client.click(
  47. `input[name="${machineName}[${permission}]"]`,
  48. () => {
  49. resolve();
  50. },
  51. );
  52. }),
  53. ),
  54. ).then(() => {
  55. done();
  56. });
  57. })
  58. .submitForm('#user-admin-permissions');
  59. }).perform(() => {
  60. if (typeof callback === 'function') {
  61. callback.call(self, machineName);
  62. }
  63. });
  64. return this;
  65. };