EnablePluginTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\DAV\Tests\unit\CalDAV\BirthdayCalendar;
  26. use OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin;
  27. use OCA\DAV\CalDAV\BirthdayService;
  28. use OCA\DAV\CalDAV\Calendar;
  29. use OCA\DAV\CalDAV\CalendarHome;
  30. use OCP\IConfig;
  31. use Test\TestCase;
  32. class EnablePluginTest extends TestCase {
  33. /** @var \Sabre\DAV\Server|\PHPUnit_Framework_MockObject_MockObject */
  34. protected $server;
  35. /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
  36. protected $config;
  37. /** @var BirthdayService |\PHPUnit_Framework_MockObject_MockObject */
  38. protected $birthdayService;
  39. /** @var \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin $plugin */
  40. protected $plugin;
  41. protected $request;
  42. protected $response;
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $this->server = $this->createMock(\Sabre\DAV\Server::class);
  46. $this->server->tree = $this->createMock(\Sabre\DAV\Tree::class);
  47. $this->server->httpResponse = $this->createMock(\Sabre\HTTP\Response::class);
  48. $this->server->xml = $this->createMock(\Sabre\DAV\Xml\Service::class);
  49. $this->config = $this->createMock(IConfig::class);
  50. $this->birthdayService = $this->createMock(BirthdayService::class);
  51. $this->plugin = new EnablePlugin($this->config, $this->birthdayService);
  52. $this->plugin->initialize($this->server);
  53. $this->request = $this->createMock(\Sabre\HTTP\RequestInterface::class);
  54. $this->response = $this->createMock(\Sabre\HTTP\ResponseInterface::class);
  55. }
  56. public function testGetFeatures() {
  57. $this->assertEquals(['nc-enable-birthday-calendar'], $this->plugin->getFeatures());
  58. }
  59. public function testGetName() {
  60. $this->assertEquals('nc-enable-birthday-calendar', $this->plugin->getPluginName());
  61. }
  62. public function testInitialize() {
  63. $server = $this->createMock(\Sabre\DAV\Server::class);
  64. $plugin = new EnablePlugin($this->config, $this->birthdayService);
  65. $server->expects($this->at(0))
  66. ->method('on')
  67. ->with('method:POST', [$plugin, 'httpPost']);
  68. $plugin->initialize($server);
  69. }
  70. public function testHttpPostNoCalendarHome() {
  71. $calendar = $this->createMock(Calendar::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($calendar);
  79. $this->config->expects($this->never())
  80. ->method('setUserValue');
  81. $this->birthdayService->expects($this->never())
  82. ->method('syncUser');
  83. $this->plugin->httpPost($this->request, $this->response);
  84. }
  85. public function testHttpPostWrongRequest() {
  86. $calendarHome = $this->createMock(CalendarHome::class);
  87. $this->server->expects($this->once())
  88. ->method('getRequestUri')
  89. ->willReturn('/bar/foo');
  90. $this->server->tree->expects($this->once())
  91. ->method('getNodeForPath')
  92. ->with('/bar/foo')
  93. ->willReturn($calendarHome);
  94. $this->request->expects($this->at(0))
  95. ->method('getBodyAsString')
  96. ->willReturn('<nc:disable-birthday-calendar xmlns:nc="http://nextcloud.com/ns"/>');
  97. $this->request->expects($this->at(1))
  98. ->method('getUrl')
  99. ->willReturn('url_abc');
  100. $this->server->xml->expects($this->once())
  101. ->method('parse')
  102. ->willReturnCallback(function ($requestBody, $url, &$documentType) {
  103. $documentType = '{http://nextcloud.com/ns}disable-birthday-calendar';
  104. });
  105. $this->config->expects($this->never())
  106. ->method('setUserValue');
  107. $this->birthdayService->expects($this->never())
  108. ->method('syncUser');
  109. $this->plugin->httpPost($this->request, $this->response);
  110. }
  111. public function testHttpPost() {
  112. $calendarHome = $this->createMock(CalendarHome::class);
  113. $this->server->expects($this->once())
  114. ->method('getRequestUri')
  115. ->willReturn('/bar/foo');
  116. $this->server->tree->expects($this->once())
  117. ->method('getNodeForPath')
  118. ->with('/bar/foo')
  119. ->willReturn($calendarHome);
  120. $calendarHome->expects($this->once())
  121. ->method('getOwner')
  122. ->willReturn('principals/users/BlaBlub');
  123. $this->request->expects($this->at(0))
  124. ->method('getBodyAsString')
  125. ->willReturn('<nc:enable-birthday-calendar xmlns:nc="http://nextcloud.com/ns"/>');
  126. $this->request->expects($this->at(1))
  127. ->method('getUrl')
  128. ->willReturn('url_abc');
  129. $this->server->xml->expects($this->once())
  130. ->method('parse')
  131. ->willReturnCallback(function ($requestBody, $url, &$documentType) {
  132. $documentType = '{http://nextcloud.com/ns}enable-birthday-calendar';
  133. });
  134. $this->config->expects($this->once())
  135. ->method('setUserValue')
  136. ->with('BlaBlub', 'dav', 'generateBirthdayCalendar', 'yes');
  137. $this->birthdayService->expects($this->once())
  138. ->method('syncUser')
  139. ->with('BlaBlub');
  140. $this->server->httpResponse->expects($this->once())
  141. ->method('setStatus')
  142. ->with(204);
  143. $result = $this->plugin->httpPost($this->request, $this->response);
  144. $this->assertEquals(false, $result);
  145. }
  146. }