You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConverterTest.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\DAV\Tests\unit\CardDAV;
  27. use OC\Accounts\AccountManager;
  28. use OCA\DAV\CardDAV\Converter;
  29. use OCP\IDBConnection;
  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. public function setUp() {
  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) $this->assertTrue(false, 'Expected data: ' . $key . ' not found.');
  110. }
  111. }
  112. public function providesNewUsers() {
  113. return [
  114. [
  115. null
  116. ],
  117. [
  118. null,
  119. null,
  120. 'foo@bar.net'
  121. ],
  122. [
  123. [
  124. 'cloud' => 'foo@cloud.net',
  125. 'email' => 'foo@bar.net',
  126. 'photo' => 'MTIzNDU2Nzg5',
  127. ],
  128. null,
  129. 'foo@bar.net',
  130. 'foo@cloud.net'
  131. ],
  132. [
  133. [
  134. 'cloud' => 'foo@cloud.net',
  135. 'email' => 'foo@bar.net',
  136. 'fn' => 'Dr. Foo Bar',
  137. 'photo' => 'MTIzNDU2Nzg5',
  138. ],
  139. "Dr. Foo Bar",
  140. "foo@bar.net",
  141. 'foo@cloud.net'
  142. ],
  143. [
  144. [
  145. 'cloud' => 'foo@cloud.net',
  146. 'fn' => 'Dr. Foo Bar',
  147. 'photo' => 'MTIzNDU2Nzg5',
  148. ],
  149. "Dr. Foo Bar",
  150. null,
  151. "foo@cloud.net"
  152. ],
  153. [
  154. [
  155. 'cloud' => 'foo@cloud.net',
  156. 'fn' => 'Dr. Foo Bar',
  157. 'photo' => 'MTIzNDU2Nzg5',
  158. ],
  159. 'Dr. Foo Bar',
  160. '',
  161. 'foo@cloud.net'
  162. ],
  163. ];
  164. }
  165. /**
  166. * @dataProvider providesNames
  167. * @param $expected
  168. * @param $fullName
  169. */
  170. public function testNameSplitter($expected, $fullName) {
  171. $converter = new Converter($this->accountManager);
  172. $r = $converter->splitFullName($fullName);
  173. $r = implode(';', $r);
  174. $this->assertEquals($expected, $r);
  175. }
  176. public function providesNames() {
  177. return [
  178. ['Sauron;;;;', 'Sauron'],
  179. ['Baggins;Bilbo;;;', 'Bilbo Baggins'],
  180. ['Tolkien;John;Ronald Reuel;;', 'John Ronald Reuel Tolkien'],
  181. ];
  182. }
  183. /**
  184. * @param $displayName
  185. * @param $eMailAddress
  186. * @param $cloudId
  187. * @return IUser | PHPUnit_Framework_MockObject_MockObject
  188. */
  189. protected function getUserMock($displayName, $eMailAddress, $cloudId) {
  190. $image0 = $this->getMockBuilder(IImage::class)->disableOriginalConstructor()->getMock();
  191. $image0->method('mimeType')->willReturn('image/jpeg');
  192. $image0->method('data')->willReturn('123456789');
  193. $user = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
  194. $user->method('getUID')->willReturn('12345');
  195. $user->method('getDisplayName')->willReturn($displayName);
  196. $user->method('getEMailAddress')->willReturn($eMailAddress);
  197. $user->method('getCloudId')->willReturn($cloudId);
  198. $user->method('getAvatarImage')->willReturn($image0);
  199. return $user;
  200. }
  201. }