Backend.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-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\User;
  8. /**
  9. * Abstract class to provide the basis of backend-specific unit test classes.
  10. *
  11. * All subclasses MUST assign a backend property in setUp() which implements
  12. * user operations (add, remove, etc.). Test methods in this class will then be
  13. * run on each separate subclass and backend therein.
  14. *
  15. * For an example see /tests/lib/user/dummy.php
  16. */
  17. abstract class Backend extends \Test\TestCase {
  18. /**
  19. * @var \OC\User\Backend $backend
  20. */
  21. protected $backend;
  22. /**
  23. * get a new unique user name
  24. * test cases can override this in order to clean up created user
  25. * @return string
  26. */
  27. public function getUser() {
  28. return $this->getUniqueID('test_');
  29. }
  30. public function testAddRemove(): void {
  31. //get the number of groups we start with, in case there are exising groups
  32. $startCount = count($this->backend->getUsers());
  33. $name1 = $this->getUser();
  34. $name2 = $this->getUser();
  35. $this->backend->createUser($name1, '');
  36. $count = count($this->backend->getUsers()) - $startCount;
  37. $this->assertEquals(1, $count);
  38. $this->assertTrue((array_search($name1, $this->backend->getUsers()) !== false));
  39. $this->assertFalse((array_search($name2, $this->backend->getUsers()) !== false));
  40. $this->backend->createUser($name2, '');
  41. $count = count($this->backend->getUsers()) - $startCount;
  42. $this->assertEquals(2, $count);
  43. $this->assertTrue((array_search($name1, $this->backend->getUsers()) !== false));
  44. $this->assertTrue((array_search($name2, $this->backend->getUsers()) !== false));
  45. $this->backend->deleteUser($name2);
  46. $count = count($this->backend->getUsers()) - $startCount;
  47. $this->assertEquals(1, $count);
  48. $this->assertTrue((array_search($name1, $this->backend->getUsers()) !== false));
  49. $this->assertFalse((array_search($name2, $this->backend->getUsers()) !== false));
  50. }
  51. public function testLogin(): void {
  52. $name1 = $this->getUser();
  53. $name2 = $this->getUser();
  54. $this->assertFalse($this->backend->userExists($name1));
  55. $this->assertFalse($this->backend->userExists($name2));
  56. $this->backend->createUser($name1, 'pass1');
  57. $this->backend->createUser($name2, 'pass2');
  58. $this->assertTrue($this->backend->userExists($name1));
  59. $this->assertTrue($this->backend->userExists($name2));
  60. $this->assertSame($name1, $this->backend->checkPassword($name1, 'pass1'));
  61. $this->assertSame($name2, $this->backend->checkPassword($name2, 'pass2'));
  62. $this->assertFalse($this->backend->checkPassword($name1, 'pass2'));
  63. $this->assertFalse($this->backend->checkPassword($name2, 'pass1'));
  64. $this->assertFalse($this->backend->checkPassword($name1, 'dummy'));
  65. $this->assertFalse($this->backend->checkPassword($name2, 'foobar'));
  66. $this->backend->setPassword($name1, 'newpass1');
  67. $this->assertFalse($this->backend->checkPassword($name1, 'pass1'));
  68. $this->assertSame($name1, $this->backend->checkPassword($name1, 'newpass1'));
  69. $this->assertFalse($this->backend->checkPassword($name2, 'newpass1'));
  70. }
  71. public function testSearch(): void {
  72. $name1 = 'foobarbaz';
  73. $name2 = 'bazbarfoo';
  74. $name3 = 'notme';
  75. $name4 = 'under_score';
  76. $this->backend->createUser($name1, 'pass1');
  77. $this->backend->createUser($name2, 'pass2');
  78. $this->backend->createUser($name3, 'pass3');
  79. $this->backend->createUser($name4, 'pass4');
  80. $result = $this->backend->getUsers('bar');
  81. $this->assertCount(2, $result);
  82. $result = $this->backend->getDisplayNames('bar');
  83. $this->assertCount(2, $result);
  84. $result = $this->backend->getUsers('under_');
  85. $this->assertCount(1, $result);
  86. $result = $this->backend->getUsers('not_');
  87. $this->assertCount(0, $result);
  88. }
  89. }