diff options
Diffstat (limited to 'apps/settings/tests/UserMigration')
-rw-r--r-- | apps/settings/tests/UserMigration/AccountMigratorTest.php | 105 | ||||
-rw-r--r-- | apps/settings/tests/UserMigration/assets/account-complex-config.json | 1 | ||||
-rw-r--r-- | apps/settings/tests/UserMigration/assets/account-complex.jpg | bin | 1063519 -> 1040846 bytes | |||
-rw-r--r-- | apps/settings/tests/UserMigration/assets/account-complex.json | 2 | ||||
-rw-r--r-- | apps/settings/tests/UserMigration/assets/account-config.json | 1 | ||||
-rw-r--r-- | apps/settings/tests/UserMigration/assets/account.json | 2 | ||||
-rw-r--r-- | apps/settings/tests/UserMigration/assets/account.png | bin | 913877 -> 451063 bytes |
7 files changed, 57 insertions, 54 deletions
diff --git a/apps/settings/tests/UserMigration/AccountMigratorTest.php b/apps/settings/tests/UserMigration/AccountMigratorTest.php index 573d18380e5..b8f8301f777 100644 --- a/apps/settings/tests/UserMigration/AccountMigratorTest.php +++ b/apps/settings/tests/UserMigration/AccountMigratorTest.php @@ -1,29 +1,10 @@ <?php declare(strict_types=1); - /** - * @copyright 2022 Christopher Ng <chrng8@gmail.com> - * - * @author Christopher Ng <chrng8@gmail.com> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\Settings\Tests\UserMigration; use OCA\Settings\AppInfo\Application; @@ -31,9 +12,12 @@ use OCA\Settings\UserMigration\AccountMigrator; use OCP\Accounts\IAccountManager; use OCP\AppFramework\App; use OCP\IAvatarManager; +use OCP\IConfig; use OCP\IUserManager; +use OCP\Server; use OCP\UserMigration\IExportDestination; use OCP\UserMigration\IImportSource; +use PHPUnit\Framework\Constraint\JsonMatches; use PHPUnit\Framework\MockObject\MockObject; use Sabre\VObject\UUIDUtil; use Symfony\Component\Console\Output\OutputInterface; @@ -43,21 +27,12 @@ use Test\TestCase; * @group DB */ class AccountMigratorTest extends TestCase { - private IUserManager $userManager; - private IAvatarManager $avatarManager; - private AccountMigrator $migrator; - - /** @var IImportSource|MockObject */ - private $importSource; - - /** @var IExportDestination|MockObject */ - private $exportDestination; - - /** @var OutputInterface|MockObject */ - private $output; + private IImportSource&MockObject $importSource; + private IExportDestination&MockObject $exportDestination; + private OutputInterface&MockObject $output; private const ASSETS_DIR = __DIR__ . '/assets/'; @@ -65,9 +40,14 @@ class AccountMigratorTest extends TestCase { private const REGEX_AVATAR_FILE = '/^' . Application::APP_ID . '\/' . 'avatar\.(jpg|png)' . '$/'; + private const REGEX_CONFIG_FILE = '/^' . Application::APP_ID . '\/' . '[a-z]+\.json' . '$/'; + protected function setUp(): void { + parent::setUp(); + $app = new App(Application::APP_ID); $container = $app->getContainer(); + $container->get(IConfig::class)->setSystemValue('has_internet_connection', false); $this->userManager = $container->get(IUserManager::class); $this->avatarManager = $container->get(IAvatarManager::class); @@ -78,33 +58,39 @@ class AccountMigratorTest extends TestCase { $this->output = $this->createMock(OutputInterface::class); } - public function dataImportExportAccount(): array { + protected function tearDown(): void { + Server::get(IConfig::class)->setSystemValue('has_internet_connection', true); + parent::tearDown(); + } + + public static function dataImportExportAccount(): array { return array_map( - function (string $filename) { - $dataPath = self::ASSETS_DIR . $filename; - // For each json file there is an avatar image with the same basename - $avatarBasename = pathinfo($filename, PATHINFO_FILENAME); - $avatarPath = self::ASSETS_DIR . (file_exists(self::ASSETS_DIR . "$avatarBasename.jpg") ? "$avatarBasename.jpg" : "$avatarBasename.png"); + static function (string $filename): array { + $dataPath = static::ASSETS_DIR . $filename; + // For each account json file there is an avatar image and a config json file with the same basename + $basename = pathinfo($filename, PATHINFO_FILENAME); + $avatarPath = static::ASSETS_DIR . (file_exists(static::ASSETS_DIR . "$basename.jpg") ? "$basename.jpg" : "$basename.png"); + $configPath = static::ASSETS_DIR . "$basename-config." . pathinfo($filename, PATHINFO_EXTENSION); return [ UUIDUtil::getUUID(), json_decode(file_get_contents($dataPath), true, 512, JSON_THROW_ON_ERROR), $avatarPath, + json_decode(file_get_contents($configPath), true, 512, JSON_THROW_ON_ERROR), ]; }, array_filter( - scandir(self::ASSETS_DIR), - fn (string $filename) => pathinfo($filename, PATHINFO_EXTENSION) === 'json', + scandir(static::ASSETS_DIR), + fn (string $filename) => pathinfo($filename, PATHINFO_EXTENSION) === 'json' && mb_strpos(pathinfo($filename, PATHINFO_FILENAME), 'config') === false, ), ); } - /** - * @dataProvider dataImportExportAccount - */ - public function testImportExportAccount(string $userId, array $importData, string $avatarPath): void { + #[\PHPUnit\Framework\Attributes\DataProvider('dataImportExportAccount')] + public function testImportExportAccount(string $userId, array $importData, string $avatarPath, array $importConfig): void { $user = $this->userManager->createUser($userId, 'topsecretpassword'); $avatarExt = pathinfo($avatarPath, PATHINFO_EXTENSION); $exportData = $importData; + $exportConfig = $importConfig; // Verification status of email will be set to in progress on import so we set the export data to reflect that $exportData[IAccountManager::PROPERTY_EMAIL]['verified'] = IAccountManager::VERIFICATION_IN_PROGRESS; @@ -114,11 +100,18 @@ class AccountMigratorTest extends TestCase { ->with($this->migrator->getId()) ->willReturn(1); + $calls = [ + [static::REGEX_ACCOUNT_FILE, json_encode($importData)], + [static::REGEX_CONFIG_FILE, json_encode($importConfig)], + ]; $this->importSource - ->expects($this->once()) + ->expects($this->exactly(2)) ->method('getFileContents') - ->with($this->matchesRegularExpression(self::REGEX_ACCOUNT_FILE)) - ->willReturn(json_encode($importData)); + ->willReturnCallback(function ($path) use (&$calls) { + $expected = array_shift($calls); + $this->assertMatchesRegularExpression($expected[0], $path); + return $expected[1]; + }); $this->importSource ->expects($this->once()) @@ -129,7 +122,7 @@ class AccountMigratorTest extends TestCase { $this->importSource ->expects($this->once()) ->method('getFileAsStream') - ->with($this->matchesRegularExpression(self::REGEX_AVATAR_FILE)) + ->with($this->matchesRegularExpression(static::REGEX_AVATAR_FILE)) ->willReturn(fopen($avatarPath, 'r')); $this->migrator->import($user, $this->importSource, $this->output); @@ -149,15 +142,23 @@ class AccountMigratorTest extends TestCase { ); } + $calls = [ + [static::REGEX_ACCOUNT_FILE, new JsonMatches(json_encode($importData))], + [static::REGEX_CONFIG_FILE,new JsonMatches(json_encode($importConfig))], + ]; $this->exportDestination - ->expects($this->once()) + ->expects($this->exactly(2)) ->method('addFileContents') - ->with($this->matchesRegularExpression(self::REGEX_ACCOUNT_FILE), json_encode($exportData)); + ->willReturnCallback(function ($path) use (&$calls) { + $expected = array_shift($calls); + $this->assertMatchesRegularExpression($expected[0], $path); + return $expected[1]; + }); $this->exportDestination ->expects($this->once()) ->method('addFileAsStream') - ->with($this->matchesRegularExpression(self::REGEX_AVATAR_FILE), $this->isType('resource')); + ->with($this->matchesRegularExpression(static::REGEX_AVATAR_FILE), $this->isType('resource')); $this->migrator->export($user, $this->exportDestination, $this->output); } diff --git a/apps/settings/tests/UserMigration/assets/account-complex-config.json b/apps/settings/tests/UserMigration/assets/account-complex-config.json new file mode 100644 index 00000000000..fecf819057c --- /dev/null +++ b/apps/settings/tests/UserMigration/assets/account-complex-config.json @@ -0,0 +1 @@ +{"address":{"visibility":"show_users_only"},"avatar":{"visibility":"show_users_only"},"biography":{"visibility":"show"},"displayname":{"visibility":"show"},"fediverse":{"visibility":"show_users_only"},"headline":{"visibility":"show"},"organisation":{"visibility":"show"},"role":{"visibility":"show"},"email":{"visibility":"hide"},"phone":{"visibility":"hide"},"twitter":{"visibility":"show_users_only"},"website":{"visibility":"show_users_only"},"talk":{"visibility":"show"},"birthdate":{"visibility":"show_users_only"},"pronouns":{"visibility":"show"}}
\ No newline at end of file diff --git a/apps/settings/tests/UserMigration/assets/account-complex.jpg b/apps/settings/tests/UserMigration/assets/account-complex.jpg Binary files differindex bece3675c11..94508343322 100644 --- a/apps/settings/tests/UserMigration/assets/account-complex.jpg +++ b/apps/settings/tests/UserMigration/assets/account-complex.jpg diff --git a/apps/settings/tests/UserMigration/assets/account-complex.json b/apps/settings/tests/UserMigration/assets/account-complex.json index 4199b13b704..cb4668cf18c 100644 --- a/apps/settings/tests/UserMigration/assets/account-complex.json +++ b/apps/settings/tests/UserMigration/assets/account-complex.json @@ -1 +1 @@ -{"displayname":{"name":"displayname","value":"Steve Smith","scope":"v2-local","verified":"0","verificationData":""},"address":{"name":"address","value":"123 Water St","scope":"v2-local","verified":"0","verificationData":""},"website":{"name":"website","value":"https:\/\/example.org","scope":"v2-local","verified":"0","verificationData":""},"email":{"name":"email","value":"steve@example.org","scope":"v2-federated","verified":"0","verificationData":""},"avatar":{"name":"avatar","value":"","scope":"v2-local","verified":"0","verificationData":""},"phone":{"name":"phone","value":"+12178515387","scope":"v2-private","verified":"0","verificationData":""},"twitter":{"name":"twitter","value":"steve","scope":"v2-federated","verified":"0","verificationData":""},"organisation":{"name":"organisation","value":"Mytery Machine","scope":"v2-private","verified":"0","verificationData":""},"role":{"name":"role","value":"Manager","scope":"v2-private","verified":"0","verificationData":""},"headline":{"name":"headline","value":"I am Steve","scope":"v2-local","verified":"0","verificationData":""},"biography":{"name":"biography","value":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris porttitor ullamcorper dictum. Sed fermentum ut ligula scelerisque semper. Aliquam interdum convallis tellus eu dapibus. Integer in justo sollicitudin, hendrerit ligula sit amet, blandit sem.\n\nSuspendisse consectetur ultrices accumsan. Quisque sagittis bibendum lectus ut placerat. Mauris tincidunt ornare neque, et pulvinar tortor porttitor eu.","scope":"v2-local","verified":"0","verificationData":""},"profile_enabled":{"name":"profile_enabled","value":"1","scope":"v2-local","verified":"0","verificationData":""},"additional_mail":[{"name":"additional_mail","value":"steve@example.com","scope":"v2-published","verified":"0","verificationData":""},{"name":"additional_mail","value":"steve@earth.world","scope":"v2-local","verified":"0","verificationData":""}]}
\ No newline at end of file +{"displayname":{"name":"displayname","value":"Steve Smith","scope":"v2-local","verified":"0","verificationData":""},"address":{"name":"address","value":"123 Water St","scope":"v2-local","verified":"0","verificationData":""},"website":{"name":"website","value":"https://example.org","scope":"v2-local","verified":"0","verificationData":""},"email":{"name":"email","value":"steve@example.org","scope":"v2-federated","verified":"1","verificationData":""},"avatar":{"name":"avatar","value":"","scope":"v2-local","verified":"0","verificationData":""},"phone":{"name":"phone","value":"+12178515387","scope":"v2-private","verified":"0","verificationData":""},"twitter":{"name":"twitter","value":"steve","scope":"v2-federated","verified":"0","verificationData":""},"fediverse":{"name":"fediverse","value":"steve@floss.social","scope":"v2-federated","verified":"0","verificationData":""},"organisation":{"name":"organisation","value":"Mytery Machine","scope":"v2-private","verified":"0","verificationData":""},"role":{"name":"role","value":"Manager","scope":"v2-private","verified":"0","verificationData":""},"headline":{"name":"headline","value":"I am Steve","scope":"v2-local","verified":"0","verificationData":""},"biography":{"name":"biography","value":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris porttitor ullamcorper dictum. Sed fermentum ut ligula scelerisque semper. Aliquam interdum convallis tellus eu dapibus. Integer in justo sollicitudin, hendrerit ligula sit amet, blandit sem.\n\nSuspendisse consectetur ultrices accumsan. Quisque sagittis bibendum lectus ut placerat. Mauris tincidunt ornare neque, et pulvinar tortor porttitor eu.","scope":"v2-local","verified":"0","verificationData":""},"birthdate":{"name":"birthdate","value":"","scope":"v2-local","verified":"0","verificationData":""},"profile_enabled":{"name":"profile_enabled","value":"1","scope":"v2-local","verified":"0","verificationData":""},"pronouns":{"name":"pronouns","value":"they/them","scope":"v2-local","verified":"0","verificationData":""},"additional_mail":[{"name":"additional_mail","value":"steve@example.com","scope":"v2-published","verified":"0","verificationData":""},{"name":"additional_mail","value":"steve@earth.world","scope":"v2-local","verified":"0","verificationData":""}]}
\ No newline at end of file diff --git a/apps/settings/tests/UserMigration/assets/account-config.json b/apps/settings/tests/UserMigration/assets/account-config.json new file mode 100644 index 00000000000..a1250fab8e9 --- /dev/null +++ b/apps/settings/tests/UserMigration/assets/account-config.json @@ -0,0 +1 @@ +{"address":{"visibility":"show_users_only"},"avatar":{"visibility":"show"},"biography":{"visibility":"show"},"displayname":{"visibility":"show"},"fediverse":{"visibility":"show"},"headline":{"visibility":"show"},"organisation":{"visibility":"show"},"role":{"visibility":"show"},"email":{"visibility":"show_users_only"},"phone":{"visibility":"show_users_only"},"twitter":{"visibility":"show"},"website":{"visibility":"show"},"birthdate":{"visibility":"show"},"pronouns":{"visibility":"show"}}
\ No newline at end of file diff --git a/apps/settings/tests/UserMigration/assets/account.json b/apps/settings/tests/UserMigration/assets/account.json index 7f53f28c4d3..6bdc3c72d47 100644 --- a/apps/settings/tests/UserMigration/assets/account.json +++ b/apps/settings/tests/UserMigration/assets/account.json @@ -1 +1 @@ -{"displayname":{"name":"displayname","value":"Emma Jones","scope":"v2-federated","verified":"0","verificationData":""},"address":{"name":"address","value":"920 Grass St","scope":"v2-local","verified":"0","verificationData":""},"website":{"name":"website","value":"","scope":"v2-local","verified":"0","verificationData":""},"email":{"name":"email","value":"","scope":"v2-federated","verified":"0","verificationData":""},"avatar":{"name":"avatar","value":"","scope":"v2-federated","verified":"0","verificationData":""},"phone":{"name":"phone","value":"","scope":"v2-local","verified":"0","verificationData":""},"twitter":{"name":"twitter","value":"","scope":"v2-local","verified":"0","verificationData":""},"organisation":{"name":"organisation","value":"","scope":"v2-local","verified":"0","verificationData":""},"role":{"name":"role","value":"","scope":"v2-local","verified":"0","verificationData":""},"headline":{"name":"headline","value":"","scope":"v2-local","verified":"0","verificationData":""},"biography":{"name":"biography","value":"","scope":"v2-local","verified":"0","verificationData":""},"profile_enabled":{"name":"profile_enabled","value":"1","scope":"v2-local","verified":"0","verificationData":""},"additional_mail":[]}
\ No newline at end of file +{"displayname":{"name":"displayname","value":"Emma Jones","scope":"v2-federated","verified":"0","verificationData":""},"address":{"name":"address","value":"920 Grass St","scope":"v2-local","verified":"0","verificationData":""},"website":{"name":"website","value":"","scope":"v2-local","verified":"0","verificationData":""},"email":{"name":"email","value":"","scope":"v2-federated","verified":"1","verificationData":""},"avatar":{"name":"avatar","value":"","scope":"v2-federated","verified":"0","verificationData":""},"phone":{"name":"phone","value":"","scope":"v2-local","verified":"0","verificationData":""},"twitter":{"name":"twitter","value":"","scope":"v2-local","verified":"0","verificationData":""},"fediverse":{"name":"fediverse","value":"","scope":"v2-local","verified":"0","verificationData":""},"organisation":{"name":"organisation","value":"","scope":"v2-local","verified":"0","verificationData":""},"role":{"name":"role","value":"","scope":"v2-local","verified":"0","verificationData":""},"headline":{"name":"headline","value":"","scope":"v2-local","verified":"0","verificationData":""},"biography":{"name":"biography","value":"","scope":"v2-local","verified":"0","verificationData":""},"birthdate":{"name":"birthdate","value":"","scope":"v2-local","verified":"0","verificationData":""},"profile_enabled":{"name":"profile_enabled","value":"1","scope":"v2-local","verified":"0","verificationData":""},"pronouns":{"name":"pronouns","value":"","scope":"v2-federated","verified":"0","verificationData":""},"additional_mail":[]}
\ No newline at end of file diff --git a/apps/settings/tests/UserMigration/assets/account.png b/apps/settings/tests/UserMigration/assets/account.png Binary files differindex 12226f14334..41c4924e569 100644 --- a/apps/settings/tests/UserMigration/assets/account.png +++ b/apps/settings/tests/UserMigration/assets/account.png |