1
0

DeletedUsersIndexTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\User_LDAP\Tests\User;
  7. use OCA\User_LDAP\Mapping\UserMapping;
  8. use OCA\User_LDAP\User\DeletedUsersIndex;
  9. use OCP\IConfig;
  10. use OCP\IDBConnection;
  11. use OCP\Share\IManager;
  12. /**
  13. * Class DeletedUsersIndexTest
  14. *
  15. * @group DB
  16. *
  17. * @package OCA\User_LDAP\Tests\User
  18. */
  19. class DeletedUsersIndexTest extends \Test\TestCase {
  20. /** @var DeletedUsersIndex */
  21. protected $dui;
  22. /** @var IConfig */
  23. protected $config;
  24. /** @var IDBConnection */
  25. protected $db;
  26. /** @var UserMapping|\PHPUnit\Framework\MockObject\MockObject */
  27. protected $mapping;
  28. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  29. protected $shareManager;
  30. protected function setUp(): void {
  31. parent::setUp();
  32. // no mocks for those as tests go against DB
  33. $this->config = \OC::$server->getConfig();
  34. $this->db = \OC::$server->getDatabaseConnection();
  35. // ensure a clean database
  36. $this->config->deleteAppFromAllUsers('user_ldap');
  37. $this->mapping = $this->createMock(UserMapping::class);
  38. $this->shareManager = $this->createMock(IManager::class);
  39. $this->dui = new DeletedUsersIndex($this->config, $this->mapping, $this->shareManager);
  40. }
  41. protected function tearDown(): void {
  42. $this->config->deleteAppFromAllUsers('user_ldap');
  43. parent::tearDown();
  44. }
  45. public function testMarkAndFetchUser(): void {
  46. $uids = [
  47. 'cef3775c-71d2-48eb-8984-39a4051b0b95',
  48. '8c4bbb40-33ed-42d0-9b14-85b0ab76c1cc',
  49. ];
  50. // ensure test works on a pristine state
  51. $this->assertFalse($this->dui->hasUsers());
  52. $this->dui->markUser($uids[0]);
  53. $this->assertTrue($this->dui->hasUsers());
  54. $this->dui->markUser($uids[1]);
  55. $deletedUsers = $this->dui->getUsers();
  56. $this->assertSame(2, count($deletedUsers));
  57. // ensure the different uids were used
  58. foreach ($deletedUsers as $deletedUser) {
  59. $this->assertTrue(in_array($deletedUser->getOCName(), $uids));
  60. $i = array_search($deletedUser->getOCName(), $uids);
  61. $this->assertNotFalse($i);
  62. unset($uids[$i]);
  63. }
  64. $this->assertEmpty($uids);
  65. }
  66. public function testUnmarkUser(): void {
  67. $uids = [
  68. '22a162c7-a9ee-487c-9f33-0563795583fb',
  69. '1fb4e0da-4a75-47f3-8fa7-becc7e35c9c5',
  70. ];
  71. // we know this works, because of "testMarkAndFetchUser"
  72. $this->dui->markUser($uids[0]);
  73. // this returns a working instance of OfflineUser
  74. $testUser = $this->dui->getUsers()[0];
  75. $testUser->unmark();
  76. // the DUI caches the users, to clear mark someone else
  77. $this->dui->markUser($uids[1]);
  78. $deletedUsers = $this->dui->getUsers();
  79. foreach ($deletedUsers as $deletedUser) {
  80. $this->assertNotSame($testUser->getOCName(), $deletedUser->getOCName());
  81. }
  82. }
  83. }