ConverterTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Bjoern Schiessle <bjoern@schiessle.org>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\DAV\Tests\unit\CardDAV;
  30. use OCA\DAV\CardDAV\Converter;
  31. use OCP\Accounts\IAccount;
  32. use OCP\Accounts\IAccountManager;
  33. use OCP\Accounts\IAccountProperty;
  34. use OCP\IImage;
  35. use OCP\IUser;
  36. use OCP\IUserManager;
  37. use PHPUnit\Framework\MockObject\MockObject;
  38. use Test\TestCase;
  39. class ConverterTest extends TestCase {
  40. /** @var IAccountManager|\PHPUnit\Framework\MockObject\MockObject */
  41. private $accountManager;
  42. /** @var IUserManager|(IUserManager&MockObject)|MockObject */
  43. private IUserManager|MockObject $userManager;
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $this->accountManager = $this->createMock(IAccountManager::class);
  47. $this->userManager = $this->createMock(IUserManager::class);
  48. }
  49. /**
  50. * @return IAccountProperty|MockObject
  51. */
  52. protected function getAccountPropertyMock(string $name, ?string $value, string $scope) {
  53. $property = $this->createMock(IAccountProperty::class);
  54. $property->expects($this->any())
  55. ->method('getName')
  56. ->willReturn($name);
  57. $property->expects($this->any())
  58. ->method('getValue')
  59. ->willReturn((string)$value);
  60. $property->expects($this->any())
  61. ->method('getScope')
  62. ->willReturn($scope);
  63. $property->expects($this->any())
  64. ->method('getVerified')
  65. ->willReturn(IAccountManager::NOT_VERIFIED);
  66. return $property;
  67. }
  68. public function getAccountManager(IUser $user) {
  69. $account = $this->createMock(IAccount::class);
  70. $account->expects($this->any())
  71. ->method('getAllProperties')
  72. ->willReturnCallback(function () use ($user) {
  73. yield $this->getAccountPropertyMock(IAccountManager::PROPERTY_DISPLAYNAME, $user->getDisplayName(), IAccountManager::SCOPE_FEDERATED);
  74. yield $this->getAccountPropertyMock(IAccountManager::PROPERTY_ADDRESS, '', IAccountManager::SCOPE_LOCAL);
  75. yield $this->getAccountPropertyMock(IAccountManager::PROPERTY_WEBSITE, '', IAccountManager::SCOPE_LOCAL);
  76. yield $this->getAccountPropertyMock(IAccountManager::PROPERTY_EMAIL, $user->getEMailAddress(), IAccountManager::SCOPE_FEDERATED);
  77. yield $this->getAccountPropertyMock(IAccountManager::PROPERTY_AVATAR, $user->getAvatarImage(-1)->data(), IAccountManager::SCOPE_FEDERATED);
  78. yield $this->getAccountPropertyMock(IAccountManager::PROPERTY_PHONE, '', IAccountManager::SCOPE_LOCAL);
  79. yield $this->getAccountPropertyMock(IAccountManager::PROPERTY_TWITTER, '', IAccountManager::SCOPE_LOCAL);
  80. });
  81. $accountManager = $this->getMockBuilder(IAccountManager::class)
  82. ->disableOriginalConstructor()->getMock();
  83. $accountManager->expects($this->any())->method('getAccount')->willReturn($account);
  84. return $accountManager;
  85. }
  86. /**
  87. * @dataProvider providesNewUsers
  88. */
  89. public function testCreation($expectedVCard, $displayName = null, $eMailAddress = null, $cloudId = null): void {
  90. $user = $this->getUserMock((string)$displayName, $eMailAddress, $cloudId);
  91. $accountManager = $this->getAccountManager($user);
  92. $converter = new Converter($accountManager, $this->userManager);
  93. $vCard = $converter->createCardFromUser($user);
  94. if ($expectedVCard !== null) {
  95. $this->assertInstanceOf('Sabre\VObject\Component\VCard', $vCard);
  96. $cardData = $vCard->jsonSerialize();
  97. $this->compareData($expectedVCard, $cardData);
  98. } else {
  99. $this->assertSame($expectedVCard, $vCard);
  100. }
  101. }
  102. public function testManagerProp(): void {
  103. $user = $this->getUserMock("user", "user@domain.tld", "user@cloud.domain.tld");
  104. $user->method('getManagerUids')
  105. ->willReturn(['mgr']);
  106. $this->userManager->expects(self::once())
  107. ->method('getDisplayName')
  108. ->with('mgr')
  109. ->willReturn('Manager');
  110. $accountManager = $this->getAccountManager($user);
  111. $converter = new Converter($accountManager, $this->userManager);
  112. $vCard = $converter->createCardFromUser($user);
  113. $this->compareData(
  114. [
  115. 'cloud' => 'user@cloud.domain.tld',
  116. 'email' => 'user@domain.tld',
  117. 'x-managersname' => 'Manager',
  118. ],
  119. $vCard->jsonSerialize()
  120. );
  121. }
  122. protected function compareData($expected, $data) {
  123. foreach ($expected as $key => $value) {
  124. $found = false;
  125. foreach ($data[1] as $d) {
  126. if ($d[0] === $key && $d[3] === $value) {
  127. $found = true;
  128. break;
  129. }
  130. }
  131. if (!$found) {
  132. $this->assertTrue(false, 'Expected data: ' . $key . ' not found.');
  133. }
  134. }
  135. }
  136. public function providesNewUsers() {
  137. return [
  138. [
  139. null
  140. ],
  141. [
  142. null,
  143. null,
  144. 'foo@bar.net'
  145. ],
  146. [
  147. [
  148. 'cloud' => 'foo@cloud.net',
  149. 'email' => 'foo@bar.net',
  150. 'photo' => 'MTIzNDU2Nzg5',
  151. ],
  152. null,
  153. 'foo@bar.net',
  154. 'foo@cloud.net'
  155. ],
  156. [
  157. [
  158. 'cloud' => 'foo@cloud.net',
  159. 'email' => 'foo@bar.net',
  160. 'fn' => 'Dr. Foo Bar',
  161. 'photo' => 'MTIzNDU2Nzg5',
  162. ],
  163. "Dr. Foo Bar",
  164. "foo@bar.net",
  165. 'foo@cloud.net'
  166. ],
  167. [
  168. [
  169. 'cloud' => 'foo@cloud.net',
  170. 'fn' => 'Dr. Foo Bar',
  171. 'photo' => 'MTIzNDU2Nzg5',
  172. ],
  173. "Dr. Foo Bar",
  174. null,
  175. "foo@cloud.net"
  176. ],
  177. [
  178. [
  179. 'cloud' => 'foo@cloud.net',
  180. 'fn' => 'Dr. Foo Bar',
  181. 'photo' => 'MTIzNDU2Nzg5',
  182. ],
  183. 'Dr. Foo Bar',
  184. '',
  185. 'foo@cloud.net'
  186. ],
  187. ];
  188. }
  189. /**
  190. * @dataProvider providesNames
  191. * @param $expected
  192. * @param $fullName
  193. */
  194. public function testNameSplitter($expected, $fullName): void {
  195. $converter = new Converter($this->accountManager, $this->userManager);
  196. $r = $converter->splitFullName($fullName);
  197. $r = implode(';', $r);
  198. $this->assertEquals($expected, $r);
  199. }
  200. public function providesNames() {
  201. return [
  202. ['Sauron;;;;', 'Sauron'],
  203. ['Baggins;Bilbo;;;', 'Bilbo Baggins'],
  204. ['Tolkien;John;Ronald Reuel;;', 'John Ronald Reuel Tolkien'],
  205. ];
  206. }
  207. /**
  208. * @param $displayName
  209. * @param $eMailAddress
  210. * @param $cloudId
  211. * @return IUser | \PHPUnit\Framework\MockObject\MockObject
  212. */
  213. protected function getUserMock(string $displayName, ?string $eMailAddress, ?string $cloudId) {
  214. $image0 = $this->getMockBuilder(IImage::class)->disableOriginalConstructor()->getMock();
  215. $image0->method('mimeType')->willReturn('image/jpeg');
  216. $image0->method('data')->willReturn('123456789');
  217. $user = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  218. $user->method('getUID')->willReturn('12345');
  219. $user->method('getDisplayName')->willReturn($displayName);
  220. $user->method('getEMailAddress')->willReturn($eMailAddress);
  221. $user->method('getCloudId')->willReturn($cloudId);
  222. $user->method('getAvatarImage')->willReturn($image0);
  223. return $user;
  224. }
  225. }