Backend.php 4.0 KB

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