EnablePluginTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author François Freitag <mail@franek.fr>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\DAV\Tests\unit\CalDAV\BirthdayCalendar;
  28. use OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin;
  29. use OCA\DAV\CalDAV\BirthdayService;
  30. use OCA\DAV\CalDAV\Calendar;
  31. use OCA\DAV\CalDAV\CalendarHome;
  32. use OCP\IConfig;
  33. use OCP\IUser;
  34. use Test\TestCase;
  35. class EnablePluginTest extends TestCase {
  36. /** @var \Sabre\DAV\Server|\PHPUnit\Framework\MockObject\MockObject */
  37. protected $server;
  38. /** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */
  39. protected $config;
  40. /** @var BirthdayService |\PHPUnit\Framework\MockObject\MockObject */
  41. protected $birthdayService;
  42. /** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
  43. protected $user;
  44. /** @var \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin $plugin */
  45. protected $plugin;
  46. protected $request;
  47. protected $response;
  48. protected function setUp(): void {
  49. parent::setUp();
  50. $this->server = $this->createMock(\Sabre\DAV\Server::class);
  51. $this->server->tree = $this->createMock(\Sabre\DAV\Tree::class);
  52. $this->server->httpResponse = $this->createMock(\Sabre\HTTP\Response::class);
  53. $this->server->xml = $this->createMock(\Sabre\DAV\Xml\Service::class);
  54. $this->config = $this->createMock(IConfig::class);
  55. $this->birthdayService = $this->createMock(BirthdayService::class);
  56. $this->user = $this->createMock(IUser::class);
  57. $this->plugin = new EnablePlugin($this->config, $this->birthdayService, $this->user);
  58. $this->plugin->initialize($this->server);
  59. $this->request = $this->createMock(\Sabre\HTTP\RequestInterface::class);
  60. $this->response = $this->createMock(\Sabre\HTTP\ResponseInterface::class);
  61. }
  62. public function testGetFeatures(): void {
  63. $this->assertEquals(['nc-enable-birthday-calendar'], $this->plugin->getFeatures());
  64. }
  65. public function testGetName(): void {
  66. $this->assertEquals('nc-enable-birthday-calendar', $this->plugin->getPluginName());
  67. }
  68. public function testInitialize(): void {
  69. $server = $this->createMock(\Sabre\DAV\Server::class);
  70. $plugin = new EnablePlugin($this->config, $this->birthdayService, $this->user);
  71. $server->expects($this->once())
  72. ->method('on')
  73. ->with('method:POST', [$plugin, 'httpPost']);
  74. $plugin->initialize($server);
  75. }
  76. public function testHttpPostNoCalendarHome(): void {
  77. $calendar = $this->createMock(Calendar::class);
  78. $this->server->expects($this->once())
  79. ->method('getRequestUri')
  80. ->willReturn('/bar/foo');
  81. $this->server->tree->expects($this->once())
  82. ->method('getNodeForPath')
  83. ->with('/bar/foo')
  84. ->willReturn($calendar);
  85. $this->config->expects($this->never())
  86. ->method('setUserValue');
  87. $this->birthdayService->expects($this->never())
  88. ->method('syncUser');
  89. $this->plugin->httpPost($this->request, $this->response);
  90. }
  91. public function testHttpPostWrongRequest(): void {
  92. $calendarHome = $this->createMock(CalendarHome::class);
  93. $this->server->expects($this->once())
  94. ->method('getRequestUri')
  95. ->willReturn('/bar/foo');
  96. $this->server->tree->expects($this->once())
  97. ->method('getNodeForPath')
  98. ->with('/bar/foo')
  99. ->willReturn($calendarHome);
  100. $this->request->expects($this->once())
  101. ->method('getBodyAsString')
  102. ->willReturn('<nc:disable-birthday-calendar xmlns:nc="http://nextcloud.com/ns"/>');
  103. $this->request->expects($this->once())
  104. ->method('getUrl')
  105. ->willReturn('url_abc');
  106. $this->server->xml->expects($this->once())
  107. ->method('parse')
  108. ->willReturnCallback(function ($requestBody, $url, &$documentType): void {
  109. $documentType = '{http://nextcloud.com/ns}disable-birthday-calendar';
  110. });
  111. $this->config->expects($this->never())
  112. ->method('setUserValue');
  113. $this->birthdayService->expects($this->never())
  114. ->method('syncUser');
  115. $this->plugin->httpPost($this->request, $this->response);
  116. }
  117. public function testHttpPostNotAuthorized(): void {
  118. $calendarHome = $this->createMock(CalendarHome::class);
  119. $this->server->expects($this->once())
  120. ->method('getRequestUri')
  121. ->willReturn('/bar/foo');
  122. $this->server->tree->expects($this->once())
  123. ->method('getNodeForPath')
  124. ->with('/bar/foo')
  125. ->willReturn($calendarHome);
  126. $calendarHome->expects($this->once())
  127. ->method('getOwner')
  128. ->willReturn('principals/users/BlaBlub');
  129. $this->request->expects($this->once())
  130. ->method('getBodyAsString')
  131. ->willReturn('<nc:enable-birthday-calendar xmlns:nc="http://nextcloud.com/ns"/>');
  132. $this->request->expects($this->once())
  133. ->method('getUrl')
  134. ->willReturn('url_abc');
  135. $this->server->xml->expects($this->once())
  136. ->method('parse')
  137. ->willReturnCallback(function ($requestBody, $url, &$documentType): void {
  138. $documentType = '{http://nextcloud.com/ns}enable-birthday-calendar';
  139. });
  140. $this->user->expects(self::once())
  141. ->method('getUID')
  142. ->willReturn('admin');
  143. $this->server->httpResponse->expects($this->once())
  144. ->method('setStatus')
  145. ->with(403);
  146. $this->config->expects($this->never())
  147. ->method('setUserValue');
  148. $this->birthdayService->expects($this->never())
  149. ->method('syncUser');
  150. $result = $this->plugin->httpPost($this->request, $this->response);
  151. $this->assertEquals(false, $result);
  152. }
  153. public function testHttpPost(): void {
  154. $calendarHome = $this->createMock(CalendarHome::class);
  155. $this->server->expects($this->once())
  156. ->method('getRequestUri')
  157. ->willReturn('/bar/foo');
  158. $this->server->tree->expects($this->once())
  159. ->method('getNodeForPath')
  160. ->with('/bar/foo')
  161. ->willReturn($calendarHome);
  162. $calendarHome->expects($this->once())
  163. ->method('getOwner')
  164. ->willReturn('principals/users/BlaBlub');
  165. $this->request->expects($this->once())
  166. ->method('getBodyAsString')
  167. ->willReturn('<nc:enable-birthday-calendar xmlns:nc="http://nextcloud.com/ns"/>');
  168. $this->request->expects($this->once())
  169. ->method('getUrl')
  170. ->willReturn('url_abc');
  171. $this->server->xml->expects($this->once())
  172. ->method('parse')
  173. ->willReturnCallback(function ($requestBody, $url, &$documentType): void {
  174. $documentType = '{http://nextcloud.com/ns}enable-birthday-calendar';
  175. });
  176. $this->user->expects(self::exactly(3))
  177. ->method('getUID')
  178. ->willReturn('BlaBlub');
  179. $this->config->expects($this->once())
  180. ->method('setUserValue')
  181. ->with('BlaBlub', 'dav', 'generateBirthdayCalendar', 'yes');
  182. $this->birthdayService->expects($this->once())
  183. ->method('syncUser')
  184. ->with('BlaBlub');
  185. $this->server->httpResponse->expects($this->once())
  186. ->method('setStatus')
  187. ->with(204);
  188. $result = $this->plugin->httpPost($this->request, $this->response);
  189. $this->assertEquals(false, $result);
  190. }
  191. }