Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / Nightwatch / Commands / drupalCreateUser.js
diff --git a/web/core/tests/Drupal/Nightwatch/Commands/drupalCreateUser.js b/web/core/tests/Drupal/Nightwatch/Commands/drupalCreateUser.js
new file mode 100644 (file)
index 0000000..0b81be3
--- /dev/null
@@ -0,0 +1,60 @@
+/**
+ * Logs into Drupal as the given user.
+ *
+ * @param {object} settings
+ *   Settings object
+ * @param {string} settings.name
+ *   The user name.
+ * @param {string} settings.password
+ *   The user password.
+ * @param {array} [settings.permissions=[]]
+ *   The list of permissions granted for the user.
+ * @param {function} callback
+ *   A callback which will be called, when the creating the use is finished.
+ * @return {object}
+ *   The drupalCreateUser command.
+ */
+exports.command = function drupalCreateUser(
+  { name, password, permissions = [] },
+  callback,
+) {
+  const self = this;
+
+  let role;
+  this.perform((client, done) => {
+    if (permissions) {
+      client.drupalCreateRole({ permissions, name: null }, newRole => {
+        role = newRole;
+        done();
+      });
+    } else {
+      done();
+    }
+  }).drupalLoginAsAdmin(() => {
+    this.drupalRelativeURL('/admin/people/create')
+      .setValue('input[name="name"]', name)
+      .setValue('input[name="pass[pass1]"]', password)
+      .setValue('input[name="pass[pass2]"]', password)
+      .perform((client, done) => {
+        if (role) {
+          client.click(`input[name="roles[${role}]`, () => {
+            done();
+          });
+        } else {
+          done();
+        }
+      })
+      .submitForm('#user-register-form')
+      .assert.containsText(
+        '.messages',
+        'Created a new user account',
+        `User "${name}" was created succesfully.`,
+      );
+  });
+
+  if (typeof callback === 'function') {
+    callback.call(self);
+  }
+
+  return this;
+};