ContactsStoreTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
  4. *
  5. * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Tests\Contacts\ContactsMenu;
  24. use OC\Contacts\ContactsMenu\ContactsStore;
  25. use OCP\Contacts\IManager;
  26. use OCP\IUser;
  27. use PHPUnit_Framework_MockObject_MockObject;
  28. use Test\TestCase;
  29. class ContactsStoreTest extends TestCase {
  30. /** @var ContactsStore */
  31. private $contactsStore;
  32. /** @var IManager|PHPUnit_Framework_MockObject_MockObject */
  33. private $contactsManager;
  34. protected function setUp() {
  35. parent::setUp();
  36. $this->contactsManager = $this->createMock(IManager::class);
  37. $this->contactsStore = new ContactsStore($this->contactsManager);
  38. }
  39. public function testGetContactsWithoutFilter() {
  40. $user = $this->createMock(IUser::class);
  41. $this->contactsManager->expects($this->once())
  42. ->method('search')
  43. ->with($this->equalTo(''), $this->equalTo(['FN']))
  44. ->willReturn([
  45. [
  46. 'UID' => 123,
  47. ],
  48. [
  49. 'UID' => 567,
  50. 'FN' => 'Darren Roner',
  51. 'EMAIL' => [
  52. 'darren@roner.au'
  53. ],
  54. ],
  55. ]);
  56. $user->expects($this->once())
  57. ->method('getUID')
  58. ->willReturn('user123');
  59. $entries = $this->contactsStore->getContacts($user, '');
  60. $this->assertCount(2, $entries);
  61. $this->assertEquals([
  62. 'darren@roner.au'
  63. ], $entries[1]->getEMailAddresses());
  64. }
  65. public function testGetContactsHidesOwnEntry() {
  66. $user = $this->createMock(IUser::class);
  67. $this->contactsManager->expects($this->once())
  68. ->method('search')
  69. ->with($this->equalTo(''), $this->equalTo(['FN']))
  70. ->willReturn([
  71. [
  72. 'UID' => 'user123',
  73. ],
  74. [
  75. 'UID' => 567,
  76. 'FN' => 'Darren Roner',
  77. 'EMAIL' => [
  78. 'darren@roner.au'
  79. ],
  80. ],
  81. ]);
  82. $user->expects($this->once())
  83. ->method('getUID')
  84. ->willReturn('user123');
  85. $entries = $this->contactsStore->getContacts($user, '');
  86. $this->assertCount(1, $entries);
  87. }
  88. public function testGetContactsWithoutBinaryImage() {
  89. $user = $this->createMock(IUser::class);
  90. $this->contactsManager->expects($this->once())
  91. ->method('search')
  92. ->with($this->equalTo(''), $this->equalTo(['FN']))
  93. ->willReturn([
  94. [
  95. 'UID' => 123,
  96. ],
  97. [
  98. 'UID' => 567,
  99. 'FN' => 'Darren Roner',
  100. 'EMAIL' => [
  101. 'darren@roner.au'
  102. ],
  103. 'PHOTO' => base64_encode('photophotophoto'),
  104. ],
  105. ]);
  106. $user->expects($this->once())
  107. ->method('getUID')
  108. ->willReturn('user123');
  109. $entries = $this->contactsStore->getContacts($user, '');
  110. $this->assertCount(2, $entries);
  111. $this->assertNull($entries[1]->getAvatar());
  112. }
  113. public function testGetContactsWithoutAvatarURI() {
  114. $user = $this->createMock(IUser::class);
  115. $this->contactsManager->expects($this->once())
  116. ->method('search')
  117. ->with($this->equalTo(''), $this->equalTo(['FN']))
  118. ->willReturn([
  119. [
  120. 'UID' => 123,
  121. ],
  122. [
  123. 'UID' => 567,
  124. 'FN' => 'Darren Roner',
  125. 'EMAIL' => [
  126. 'darren@roner.au'
  127. ],
  128. 'PHOTO' => 'VALUE=uri:https://photo',
  129. ],
  130. ]);
  131. $user->expects($this->once())
  132. ->method('getUID')
  133. ->willReturn('user123');
  134. $entries = $this->contactsStore->getContacts($user, '');
  135. $this->assertCount(2, $entries);
  136. $this->assertEquals('https://photo', $entries[1]->getAvatar());
  137. }
  138. public function testFindOneUser() {
  139. $user = $this->createMock(IUser::class);
  140. $this->contactsManager->expects($this->once())
  141. ->method('search')
  142. ->with($this->equalTo('a567'), $this->equalTo(['UID']))
  143. ->willReturn([
  144. [
  145. 'UID' => 123,
  146. 'isLocalSystemBook' => false
  147. ],
  148. [
  149. 'UID' => 'a567',
  150. 'FN' => 'Darren Roner',
  151. 'EMAIL' => [
  152. 'darren@roner.au'
  153. ],
  154. 'isLocalSystemBook' => true
  155. ],
  156. ]);
  157. $user->expects($this->once())
  158. ->method('getUID')
  159. ->willReturn('user123');
  160. $entry = $this->contactsStore->findOne($user, 0, 'a567');
  161. $this->assertEquals([
  162. 'darren@roner.au'
  163. ], $entry->getEMailAddresses());
  164. }
  165. public function testFindOneEMail() {
  166. $user = $this->createMock(IUser::class);
  167. $this->contactsManager->expects($this->once())
  168. ->method('search')
  169. ->with($this->equalTo('darren@roner.au'), $this->equalTo(['EMAIL']))
  170. ->willReturn([
  171. [
  172. 'UID' => 123,
  173. 'isLocalSystemBook' => false
  174. ],
  175. [
  176. 'UID' => 'a567',
  177. 'FN' => 'Darren Roner',
  178. 'EMAIL' => [
  179. 'darren@roner.au'
  180. ],
  181. 'isLocalSystemBook' => false
  182. ],
  183. ]);
  184. $user->expects($this->once())
  185. ->method('getUID')
  186. ->willReturn('user123');
  187. $entry = $this->contactsStore->findOne($user, 4, 'darren@roner.au');
  188. $this->assertEquals([
  189. 'darren@roner.au'
  190. ], $entry->getEMailAddresses());
  191. }
  192. public function testFindOneNotSupportedType() {
  193. $user = $this->createMock(IUser::class);
  194. $entry = $this->contactsStore->findOne($user, 42, 'darren@roner.au');
  195. $this->assertEquals(null, $entry);
  196. }
  197. public function testFindOneNoMatches() {
  198. $user = $this->createMock(IUser::class);
  199. $this->contactsManager->expects($this->once())
  200. ->method('search')
  201. ->with($this->equalTo('a567'), $this->equalTo(['UID']))
  202. ->willReturn([
  203. [
  204. 'UID' => 123,
  205. 'isLocalSystemBook' => false
  206. ],
  207. [
  208. 'UID' => 'a567',
  209. 'FN' => 'Darren Roner',
  210. 'EMAIL' => [
  211. 'darren@roner.au123'
  212. ],
  213. 'isLocalSystemBook' => false
  214. ],
  215. ]);
  216. $user->expects($this->once())
  217. ->method('getUID')
  218. ->willReturn('user123');
  219. $entry = $this->contactsStore->findOne($user, 0, 'a567');
  220. $this->assertEquals(null, $entry);
  221. }
  222. }