1
0

UserTrait.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Traits;
  9. use OC\User\User;
  10. use OCP\EventDispatcher\IEventDispatcher;
  11. use OCP\IUser;
  12. use OCP\Server;
  13. class DummyUser extends User {
  14. private string $uid;
  15. public function __construct(string $uid) {
  16. $this->uid = $uid;
  17. parent::__construct($uid, null, Server::get(IEventDispatcher::class));
  18. }
  19. public function getUID(): string {
  20. return $this->uid;
  21. }
  22. }
  23. /**
  24. * Allow creating users in a temporary backend
  25. */
  26. trait UserTrait {
  27. /**
  28. * @var \Test\Util\User\Dummy|\OCP\UserInterface
  29. */
  30. protected $userBackend;
  31. protected function createUser($name, $password): IUser {
  32. $this->userBackend->createUser($name, $password);
  33. return new DummyUser($name);
  34. }
  35. protected function setUpUserTrait() {
  36. $this->userBackend = new \Test\Util\User\Dummy();
  37. \OC::$server->getUserManager()->registerBackend($this->userBackend);
  38. }
  39. protected function tearDownUserTrait() {
  40. \OC::$server->getUserManager()->removeBackend($this->userBackend);
  41. }
  42. }