ManagerTest.php 6.1 KB

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