1
0

ConverterTest.php 5.7 KB

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