ConverterTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCA\DAV\Tests\unit\CardDAV;
  9. use OCA\DAV\CardDAV\Converter;
  10. use OCP\Accounts\IAccount;
  11. use OCP\Accounts\IAccountManager;
  12. use OCP\Accounts\IAccountProperty;
  13. use OCP\IImage;
  14. use OCP\IURLGenerator;
  15. use OCP\IUser;
  16. use OCP\IUserManager;
  17. use PHPUnit\Framework\MockObject\MockObject;
  18. use Psr\Log\LoggerInterface;
  19. use Test\TestCase;
  20. class ConverterTest extends TestCase {
  21. /** @var IAccountManager|\PHPUnit\Framework\MockObject\MockObject */
  22. private $accountManager;
  23. /** @var IUserManager|(IUserManager&MockObject)|MockObject */
  24. private IUserManager|MockObject $userManager;
  25. /** @var IURLGenerator */
  26. private $urlGenerator;
  27. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  28. private $logger;
  29. protected function setUp(): void {
  30. parent::setUp();
  31. $this->accountManager = $this->createMock(IAccountManager::class);
  32. $this->userManager = $this->createMock(IUserManager::class);
  33. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  34. $this->logger = $this->createMock(LoggerInterface::class);
  35. }
  36. /**
  37. * @return IAccountProperty|MockObject
  38. */
  39. protected function getAccountPropertyMock(string $name, ?string $value, string $scope) {
  40. $property = $this->createMock(IAccountProperty::class);
  41. $property->expects($this->any())
  42. ->method('getName')
  43. ->willReturn($name);
  44. $property->expects($this->any())
  45. ->method('getValue')
  46. ->willReturn((string)$value);
  47. $property->expects($this->any())
  48. ->method('getScope')
  49. ->willReturn($scope);
  50. $property->expects($this->any())
  51. ->method('getVerified')
  52. ->willReturn(IAccountManager::NOT_VERIFIED);
  53. return $property;
  54. }
  55. public function getAccountManager(IUser $user) {
  56. $account = $this->createMock(IAccount::class);
  57. $account->expects($this->any())
  58. ->method('getAllProperties')
  59. ->willReturnCallback(function () use ($user) {
  60. yield $this->getAccountPropertyMock(IAccountManager::PROPERTY_DISPLAYNAME, $user->getDisplayName(), IAccountManager::SCOPE_FEDERATED);
  61. yield $this->getAccountPropertyMock(IAccountManager::PROPERTY_ADDRESS, '', IAccountManager::SCOPE_LOCAL);
  62. yield $this->getAccountPropertyMock(IAccountManager::PROPERTY_WEBSITE, '', IAccountManager::SCOPE_LOCAL);
  63. yield $this->getAccountPropertyMock(IAccountManager::PROPERTY_EMAIL, $user->getEMailAddress(), IAccountManager::SCOPE_FEDERATED);
  64. yield $this->getAccountPropertyMock(IAccountManager::PROPERTY_AVATAR, $user->getAvatarImage(-1)->data(), IAccountManager::SCOPE_FEDERATED);
  65. yield $this->getAccountPropertyMock(IAccountManager::PROPERTY_PHONE, '', IAccountManager::SCOPE_LOCAL);
  66. yield $this->getAccountPropertyMock(IAccountManager::PROPERTY_TWITTER, '', IAccountManager::SCOPE_LOCAL);
  67. });
  68. $accountManager = $this->getMockBuilder(IAccountManager::class)
  69. ->disableOriginalConstructor()->getMock();
  70. $accountManager->expects($this->any())->method('getAccount')->willReturn($account);
  71. return $accountManager;
  72. }
  73. /**
  74. * @dataProvider providesNewUsers
  75. */
  76. public function testCreation($expectedVCard, $displayName = null, $eMailAddress = null, $cloudId = null): void {
  77. $user = $this->getUserMock((string)$displayName, $eMailAddress, $cloudId);
  78. $accountManager = $this->getAccountManager($user);
  79. $converter = new Converter($accountManager, $this->userManager, $this->urlGenerator, $this->logger);
  80. $vCard = $converter->createCardFromUser($user);
  81. if ($expectedVCard !== null) {
  82. $this->assertInstanceOf('Sabre\VObject\Component\VCard', $vCard);
  83. $cardData = $vCard->jsonSerialize();
  84. $this->compareData($expectedVCard, $cardData);
  85. } else {
  86. $this->assertSame($expectedVCard, $vCard);
  87. }
  88. }
  89. public function testManagerProp(): void {
  90. $user = $this->getUserMock("user", "user@domain.tld", "user@cloud.domain.tld");
  91. $user->method('getManagerUids')
  92. ->willReturn(['mgr']);
  93. $this->userManager->expects(self::once())
  94. ->method('getDisplayName')
  95. ->with('mgr')
  96. ->willReturn('Manager');
  97. $accountManager = $this->getAccountManager($user);
  98. $converter = new Converter($accountManager, $this->userManager, $this->urlGenerator, $this->logger);
  99. $vCard = $converter->createCardFromUser($user);
  100. $this->compareData(
  101. [
  102. 'cloud' => 'user@cloud.domain.tld',
  103. 'email' => 'user@domain.tld',
  104. 'x-managersname' => 'Manager',
  105. ],
  106. $vCard->jsonSerialize()
  107. );
  108. }
  109. protected function compareData($expected, $data) {
  110. foreach ($expected as $key => $value) {
  111. $found = false;
  112. foreach ($data[1] as $d) {
  113. if ($d[0] === $key && $d[3] === $value) {
  114. $found = true;
  115. break;
  116. }
  117. }
  118. if (!$found) {
  119. $this->assertTrue(false, 'Expected data: ' . $key . ' not found.');
  120. }
  121. }
  122. }
  123. public function providesNewUsers() {
  124. return [
  125. [
  126. null
  127. ],
  128. [
  129. null,
  130. null,
  131. 'foo@bar.net'
  132. ],
  133. [
  134. [
  135. 'cloud' => 'foo@cloud.net',
  136. 'email' => 'foo@bar.net',
  137. 'photo' => 'MTIzNDU2Nzg5',
  138. ],
  139. null,
  140. 'foo@bar.net',
  141. 'foo@cloud.net'
  142. ],
  143. [
  144. [
  145. 'cloud' => 'foo@cloud.net',
  146. 'email' => 'foo@bar.net',
  147. 'fn' => 'Dr. Foo Bar',
  148. 'photo' => 'MTIzNDU2Nzg5',
  149. ],
  150. "Dr. Foo Bar",
  151. "foo@bar.net",
  152. 'foo@cloud.net'
  153. ],
  154. [
  155. [
  156. 'cloud' => 'foo@cloud.net',
  157. 'fn' => 'Dr. Foo Bar',
  158. 'photo' => 'MTIzNDU2Nzg5',
  159. ],
  160. "Dr. Foo Bar",
  161. null,
  162. "foo@cloud.net"
  163. ],
  164. [
  165. [
  166. 'cloud' => 'foo@cloud.net',
  167. 'fn' => 'Dr. Foo Bar',
  168. 'photo' => 'MTIzNDU2Nzg5',
  169. ],
  170. 'Dr. Foo Bar',
  171. '',
  172. 'foo@cloud.net'
  173. ],
  174. ];
  175. }
  176. /**
  177. * @dataProvider providesNames
  178. * @param $expected
  179. * @param $fullName
  180. */
  181. public function testNameSplitter($expected, $fullName): void {
  182. $converter = new Converter($this->accountManager, $this->userManager, $this->urlGenerator, $this->logger);
  183. $r = $converter->splitFullName($fullName);
  184. $r = implode(';', $r);
  185. $this->assertEquals($expected, $r);
  186. }
  187. public function providesNames() {
  188. return [
  189. ['Sauron;;;;', 'Sauron'],
  190. ['Baggins;Bilbo;;;', 'Bilbo Baggins'],
  191. ['Tolkien;John;Ronald Reuel;;', 'John Ronald Reuel Tolkien'],
  192. ];
  193. }
  194. /**
  195. * @param $displayName
  196. * @param $eMailAddress
  197. * @param $cloudId
  198. * @return IUser | \PHPUnit\Framework\MockObject\MockObject
  199. */
  200. protected function getUserMock(string $displayName, ?string $eMailAddress, ?string $cloudId) {
  201. $image0 = $this->getMockBuilder(IImage::class)->disableOriginalConstructor()->getMock();
  202. $image0->method('mimeType')->willReturn('image/jpeg');
  203. $image0->method('data')->willReturn('123456789');
  204. $user = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  205. $user->method('getUID')->willReturn('12345');
  206. $user->method('getDisplayName')->willReturn($displayName);
  207. $user->method('getEMailAddress')->willReturn($eMailAddress);
  208. $user->method('getCloudId')->willReturn($cloudId);
  209. $user->method('getAvatarImage')->willReturn($image0);
  210. return $user;
  211. }
  212. }