1
0

RecentContactMapperTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Daniel Kesselberg <mail@danielkesselberg.de>
  5. *
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  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\ContactsInteraction\Tests\Db;
  25. use OCA\ContactsInteraction\Db\RecentContact;
  26. use OCA\ContactsInteraction\Db\RecentContactMapper;
  27. use OCP\AppFramework\Utility\ITimeFactory;
  28. use OCP\IUser;
  29. use Sabre\VObject\Component\VCard;
  30. use Sabre\VObject\UUIDUtil;
  31. use Test\TestCase;
  32. /**
  33. * @group DB
  34. */
  35. class RecentContactMapperTest extends TestCase {
  36. /** @var RecentContactMapper */
  37. private $recentContactMapper;
  38. /** @var ITimeFactory */
  39. private $time;
  40. protected function setUp(): void {
  41. parent::setUp();
  42. $this->recentContactMapper = \OC::$server->get(RecentContactMapper::class);
  43. $this->time = \OC::$server->get(ITimeFactory::class);
  44. }
  45. protected function tearDown(): void {
  46. parent::tearDown();
  47. $this->recentContactMapper->cleanUp(time());
  48. }
  49. public function testCreateRecentContact(): void {
  50. $contact = $this->createRecentContact();
  51. $this->assertNotNull($contact->getId());
  52. }
  53. public function testFindAll(): void {
  54. $this->createRecentContact();
  55. $this->createRecentContact();
  56. $contacts = $this->recentContactMapper->findAll('admin');
  57. $this->assertCount(2, $contacts);
  58. }
  59. public function testFindMatchByEmail(): void {
  60. $this->createRecentContact();
  61. $contact = $this->createRecentContact('foo@bar');
  62. $user = $this->createMock(IUser::class);
  63. $user->method('getUID')
  64. ->willReturn('admin');
  65. $result = $this->recentContactMapper->findMatch($user, null, 'foo@bar', null);
  66. $this->assertCount(1, $result);
  67. $this->assertEquals($contact->getId(), $result[0]->getId());
  68. }
  69. public function testFindMatchByFederatedCloudId(): void {
  70. $this->createRecentContact();
  71. $contact = $this->createRecentContact(null, 'foo.bar');
  72. $user = $this->createMock(IUser::class);
  73. $user->method('getUID')
  74. ->willReturn('admin');
  75. $result = $this->recentContactMapper->findMatch($user, null, null, 'foo.bar');
  76. $this->assertCount(1, $result);
  77. $this->assertEquals($contact->getId(), $result[0]->getId());
  78. }
  79. public function testCleanUp(): void {
  80. $this->createRecentContact();
  81. $this->createRecentContact();
  82. $this->assertCount(2, $this->recentContactMapper->findAll('admin'));
  83. $this->recentContactMapper->cleanUp(time());
  84. $this->assertCount(0, $this->recentContactMapper->findAll('admin'));
  85. }
  86. protected function createRecentContact(string $email = null, string $federatedCloudId = null): RecentContact {
  87. $props = [
  88. 'URI' => UUIDUtil::getUUID(),
  89. 'FN' => 'Foo Bar',
  90. 'CATEGORIES' => 'Recently contacted',
  91. ];
  92. $time = $this->time->getDateTime();
  93. $time->modify('-14days');
  94. $contact = new RecentContact();
  95. $contact->setActorUid('admin');
  96. $contact->setCard((new VCard($props))->serialize());
  97. $contact->setLastContact($time->getTimestamp());
  98. if ($email !== null) {
  99. $contact->setEmail($email);
  100. }
  101. if ($federatedCloudId !== null) {
  102. $contact->setFederatedCloudId($federatedCloudId);
  103. }
  104. return $this->recentContactMapper->insert($contact);
  105. }
  106. }