EnablePluginTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 Test\TestCase;
  34. class EnablePluginTest extends TestCase {
  35. /** @var \Sabre\DAV\Server|\PHPUnit\Framework\MockObject\MockObject */
  36. protected $server;
  37. /** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */
  38. protected $config;
  39. /** @var BirthdayService |\PHPUnit\Framework\MockObject\MockObject */
  40. protected $birthdayService;
  41. /** @var \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin $plugin */
  42. protected $plugin;
  43. protected $request;
  44. protected $response;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->server = $this->createMock(\Sabre\DAV\Server::class);
  48. $this->server->tree = $this->createMock(\Sabre\DAV\Tree::class);
  49. $this->server->httpResponse = $this->createMock(\Sabre\HTTP\Response::class);
  50. $this->server->xml = $this->createMock(\Sabre\DAV\Xml\Service::class);
  51. $this->config = $this->createMock(IConfig::class);
  52. $this->birthdayService = $this->createMock(BirthdayService::class);
  53. $this->plugin = new EnablePlugin($this->config, $this->birthdayService);
  54. $this->plugin->initialize($this->server);
  55. $this->request = $this->createMock(\Sabre\HTTP\RequestInterface::class);
  56. $this->response = $this->createMock(\Sabre\HTTP\ResponseInterface::class);
  57. }
  58. public function testGetFeatures() {
  59. $this->assertEquals(['nc-enable-birthday-calendar'], $this->plugin->getFeatures());
  60. }
  61. public function testGetName() {
  62. $this->assertEquals('nc-enable-birthday-calendar', $this->plugin->getPluginName());
  63. }
  64. public function testInitialize() {
  65. $server = $this->createMock(\Sabre\DAV\Server::class);
  66. $plugin = new EnablePlugin($this->config, $this->birthdayService);
  67. $server->expects($this->once())
  68. ->method('on')
  69. ->with('method:POST', [$plugin, 'httpPost']);
  70. $plugin->initialize($server);
  71. }
  72. public function testHttpPostNoCalendarHome() {
  73. $calendar = $this->createMock(Calendar::class);
  74. $this->server->expects($this->once())
  75. ->method('getRequestUri')
  76. ->willReturn('/bar/foo');
  77. $this->server->tree->expects($this->once())
  78. ->method('getNodeForPath')
  79. ->with('/bar/foo')
  80. ->willReturn($calendar);
  81. $this->config->expects($this->never())
  82. ->method('setUserValue');
  83. $this->birthdayService->expects($this->never())
  84. ->method('syncUser');
  85. $this->plugin->httpPost($this->request, $this->response);
  86. }
  87. public function testHttpPostWrongRequest() {
  88. $calendarHome = $this->createMock(CalendarHome::class);
  89. $this->server->expects($this->once())
  90. ->method('getRequestUri')
  91. ->willReturn('/bar/foo');
  92. $this->server->tree->expects($this->once())
  93. ->method('getNodeForPath')
  94. ->with('/bar/foo')
  95. ->willReturn($calendarHome);
  96. $this->request->expects($this->once())
  97. ->method('getBodyAsString')
  98. ->willReturn('<nc:disable-birthday-calendar xmlns:nc="http://nextcloud.com/ns"/>');
  99. $this->request->expects($this->once())
  100. ->method('getUrl')
  101. ->willReturn('url_abc');
  102. $this->server->xml->expects($this->once())
  103. ->method('parse')
  104. ->willReturnCallback(function ($requestBody, $url, &$documentType) {
  105. $documentType = '{http://nextcloud.com/ns}disable-birthday-calendar';
  106. });
  107. $this->config->expects($this->never())
  108. ->method('setUserValue');
  109. $this->birthdayService->expects($this->never())
  110. ->method('syncUser');
  111. $this->plugin->httpPost($this->request, $this->response);
  112. }
  113. public function testHttpPost() {
  114. $calendarHome = $this->createMock(CalendarHome::class);
  115. $this->server->expects($this->once())
  116. ->method('getRequestUri')
  117. ->willReturn('/bar/foo');
  118. $this->server->tree->expects($this->once())
  119. ->method('getNodeForPath')
  120. ->with('/bar/foo')
  121. ->willReturn($calendarHome);
  122. $calendarHome->expects($this->once())
  123. ->method('getOwner')
  124. ->willReturn('principals/users/BlaBlub');
  125. $this->request->expects($this->once())
  126. ->method('getBodyAsString')
  127. ->willReturn('<nc:enable-birthday-calendar xmlns:nc="http://nextcloud.com/ns"/>');
  128. $this->request->expects($this->once())
  129. ->method('getUrl')
  130. ->willReturn('url_abc');
  131. $this->server->xml->expects($this->once())
  132. ->method('parse')
  133. ->willReturnCallback(function ($requestBody, $url, &$documentType) {
  134. $documentType = '{http://nextcloud.com/ns}enable-birthday-calendar';
  135. });
  136. $this->config->expects($this->once())
  137. ->method('setUserValue')
  138. ->with('BlaBlub', 'dav', 'generateBirthdayCalendar', 'yes');
  139. $this->birthdayService->expects($this->once())
  140. ->method('syncUser')
  141. ->with('BlaBlub');
  142. $this->server->httpResponse->expects($this->once())
  143. ->method('setStatus')
  144. ->with(204);
  145. $result = $this->plugin->httpPost($this->request, $this->response);
  146. $this->assertEquals(false, $result);
  147. }
  148. }