You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RecentContactMapperTest.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Daniel Kesselberg <mail@danielkesselberg.de>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program 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 License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\ContactsInteraction\Tests\Db;
  23. use OCA\ContactsInteraction\Db\RecentContact;
  24. use OCA\ContactsInteraction\Db\RecentContactMapper;
  25. use OCP\AppFramework\Utility\ITimeFactory;
  26. use OCP\IUser;
  27. use Sabre\VObject\Component\VCard;
  28. use Sabre\VObject\UUIDUtil;
  29. use Test\TestCase;
  30. /**
  31. * @group DB
  32. */
  33. class RecentContactMapperTest extends TestCase {
  34. /** @var RecentContactMapper */
  35. private $recentContactMapper;
  36. /** @var ITimeFactory */
  37. private $time;
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $this->recentContactMapper = \OC::$server->get(RecentContactMapper::class);
  41. $this->time = \OC::$server->get(ITimeFactory::class);
  42. }
  43. protected function tearDown(): void {
  44. parent::tearDown();
  45. $this->recentContactMapper->cleanUp(time());
  46. }
  47. public function testCreateRecentContact(): void {
  48. $contact = $this->createRecentContact();
  49. $this->assertNotNull($contact->getId());
  50. }
  51. public function testFindAll(): void {
  52. $this->createRecentContact();
  53. $this->createRecentContact();
  54. $contacts = $this->recentContactMapper->findAll('admin');
  55. $this->assertCount(2, $contacts);
  56. }
  57. public function testFindMatchByEmail(): void {
  58. $this->createRecentContact();
  59. $contact = $this->createRecentContact('foo@bar');
  60. $user = $this->createMock(IUser::class);
  61. $user->method('getUID')
  62. ->willReturn('admin');
  63. $result = $this->recentContactMapper->findMatch($user, null, 'foo@bar', null);
  64. $this->assertCount(1, $result);
  65. $this->assertEquals($contact->getId(), $result[0]->getId());
  66. }
  67. public function testFindMatchByFederatedCloudId(): void {
  68. $this->createRecentContact();
  69. $contact = $this->createRecentContact(null, 'foo.bar');
  70. $user = $this->createMock(IUser::class);
  71. $user->method('getUID')
  72. ->willReturn('admin');
  73. $result = $this->recentContactMapper->findMatch($user, null, null, 'foo.bar');
  74. $this->assertCount(1, $result);
  75. $this->assertEquals($contact->getId(), $result[0]->getId());
  76. }
  77. public function testCleanUp(): void {
  78. $this->createRecentContact();
  79. $this->createRecentContact();
  80. $this->assertCount(2, $this->recentContactMapper->findAll('admin'));
  81. $this->recentContactMapper->cleanUp(time());
  82. $this->assertCount(0, $this->recentContactMapper->findAll('admin'));
  83. }
  84. protected function createRecentContact(string $email = null, string $federatedCloudId = null): RecentContact {
  85. $props = [
  86. 'URI' => UUIDUtil::getUUID(),
  87. 'FN' => 'Foo Bar',
  88. 'CATEGORIES' => 'Recently contacted',
  89. ];
  90. $time = $this->time->getDateTime();
  91. $time->modify('-14days');
  92. $contact = new RecentContact();
  93. $contact->setActorUid('admin');
  94. $contact->setCard((new VCard($props))->serialize());
  95. $contact->setLastContact($time->getTimestamp());
  96. if ($email !== null) {
  97. $contact->setEmail($email);
  98. }
  99. if ($federatedCloudId !== null) {
  100. $contact->setFederatedCloudId($federatedCloudId);
  101. }
  102. return $this->recentContactMapper->insert($contact);
  103. }
  104. }