1
0

RecentContactMapperTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\ContactsInteraction\Tests\Db;
  8. use OCA\ContactsInteraction\Db\RecentContact;
  9. use OCA\ContactsInteraction\Db\RecentContactMapper;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. use OCP\IUser;
  12. use Sabre\VObject\Component\VCard;
  13. use Sabre\VObject\UUIDUtil;
  14. use Test\TestCase;
  15. /**
  16. * @group DB
  17. */
  18. class RecentContactMapperTest extends TestCase {
  19. /** @var RecentContactMapper */
  20. private $recentContactMapper;
  21. /** @var ITimeFactory */
  22. private $time;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $this->recentContactMapper = \OC::$server->get(RecentContactMapper::class);
  26. $this->time = \OC::$server->get(ITimeFactory::class);
  27. }
  28. protected function tearDown(): void {
  29. parent::tearDown();
  30. $this->recentContactMapper->cleanUp(time());
  31. }
  32. public function testCreateRecentContact(): void {
  33. $contact = $this->createRecentContact();
  34. $this->assertNotNull($contact->getId());
  35. }
  36. public function testFindAll(): void {
  37. $this->createRecentContact();
  38. $this->createRecentContact();
  39. $contacts = $this->recentContactMapper->findAll('admin');
  40. $this->assertCount(2, $contacts);
  41. }
  42. public function testFindMatchByEmail(): void {
  43. $this->createRecentContact();
  44. $contact = $this->createRecentContact('foo@bar');
  45. $user = $this->createMock(IUser::class);
  46. $user->method('getUID')
  47. ->willReturn('admin');
  48. $result = $this->recentContactMapper->findMatch($user, null, 'foo@bar', null);
  49. $this->assertCount(1, $result);
  50. $this->assertEquals($contact->getId(), $result[0]->getId());
  51. }
  52. public function testFindMatchByFederatedCloudId(): void {
  53. $this->createRecentContact();
  54. $contact = $this->createRecentContact(null, 'foo.bar');
  55. $user = $this->createMock(IUser::class);
  56. $user->method('getUID')
  57. ->willReturn('admin');
  58. $result = $this->recentContactMapper->findMatch($user, null, null, 'foo.bar');
  59. $this->assertCount(1, $result);
  60. $this->assertEquals($contact->getId(), $result[0]->getId());
  61. }
  62. public function testCleanUp(): void {
  63. $this->createRecentContact();
  64. $this->createRecentContact();
  65. $this->assertCount(2, $this->recentContactMapper->findAll('admin'));
  66. $this->recentContactMapper->cleanUp(time());
  67. $this->assertCount(0, $this->recentContactMapper->findAll('admin'));
  68. }
  69. protected function createRecentContact(?string $email = null, ?string $federatedCloudId = null): RecentContact {
  70. $props = [
  71. 'URI' => UUIDUtil::getUUID(),
  72. 'FN' => 'Foo Bar',
  73. 'CATEGORIES' => 'Recently contacted',
  74. ];
  75. $time = $this->time->getDateTime();
  76. $time->modify('-14days');
  77. $contact = new RecentContact();
  78. $contact->setActorUid('admin');
  79. $contact->setCard((new VCard($props))->serialize());
  80. $contact->setLastContact($time->getTimestamp());
  81. if ($email !== null) {
  82. $contact->setEmail($email);
  83. }
  84. if ($federatedCloudId !== null) {
  85. $contact->setFederatedCloudId($federatedCloudId);
  86. }
  87. return $this->recentContactMapper->insert($contact);
  88. }
  89. }