diff --git a/tests/lib/Accounts/AccountPropertyTest.php b/tests/lib/Accounts/AccountPropertyTest.php new file mode 100644 index 0000000000..5d71287fed --- /dev/null +++ b/tests/lib/Accounts/AccountPropertyTest.php @@ -0,0 +1,104 @@ + + * + * @author Julius Härtl + * + * @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 . + * + */ + +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()); + } + + +} diff --git a/tests/lib/Accounts/AccountTest.php b/tests/lib/Accounts/AccountTest.php new file mode 100644 index 0000000000..3fc73f75af --- /dev/null +++ b/tests/lib/Accounts/AccountTest.php @@ -0,0 +1,112 @@ + + * + * @author Julius Härtl + * + * @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 . + * + */ + +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()); + } + +} diff --git a/tests/lib/Accounts/AccountsManagerTest.php b/tests/lib/Accounts/AccountsManagerTest.php index c4add0244b..dee8ba5d81 100644 --- a/tests/lib/Accounts/AccountsManagerTest.php +++ b/tests/lib/Accounts/AccountsManagerTest.php @@ -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)); + } + }