2015-11-30 15:59:41 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 14:49:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-11-30 15:59:41 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @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/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-05-25 14:04:15 +00:00
|
|
|
namespace OCA\DAV\Tests\unit\CardDAV;
|
2015-11-30 15:59:41 +00:00
|
|
|
|
|
|
|
use OCA\DAV\CardDAV\Converter;
|
2016-10-13 10:15:10 +00:00
|
|
|
use OCP\IImage;
|
|
|
|
use OCP\IUser;
|
|
|
|
use PHPUnit_Framework_MockObject_MockObject;
|
2015-11-30 15:59:41 +00:00
|
|
|
use Test\TestCase;
|
|
|
|
|
2016-05-25 14:04:15 +00:00
|
|
|
class ConverterTest extends TestCase {
|
2015-11-30 15:59:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providesNewUsers
|
|
|
|
*/
|
|
|
|
public function testCreation($expectedVCard, $displayName = null, $eMailAddress = null, $cloudId = null) {
|
2015-12-02 10:09:15 +00:00
|
|
|
$user = $this->getUserMock($displayName, $eMailAddress, $cloudId);
|
2015-11-30 15:59:41 +00:00
|
|
|
|
|
|
|
$converter = new Converter();
|
|
|
|
$vCard = $converter->createCardFromUser($user);
|
|
|
|
$cardData = $vCard->serialize();
|
|
|
|
|
|
|
|
$this->assertEquals($expectedVCard, $cardData);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providesNewUsers() {
|
|
|
|
return [
|
2016-10-13 10:15:10 +00:00
|
|
|
["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nPHOTO;ENCODING=b;TYPE=JPEG:MTIzNDU2Nzg5\r\nEND:VCARD\r\n"],
|
|
|
|
["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:Dr. Foo Bar\r\nN:Bar;Dr.;Foo;;\r\nPHOTO;ENCODING=b;TYPE=JPEG:MTIzNDU2Nzg5\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
|
|
|
|
["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:Dr. Foo Bar\r\nN:Bar;Dr.;Foo;;\r\nEMAIL;TYPE=OTHER:foo@bar.net\r\nPHOTO;ENCODING=b;TYPE=JPEG:MTIzNDU2Nzg5\r\nEND:VCARD\r\n", "Dr. Foo Bar", "foo@bar.net"],
|
|
|
|
["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:Dr. Foo Bar\r\nN:Bar;Dr.;Foo;;\r\nCLOUD:foo@bar.net\r\nPHOTO;ENCODING=b;TYPE=JPEG:MTIzNDU2Nzg5\r\nEND:VCARD\r\n", "Dr. Foo Bar", null, "foo@bar.net"],
|
2015-11-30 15:59:41 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-02 10:09:15 +00:00
|
|
|
* @dataProvider providesNewUsers
|
2015-11-30 15:59:41 +00:00
|
|
|
*/
|
|
|
|
public function testUpdateOfUnchangedUser($expectedVCard, $displayName = null, $eMailAddress = null, $cloudId = null) {
|
2015-12-02 10:09:15 +00:00
|
|
|
$user = $this->getUserMock($displayName, $eMailAddress, $cloudId);
|
2015-11-30 15:59:41 +00:00
|
|
|
|
|
|
|
$converter = new Converter();
|
|
|
|
$vCard = $converter->createCardFromUser($user);
|
|
|
|
$updated = $converter->updateCard($vCard, $user);
|
|
|
|
$this->assertFalse($updated);
|
|
|
|
$cardData = $vCard->serialize();
|
|
|
|
|
|
|
|
$this->assertEquals($expectedVCard, $cardData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providesUsersForUpdateOfRemovedElement
|
|
|
|
*/
|
|
|
|
public function testUpdateOfRemovedElement($expectedVCard, $displayName = null, $eMailAddress = null, $cloudId = null) {
|
2015-12-02 10:09:15 +00:00
|
|
|
$user = $this->getUserMock($displayName, $eMailAddress, $cloudId);
|
2015-11-30 15:59:41 +00:00
|
|
|
|
|
|
|
$converter = new Converter();
|
|
|
|
$vCard = $converter->createCardFromUser($user);
|
|
|
|
|
|
|
|
$user1 = $this->getMockBuilder('OCP\IUser')->disableOriginalConstructor()->getMock();
|
|
|
|
$user1->method('getUID')->willReturn('12345');
|
|
|
|
$user1->method('getDisplayName')->willReturn(null);
|
|
|
|
$user1->method('getEMailAddress')->willReturn(null);
|
|
|
|
$user1->method('getCloudId')->willReturn(null);
|
2015-12-02 10:09:15 +00:00
|
|
|
$user1->method('getAvatarImage')->willReturn(null);
|
2015-11-30 15:59:41 +00:00
|
|
|
|
|
|
|
$updated = $converter->updateCard($vCard, $user1);
|
|
|
|
$this->assertTrue($updated);
|
|
|
|
$cardData = $vCard->serialize();
|
|
|
|
|
|
|
|
$this->assertEquals($expectedVCard, $cardData);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providesUsersForUpdateOfRemovedElement() {
|
|
|
|
return [
|
2016-10-13 10:15:10 +00:00
|
|
|
["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
|
|
|
|
["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nEND:VCARD\r\n", "Dr. Foo Bar", "foo@bar.net"],
|
|
|
|
["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nEND:VCARD\r\n", "Dr. Foo Bar", null, "foo@bar.net"],
|
2015-11-30 15:59:41 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-11-30 21:28:36 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider providesNames
|
|
|
|
* @param $expected
|
|
|
|
* @param $fullName
|
|
|
|
*/
|
|
|
|
public function testNameSplitter($expected, $fullName) {
|
|
|
|
|
|
|
|
$converter = new Converter();
|
|
|
|
$r = $converter->splitFullName($fullName);
|
|
|
|
$r = implode(';', $r);
|
|
|
|
$this->assertEquals($expected, $r);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providesNames() {
|
|
|
|
return [
|
|
|
|
['Sauron;;;;', 'Sauron'],
|
|
|
|
['Baggins;Bilbo;;;', 'Bilbo Baggins'],
|
|
|
|
['Tolkien;John;Ronald Reuel;;', 'John Ronald Reuel Tolkien'],
|
|
|
|
];
|
|
|
|
}
|
2015-12-02 10:09:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $displayName
|
|
|
|
* @param $eMailAddress
|
|
|
|
* @param $cloudId
|
2016-10-13 10:15:10 +00:00
|
|
|
* @return IUser | PHPUnit_Framework_MockObject_MockObject
|
2015-12-02 10:09:15 +00:00
|
|
|
*/
|
|
|
|
protected function getUserMock($displayName, $eMailAddress, $cloudId) {
|
2016-10-13 10:15:10 +00:00
|
|
|
$image0 = $this->getMockBuilder(IImage::class)->disableOriginalConstructor()->getMock();
|
2015-12-02 10:09:15 +00:00
|
|
|
$image0->method('mimeType')->willReturn('JPEG');
|
|
|
|
$image0->method('data')->willReturn('123456789');
|
2016-10-13 10:15:10 +00:00
|
|
|
$user = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
|
2015-12-02 10:09:15 +00:00
|
|
|
$user->method('getUID')->willReturn('12345');
|
|
|
|
$user->method('getDisplayName')->willReturn($displayName);
|
|
|
|
$user->method('getEMailAddress')->willReturn($eMailAddress);
|
|
|
|
$user->method('getCloudId')->willReturn($cloudId);
|
|
|
|
$user->method('getAvatarImage')->willReturn($image0);
|
|
|
|
return $user;
|
|
|
|
}
|
2015-11-30 15:59:41 +00:00
|
|
|
}
|