DatabaseTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Robin Appelman
  6. * @copyright 2012 Robin Appelman icewind@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\User;
  23. use OC\HintException;
  24. use Symfony\Component\EventDispatcher\EventDispatcher;
  25. use Symfony\Component\EventDispatcher\GenericEvent;
  26. use OC\User\User;
  27. /**
  28. * Class DatabaseTest
  29. *
  30. * @group DB
  31. */
  32. class DatabaseTest extends Backend {
  33. /** @var array */
  34. private $users;
  35. /** @var EventDispatcher | \PHPUnit_Framework_MockObject_MockObject */
  36. private $eventDispatcher;
  37. public function getUser() {
  38. $user = parent::getUser();
  39. $this->users[]=$user;
  40. return $user;
  41. }
  42. protected function setUp() {
  43. parent::setUp();
  44. $this->eventDispatcher = $this->createMock(EventDispatcher::class);
  45. $this->backend=new \OC\User\Database($this->eventDispatcher);
  46. }
  47. protected function tearDown() {
  48. if(!isset($this->users)) {
  49. return;
  50. }
  51. foreach($this->users as $user) {
  52. $this->backend->deleteUser($user);
  53. }
  54. parent::tearDown();
  55. }
  56. public function testVerifyPasswordEvent() {
  57. $user = $this->getUser();
  58. $this->backend->createUser($user, 'pass1');
  59. $this->eventDispatcher->expects($this->once())->method('dispatch')
  60. ->willReturnCallback(
  61. function ($eventName, GenericEvent $event) {
  62. $this->assertSame('OCP\PasswordPolicy::validate', $eventName);
  63. $this->assertSame('newpass', $event->getSubject());
  64. }
  65. );
  66. $this->backend->setPassword($user, 'newpass');
  67. $this->assertSame($user, $this->backend->checkPassword($user, 'newpass'));
  68. }
  69. /**
  70. * @expectedException \OC\HintException
  71. * @expectedExceptionMessage password change failed
  72. */
  73. public function testVerifyPasswordEventFail() {
  74. $user = $this->getUser();
  75. $this->backend->createUser($user, 'pass1');
  76. $this->eventDispatcher->expects($this->once())->method('dispatch')
  77. ->willReturnCallback(
  78. function ($eventName, GenericEvent $event) {
  79. $this->assertSame('OCP\PasswordPolicy::validate', $eventName);
  80. $this->assertSame('newpass', $event->getSubject());
  81. throw new HintException('password change failed', 'password change failed');
  82. }
  83. );
  84. $this->backend->setPassword($user, 'newpass');
  85. $this->assertSame($user, $this->backend->checkPassword($user, 'newpass'));
  86. }
  87. public function testCreateUserInvalidatesCache() {
  88. $user1 = $this->getUniqueID('test_');
  89. $this->assertFalse($this->backend->userExists($user1));
  90. $this->backend->createUser($user1, 'pw');
  91. $this->assertTrue($this->backend->userExists($user1));
  92. }
  93. public function testDeleteUserInvalidatesCache() {
  94. $user1 = $this->getUniqueID('test_');
  95. $this->backend->createUser($user1, 'pw');
  96. $this->assertTrue($this->backend->userExists($user1));
  97. $this->backend->deleteUser($user1);
  98. $this->assertFalse($this->backend->userExists($user1));
  99. $this->backend->createUser($user1, 'pw2');
  100. $this->assertTrue($this->backend->userExists($user1));
  101. }
  102. public function testSearch() {
  103. parent::testSearch();
  104. $user1 = $this->getUser();
  105. $this->backend->createUser($user1, 'pass1');
  106. $user2 = $this->getUser();
  107. $this->backend->createUser($user2, 'pass1');
  108. $user1Obj = new User($user1, $this->backend);
  109. $user2Obj = new User($user2, $this->backend);
  110. $emailAddr1 = "$user1@nextcloud.com";
  111. $emailAddr2 = "$user2@nextcloud.com";
  112. $user1Obj->setDisplayName('User 1 Display');
  113. $result = $this->backend->getDisplayNames('display');
  114. $this->assertCount(1, $result);
  115. $result = $this->backend->getDisplayNames(strtoupper($user1));
  116. $this->assertCount(1, $result);
  117. $user1Obj->setEMailAddress($emailAddr1);
  118. $user2Obj->setEMailAddress($emailAddr2);
  119. $result = $this->backend->getUsers('@nextcloud.com');
  120. $this->assertCount(2, $result);
  121. $result = $this->backend->getDisplayNames('@nextcloud.com');
  122. $this->assertCount(2, $result);
  123. $result = $this->backend->getDisplayNames('@nextcloud.COM');
  124. $this->assertCount(2, $result);
  125. }
  126. }