1
0

DeletedUsersIndexTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\User_LDAP\Tests\User;
  25. use OCA\User_LDAP\Mapping\UserMapping;
  26. use OCA\User_LDAP\User\DeletedUsersIndex;
  27. use OCP\IConfig;
  28. use OCP\IDBConnection;
  29. /**
  30. * Class DeletedUsersIndexTest
  31. *
  32. * @group DB
  33. *
  34. * @package OCA\User_LDAP\Tests\User
  35. */
  36. class DeletedUsersIndexTest extends \Test\TestCase {
  37. /** @var DeletedUsersIndex */
  38. protected $dui;
  39. /** @var IConfig */
  40. protected $config;
  41. /** @var IDBConnection */
  42. protected $db;
  43. /** @var UserMapping|\PHPUnit_Framework_MockObject_MockObject */
  44. protected $mapping;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. // no mocks for those as tests go against DB
  48. $this->config = \OC::$server->getConfig();
  49. $this->db = \OC::$server->getDatabaseConnection();
  50. // ensure a clean database
  51. $this->config->deleteAppFromAllUsers('user_ldap');
  52. $this->mapping = $this->createMock(UserMapping::class);
  53. $this->dui = new DeletedUsersIndex($this->config, $this->db, $this->mapping);
  54. }
  55. protected function tearDown(): void {
  56. $this->config->deleteAppFromAllUsers('user_ldap');
  57. parent::tearDown();
  58. }
  59. public function testMarkAndFetchUser() {
  60. $uids = [
  61. 'cef3775c-71d2-48eb-8984-39a4051b0b95',
  62. '8c4bbb40-33ed-42d0-9b14-85b0ab76c1cc',
  63. ];
  64. // ensure test works on a pristine state
  65. $this->assertFalse($this->dui->hasUsers());
  66. $this->dui->markUser($uids[0]);
  67. $this->assertTrue($this->dui->hasUsers());
  68. $this->dui->markUser($uids[1]);
  69. $deletedUsers = $this->dui->getUsers();
  70. $this->assertSame(2, count($deletedUsers));
  71. // ensure the different uids were used
  72. foreach($deletedUsers as $deletedUser) {
  73. $this->assertTrue(in_array($deletedUser->getOCName(), $uids));
  74. $i = array_search($deletedUser->getOCName(), $uids);
  75. $this->assertNotFalse($i);
  76. unset($uids[$i]);
  77. }
  78. $this->assertEmpty($uids);
  79. }
  80. public function testUnmarkUser() {
  81. $uids = [
  82. '22a162c7-a9ee-487c-9f33-0563795583fb',
  83. '1fb4e0da-4a75-47f3-8fa7-becc7e35c9c5',
  84. ];
  85. // we know this works, because of "testMarkAndFetchUser"
  86. $this->dui->markUser($uids[0]);
  87. // this returns a working instance of OfflineUser
  88. $testUser = $this->dui->getUsers()[0];
  89. $testUser->unmark();
  90. // the DUI caches the users, to clear mark someone else
  91. $this->dui->markUser($uids[1]);
  92. $deletedUsers = $this->dui->getUsers();
  93. foreach ($deletedUsers as $deletedUser) {
  94. $this->assertNotSame($testUser->getOCName(), $deletedUser->getOCName());
  95. }
  96. }
  97. }