IMipServiceTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2017, Georg Ehrke
  5. *
  6. * @author brad2014 <brad2014@users.noreply.github.com>
  7. * @author Brad Rubenstein <brad@wbr.tech>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Georg Ehrke <oc.list@georgehrke.com>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Thomas Citharel <nextcloud@tcit.fr>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OCA\DAV\Tests\unit\CalDAV\Schedule;
  31. use OC\L10N\L10N;
  32. use OC\L10N\LazyL10N;
  33. use OC\URLGenerator;
  34. use OCA\DAV\CalDAV\Schedule\IMipService;
  35. use OCP\IConfig;
  36. use OCP\IDBConnection;
  37. use OCP\L10N\IFactory as L10NFactory;
  38. use OCP\Security\ISecureRandom;
  39. use PHPUnit\Framework\MockObject\MockObject;
  40. use Sabre\VObject\Component\VCalendar;
  41. use Sabre\VObject\Component\VEvent;
  42. use Sabre\VObject\Property\ICalendar\DateTime;
  43. use Test\TestCase;
  44. class IMipServiceTest extends TestCase {
  45. /** @var URLGenerator|MockObject */
  46. private $urlGenerator;
  47. /** @var IConfig|MockObject */
  48. private $config;
  49. /** @var IDBConnection|MockObject */
  50. private $db;
  51. /** @var ISecureRandom|MockObject */
  52. private $random;
  53. /** @var L10NFactory|MockObject */
  54. private $l10nFactory;
  55. /** @var L10N|MockObject */
  56. private $l10n;
  57. /** @var IMipService */
  58. private $service;
  59. protected function setUp(): void {
  60. $this->urlGenerator = $this->createMock(URLGenerator::class);
  61. $this->config = $this->createMock(IConfig::class);
  62. $this->db = $this->createMock(IDBConnection::class);
  63. $this->random = $this->createMock(ISecureRandom::class);
  64. $this->l10nFactory = $this->createMock(L10NFactory::class);
  65. $this->l10n = $this->createMock(LazyL10N::class);
  66. $this->l10nFactory->expects(self::once())
  67. ->method('findGenericLanguage')
  68. ->willReturn('en');
  69. $this->l10nFactory->expects(self::once())
  70. ->method('get')
  71. ->with('dav', 'en')
  72. ->willReturn($this->l10n);
  73. $this->service = new IMipService(
  74. $this->urlGenerator,
  75. $this->config,
  76. $this->db,
  77. $this->random,
  78. $this->l10nFactory
  79. );
  80. }
  81. public function testGetFrom(): void {
  82. $senderName = "Detective McQueen";
  83. $default = "Twin Lakes Police Department - Darkside Division";
  84. $expected = "Detective McQueen via Twin Lakes Police Department - Darkside Division";
  85. $this->l10n->expects(self::once())
  86. ->method('t')
  87. ->willReturn($expected);
  88. $actual = $this->service->getFrom($senderName, $default);
  89. $this->assertEquals($expected, $actual);
  90. }
  91. public function testBuildBodyDataCreated(): void {
  92. $vCalendar = new VCalendar();
  93. $oldVevent = null;
  94. $newVevent = new VEvent($vCalendar, 'two', [
  95. 'UID' => 'uid-1234',
  96. 'SEQUENCE' => 3,
  97. 'LAST-MODIFIED' => 789456,
  98. 'SUMMARY' => 'Second Breakfast',
  99. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  100. 'RECURRENCE-ID' => new \DateTime('2016-01-01 00:00:00')
  101. ]);
  102. $expected = [
  103. 'meeting_when' => $this->service->generateWhenString($newVevent),
  104. 'meeting_description' => '',
  105. 'meeting_title' => 'Second Breakfast',
  106. 'meeting_location' => '',
  107. 'meeting_url' => '',
  108. 'meeting_url_html' => '',
  109. ];
  110. $actual = $this->service->buildBodyData($newVevent, $oldVevent);
  111. $this->assertEquals($expected, $actual);
  112. }
  113. public function testBuildBodyDataUpdate(): void {
  114. $vCalendar = new VCalendar();
  115. $oldVevent = new VEvent($vCalendar, 'two', [
  116. 'UID' => 'uid-1234',
  117. 'SEQUENCE' => 1,
  118. 'LAST-MODIFIED' => 456789,
  119. 'SUMMARY' => 'Elevenses',
  120. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  121. 'RECURRENCE-ID' => new \DateTime('2016-01-01 00:00:00')
  122. ]);
  123. $oldVevent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
  124. $oldVevent->add('ATTENDEE', 'mailto:' . 'frodo@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'Frodo']);
  125. $newVevent = new VEvent($vCalendar, 'two', [
  126. 'UID' => 'uid-1234',
  127. 'SEQUENCE' => 3,
  128. 'LAST-MODIFIED' => 789456,
  129. 'SUMMARY' => 'Second Breakfast',
  130. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  131. 'RECURRENCE-ID' => new \DateTime('2016-01-01 00:00:00')
  132. ]);
  133. $expected = [
  134. 'meeting_when' => $this->service->generateWhenString($newVevent),
  135. 'meeting_description' => '',
  136. 'meeting_title' => 'Second Breakfast',
  137. 'meeting_location' => '',
  138. 'meeting_url' => '',
  139. 'meeting_url_html' => '',
  140. 'meeting_when_html' => $this->service->generateWhenString($newVevent),
  141. 'meeting_title_html' => sprintf("<span style='text-decoration: line-through'>%s</span><br />%s", 'Elevenses', 'Second Breakfast'),
  142. 'meeting_description_html' => '',
  143. 'meeting_location_html' => ''
  144. ];
  145. $actual = $this->service->buildBodyData($newVevent, $oldVevent);
  146. $this->assertEquals($expected, $actual);
  147. }
  148. public function testGenerateWhenStringHourlyEvent(): void {
  149. $vCalendar = new VCalendar();
  150. $vevent = new VEvent($vCalendar, 'two', [
  151. 'UID' => 'uid-1234',
  152. 'SEQUENCE' => 1,
  153. 'LAST-MODIFIED' => 456789,
  154. 'SUMMARY' => 'Elevenses',
  155. 'TZID' => 'Europe/Vienna',
  156. 'DTSTART' => (new \DateTime('2016-01-01 08:00:00'))->setTimezone(new \DateTimeZone('Europe/Vienna')),
  157. 'DTEND' => (new \DateTime('2016-01-01 09:00:00'))->setTimezone(new \DateTimeZone('Europe/Vienna')),
  158. ]);
  159. $this->l10n->expects(self::exactly(3))
  160. ->method('l')
  161. ->withConsecutive(
  162. ['weekdayName', (new \DateTime('2016-01-01 08:00:00'))->setTimezone(new \DateTimeZone('Europe/Vienna')), ['width' => 'abbreviated']],
  163. ['datetime', (new \DateTime('2016-01-01 08:00:00'))->setTimezone(new \DateTimeZone('Europe/Vienna')), ['width' => 'medium|short']],
  164. ['time', (new \DateTime('2016-01-01 09:00:00'))->setTimezone(new \DateTimeZone('Europe/Vienna')), ['width' => 'short']]
  165. )->willReturnOnConsecutiveCalls(
  166. 'Fr.',
  167. '01.01. 08:00',
  168. '09:00'
  169. );
  170. $expected = 'Fr., 01.01. 08:00 - 09:00 (Europe/Vienna)';
  171. $actual = $this->service->generateWhenString($vevent);
  172. $this->assertEquals($expected, $actual);
  173. }
  174. public function testGetLastOccurrenceRRULE(): void {
  175. $vCalendar = new VCalendar();
  176. $vCalendar->add('VEVENT', [
  177. 'UID' => 'uid-1234',
  178. 'LAST-MODIFIED' => 123456,
  179. 'SEQUENCE' => 2,
  180. 'SUMMARY' => 'Fellowship meeting',
  181. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  182. 'RRULE' => 'FREQ=DAILY;INTERVAL=1;UNTIL=20160201T000000Z',
  183. ]);
  184. $occurrence = $this->service->getLastOccurrence($vCalendar);
  185. $this->assertEquals(1454284800, $occurrence);
  186. }
  187. public function testGetLastOccurrenceEndDate(): void {
  188. $vCalendar = new VCalendar();
  189. $vCalendar->add('VEVENT', [
  190. 'UID' => 'uid-1234',
  191. 'LAST-MODIFIED' => 123456,
  192. 'SEQUENCE' => 2,
  193. 'SUMMARY' => 'Fellowship meeting',
  194. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  195. 'DTEND' => new \DateTime('2017-01-01 00:00:00'),
  196. ]);
  197. $occurrence = $this->service->getLastOccurrence($vCalendar);
  198. $this->assertEquals(1483228800, $occurrence);
  199. }
  200. public function testGetLastOccurrenceDuration(): void {
  201. $vCalendar = new VCalendar();
  202. $vCalendar->add('VEVENT', [
  203. 'UID' => 'uid-1234',
  204. 'LAST-MODIFIED' => 123456,
  205. 'SEQUENCE' => 2,
  206. 'SUMMARY' => 'Fellowship meeting',
  207. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  208. 'DURATION' => 'P12W',
  209. ]);
  210. $occurrence = $this->service->getLastOccurrence($vCalendar);
  211. $this->assertEquals(1458864000, $occurrence);
  212. }
  213. public function testGetLastOccurrenceAllDay(): void {
  214. $vCalendar = new VCalendar();
  215. $vEvent = $vCalendar->add('VEVENT', [
  216. 'UID' => 'uid-1234',
  217. 'LAST-MODIFIED' => 123456,
  218. 'SEQUENCE' => 2,
  219. 'SUMMARY' => 'Fellowship meeting',
  220. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  221. ]);
  222. // rewrite from DateTime to Date
  223. $vEvent->DTSTART['VALUE'] = 'DATE';
  224. $occurrence = $this->service->getLastOccurrence($vCalendar);
  225. $this->assertEquals(1451692800, $occurrence);
  226. }
  227. public function testGetLastOccurrenceFallback(): void {
  228. $vCalendar = new VCalendar();
  229. $vCalendar->add('VEVENT', [
  230. 'UID' => 'uid-1234',
  231. 'LAST-MODIFIED' => 123456,
  232. 'SEQUENCE' => 2,
  233. 'SUMMARY' => 'Fellowship meeting',
  234. 'DTSTART' => new \DateTime('2016-01-01 00:00:00'),
  235. ]);
  236. $occurrence = $this->service->getLastOccurrence($vCalendar);
  237. $this->assertEquals(1451606400, $occurrence);
  238. }
  239. }