Skip to content

Commit 9644b7e

Browse files
authored
Merge pull request #31852 from nextcloud/test-account-migrator
2 parents 9f34d17 + 8aa9ea7 commit 9644b7e

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright 2022 Christopher Ng <chrng8@gmail.com>
7+
*
8+
* @author Christopher Ng <chrng8@gmail.com>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\Settings\Tests\UserMigration;
28+
29+
use OCA\Settings\AppInfo\Application;
30+
use OCA\Settings\UserMigration\AccountMigrator;
31+
use OCP\Accounts\IAccountManager;
32+
use OCP\AppFramework\App;
33+
use OCP\IUserManager;
34+
use OCP\UserMigration\IExportDestination;
35+
use OCP\UserMigration\IImportSource;
36+
use PHPUnit\Framework\MockObject\MockObject;
37+
use Sabre\VObject\UUIDUtil;
38+
use Symfony\Component\Console\Output\OutputInterface;
39+
use Test\TestCase;
40+
41+
/**
42+
* @group DB
43+
*/
44+
class AccountMigratorTest extends TestCase {
45+
46+
private IUserManager $userManager;
47+
48+
private AccountMigrator $migrator;
49+
50+
/** @var IImportSource|MockObject */
51+
private $importSource;
52+
53+
/** @var IExportDestination|MockObject */
54+
private $exportDestination;
55+
56+
/** @var OutputInterface|MockObject */
57+
private $output;
58+
59+
private const ASSETS_DIR = __DIR__ . '/assets/';
60+
61+
private const REGEX_ACCOUNT_FILE = '/' . Application::APP_ID . '\/' . '[a-z]+\.json' . '/';
62+
63+
protected function setUp(): void {
64+
$app = new App(Application::APP_ID);
65+
$container = $app->getContainer();
66+
67+
$this->userManager = $container->get(IUserManager::class);
68+
$this->migrator = $container->get(AccountMigrator::class);
69+
70+
$this->importSource = $this->createMock(IImportSource::class);
71+
$this->exportDestination = $this->createMock(IExportDestination::class);
72+
$this->output = $this->createMock(OutputInterface::class);
73+
}
74+
75+
public function dataImportExportAccount(): array {
76+
return array_map(
77+
fn (string $filename) => [
78+
UUIDUtil::getUUID(),
79+
json_decode(file_get_contents(self::ASSETS_DIR . $filename), true, 512, JSON_THROW_ON_ERROR),
80+
],
81+
array_diff(
82+
scandir(self::ASSETS_DIR),
83+
// Exclude current and parent directories
84+
['.', '..'],
85+
),
86+
);
87+
}
88+
89+
/**
90+
* @dataProvider dataImportExportAccount
91+
*/
92+
public function testImportExportAccount(string $userId, array $importData): void {
93+
$user = $this->userManager->createUser($userId, 'topsecretpassword');
94+
$exportData = $importData;
95+
// Verification status of email will be set to in progress on import so we set the export data to reflect that
96+
$exportData[IAccountManager::PROPERTY_EMAIL]['verified'] = IAccountManager::VERIFICATION_IN_PROGRESS;
97+
98+
$this->importSource
99+
->expects($this->once())
100+
->method('getMigratorVersion')
101+
->with(AccountMigrator::class)
102+
->willReturn(1);
103+
104+
$this->importSource
105+
->expects($this->once())
106+
->method('getFileContents')
107+
->with($this->matchesRegularExpression(self::REGEX_ACCOUNT_FILE))
108+
->willReturn(json_encode($importData));
109+
110+
$this->migrator->import($user, $this->importSource, $this->output);
111+
112+
$this->exportDestination
113+
->expects($this->once())
114+
->method('addFileContents')
115+
->with($this->matchesRegularExpression(self::REGEX_ACCOUNT_FILE), json_encode($exportData))
116+
->willReturn(true);
117+
118+
$this->migrator->export($user, $this->exportDestination, $this->output);
119+
}
120+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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":""}]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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":[]}

0 commit comments

Comments
 (0)