DatabaseTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /**
  27. * Class DatabaseTest
  28. *
  29. * @group DB
  30. */
  31. class DatabaseTest extends Backend {
  32. /** @var array */
  33. private $users;
  34. /** @var EventDispatcher | \PHPUnit_Framework_MockObject_MockObject */
  35. private $eventDispatcher;
  36. public function getUser() {
  37. $user = parent::getUser();
  38. $this->users[]=$user;
  39. return $user;
  40. }
  41. protected function setUp() {
  42. parent::setUp();
  43. $this->eventDispatcher = $this->createMock(EventDispatcher::class);
  44. $this->backend=new \OC\User\Database($this->eventDispatcher);
  45. }
  46. protected function tearDown() {
  47. if(!isset($this->users)) {
  48. return;
  49. }
  50. foreach($this->users as $user) {
  51. $this->backend->deleteUser($user);
  52. }
  53. parent::tearDown();
  54. }
  55. public function testVerifyPasswordEvent() {
  56. $user = $this->getUser();
  57. $this->backend->createUser($user, 'pass1');
  58. $this->eventDispatcher->expects($this->once())->method('dispatch')
  59. ->willReturnCallback(
  60. function ($eventName, GenericEvent $event) {
  61. $this->assertSame('OCP\PasswordPolicy::validate', $eventName);
  62. $this->assertSame('newpass', $event->getSubject());
  63. }
  64. );
  65. $this->backend->setPassword($user, 'newpass');
  66. $this->assertSame($user, $this->backend->checkPassword($user, 'newpass'));
  67. }
  68. /**
  69. * @expectedException \OC\HintException
  70. * @expectedExceptionMessage password change failed
  71. */
  72. public function testVerifyPasswordEventFail() {
  73. $user = $this->getUser();
  74. $this->backend->createUser($user, 'pass1');
  75. $this->eventDispatcher->expects($this->once())->method('dispatch')
  76. ->willReturnCallback(
  77. function ($eventName, GenericEvent $event) {
  78. $this->assertSame('OCP\PasswordPolicy::validate', $eventName);
  79. $this->assertSame('newpass', $event->getSubject());
  80. throw new HintException('password change failed', 'password change failed');
  81. }
  82. );
  83. $this->backend->setPassword($user, 'newpass');
  84. $this->assertSame($user, $this->backend->checkPassword($user, 'newpass'));
  85. }
  86. }