+ /** @var \Illuminate\Foundation\Application $app */
+ $app = require __DIR__ . '/../bootstrap/app.php';
+ $app->register(TestServiceProvider::class);
+ $app->make(Kernel::class)->bootstrap();
+
+ return $app;
+ }
+
+ /**
+ * Set the current user context to be an admin.
+ */
+ public function asAdmin()
+ {
+ return $this->actingAs($this->getAdmin());
+ }
+
+ /**
+ * Get the current admin user.
+ */
+ public function getAdmin(): User
+ {
+ if (is_null($this->admin)) {
+ $adminRole = Role::getSystemRole('admin');
+ $this->admin = $adminRole->users->first();
+ }
+
+ return $this->admin;
+ }
+
+ /**
+ * Set the current user context to be an editor.
+ */
+ public function asEditor()
+ {
+ return $this->actingAs($this->getEditor());
+ }
+
+ /**
+ * Get a editor user.
+ */
+ protected function getEditor(): User
+ {
+ if ($this->editor === null) {
+ $editorRole = Role::getRole('editor');
+ $this->editor = $editorRole->users->first();
+ }
+
+ return $this->editor;
+ }
+
+ /**
+ * Set the current user context to be a viewer.
+ */
+ public function asViewer()
+ {
+ return $this->actingAs($this->getViewer());
+ }
+
+ /**
+ * Get an instance of a user with 'viewer' permissions.
+ */
+ protected function getViewer(array $attributes = []): User
+ {
+ $user = Role::getRole('viewer')->users()->first();
+ if (!empty($attributes)) {
+ $user->forceFill($attributes)->save();
+ }
+
+ return $user;
+ }
+
+ /**
+ * Get a user that's not a system user such as the guest user.
+ */
+ public function getNormalUser(): User
+ {
+ return User::query()->where('system_name', '=', null)->get()->last();
+ }
+
+ /**
+ * Quickly sets an array of settings.
+ */
+ protected function setSettings(array $settingsArray): void
+ {
+ $settings = app(SettingService::class);
+ foreach ($settingsArray as $key => $value) {
+ $settings->put($key, $value);
+ }
+ }
+
+ /**
+ * Give the given user some permissions.
+ */
+ protected function giveUserPermissions(User $user, array $permissions = []): void
+ {
+ $newRole = $this->createNewRole($permissions);
+ $user->attachRole($newRole);
+ $user->load('roles');
+ $user->clearPermissionCache();
+ }
+
+ /**
+ * Completely remove the given permission name from the given user.
+ */
+ protected function removePermissionFromUser(User $user, string $permissionName)
+ {
+ $permissionBuilder = app()->make(JointPermissionBuilder::class);
+
+ /** @var RolePermission $permission */
+ $permission = RolePermission::query()->where('name', '=', $permissionName)->firstOrFail();
+
+ $roles = $user->roles()->whereHas('permissions', function ($query) use ($permission) {
+ $query->where('id', '=', $permission->id);
+ })->get();
+
+ /** @var Role $role */
+ foreach ($roles as $role) {
+ $role->detachPermission($permission);
+ $permissionBuilder->rebuildForRole($role);
+ }
+
+ $user->clearPermissionCache();