Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / Nightwatch / Commands / drupalLoginAsAdmin.js
diff --git a/web/core/tests/Drupal/Nightwatch/Commands/drupalLoginAsAdmin.js b/web/core/tests/Drupal/Nightwatch/Commands/drupalLoginAsAdmin.js
new file mode 100644 (file)
index 0000000..864b904
--- /dev/null
@@ -0,0 +1,43 @@
+import { execSync } from 'child_process';
+import { URL } from 'url';
+import { commandAsWebserver } from '../globals';
+
+/**
+ * Logs in as the admin user.
+ *
+ * @param {function} callback
+ *   A callback which will allow running commands as an administrator.
+ * @return {object}
+ *   The drupalLoginAsAdmin command.
+ */
+exports.command = function drupalLoginAsAdmin(callback) {
+  const self = this;
+  this.drupalUserIsLoggedIn(sessionExists => {
+    if (sessionExists) {
+      this.drupalLogout();
+    }
+    const userLink = execSync(
+      commandAsWebserver(
+        `php ./scripts/test-site.php user-login 1 --site-path ${
+          this.drupalSitePath
+        }`,
+      ),
+    );
+
+    this.drupalRelativeURL(userLink.toString());
+
+    this.drupalUserIsLoggedIn(sessionExists => {
+      if (!sessionExists) {
+        throw new Error('Logging in as an admin user failed.');
+      }
+    });
+  });
+
+  if (typeof callback === 'function') {
+    callback.call(self);
+  }
+
+  this.drupalLogout({ silent: true });
+
+  return this;
+};