2014-12-07 10:39:15 +03:00
|
|
|
<?php
|
|
|
|
|
2016-05-19 10:27:21 +03:00
|
|
|
namespace Test;
|
2014-12-07 10:39:15 +03:00
|
|
|
|
2018-03-21 12:43:18 +03:00
|
|
|
use OCP\IAddressBook;
|
|
|
|
|
2016-05-19 10:27:21 +03:00
|
|
|
class ContactsManagerTest extends \Test\TestCase {
|
2014-12-07 10:39:15 +03:00
|
|
|
|
|
|
|
/** @var \OC\ContactsManager */
|
|
|
|
private $cm;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2014-12-07 10:39:15 +03:00
|
|
|
parent::setUp();
|
|
|
|
$this->cm = new \OC\ContactsManager();
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function searchProvider() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$search1 = [
|
|
|
|
0 => [
|
|
|
|
'N' => [0 => '', 1 => 'Jan', 2 => 'Jansen', 3 => '', 4 => '',],
|
2014-12-07 10:39:15 +03:00
|
|
|
'UID' => '04ada7f5-01f9-4309-9c82-6b555b2170ed',
|
|
|
|
'FN' => 'Jan Jansen',
|
|
|
|
'id' => '1',
|
|
|
|
'addressbook-key' => 'simple:1',
|
2020-03-26 11:30:18 +03:00
|
|
|
],
|
|
|
|
0 => [
|
|
|
|
'N' => [0 => '', 1 => 'Tom', 2 => 'Peeters', 3 => '', 4 => '',],
|
2014-12-07 10:39:15 +03:00
|
|
|
'UID' => '04ada7f5-01f9-4309-9c82-2345-2345--6b555b2170ed',
|
|
|
|
'FN' => 'Tom Peeters',
|
|
|
|
'id' => '2',
|
|
|
|
'addressbook-key' => 'simple:1',
|
2020-03-26 11:30:18 +03:00
|
|
|
],
|
|
|
|
];
|
2014-12-07 10:39:15 +03:00
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
$search2 = [
|
|
|
|
0 => [
|
|
|
|
'N' => [0 => '', 1 => 'fg', 2 => '', 3 => '', 4 => '',],
|
2014-12-07 10:39:15 +03:00
|
|
|
'UID' => '04ada234h5jh357f5-01f9-4309-9c82-6b555b2170ed',
|
|
|
|
'FN' => 'Jan Rompuy',
|
|
|
|
'id' => '1',
|
|
|
|
'addressbook-key' => 'simple:2',
|
2020-03-26 11:30:18 +03:00
|
|
|
],
|
|
|
|
0 => [
|
|
|
|
'N' => [0 => '', 1 => 'fg', 2 => '', 3 => '', 4 => '',],
|
2014-12-07 10:39:15 +03:00
|
|
|
'UID' => '04ada7f5-01f9-4309-345kj345j9c82-2345-2345--6b555b2170ed',
|
|
|
|
'FN' => 'Tim Peeters',
|
|
|
|
'id' => '2',
|
|
|
|
'addressbook-key' => 'simple:2',
|
2020-03-26 11:30:18 +03:00
|
|
|
],
|
|
|
|
];
|
2014-12-07 10:39:15 +03:00
|
|
|
|
2020-10-05 16:12:57 +03:00
|
|
|
$expectedResult = array_merge($search1, $search2);
|
2020-03-26 11:30:18 +03:00
|
|
|
return [
|
|
|
|
[
|
2014-12-07 16:10:41 +03:00
|
|
|
$search1,
|
|
|
|
$search2,
|
2014-12-07 10:39:15 +03:00
|
|
|
$expectedResult
|
2020-03-26 11:30:18 +03:00
|
|
|
]
|
|
|
|
];
|
2014-12-07 10:39:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider searchProvider
|
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testSearch($search1, $search2, $expectedResult) {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject|IAddressBook $addressbook */
|
2014-12-08 15:18:00 +03:00
|
|
|
$addressbook1 = $this->getMockBuilder('\OCP\IAddressBook')
|
2014-12-07 16:10:41 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$addressbook1->expects($this->once())
|
|
|
|
->method('search')
|
|
|
|
->willReturn($search1);
|
|
|
|
|
|
|
|
$addressbook1->expects($this->any())
|
|
|
|
->method('getKey')
|
|
|
|
->willReturn('simple:1');
|
|
|
|
|
2014-12-08 15:18:00 +03:00
|
|
|
$addressbook2 = $this->getMockBuilder('\OCP\IAddressBook')
|
2014-12-07 16:10:41 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$addressbook2->expects($this->once())
|
|
|
|
->method('search')
|
|
|
|
->willReturn($search2);
|
|
|
|
|
|
|
|
$addressbook2->expects($this->any())
|
|
|
|
->method('getKey')
|
|
|
|
->willReturn('simple:2');
|
|
|
|
|
|
|
|
|
|
|
|
$this->cm->registerAddressBook($addressbook1);
|
|
|
|
$this->cm->registerAddressBook($addressbook2);
|
2020-10-05 16:12:57 +03:00
|
|
|
$result = $this->cm->search('');
|
2014-12-07 10:39:15 +03:00
|
|
|
$this->assertEquals($expectedResult, $result);
|
|
|
|
}
|
2020-08-11 22:32:18 +03:00
|
|
|
|
2014-12-07 10:39:15 +03:00
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testDeleteHavePermission() {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject|IAddressBook $addressbook */
|
2014-12-08 15:18:00 +03:00
|
|
|
$addressbook = $this->getMockBuilder('\OCP\IAddressBook')
|
2014-12-07 10:39:15 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$addressbook->expects($this->any())
|
|
|
|
->method('getPermissions')
|
|
|
|
->willReturn(\OCP\Constants::PERMISSION_ALL);
|
|
|
|
|
|
|
|
$addressbook->expects($this->once())
|
|
|
|
->method('delete')
|
|
|
|
->willReturn('returnMe');
|
|
|
|
|
|
|
|
|
|
|
|
$this->cm->registerAddressBook($addressbook);
|
|
|
|
$result = $this->cm->delete(1, $addressbook->getKey());
|
|
|
|
$this->assertEquals($result, 'returnMe');
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testDeleteNoPermission() {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject|IAddressBook $addressbook */
|
2014-12-08 15:18:00 +03:00
|
|
|
$addressbook = $this->getMockBuilder('\OCP\IAddressBook')
|
2014-12-07 10:39:15 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$addressbook->expects($this->any())
|
|
|
|
->method('getPermissions')
|
|
|
|
->willReturn(\OCP\Constants::PERMISSION_READ);
|
|
|
|
|
|
|
|
$addressbook->expects($this->never())
|
|
|
|
->method('delete');
|
|
|
|
|
|
|
|
$this->cm->registerAddressBook($addressbook);
|
|
|
|
$result = $this->cm->delete(1, $addressbook->getKey());
|
|
|
|
$this->assertEquals($result, null);
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testDeleteNoAddressbook() {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject|IAddressBook $addressbook */
|
2014-12-08 15:18:00 +03:00
|
|
|
$addressbook = $this->getMockBuilder('\OCP\IAddressBook')
|
2014-12-07 10:39:15 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$addressbook->expects($this->never())
|
|
|
|
->method('delete');
|
|
|
|
|
|
|
|
$this->cm->registerAddressBook($addressbook);
|
|
|
|
$result = $this->cm->delete(1, 'noaddressbook');
|
|
|
|
$this->assertEquals($result, null);
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testCreateOrUpdateHavePermission() {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject|IAddressBook $addressbook */
|
2014-12-08 15:18:00 +03:00
|
|
|
$addressbook = $this->getMockBuilder('\OCP\IAddressBook')
|
2014-12-07 10:39:15 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$addressbook->expects($this->any())
|
|
|
|
->method('getPermissions')
|
|
|
|
->willReturn(\OCP\Constants::PERMISSION_ALL);
|
|
|
|
|
|
|
|
$addressbook->expects($this->once())
|
|
|
|
->method('createOrUpdate')
|
|
|
|
->willReturn('returnMe');
|
|
|
|
|
|
|
|
$this->cm->registerAddressBook($addressbook);
|
2020-03-26 11:30:18 +03:00
|
|
|
$result = $this->cm->createOrUpdate([], $addressbook->getKey());
|
2014-12-07 10:39:15 +03:00
|
|
|
$this->assertEquals($result, 'returnMe');
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testCreateOrUpdateNoPermission() {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject|IAddressBook $addressbook */
|
2014-12-08 15:18:00 +03:00
|
|
|
$addressbook = $this->getMockBuilder('\OCP\IAddressBook')
|
2014-12-07 10:39:15 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$addressbook->expects($this->any())
|
|
|
|
->method('getPermissions')
|
|
|
|
->willReturn(\OCP\Constants::PERMISSION_READ);
|
|
|
|
|
|
|
|
$addressbook->expects($this->never())
|
|
|
|
->method('createOrUpdate');
|
|
|
|
|
|
|
|
$this->cm->registerAddressBook($addressbook);
|
2020-03-26 11:30:18 +03:00
|
|
|
$result = $this->cm->createOrUpdate([], $addressbook->getKey());
|
2014-12-07 10:39:15 +03:00
|
|
|
$this->assertEquals($result, null);
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testCreateOrUpdateNOAdressbook() {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject|IAddressBook $addressbook */
|
2014-12-08 15:18:00 +03:00
|
|
|
$addressbook = $this->getMockBuilder('\OCP\IAddressBook')
|
2014-12-07 10:39:15 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$addressbook->expects($this->never())
|
|
|
|
->method('createOrUpdate');
|
|
|
|
|
|
|
|
$this->cm->registerAddressBook($addressbook);
|
2020-03-26 11:30:18 +03:00
|
|
|
$result = $this->cm->createOrUpdate([], 'noaddressbook');
|
2014-12-07 10:39:15 +03:00
|
|
|
$this->assertEquals($result, null);
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testIsEnabledIfNot() {
|
2014-12-07 10:39:15 +03:00
|
|
|
$result = $this->cm->isEnabled();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
public function testIsEnabledIfSo() {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject|IAddressBook $addressbook */
|
2014-12-08 15:18:00 +03:00
|
|
|
$addressbook = $this->getMockBuilder('\OCP\IAddressBook')
|
2014-12-07 10:39:15 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->cm->registerAddressBook($addressbook);
|
|
|
|
$result = $this->cm->isEnabled();
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
2018-03-21 12:43:18 +03:00
|
|
|
public function testAddressBookEnumeration() {
|
|
|
|
// create mock for the addressbook
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject|IAddressBook $addressbook */
|
2018-03-21 12:43:18 +03:00
|
|
|
$addressbook = $this->getMockBuilder('\OCP\IAddressBook')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
// setup return for method calls
|
|
|
|
$addressbook->expects($this->any())
|
|
|
|
->method('getKey')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('SIMPLE_ADDRESS_BOOK');
|
2018-03-21 12:43:18 +03:00
|
|
|
$addressbook->expects($this->any())
|
|
|
|
->method('getDisplayName')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('A very simple Addressbook');
|
2018-03-21 12:43:18 +03:00
|
|
|
|
|
|
|
// register the address book
|
|
|
|
$this->cm->registerAddressBook($addressbook);
|
|
|
|
$all_books = $this->cm->getAddressBooks();
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($all_books));
|
|
|
|
$this->assertEquals('A very simple Addressbook', $all_books['SIMPLE_ADDRESS_BOOK']);
|
|
|
|
}
|
2016-05-19 10:27:21 +03:00
|
|
|
}
|