ConverterTest.php 7.6 KB

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