Skip to content

Commit

Permalink
Fix and add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
  • Loading branch information
tcitworld committed Jan 27, 2019
1 parent e114413 commit ae94278
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 4 deletions.
2 changes: 1 addition & 1 deletion apps/dav/js/settings-admin-carddav.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
"use strict";
'use strict';

$('#carddavSyncSystemAddressbook').change(function () {
var val = $(this)[0].checked;
Expand Down
22 changes: 19 additions & 3 deletions apps/dav/tests/unit/CardDAV/ContactsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,27 @@
use OCA\DAV\CardDAV\CardDavBackend;
use OCA\DAV\CardDAV\ContactsManager;
use OCP\Contacts\IManager;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IURLGenerator;
use Test\TestCase;

class ContactsManagerTest extends TestCase {
public function test() {

public function dataForContactsManagerTest() {
return [
['yes'],
['no']
];
}

/**
* @dataProvider dataForContactsManagerTest
*/
public function test(string $syncSystemAddressBook) {
/** @var IManager | \PHPUnit_Framework_MockObject_MockObject $cm */
$cm = $this->getMockBuilder(IManager::class)->disableOriginalConstructor()->getMock();
$cm->expects($this->exactly(2))->method('registerAddressBook');
$cm->expects($this->exactly($syncSystemAddressBook === 'yes' ? 2 : 1))->method('registerAddressBook');
$urlGenerator = $this->getMockBuilder(IURLGenerator::class)->disableOriginalConstructor()->getMock();
/** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $backEnd */
$backEnd = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
Expand All @@ -45,7 +57,11 @@ public function test() {
]);

$l = $this->createMock(IL10N::class);
$app = new ContactsManager($backEnd, $l);
$config = $this->createMock(IConfig::class);

$config->method('getAppValue')->with('dav', 'syncSystemAddressbook', 'yes')->willReturn($syncSystemAddressBook);

$app = new ContactsManager($backEnd, $l, $config);
$app->setupContactsProvider($cm, 'user01', $urlGenerator);
}
}
13 changes: 13 additions & 0 deletions apps/dav/tests/unit/CardDAV/SyncServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ public function testUpdateAndDeleteUser($activated, $createCalls, $updateCalls,
$ss->deleteUser($user);
}

public function testPurgeSystemAddressBook()
{
$backend = $this->getMockBuilder(CardDavBackend::class)->disableOriginalConstructor()->getMock();
$logger = $this->getMockBuilder(ILogger::class)->disableOriginalConstructor()->getMock();
$userManager = $this->getMockBuilder(IUserManager::class)->disableOriginalConstructor()->getMock();
$accountManager = $this->getMockBuilder(AccountManager::class)->disableOriginalConstructor()->getMock();

$backend->expects($this->once())->method('deleteAddressBook');

$ss = new SyncService($backend, $userManager, $logger, $accountManager);
$ss->purgeSystemAddressBook();
}

/**
* @param int $createCount
* @param int $updateCount
Expand Down
71 changes: 71 additions & 0 deletions apps/dav/tests/unit/Command/SyncSystemAddressBookTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* @copyright 2019, Thomas Citharel <tcit@tcit.fr>
*
* @author Thomas Citharel <tcit@tcit.fr>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\DAV\Tests\Command;

use OCA\DAV\Command\SyncSystemAddressBook;
use OCA\DAV\CardDAV\SyncService;
use OCP\IConfig;
use InvalidArgumentException;
use Symfony\Component\Console\Tester\CommandTester;
use Test\TestCase;
/**
* Class SyncSystemAddressBookTest
*
* @package OCA\DAV\Tests\Command
*/
class SyncSystemAddressBookTest extends TestCase {

/** @var SyncService */
private $syncService;

/** @var IConfig */
private $config;

/** @var SyncSystemAddressBook */
private $command;

protected function setUp() {
parent::setUp();
$this->syncService = $this->createMock(SyncService::class);
$this->config = $this->createMock(IConfig::class);
$this->command = new SyncSystemAddressBook(
$this->syncService,
$this->config
);
}

public function testSyncEnabled()
{
$this->config->method('getAppValue')->with('dav', 'syncSystemAddressbook', 'yes')->willReturn('yes');
$this->syncService->expects($this->once())->method('syncInstance');
$commandTester = new CommandTester($this->command);
$commandTester->execute([]);
}

public function testSyncDisabled()
{
$this->config->method('getAppValue')->with('dav', 'syncSystemAddressbook', 'yes')->willReturn('no');
$this->syncService->expects($this->once())->method('purgeSystemAddressBook');
$commandTester = new CommandTester($this->command);
$commandTester->execute([]);
}
}
58 changes: 58 additions & 0 deletions apps/dav/tests/unit/Settings/CardDAVSettingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* @copyright 2019, Thomas Citharel <tcit@tcit.fr>
*
* @author Thomas Citharel <tcit.fr@tcit.fr>
*
* @license GNU AGPL version 3 or any later version
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* 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/>.
*
*/

namespace OCA\DAV\Tests\Unit\DAV\Settings;

use OCA\DAV\Settings\CardDAVSettings;
use OCP\IConfig;
use Test\TestCase;

class CardDAVSettingsTest extends TestCase {

/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
private $config;

/** @var CardDAVSettings */
private $settings;

public function setUp() {
parent::setUp();

$this->config = $this->createMock(IConfig::class);
$this->settings = new CardDAVSettings($this->config);
}

public function testGetForm() {
$result = $this->settings->getForm();

$this->assertInstanceOf('OCP\AppFramework\Http\TemplateResponse', $result);
}

public function testGetSection() {
$this->assertEquals('groupware', $this->settings->getSection());
}

public function testGetPriority() {
$this->assertEquals(10, $this->settings->getPriority());
}
}

0 comments on commit ae94278

Please sign in to comment.