database.php 2.8 KB

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