Add tests for new account api classes

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2018-10-12 09:30:44 +02:00
parent d05080f56a
commit 9381e681a9
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
3 changed files with 253 additions and 0 deletions

View File

@ -0,0 +1,104 @@
<?php
/**
* @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @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 Test\Accounts;
use OC\Accounts\Account;
use OC\Accounts\AccountManager;
use OC\Accounts\AccountProperty;
use Test\TestCase;
/**
* Class AccountPropertyTest
*
* @package Test\Accounts
*/
class AccountPropertyTest extends TestCase {
public function testConstructor() {
$accountProperty = new AccountProperty(
AccountManager::PROPERTY_WEBSITE,
'https://example.com',
AccountManager::VISIBILITY_PUBLIC,
AccountManager::VERIFIED
);
$this->assertEquals(AccountManager::PROPERTY_WEBSITE, $accountProperty->getName());
$this->assertEquals('https://example.com', $accountProperty->getValue());
$this->assertEquals(AccountManager::VISIBILITY_PUBLIC, $accountProperty->getScope());
$this->assertEquals(AccountManager::VERIFIED, $accountProperty->getVerified());
}
public function testSetValue() {
$accountProperty = new AccountProperty(
AccountManager::PROPERTY_WEBSITE,
'https://example.com',
AccountManager::VISIBILITY_PUBLIC,
AccountManager::VERIFIED
);
$actualReturn = $accountProperty->setValue('https://example.org');
$this->assertEquals('https://example.org', $accountProperty->getValue());
$this->assertEquals('https://example.org', $actualReturn->getValue());
}
public function testSetScope() {
$accountProperty = new AccountProperty(
AccountManager::PROPERTY_WEBSITE,
'https://example.com',
AccountManager::VISIBILITY_PUBLIC,
AccountManager::VERIFIED
);
$actualReturn = $accountProperty->setScope(AccountManager::VISIBILITY_PRIVATE);
$this->assertEquals(AccountManager::VISIBILITY_PRIVATE, $accountProperty->getScope());
$this->assertEquals(AccountManager::VISIBILITY_PRIVATE, $actualReturn->getScope());
}
public function testSetVerified() {
$accountProperty = new AccountProperty(
AccountManager::PROPERTY_WEBSITE,
'https://example.com',
AccountManager::VISIBILITY_PUBLIC,
AccountManager::VERIFIED
);
$actualReturn = $accountProperty->setVerified(AccountManager::NOT_VERIFIED);
$this->assertEquals(AccountManager::NOT_VERIFIED, $accountProperty->getVerified());
$this->assertEquals(AccountManager::NOT_VERIFIED, $actualReturn->getVerified());
}
public function testJsonSerialize() {
$accountProperty = new AccountProperty(
AccountManager::PROPERTY_WEBSITE,
'https://example.com',
AccountManager::VISIBILITY_PUBLIC,
AccountManager::VERIFIED
);
$this->assertEquals([
'name' => AccountManager::PROPERTY_WEBSITE,
'value' => 'https://example.com',
'scope' => AccountManager::VISIBILITY_PUBLIC,
'verified' => AccountManager::VERIFIED
], $accountProperty->jsonSerialize());
}
}

View File

