UserTrait.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Traits;
  8. use OC\User\User;
  9. use OCP\EventDispatcher\IEventDispatcher;
  10. use OCP\IUser;
  11. use OCP\Server;
  12. class DummyUser extends User {
  13. private string $uid;
  14. public function __construct(string $uid) {
  15. $this->uid = $uid;
  16. parent::__construct($uid, null, Server::get(IEventDispatcher::class));
  17. }
  18. public function getUID(): string {
  19. return $this->uid;
  20. }
  21. }
  22. /**
  23. * Allow creating users in a temporary backend
  24. */
  25. trait UserTrait {
  26. /**
  27. * @var \Test\Util\User\Dummy|\OCP\UserInterface
  28. */
  29. protected $userBackend;
  30. protected function createUser($name, $password): IUser {
  31. $this->userBackend->createUser($name, $password);
  32. return new DummyUser($name);
  33. }
  34. protected function setUpUserTrait() {
  35. $this->userBackend = new \Test\Util\User\Dummy();
  36. \OC::$server->getUserManager()->registerBackend($this->userBackend);
  37. }
  38. protected function tearDownUserTrait() {
  39. \OC::$server->getUserManager()->removeBackend($this->userBackend);
  40. }
  41. }