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.

ManagerTest.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * @copyright 2017, Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.com>
  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 Test\Calendar;
  24. use OC\Calendar\Manager;
  25. use OCP\Calendar\ICalendar;
  26. use Test\TestCase;
  27. class ManagerTest extends TestCase {
  28. /** @var Manager */
  29. private $manager;
  30. protected function setUp(): void {
  31. parent::setUp();
  32. $this->manager = new Manager();
  33. }
  34. /**
  35. * @dataProvider searchProvider
  36. */
  37. public function testSearch($search1, $search2, $expected) {
  38. /** @var ICalendar | \PHPUnit\Framework\MockObject\MockObject $calendar1 */
  39. $calendar1 = $this->createMock(ICalendar::class);
  40. $calendar1->method('getKey')->willReturn('simple:1');
  41. $calendar1->expects($this->once())
  42. ->method('search')
  43. ->with('', [], [], null, null)
  44. ->willReturn($search1);
  45. /** @var ICalendar | \PHPUnit\Framework\MockObject\MockObject $calendar2 */
  46. $calendar2 = $this->createMock(ICalendar::class);
  47. $calendar2->method('getKey')->willReturn('simple:2');
  48. $calendar2->expects($this->once())
  49. ->method('search')
  50. ->with('', [], [], null, null)
  51. ->willReturn($search2);
  52. $this->manager->registerCalendar($calendar1);
  53. $this->manager->registerCalendar($calendar2);
  54. $result = $this->manager->search('');
  55. $this->assertEquals($expected, $result);
  56. }
  57. /**
  58. * @dataProvider searchProvider
  59. */
  60. public function testSearchOptions($search1, $search2, $expected) {
  61. /** @var ICalendar | \PHPUnit\Framework\MockObject\MockObject $calendar1 */
  62. $calendar1 = $this->createMock(ICalendar::class);
  63. $calendar1->method('getKey')->willReturn('simple:1');
  64. $calendar1->expects($this->once())
  65. ->method('search')
  66. ->with('searchTerm', ['SUMMARY', 'DESCRIPTION'],
  67. ['timerange' => ['start' => null, 'end' => null]], 5, 20)
  68. ->willReturn($search1);
  69. /** @var ICalendar | \PHPUnit\Framework\MockObject\MockObject $calendar2 */
  70. $calendar2 = $this->createMock(ICalendar::class);
  71. $calendar2->method('getKey')->willReturn('simple:2');
  72. $calendar2->expects($this->once())
  73. ->method('search')
  74. ->with('searchTerm', ['SUMMARY', 'DESCRIPTION'],
  75. ['timerange' => ['start' => null, 'end' => null]], 5, 20)
  76. ->willReturn($search2);
  77. $this->manager->registerCalendar($calendar1);
  78. $this->manager->registerCalendar($calendar2);
  79. $result = $this->manager->search('searchTerm', ['SUMMARY', 'DESCRIPTION'],
  80. ['timerange' => ['start' => null, 'end' => null]], 5, 20);
  81. $this->assertEquals($expected, $result);
  82. }
  83. public function searchProvider() {
  84. $search1 = [
  85. [
  86. 'id' => 1,
  87. 'data' => 'foobar',
  88. ],
  89. [
  90. 'id' => 2,
  91. 'data' => 'barfoo',
  92. ]
  93. ];
  94. $search2 = [
  95. [
  96. 'id' => 3,
  97. 'data' => 'blablub',
  98. ],
  99. [
  100. 'id' => 4,
  101. 'data' => 'blubbla',
  102. ]
  103. ];
  104. $expected = [
  105. [
  106. 'id' => 1,
  107. 'data' => 'foobar',
  108. 'calendar-key' => 'simple:1',
  109. ],
  110. [
  111. 'id' => 2,
  112. 'data' => 'barfoo',
  113. 'calendar-key' => 'simple:1',
  114. ],
  115. [
  116. 'id' => 3,
  117. 'data' => 'blablub',
  118. 'calendar-key' => 'simple:2',
  119. ],
  120. [
  121. 'id' => 4,
  122. 'data' => 'blubbla',
  123. 'calendar-key' => 'simple:2',
  124. ]
  125. ];
  126. return [
  127. [
  128. $search1,
  129. $search2,
  130. $expected
  131. ]
  132. ];
  133. }
  134. public function testRegisterUnregister() {
  135. /** @var ICalendar | \PHPUnit\Framework\MockObject\MockObject $calendar1 */
  136. $calendar1 = $this->createMock(ICalendar::class);
  137. $calendar1->method('getKey')->willReturn('key1');
  138. /** @var ICalendar | \PHPUnit\Framework\MockObject\MockObject $calendar2 */
  139. $calendar2 = $this->createMock(ICalendar::class);
  140. $calendar2->method('getKey')->willReturn('key2');
  141. $this->manager->registerCalendar($calendar1);
  142. $this->manager->registerCalendar($calendar2);
  143. $result = $this->manager->getCalendars();
  144. $this->assertCount(2, $result);
  145. $this->assertContains($calendar1, $result);
  146. $this->assertContains($calendar2, $result);
  147. $this->manager->unregisterCalendar($calendar1);
  148. $result = $this->manager->getCalendars();
  149. $this->assertCount(1, $result);
  150. $this->assertContains($calendar2, $result);
  151. }
  152. public function testGetCalendars() {
  153. /** @var ICalendar | \PHPUnit\Framework\MockObject\MockObject $calendar1 */
  154. $calendar1 = $this->createMock(ICalendar::class);
  155. $calendar1->method('getKey')->willReturn('key1');
  156. /** @var ICalendar | \PHPUnit\Framework\MockObject\MockObject $calendar2 */
  157. $calendar2 = $this->createMock(ICalendar::class);
  158. $calendar2->method('getKey')->willReturn('key2');
  159. $this->manager->registerCalendar($calendar1);
  160. $this->manager->registerCalendar($calendar2);
  161. $result = $this->manager->getCalendars();
  162. $this->assertCount(2, $result);
  163. $this->assertContains($calendar1, $result);
  164. $this->assertContains($calendar2, $result);
  165. $this->manager->clear();
  166. $result = $this->manager->getCalendars();
  167. $this->assertCount(0, $result);
  168. }
  169. public function testEnabledIfNot() {
  170. $isEnabled = $this->manager->isEnabled();
  171. $this->assertFalse($isEnabled);
  172. }
  173. public function testIfEnabledIfSo() {
  174. /** @var ICalendar | \PHPUnit\Framework\MockObject\MockObject $calendar */
  175. $calendar = $this->createMock(ICalendar::class);
  176. $this->manager->registerCalendar($calendar);
  177. $isEnabled = $this->manager->isEnabled();
  178. $this->assertTrue($isEnabled);
  179. }
  180. }