2017-10-20 10:53:02 -04:00
|
|
|
<?php
|
2025-05-24 17:00:05 -04:00
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2017-10-20 10:53:02 -04:00
|
|
|
/**
|
2024-05-28 06:34:11 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2017-10-20 10:53:02 -04:00
|
|
|
*/
|
|
|
|
|
namespace OCA\DAV\Tests\unit\CalDAV\BirthdayCalendar;
|
|
|
|
|
|
|
|
|
|
use OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin;
|
2017-11-10 20:02:17 -05:00
|
|
|
use OCA\DAV\CalDAV\BirthdayService;
|
2017-10-20 10:53:02 -04:00
|
|
|
use OCA\DAV\CalDAV\Calendar;
|
|
|
|
|
use OCA\DAV\CalDAV\CalendarHome;
|
|
|
|
|
use OCP\IConfig;
|
2023-08-18 03:02:59 -04:00
|
|
|
use OCP\IUser;
|
2025-05-24 17:00:05 -04:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2017-10-20 10:53:02 -04:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
|
|
class EnablePluginTest extends TestCase {
|
2025-05-24 17:00:05 -04:00
|
|
|
protected \Sabre\DAV\Server&MockObject $server;
|
|
|
|
|
protected IConfig&MockObject $config;
|
|
|
|
|
protected BirthdayService&MockObject $birthdayService;
|
|
|
|
|
protected IUser&MockObject $user;
|
|
|
|
|
protected EnablePlugin $plugin;
|
2017-10-20 10:53:02 -04:00
|
|
|
|
|
|
|
|
protected $request;
|
|
|
|
|
|
|
|
|
|
protected $response;
|
|
|
|
|
|
2019-11-27 09:27:18 -05:00
|
|
|
protected function setUp(): void {
|
2017-10-20 10:53:02 -04:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->server = $this->createMock(\Sabre\DAV\Server::class);
|
|
|
|
|
$this->server->tree = $this->createMock(\Sabre\DAV\Tree::class);
|
|
|
|
|
$this->server->httpResponse = $this->createMock(\Sabre\HTTP\Response::class);
|
|
|
|
|
$this->server->xml = $this->createMock(\Sabre\DAV\Xml\Service::class);
|
|
|
|
|
|
|
|
|
|
$this->config = $this->createMock(IConfig::class);
|
2017-11-10 20:02:17 -05:00
|
|
|
$this->birthdayService = $this->createMock(BirthdayService::class);
|
2023-08-18 03:02:59 -04:00
|
|
|
$this->user = $this->createMock(IUser::class);
|
2017-10-20 10:53:02 -04:00
|
|
|
|
2023-08-18 03:02:59 -04:00
|
|
|
$this->plugin = new EnablePlugin($this->config, $this->birthdayService, $this->user);
|
2017-10-20 10:53:02 -04:00
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
|
|
|
|
|
|
$this->request = $this->createMock(\Sabre\HTTP\RequestInterface::class);
|
|
|
|
|
$this->response = $this->createMock(\Sabre\HTTP\ResponseInterface::class);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 02:38:43 -05:00
|
|
|
public function testGetFeatures(): void {
|
2017-10-20 10:53:02 -04:00
|
|
|
$this->assertEquals(['nc-enable-birthday-calendar'], $this->plugin->getFeatures());
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 02:38:43 -05:00
|
|
|
public function testGetName(): void {
|
2017-10-20 10:53:02 -04:00
|
|
|
$this->assertEquals('nc-enable-birthday-calendar', $this->plugin->getPluginName());
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 02:38:43 -05:00
|
|
|
public function testInitialize(): void {
|
2017-10-20 10:53:02 -04:00
|
|
|
$server = $this->createMock(\Sabre\DAV\Server::class);
|
|
|
|
|
|
2023-08-18 03:02:59 -04:00
|
|
|
$plugin = new EnablePlugin($this->config, $this->birthdayService, $this->user);
|
2017-10-20 10:53:02 -04:00
|
|
|
|
2021-05-31 16:20:17 -04:00
|
|
|
$server->expects($this->once())
|
2017-10-20 10:53:02 -04:00
|
|
|
->method('on')
|
|
|
|
|
->with('method:POST', [$plugin, 'httpPost']);
|
|
|
|
|
|
|
|
|
|
$plugin->initialize($server);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 02:38:43 -05:00
|
|
|
public function testHttpPostNoCalendarHome(): void {
|
2017-10-20 10:53:02 -04:00
|
|
|
$calendar = $this->createMock(Calendar::class);
|
|
|
|
|
|
|
|
|
|
$this->server->expects($this->once())
|
|
|
|
|
->method('getRequestUri')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('/bar/foo');
|
2017-10-20 10:53:02 -04:00
|
|
|
$this->server->tree->expects($this->once())
|
|
|
|
|
->method('getNodeForPath')
|
|
|
|
|
->with('/bar/foo')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($calendar);
|
2017-10-20 10:53:02 -04:00
|
|
|
|
|
|
|
|
$this->config->expects($this->never())
|
|
|
|
|
->method('setUserValue');
|
|
|
|
|
|
2017-11-10 20:02:17 -05:00
|
|
|
$this->birthdayService->expects($this->never())
|
|
|
|
|
->method('syncUser');
|
|
|
|
|
|
2017-10-20 10:53:02 -04:00
|
|
|
$this->plugin->httpPost($this->request, $this->response);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 02:38:43 -05:00
|
|
|
public function testHttpPostWrongRequest(): void {
|
2017-10-20 10:53:02 -04:00
|
|
|
$calendarHome = $this->createMock(CalendarHome::class);
|
|
|
|
|
|
|
|
|
|
$this->server->expects($this->once())
|
|
|
|
|
->method('getRequestUri')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('/bar/foo');
|
2017-10-20 10:53:02 -04:00
|
|
|
$this->server->tree->expects($this->once())
|
|
|
|
|
->method('getNodeForPath')
|
|
|
|
|
->with('/bar/foo')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($calendarHome);
|
2017-10-20 10:53:02 -04:00
|
|
|
|
2021-05-31 16:20:17 -04:00
|
|
|
$this->request->expects($this->once())
|
2017-10-20 10:53:02 -04:00
|
|
|
->method('getBodyAsString')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('<nc:disable-birthday-calendar xmlns:nc="http://nextcloud.com/ns"/>');
|
2017-10-20 10:53:02 -04:00
|
|
|
|
2021-05-31 16:20:17 -04:00
|
|
|
$this->request->expects($this->once())
|
2017-10-20 10:53:02 -04:00
|
|
|
->method('getUrl')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('url_abc');
|
2017-10-20 10:53:02 -04:00
|
|
|
|
|
|
|
|
$this->server->xml->expects($this->once())
|
|
|
|
|
->method('parse')
|
2023-01-20 02:38:43 -05:00
|
|
|
->willReturnCallback(function ($requestBody, $url, &$documentType): void {
|
2020-10-05 09:12:57 -04:00
|
|
|
$documentType = '{http://nextcloud.com/ns}disable-birthday-calendar';
|
2020-03-25 17:21:27 -04:00
|
|
|
});
|
2017-10-20 10:53:02 -04:00
|
|
|
|
|
|
|
|
$this->config->expects($this->never())
|
|
|
|
|
->method('setUserValue');
|
|
|
|
|
|
2017-11-10 20:02:17 -05:00
|
|
|
$this->birthdayService->expects($this->never())
|
|
|
|
|
->method('syncUser');
|
|
|
|
|
|
2017-10-20 10:53:02 -04:00
|
|
|
$this->plugin->httpPost($this->request, $this->response);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 03:02:59 -04:00
|
|
|
public function testHttpPostNotAuthorized(): void {
|
|
|
|
|
$calendarHome = $this->createMock(CalendarHome::class);
|
|
|
|
|
|
|
|
|
|
$this->server->expects($this->once())
|
|
|
|
|
->method('getRequestUri')
|
|
|
|
|
->willReturn('/bar/foo');
|
|
|
|
|
$this->server->tree->expects($this->once())
|
|
|
|
|
->method('getNodeForPath')
|
|
|
|
|
->with('/bar/foo')
|
|
|
|
|
->willReturn($calendarHome);
|
|
|
|
|
|
|
|
|
|
$calendarHome->expects($this->once())
|
|
|
|
|
->method('getOwner')
|
|
|
|
|
->willReturn('principals/users/BlaBlub');
|
|
|
|
|
|
|
|
|
|
$this->request->expects($this->once())
|
|
|
|
|
->method('getBodyAsString')
|
|
|
|
|
->willReturn('<nc:enable-birthday-calendar xmlns:nc="http://nextcloud.com/ns"/>');
|
|
|
|
|
|
|
|
|
|
$this->request->expects($this->once())
|
|
|
|
|
->method('getUrl')
|
|
|
|
|
->willReturn('url_abc');
|
|
|
|
|
|
|
|
|
|
$this->server->xml->expects($this->once())
|
|
|
|
|
->method('parse')
|
|
|
|
|
->willReturnCallback(function ($requestBody, $url, &$documentType): void {
|
|
|
|
|
$documentType = '{http://nextcloud.com/ns}enable-birthday-calendar';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->user->expects(self::once())
|
|
|
|
|
->method('getUID')
|
|
|
|
|
->willReturn('admin');
|
|
|
|
|
|
|
|
|
|
$this->server->httpResponse->expects($this->once())
|
|
|
|
|
->method('setStatus')
|
|
|
|
|
->with(403);
|
|
|
|
|
|
|
|
|
|
$this->config->expects($this->never())
|
|
|
|
|
->method('setUserValue');
|
|
|
|
|
|
|
|
|
|
$this->birthdayService->expects($this->never())
|
|
|
|
|
->method('syncUser');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$result = $this->plugin->httpPost($this->request, $this->response);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(false, $result);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-20 02:38:43 -05:00
|
|
|
public function testHttpPost(): void {
|
2017-10-20 10:53:02 -04:00
|
|
|
$calendarHome = $this->createMock(CalendarHome::class);
|
|
|
|
|
|
|
|
|
|
$this->server->expects($this->once())
|
|
|
|
|
->method('getRequestUri')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('/bar/foo');
|
2017-10-20 10:53:02 -04:00
|
|
|
$this->server->tree->expects($this->once())
|
|
|
|
|
->method('getNodeForPath')
|
|
|
|
|
->with('/bar/foo')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($calendarHome);
|
2017-10-20 10:53:02 -04:00
|
|
|
|
|
|
|
|
$calendarHome->expects($this->once())
|
|
|
|
|
->method('getOwner')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('principals/users/BlaBlub');
|
2017-10-20 10:53:02 -04:00
|
|
|
|
2021-05-31 16:20:17 -04:00
|
|
|
$this->request->expects($this->once())
|
2017-10-20 10:53:02 -04:00
|
|
|
->method('getBodyAsString')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('<nc:enable-birthday-calendar xmlns:nc="http://nextcloud.com/ns"/>');
|
2017-10-20 10:53:02 -04:00
|
|
|
|
2021-05-31 16:20:17 -04:00
|
|
|
$this->request->expects($this->once())
|
2017-10-20 10:53:02 -04:00
|
|
|
->method('getUrl')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('url_abc');
|
2017-10-20 10:53:02 -04:00
|
|
|
|
|
|
|
|
$this->server->xml->expects($this->once())
|
|
|
|
|
->method('parse')
|
2023-01-20 02:38:43 -05:00
|
|
|
->willReturnCallback(function ($requestBody, $url, &$documentType): void {
|
2017-10-20 10:53:02 -04:00
|
|
|
$documentType = '{http://nextcloud.com/ns}enable-birthday-calendar';
|
2020-03-25 17:21:27 -04:00
|
|
|
});
|
2017-10-20 10:53:02 -04:00
|
|
|
|
2023-08-18 03:02:59 -04:00
|
|
|
$this->user->expects(self::exactly(3))
|
|
|
|
|
->method('getUID')
|
|
|
|
|
->willReturn('BlaBlub');
|
|
|
|
|
|
2017-10-20 10:53:02 -04:00
|
|
|
$this->config->expects($this->once())
|
|
|
|
|
->method('setUserValue')
|
|
|
|
|
->with('BlaBlub', 'dav', 'generateBirthdayCalendar', 'yes');
|
|
|
|
|
|
2017-11-10 20:02:17 -05:00
|
|
|
$this->birthdayService->expects($this->once())
|
|
|
|
|
->method('syncUser')
|
|
|
|
|
->with('BlaBlub');
|
|
|
|
|
|
2017-10-20 10:53:02 -04:00
|
|
|
$this->server->httpResponse->expects($this->once())
|
|
|
|
|
->method('setStatus')
|
|
|
|
|
->with(204);
|
|
|
|
|
|
|
|
|
|
$result = $this->plugin->httpPost($this->request, $this->response);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(false, $result);
|
|
|
|
|
}
|
|
|
|
|
}
|