@ -0,0 +1,112 @@
<?php
/**
* @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @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 Test\Accounts;
use OC\Accounts\Account;
use OC\Accounts\AccountManager;
use OC\Accounts\AccountProperty;
use OCP\IUser;
use Test\TestCase;
/**
* Class AccountTest
*
* @package Test\Accounts
*/
class AccountTest extends TestCase {
public function testConstructor() {
$user = $this->createMock(IUser::class);
$account = new Account($user);
$this->assertEquals($user, $account->getUser());
}
public function testSetProperty() {
$user = $this->createMock(IUser::class);
$property = new AccountProperty(AccountManager::PROPERTY_WEBSITE, 'https://example.com', AccountManager::VISIBILITY_PUBLIC, AccountManager::NOT_VERIFIED);
$account = new Account($user);
$account->setProperty(AccountManager::PROPERTY_WEBSITE, 'https://example.com', AccountManager::VISIBILITY_PUBLIC, AccountManager::NOT_VERIFIED);
$this->assertEquals($property, $account->getProperty(AccountManager::PROPERTY_WEBSITE));
}
public function testGetProperties() {
$user = $this->createMock(IUser::class);
$properties = [
AccountManager::PROPERTY_WEBSITE => new AccountProperty(AccountManager::PROPERTY_WEBSITE, 'https://example.com', AccountManager::VISIBILITY_PUBLIC, AccountManager::NOT_VERIFIED),
AccountManager::PROPERTY_EMAIL => new AccountProperty(AccountManager::PROPERTY_EMAIL, 'user@example.com', AccountManager::VISIBILITY_PRIVATE, AccountManager::VERIFIED)
];
$account = new Account($user);
$account->setProperty(AccountManager::PROPERTY_WEBSITE, 'https://example.com', AccountManager::VISIBILITY_PUBLIC, AccountManager::NOT_VERIFIED);
$account->setProperty(AccountManager::PROPERTY_EMAIL, 'user@example.com', AccountManager::VISIBILITY_PRIVATE, AccountManager::VERIFIED);
$this->assertEquals($properties, $account->getProperties());
}
public function testGetFilteredProperties() {
$user = $this->createMock(IUser::class);
$properties = [
AccountManager::PROPERTY_WEBSITE => new AccountProperty(AccountManager::PROPERTY_WEBSITE, 'https://example.com', AccountManager::VISIBILITY_PUBLIC, AccountManager::NOT_VERIFIED),
AccountManager::PROPERTY_EMAIL => new AccountProperty(AccountManager::PROPERTY_EMAIL, 'user@example.com', AccountManager::VISIBILITY_PRIVATE, AccountManager::VERIFIED),
AccountManager::PROPERTY_PHONE => new AccountProperty(AccountManager::PROPERTY_PHONE, '123456', AccountManager::VISIBILITY_PUBLIC, AccountManager::VERIFIED),
];
$account = new Account($user);
$account->setProperty(AccountManager::PROPERTY_WEBSITE, 'https://example.com', AccountManager::VISIBILITY_PUBLIC, AccountManager::NOT_VERIFIED);
$account->setProperty(AccountManager::PROPERTY_EMAIL, 'user@example.com', AccountManager::VISIBILITY_PRIVATE, AccountManager::VERIFIED);
$account->setProperty(AccountManager::PROPERTY_PHONE, '123456', AccountManager::VISIBILITY_PUBLIC, AccountManager::VERIFIED);
$this->assertEquals(
[
AccountManager::PROPERTY_WEBSITE => $properties[AccountManager::PROPERTY_WEBSITE],
AccountManager::PROPERTY_PHONE => $properties[AccountManager::PROPERTY_PHONE],
],
$account->getFilteredProperties(AccountManager::VISIBILITY_PUBLIC)
);
$this->assertEquals(
[
AccountManager::PROPERTY_EMAIL => $properties[AccountManager::PROPERTY_EMAIL],
AccountManager::PROPERTY_PHONE => $properties[AccountManager::PROPERTY_PHONE],
],
$account->getFilteredProperties(null, AccountManager::VERIFIED)
);
$this->assertEquals(
[AccountManager::PROPERTY_PHONE => $properties[AccountManager::PROPERTY_PHONE]],
$account->getFilteredProperties(AccountManager::VISIBILITY_PUBLIC, AccountManager::VERIFIED)
);
}
public function testJsonSerialize() {
$user = $this->createMock(IUser::class);
$properties = [
AccountManager::PROPERTY_WEBSITE => new AccountProperty(AccountManager::PROPERTY_WEBSITE, 'https://example.com', AccountManager::VISIBILITY_PUBLIC, AccountManager::NOT_VERIFIED),
AccountManager::PROPERTY_EMAIL => new AccountProperty(AccountManager::PROPERTY_EMAIL, 'user@example.com', AccountManager::VISIBILITY_PRIVATE, AccountManager::VERIFIED)
];
$account = new Account($user);
$account->setProperty(AccountManager::PROPERTY_WEBSITE, 'https://example.com', AccountManager::VISIBILITY_PUBLIC, AccountManager::NOT_VERIFIED);
$account->setProperty(AccountManager::PROPERTY_EMAIL, 'user@example.com', AccountManager::VISIBILITY_PRIVATE, AccountManager::VERIFIED);
$this->assertEquals($properties, $account->jsonSerialize());
}
}

View File

@ -23,6 +23,7 @@
namespace Test\Accounts;
use OC\Accounts\Account;
use OC\Accounts\AccountManager;
use OCP\BackgroundJob\IJobList;
use OCP\IUser;
@ -254,4 +255,40 @@ class AccountsManagerTest extends TestCase {
}
}
public function testGetAccount() {
$accountManager = $this->getInstance(['getUser']);
/** @var IUser $user */
$user = $this->createMock(IUser::class);
$data = [
AccountManager::PROPERTY_TWITTER =>
[
'value' => '@twitterhandle',
'scope' => AccountManager::VISIBILITY_PRIVATE,
'verified' => AccountManager::NOT_VERIFIED,
],
AccountManager::PROPERTY_EMAIL =>
[
'value' => 'test@example.com',
'scope' => AccountManager::VISIBILITY_PUBLIC,
'verified' => AccountManager::VERIFICATION_IN_PROGRESS,
],
AccountManager::PROPERTY_WEBSITE =>
[
'value' => 'https://example.com',
'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY,
'verified' => AccountManager::VERIFIED,
],
];
$expected = new Account($user);
$expected->setProperty(AccountManager::PROPERTY_TWITTER, '@twitterhandle', AccountManager::VISIBILITY_PRIVATE, AccountManager::NOT_VERIFIED);
$expected->setProperty(AccountManager::PROPERTY_EMAIL, 'test@example.com', AccountManager::VISIBILITY_PUBLIC, AccountManager::VERIFICATION_IN_PROGRESS);
$expected->setProperty(AccountManager::PROPERTY_WEBSITE, 'https://example.com', AccountManager::VISIBILITY_CONTACTS_ONLY, AccountManager::VERIFIED);
$accountManager->expects($this->once())
->method('getUser')
->willReturn($data);
$this->assertEquals($expected, $accountManager->getAccount($user));
}
}