2015-11-30 15:59:41 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 14:49:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-07-21 14:49:16 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 14:49:16 +00:00
|
|
|
* @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
|
|
|
|
2016-11-17 09:27:46 +00:00
|
|
|
use OC\Accounts\AccountManager;
|
2015-11-30 15:59:41 +00:00
|
|
|
use OCA\DAV\CardDAV\Converter;
|
2016-11-17 09:27:46 +00:00
|
|
|
use OCP\IDBConnection;
|
2016-10-13 10:15:10 +00:00
|
|
|
use OCP\IImage;
|
|
|
|
use OCP\IUser;
|
|
|
|
use PHPUnit_Framework_MockObject_MockObject;
|
2016-11-17 09:27:46 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
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
|
|
|
|
2016-11-17 09:27:46 +00:00
|
|
|
/** @var AccountManager | PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $accountManager;
|
2015-11-30 15:59:41 +00:00
|
|
|
|
2016-11-17 09:27:46 +00:00
|
|
|
/** @var EventDispatcher | PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $eventDispatcher;
|
2015-11-30 15:59:41 +00:00
|
|
|
|
2016-11-17 09:27:46 +00:00
|
|
|
/** @var IDBConnection | PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $databaseConnection;
|
2015-11-30 15:59:41 +00:00
|
|
|
|
2016-11-17 09:27:46 +00:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2017-10-24 22:03:28 +00:00
|
|
|
$this->databaseConnection = $this->getMockBuilder(IDBConnection::class)->getMock();
|
|
|
|
$this->eventDispatcher = $this->getMockBuilder(EventDispatcher::class)
|
2016-11-17 09:27:46 +00:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2017-10-24 13:26:53 +00:00
|
|
|
$this->accountManager = $this->getMockBuilder(AccountManager::class)
|
2016-11-17 09:27:46 +00:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2015-11-30 15:59:41 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 09:27:46 +00:00
|
|
|
public function getAccountManager(IUser $user) {
|
2017-10-24 13:26:53 +00:00
|
|
|
$accountManager = $this->getMockBuilder(AccountManager::class)
|
2016-11-17 09:27:46 +00:00
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$accountManager->expects($this->any())->method('getUser')->willReturn(
|
|
|
|
[
|
|
|
|
AccountManager::PROPERTY_DISPLAYNAME =>
|
|
|
|
[
|
|
|
|
'value' => $user->getDisplayName(),
|
|
|
|
'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY,
|
|
|
|
],
|
|
|
|
AccountManager::PROPERTY_ADDRESS =>
|
|
|
|
[
|
|
|
|
'value' => '',
|
|
|
|
'scope' => AccountManager::VISIBILITY_PRIVATE,
|
|
|
|
],
|
|
|
|
AccountManager::PROPERTY_WEBSITE =>
|
|
|
|
[
|
|
|
|
'value' => '',
|
|
|
|
'scope' => AccountManager::VISIBILITY_PRIVATE,
|
|
|
|
],
|
|
|
|
AccountManager::PROPERTY_EMAIL =>
|
|
|
|
[
|
|
|
|
'value' => $user->getEMailAddress(),
|
|
|
|
'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY,
|
|
|
|
],
|
|
|
|
AccountManager::PROPERTY_AVATAR =>
|
|
|
|
[
|
|
|
|
'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY
|
|
|
|
],
|
|
|
|
AccountManager::PROPERTY_PHONE =>
|
|
|
|
[
|
|
|
|
'value' => '',
|
|
|
|
'scope' => AccountManager::VISIBILITY_PRIVATE,
|
|
|
|
],
|
|
|
|
AccountManager::PROPERTY_TWITTER =>
|
|
|
|
[
|
|
|
|
'value' => '',
|
|
|
|
'scope' => AccountManager::VISIBILITY_PRIVATE,
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
return $accountManager;
|
2015-11-30 15:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-17 09:27:46 +00:00
|
|
|
* @dataProvider providesNewUsers
|
2015-11-30 15:59:41 +00:00
|
|
|
*/
|
2016-11-17 09:27:46 +00:00
|
|
|
public function testCreation($expectedVCard, $displayName = null, $eMailAddress = null, $cloudId = null) {
|
2015-12-02 10:09:15 +00:00
|
|
|
$user = $this->getUserMock($displayName, $eMailAddress, $cloudId);
|
2016-11-17 09:27:46 +00:00
|
|
|
$accountManager = $this->getAccountManager($user);
|
2015-11-30 15:59:41 +00:00
|
|
|
|
2016-11-17 09:27:46 +00:00
|
|
|
$converter = new Converter($accountManager);
|
2015-11-30 15:59:41 +00:00
|
|
|
$vCard = $converter->createCardFromUser($user);
|
2016-11-17 09:27:46 +00:00
|
|
|
if ($expectedVCard !== null) {
|
|
|
|
$this->assertInstanceOf('Sabre\VObject\Component\VCard', $vCard);
|
|
|
|
$cardData = $vCard->jsonSerialize();
|
|
|
|
$this->compareData($expectedVCard, $cardData);
|
2015-11-30 15:59:41 +00:00
|
|
|
|
2016-11-17 09:27:46 +00:00
|
|
|
} else {
|
|
|
|
$this->assertSame($expectedVCard, $vCard);
|
|
|
|
}
|
2015-11-30 15:59:41 +00:00
|
|
|
|
2016-11-17 09:27:46 +00:00
|
|
|
}
|
2015-11-30 15:59:41 +00:00
|
|
|
|
2016-11-17 09:27:46 +00:00
|
|
|
protected function compareData($expected, $data) {
|
|
|
|
foreach ($expected as $key => $value) {
|
|
|
|
$found = false;
|
|
|
|
foreach ($data[1] as $d) {
|
|
|
|
if($d[0] === $key && $d[3] === $value) {
|
|
|
|
$found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$found) $this->assertTrue(false, 'Expected data: ' . $key . ' not found.');
|
|
|
|
}
|
2015-11-30 15:59:41 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 09:27:46 +00:00
|
|
|
public function providesNewUsers() {
|
2015-11-30 15:59:41 +00:00
|
|
|
return [
|
2016-11-22 20:26:49 +00:00
|
|
|
[
|
|
|
|
null
|
|
|
|
],
|
|
|
|
[
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
'foo@bar.net'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'cloud' => 'foo@cloud.net',
|
|
|
|
'email' => 'foo@bar.net',
|
2017-03-13 15:54:27 +00:00
|
|
|
'photo' => 'MTIzNDU2Nzg5',
|
2016-11-22 20:26:49 +00:00
|
|
|
],
|
|
|
|
null,
|
|
|
|
'foo@bar.net',
|
|
|
|
'foo@cloud.net'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'cloud' => 'foo@cloud.net',
|
|
|
|
'email' => 'foo@bar.net',
|
|
|
|
'fn' => 'Dr. Foo Bar',
|
2017-03-13 15:54:27 +00:00
|
|
|
'photo' => 'MTIzNDU2Nzg5',
|
2016-11-22 20:26:49 +00:00
|
|
|
],
|
|
|
|
"Dr. Foo Bar",
|
|
|
|
"foo@bar.net",
|
|
|
|
'foo@cloud.net'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'cloud' => 'foo@cloud.net',
|
|
|
|
'fn' => 'Dr. Foo Bar',
|
2017-03-13 15:54:27 +00:00
|
|
|
'photo' => 'MTIzNDU2Nzg5',
|
2016-11-22 20:26:49 +00:00
|
|
|
],
|
|
|
|
"Dr. Foo Bar",
|
|
|
|
null,
|
|
|
|
"foo@cloud.net"
|
|
|
|
],
|
2017-03-13 13:58:37 +00:00
|
|
|
[
|
|
|
|
[
|
|
|
|
'cloud' => 'foo@cloud.net',
|
|
|
|
'fn' => 'Dr. Foo Bar',
|
2017-03-13 15:54:27 +00:00
|
|
|
'photo' => 'MTIzNDU2Nzg5',
|
2017-03-13 13:58:37 +00:00
|
|
|
],
|
|
|
|
'Dr. Foo Bar',
|
|
|
|
'',
|
|
|
|
'foo@cloud.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) {
|
|
|
|
|
2016-11-17 09:27:46 +00:00
|
|
|
$converter = new Converter($this->accountManager);
|
2015-11-30 21:28:36 +00:00
|
|
|
$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();
|
2016-11-22 20:26:49 +00:00
|
|
|
$image0->method('mimeType')->willReturn('image/jpeg');
|
2015-12-02 10:09:15 +00:00
|
|
|
$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
|
|
|
}
|