Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / Nightwatch / Commands / drupalCreateRole.js
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
26   let machineName;
27   this.drupalLoginAsAdmin(() => {
28     this.drupalRelativeURL('/admin/people/roles/add')
29       .setValue('input[name="label"]', roleName)
30       // Wait for the machine name to appear so that it can be used later to
31       // select the permissions from the permission page.
32       .expect.element('.user-role-form .machine-name-value')
33       .to.be.visible.before(2000);
34
35     this.perform(done => {
36       this.getText('.user-role-form .machine-name-value', element => {
37         machineName = element.value;
38         done();
39       });
40     })
41       .submitForm('#user-role-form')
42       .drupalRelativeURL('/admin/people/permissions')
43       .perform((client, done) => {
44         Promise.all(
45           permissions.map(
46             permission =>
47               new Promise(resolve => {
48                 client.click(
49                   `input[name="${machineName}[${permission}]"]`,
50                   () => {
51                     resolve();
52                   },
53                 );
54               }),
55           ),
56         ).then(() => {
57           done();
58         });
59       })
60       .submitForm('#user-admin-permissions');
61   }).perform(() => {
62     if (typeof callback === 'function') {
63       callback.call(self, machineName);
64     }
65   });
66
67   return this;
68 };