EnablePluginTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Tests\unit\CalDAV\BirthdayCalendar;
  7. use OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin;
  8. use OCA\DAV\CalDAV\BirthdayService;
  9. use OCA\DAV\CalDAV\Calendar;
  10. use OCA\DAV\CalDAV\CalendarHome;
  11. use OCP\IConfig;
  12. use OCP\IUser;
  13. use Test\TestCase;
  14. class EnablePluginTest extends TestCase {
  15. /** @var \Sabre\DAV\Server|\PHPUnit\Framework\MockObject\MockObject */
  16. protected $server;
  17. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  18. protected $config;
  19. /** @var BirthdayService |\PHPUnit\Framework\MockObject\MockObject */
  20. protected $birthdayService;
  21. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
  22. protected $user;
  23. /** @var EnablePlugin $plugin */
  24. protected $plugin;
  25. protected $request;
  26. protected $response;
  27. protected function setUp(): void {
  28. parent::setUp();
  29. $this->server = $this->createMock(\Sabre\DAV\Server::class);
  30. $this->server->tree = $this->createMock(\Sabre\DAV\Tree::class);
  31. $this->server->httpResponse = $this->createMock(\Sabre\HTTP\Response::class);
  32. $this->server->xml = $this->createMock(\Sabre\DAV\Xml\Service::class);
  33. $this->config = $this->createMock(IConfig::class);
  34. $this->birthdayService = $this->createMock(BirthdayService::class);
  35. $this->user = $this->createMock(IUser::class);
  36. $this->plugin = new EnablePlugin($this->config, $this->birthdayService, $this->user);
  37. $this->plugin->initialize($this->server);
  38. $this->request = $this->createMock(\Sabre\HTTP\RequestInterface::class);
  39. $this->response = $this->createMock(\Sabre\HTTP\ResponseInterface::class);
  40. }
  41. public function testGetFeatures(): void {
  42. $this->assertEquals(['nc-enable-birthday-calendar'], $this->plugin->getFeatures());
  43. }
  44. public function testGetName(): void {
  45. $this->assertEquals('nc-enable-birthday-calendar', $this->plugin->getPluginName());
  46. }
  47. public function testInitialize(): void {
  48. $server = $this->createMock(\Sabre\DAV\Server::class);
  49. $plugin = new EnablePlugin($this->config, $this->birthdayService, $this->user);
  50. $server->expects($this->once())
  51. ->method('on')
  52. ->with('method:POST', [$plugin, 'httpPost']);
  53. $plugin->initialize($server);
  54. }
  55. public function testHttpPostNoCalendarHome(): void {
  56. $calendar = $this->createMock(Calendar::class);
  57. $this->server->expects($this->once())
  58. ->method('getRequestUri')
  59. ->willReturn('/bar/foo');
  60. $this->server->tree->expects($this->once())
  61. ->method('getNodeForPath')
  62. ->with('/bar/foo')
  63. ->willReturn($calendar);
  64. $this->config->expects($this->never())
  65. ->method('setUserValue');
  66. $this->birthdayService->expects($this->never())
  67. ->method('syncUser');
  68. $this->plugin->httpPost($this->request, $this->response);
  69. }
  70. public function testHttpPostWrongRequest(): void {
  71. $calendarHome = $this->createMock(CalendarHome::class);
  72. $this->server->expects($this->once())
  73. ->method('getRequestUri')
  74. ->willReturn('/bar/foo');
  75. $this->server->tree->expects($this->once())
  76. ->method('getNodeForPath')
  77. ->with('/bar/foo')
  78. ->willReturn($calendarHome);
  79. $this->request->expects($this->once())
  80. ->method('getBodyAsString')
  81. ->willReturn('<nc:disable-birthday-calendar xmlns:nc="http://nextcloud.com/ns"/>');
  82. $this->request->expects($this->once())
  83. ->method('getUrl')
  84. ->willReturn('url_abc');
  85. $this->server->xml->expects($this->once())
  86. ->method('parse')
  87. ->willReturnCallback(function ($requestBody, $url, &$documentType): void {
  88. $documentType = '{http://nextcloud.com/ns}disable-birthday-calendar';
  89. });
  90. $this->config->expects($this->never())
  91. ->method('setUserValue');
  92. $this->birthdayService->expects($this->never())
  93. ->method('syncUser');
  94. $this->plugin->httpPost($this->request, $this->response);
  95. }
  96. public function testHttpPostNotAuthorized(): void {
  97. $calendarHome = $this->createMock(CalendarHome::class);
  98. $this->server->expects($this->once())
  99. ->method('getRequestUri')
  100. ->willReturn('/bar/foo');
  101. $this->server->tree->expects($this->once())
  102. ->method('getNodeForPath')
  103. ->with('/bar/foo')
  104. ->willReturn($calendarHome);
  105. $calendarHome->expects($this->once())
  106. ->method('getOwner')
  107. ->willReturn('principals/users/BlaBlub');
  108. $this->request->expects($this->once())
  109. ->method('getBodyAsString')
  110. ->willReturn('<nc:enable-birthday-calendar xmlns:nc="http://nextcloud.com/ns"/>');
  111. $this->request->expects($this->once())
  112. ->method('getUrl')
  113. ->willReturn('url_abc');
  114. $this->server->xml->expects($this->once())
  115. ->method('parse')
  116. ->willReturnCallback(function ($requestBody, $url, &$documentType): void {
  117. $documentType = '{http://nextcloud.com/ns}enable-birthday-calendar';
  118. });
  119. $this->user->expects(self::once())
  120. ->method('getUID')
  121. ->willReturn('admin');
  122. $this->server->httpResponse->expects($this->once())
  123. ->method('setStatus')
  124. ->with(403);
  125. $this->config->expects($this->never())
  126. ->method('setUserValue');
  127. $this->birthdayService->expects($this->never())
  128. ->method('syncUser');
  129. $result = $this->plugin->httpPost($this->request, $this->response);
  130. $this->assertEquals(false, $result);
  131. }
  132. public function testHttpPost(): void {
  133. $calendarHome = $this->createMock(CalendarHome::class);
  134. $this->server->expects($this->once())
  135. ->method('getRequestUri')
  136. ->willReturn('/bar/foo');
  137. $this->server->tree->expects($this->once())
  138. ->method('getNodeForPath')
  139. ->with('/bar/foo')
  140. ->willReturn($calendarHome);
  141. $calendarHome->expects($this->once())
  142. ->method('getOwner')
  143. ->willReturn('principals/users/BlaBlub');
  144. $this->request->expects($this->once())
  145. ->method('getBodyAsString')
  146. ->willReturn('<nc:enable-birthday-calendar xmlns:nc="http://nextcloud.com/ns"/>');
  147. $this->request->expects($this->once())
  148. ->method('getUrl')
  149. ->willReturn('url_abc');
  150. $this->server->xml->expects($this->once())
  151. ->method('parse')
  152. ->willReturnCallback(function ($requestBody, $url, &$documentType): void {
  153. $documentType = '{http://nextcloud.com/ns}enable-birthday-calendar';
  154. });
  155. $this->user->expects(self::exactly(3))
  156. ->method('getUID')
  157. ->willReturn('BlaBlub');
  158. $this->config->expects($this->once())
  159. ->method('setUserValue')
  160. ->with('BlaBlub', 'dav', 'generateBirthdayCalendar', 'yes');
  161. $this->birthdayService->expects($this->once())
  162. ->method('syncUser')
  163. ->with('BlaBlub');
  164. $this->server->httpResponse->expects($this->once())
  165. ->method('setStatus')
  166. ->with(204);
  167. $result = $this->plugin->httpPost($this->request, $this->response);
  168. $this->assertEquals(false, $result);
  169. }
  170. }