DeletedUsersIndexTest.php 3.5 KB

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