1
0

ConverterTest.php 5.6 KB

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