2017-10-20 17:53:02 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2019-12-03 21:57:53 +03:00
|
|
|
* @copyright Copyright (c) 2017 Georg Ehrke <oc.list@georgehrke.com>
|
|
|
|
*
|
2017-10-20 17:53:02 +03:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2017-10-20 17:53:02 +03:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2017-10-20 17:53:02 +03:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2019-12-03 21:57:53 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-10-20 17:53:02 +03:00
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-10-20 17:53:02 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\DAV\Tests\unit\CalDAV\BirthdayCalendar;
|
|
|
|
|
|
|
|
use OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin;
|
2017-11-11 04:02:17 +03:00
|
|
|
use OCA\DAV\CalDAV\BirthdayService;
|
2017-10-20 17:53:02 +03:00
|
|
|
use OCA\DAV\CalDAV\Calendar;
|
|
|
|
use OCA\DAV\CalDAV\CalendarHome;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class EnablePluginTest extends TestCase {
|
|
|
|
|
|
|
|
/** @var \Sabre\DAV\Server|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $server;
|
|
|
|
|
|
|
|
/** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $config;
|
|
|
|
|
2017-11-11 04:02:17 +03:00
|
|
|
/** @var BirthdayService |\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
protected $birthdayService;
|
|
|
|
|
2017-10-20 17:53:02 +03:00
|
|
|
/** @var \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin $plugin */
|
|
|
|
protected $plugin;
|
|
|
|
|
|
|
|
protected $request;
|
|
|
|
|
|
|
|
protected $response;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2017-10-20 17:53:02 +03: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-11 04:02:17 +03:00
|
|
|
$this->birthdayService = $this->createMock(BirthdayService::class);
|
2017-10-20 17:53:02 +03:00
|
|
|
|
2017-11-11 04:02:17 +03:00
|
|
|
$this->plugin = new EnablePlugin($this->config, $this->birthdayService);
|
2017-10-20 17:53:02 +03:00
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
|
|
|
|
$this->request = $this->createMock(\Sabre\HTTP\RequestInterface::class);
|
|
|
|
$this->response = $this->createMock(\Sabre\HTTP\ResponseInterface::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetFeatures() {
|
|
|
|
$this->assertEquals(['nc-enable-birthday-calendar'], $this->plugin->getFeatures());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetName() {
|
|
|
|
$this->assertEquals('nc-enable-birthday-calendar', $this->plugin->getPluginName());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInitialize() {
|
|
|
|
$server = $this->createMock(\Sabre\DAV\Server::class);
|
|
|
|
|
2017-11-11 04:02:17 +03:00
|
|
|
$plugin = new EnablePlugin($this->config, $this->birthdayService);
|
2017-10-20 17:53:02 +03:00
|
|
|
|
|
|
|
$server->expects($this->at(0))
|
|
|
|
->method('on')
|
|
|
|
->with('method:POST', [$plugin, 'httpPost']);
|
|
|
|
|
|
|
|
$plugin->initialize($server);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHttpPostNoCalendarHome() {
|
|
|
|
$calendar = $this->createMock(Calendar::class);
|
|
|
|
|
|
|
|
$this->server->expects($this->once())
|
|
|
|
->method('getRequestUri')
|
|
|
|
->will($this->returnValue('/bar/foo'));
|
|
|
|
$this->server->tree->expects($this->once())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/bar/foo')
|
|
|
|
->will($this->returnValue($calendar));
|
|
|
|
|
|
|
|
$this->config->expects($this->never())
|
|
|
|
->method('setUserValue');
|
|
|
|
|
2017-11-11 04:02:17 +03:00
|
|
|
$this->birthdayService->expects($this->never())
|
|
|
|
->method('syncUser');
|
|
|
|
|
2017-10-20 17:53:02 +03:00
|
|
|
$this->plugin->httpPost($this->request, $this->response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHttpPostWrongRequest() {
|
|
|
|
$calendarHome = $this->createMock(CalendarHome::class);
|
|
|
|
|
|
|
|
$this->server->expects($this->once())
|
|
|
|
->method('getRequestUri')
|
|
|
|
->will($this->returnValue('/bar/foo'));
|
|
|
|
$this->server->tree->expects($this->once())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/bar/foo')
|
|
|
|
->will($this->returnValue($calendarHome));
|
|
|
|
|
|
|
|
$this->request->expects($this->at(0))
|
|
|
|
->method('getBodyAsString')
|
|
|
|
->will($this->returnValue('<nc:disable-birthday-calendar xmlns:nc="http://nextcloud.com/ns"/>'));
|
|
|
|
|
|
|
|
$this->request->expects($this->at(1))
|
|
|
|
->method('getUrl')
|
|
|
|
->will($this->returnValue('url_abc'));
|
|
|
|
|
|
|
|
$this->server->xml->expects($this->once())
|
|
|
|
->method('parse')
|
|
|
|
->will($this->returnCallback(function($requestBody, $url, &$documentType) {
|
|
|
|
$documentType = '{http://nextcloud.com/ns}disable-birthday-calendar';
|
|
|
|
}));
|
|
|
|
|
|
|
|
$this->config->expects($this->never())
|
|
|
|
->method('setUserValue');
|
|
|
|
|
2017-11-11 04:02:17 +03:00
|
|
|
$this->birthdayService->expects($this->never())
|
|
|
|
->method('syncUser');
|
|
|
|
|
2017-10-20 17:53:02 +03:00
|
|
|
$this->plugin->httpPost($this->request, $this->response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHttpPost() {
|
|
|
|
$calendarHome = $this->createMock(CalendarHome::class);
|
|
|
|
|
|
|
|
$this->server->expects($this->once())
|
|
|
|
->method('getRequestUri')
|
|
|
|
->will($this->returnValue('/bar/foo'));
|
|
|
|
$this->server->tree->expects($this->once())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/bar/foo')
|
|
|
|
->will($this->returnValue($calendarHome));
|
|
|
|
|
|
|
|
$calendarHome->expects($this->once())
|
|
|
|
->method('getOwner')
|
|
|
|
->will($this->returnValue('principals/users/BlaBlub'));
|
|
|
|
|
|
|
|
$this->request->expects($this->at(0))
|
|
|
|
->method('getBodyAsString')
|
|
|
|
->will($this->returnValue('<nc:enable-birthday-calendar xmlns:nc="http://nextcloud.com/ns"/>'));
|
|
|
|
|
|
|
|
$this->request->expects($this->at(1))
|
|
|
|
->method('getUrl')
|
|
|
|
->will($this->returnValue('url_abc'));
|
|
|
|
|
|
|
|
$this->server->xml->expects($this->once())
|
|
|
|
->method('parse')
|
|
|
|
->will($this->returnCallback(function($requestBody, $url, &$documentType) {
|
|
|
|
$documentType = '{http://nextcloud.com/ns}enable-birthday-calendar';
|
|
|
|
}));
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('setUserValue')
|
|
|
|
->with('BlaBlub', 'dav', 'generateBirthdayCalendar', 'yes');
|
|
|
|
|
2017-11-11 04:02:17 +03:00
|
|
|
$this->birthdayService->expects($this->once())
|
|
|
|
->method('syncUser')
|
|
|
|
->with('BlaBlub');
|
|
|
|
|
2017-10-20 17:53:02 +03:00
|
|
|
$this->server->httpResponse->expects($this->once())
|
|
|
|
->method('setStatus')
|
|
|
|
->with(204);
|
|
|
|
|
|
|
|
$result = $this->plugin->httpPost($this->request, $this->response);
|
|
|
|
|
|
|
|
$this->assertEquals(false, $result);
|
|
|
|
}
|
|
|
|
}
|