BirthdayServiceTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\DAV\Tests\unit\CardDAV;
  25. use OCA\DAV\CalDAV\BirthdayService;
  26. use OCA\DAV\CalDAV\CalDavBackend;
  27. use OCA\DAV\CardDAV\CardDavBackend;
  28. use OCA\DAV\DAV\GroupPrincipalBackend;
  29. use Sabre\VObject\Component\VCalendar;
  30. use Sabre\VObject\Reader;
  31. use Test\TestCase;
  32. class BirthdayServiceTest extends TestCase {
  33. /** @var BirthdayService */
  34. private $service;
  35. /** @var CalDavBackend | \PHPUnit_Framework_MockObject_MockObject */
  36. private $calDav;
  37. /** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject */
  38. private $cardDav;
  39. /** @var GroupPrincipalBackend | \PHPUnit_Framework_MockObject_MockObject */
  40. private $groupPrincipalBackend;
  41. public function setUp() {
  42. parent::setUp();
  43. $this->calDav = $this->getMockBuilder(CalDavBackend::class)->disableOriginalConstructor()->getMock();
  44. $this->cardDav = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
  45. $this->groupPrincipalBackend = $this->getMockBuilder(GroupPrincipalBackend::class)->disableOriginalConstructor()->getMock();
  46. $this->service = new BirthdayService($this->calDav, $this->cardDav, $this->groupPrincipalBackend);
  47. }
  48. /**
  49. * @dataProvider providesVCards
  50. * @param boolean $expectedSummary
  51. * @param string | null $data
  52. */
  53. public function testBuildBirthdayFromContact($expectedSummary, $data) {
  54. $cal = $this->service->buildDateFromContact($data, 'BDAY', '*');
  55. if ($expectedSummary === null) {
  56. $this->assertNull($cal);
  57. } else {
  58. $this->assertInstanceOf('Sabre\VObject\Component\VCalendar', $cal);
  59. $this->assertTrue(isset($cal->VEVENT));
  60. $this->assertEquals('FREQ=YEARLY', $cal->VEVENT->RRULE->getValue());
  61. $this->assertEquals($expectedSummary, $cal->VEVENT->SUMMARY->getValue());
  62. $this->assertEquals('TRANSPARENT', $cal->VEVENT->TRANSP->getValue());
  63. }
  64. }
  65. public function testOnCardDeleted() {
  66. $this->cardDav->expects($this->once())->method('getAddressBookById')
  67. ->with(666)
  68. ->willReturn([
  69. 'principaluri' => 'principals/users/user01',
  70. 'uri' => 'default'
  71. ]);
  72. $this->calDav->expects($this->once())->method('getCalendarByUri')
  73. ->with('principals/users/user01', 'contact_birthdays')
  74. ->willReturn([
  75. 'id' => 1234
  76. ]);
  77. $this->calDav->expects($this->at(1))->method('deleteCalendarObject')->with(1234, 'default-gump.vcf.ics');
  78. $this->calDav->expects($this->at(2))->method('deleteCalendarObject')->with(1234, 'default-gump.vcf-death.ics');
  79. $this->calDav->expects($this->at(3))->method('deleteCalendarObject')->with(1234, 'default-gump.vcf-anniversary.ics');
  80. $this->cardDav->expects($this->once())->method('getShares')->willReturn([]);
  81. $this->service->onCardDeleted(666, 'gump.vcf');
  82. }
  83. /**
  84. * @dataProvider providesCardChanges
  85. */
  86. public function testOnCardChanged($expectedOp) {
  87. $this->cardDav->expects($this->once())->method('getAddressBookById')
  88. ->with(666)
  89. ->willReturn([
  90. 'principaluri' => 'principals/users/user01',
  91. 'uri' => 'default'
  92. ]);
  93. $this->calDav->expects($this->once())->method('getCalendarByUri')
  94. ->with('principals/users/user01', 'contact_birthdays')
  95. ->willReturn([
  96. 'id' => 1234
  97. ]);
  98. $this->cardDav->expects($this->once())->method('getShares')->willReturn([]);
  99. /** @var BirthdayService | \PHPUnit_Framework_MockObject_MockObject $service */
  100. $service = $this->getMockBuilder(BirthdayService::class)
  101. ->setMethods(['buildDateFromContact', 'birthdayEvenChanged'])
  102. ->setConstructorArgs([$this->calDav, $this->cardDav, $this->groupPrincipalBackend])
  103. ->getMock();
  104. if ($expectedOp === 'delete') {
  105. $this->calDav->expects($this->exactly(3))->method('getCalendarObject')->willReturn('');
  106. $service->expects($this->exactly(3))->method('buildDateFromContact')->willReturn(null);
  107. $this->calDav->expects($this->exactly(3))->method('deleteCalendarObject')->withConsecutive(
  108. [1234, 'default-gump.vcf.ics'],
  109. [1234, 'default-gump.vcf-death.ics'],
  110. [1234, 'default-gump.vcf-anniversary.ics']
  111. );
  112. }
  113. if ($expectedOp === 'create') {
  114. $service->expects($this->exactly(3))->method('buildDateFromContact')->willReturn(new VCalendar());
  115. $this->calDav->expects($this->exactly(3))->method('createCalendarObject')->withConsecutive(
  116. [1234, 'default-gump.vcf.ics', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nCALSCALE:GREGORIAN\r\nEND:VCALENDAR\r\n"],
  117. [1234, 'default-gump.vcf-death.ics', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nCALSCALE:GREGORIAN\r\nEND:VCALENDAR\r\n"],
  118. [1234, 'default-gump.vcf-anniversary.ics', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nCALSCALE:GREGORIAN\r\nEND:VCALENDAR\r\n"]
  119. );
  120. }
  121. if ($expectedOp === 'update') {
  122. $service->expects($this->exactly(3))->method('buildDateFromContact')->willReturn(new VCalendar());
  123. $service->expects($this->exactly(3))->method('birthdayEvenChanged')->willReturn(true);
  124. $this->calDav->expects($this->exactly(3))->method('getCalendarObject')->willReturn(['calendardata' => '']);
  125. $this->calDav->expects($this->exactly(3))->method('updateCalendarObject')->withConsecutive(
  126. [1234, 'default-gump.vcf.ics', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nCALSCALE:GREGORIAN\r\nEND:VCALENDAR\r\n"],
  127. [1234, 'default-gump.vcf-death.ics', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nCALSCALE:GREGORIAN\r\nEND:VCALENDAR\r\n"],
  128. [1234, 'default-gump.vcf-anniversary.ics', "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nCALSCALE:GREGORIAN\r\nEND:VCALENDAR\r\n"]
  129. );
  130. }
  131. $service->onCardChanged(666, 'gump.vcf', '');
  132. }
  133. /**
  134. * @dataProvider providesBirthday
  135. * @param $expected
  136. * @param $old
  137. * @param $new
  138. */
  139. public function testBirthdayEvenChanged($expected, $old, $new) {
  140. $new = Reader::read($new);
  141. $this->assertEquals($expected, $this->service->birthdayEvenChanged($old, $new));
  142. }
  143. public function testGetAllAffectedPrincipals() {
  144. $this->cardDav->expects($this->once())->method('getShares')->willReturn([
  145. [
  146. '{http://owncloud.org/ns}group-share' => false,
  147. '{http://owncloud.org/ns}principal' => 'principals/users/user01'
  148. ],
  149. [
  150. '{http://owncloud.org/ns}group-share' => false,
  151. '{http://owncloud.org/ns}principal' => 'principals/users/user01'
  152. ],
  153. [
  154. '{http://owncloud.org/ns}group-share' => false,
  155. '{http://owncloud.org/ns}principal' => 'principals/users/user02'
  156. ],
  157. [
  158. '{http://owncloud.org/ns}group-share' => true,
  159. '{http://owncloud.org/ns}principal' => 'principals/groups/users'
  160. ],
  161. ]);
  162. $this->groupPrincipalBackend->expects($this->once())->method('getGroupMemberSet')
  163. ->willReturn([
  164. [
  165. 'uri' => 'principals/users/user01',
  166. ],
  167. [
  168. 'uri' => 'principals/users/user02',
  169. ],
  170. [
  171. 'uri' => 'principals/users/user03',
  172. ],
  173. ]);
  174. $users = $this->invokePrivate($this->service, 'getAllAffectedPrincipals', [6666]);
  175. $this->assertEquals([
  176. 'principals/users/user01',
  177. 'principals/users/user02',
  178. 'principals/users/user03'
  179. ], $users);
  180. }
  181. public function testBirthdayCalendarHasComponentEvent() {
  182. $this->calDav->expects($this->once())
  183. ->method('createCalendar')
  184. ->with('principal001', 'contact_birthdays', [
  185. '{DAV:}displayname' => 'Contact birthdays',
  186. '{http://apple.com/ns/ical/}calendar-color' => '#FFFFCA',
  187. 'components' => 'VEVENT',
  188. ]);
  189. $this->service->ensureCalendarExists('principal001');
  190. }
  191. public function providesBirthday() {
  192. return [
  193. [true,
  194. '',
  195. "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nCALSCALE:GREGORIAN\r\nBEGIN:VEVENT\r\nUID:12345\r\nDTSTAMP:20160218T133704Z\r\nDTSTART;VALUE=DATE:19000101\r\nDTEND;VALUE=DATE:19000102\r\nRRULE:FREQ=YEARLY\r\nSUMMARY:12345's Birthday (1900)\r\nTRANSP:TRANSPARENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"],
  196. [false,
  197. "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nCALSCALE:GREGORIAN\r\nBEGIN:VEVENT\r\nUID:12345\r\nDTSTAMP:20160218T133704Z\r\nDTSTART;VALUE=DATE:19000101\r\nDTEND;VALUE=DATE:19000102\r\nRRULE:FREQ=YEARLY\r\nSUMMARY:12345's Birthday (1900)\r\nTRANSP:TRANSPARENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n",
  198. "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nCALSCALE:GREGORIAN\r\nBEGIN:VEVENT\r\nUID:12345\r\nDTSTAMP:20160218T133704Z\r\nDTSTART;VALUE=DATE:19000101\r\nDTEND;VALUE=DATE:19000102\r\nRRULE:FREQ=YEARLY\r\nSUMMARY:12345's Birthday (1900)\r\nTRANSP:TRANSPARENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"],
  199. [true,
  200. "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nCALSCALE:GREGORIAN\r\nBEGIN:VEVENT\r\nUID:12345\r\nDTSTAMP:20160218T133704Z\r\nDTSTART;VALUE=DATE:19000101\r\nDTEND;VALUE=DATE:19000102\r\nRRULE:FREQ=YEARLY\r\nSUMMARY:4567's Birthday (1900)\r\nTRANSP:TRANSPARENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n",
  201. "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nCALSCALE:GREGORIAN\r\nBEGIN:VEVENT\r\nUID:12345\r\nDTSTAMP:20160218T133704Z\r\nDTSTART;VALUE=DATE:19000101\r\nDTEND;VALUE=DATE:19000102\r\nRRULE:FREQ=YEARLY\r\nSUMMARY:12345's Birthday (1900)\r\nTRANSP:TRANSPARENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"],
  202. [true,
  203. "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nCALSCALE:GREGORIAN\r\nBEGIN:VEVENT\r\nUID:12345\r\nDTSTAMP:20160218T133704Z\r\nDTSTART;VALUE=DATE:19000101\r\nDTEND;VALUE=DATE:19000102\r\nRRULE:FREQ=YEARLY\r\nSUMMARY:12345's Birthday (1900)\r\nTRANSP:TRANSPARENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n",
  204. "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nCALSCALE:GREGORIAN\r\nBEGIN:VEVENT\r\nUID:12345\r\nDTSTAMP:20160218T133704Z\r\nDTSTART;VALUE=DATE:19000102\r\nDTEND;VALUE=DATE:19000102\r\nRRULE:FREQ=YEARLY\r\nSUMMARY:12345's Birthday (1900)\r\nTRANSP:TRANSPARENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"]
  205. ];
  206. }
  207. public function providesCardChanges(){
  208. return[
  209. ['delete'],
  210. ['create'],
  211. ['update']
  212. ];
  213. }
  214. public function providesVCards() {
  215. return [
  216. [null, null],
  217. [null, ''],
  218. [null, 'yasfewf'],
  219. [null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
  220. [null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
  221. [null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:someday\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
  222. ['12345 (*1900)', "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:19000101\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
  223. ['12345 (*1900)', "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:19001231\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
  224. ['12345 *', "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:--1231\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
  225. ['12345 *', "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY;X-APPLE-OMIT-YEAR=1604:16041231\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
  226. [null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:;VALUE=text:circa 1800\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
  227. [null, "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nN:12345;;;;\r\nBDAY:20031231\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
  228. ['12345 (*900)', "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nBDAY:09001231\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
  229. ];
  230. }
  231. }