Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test migration of avatars #31866

Merged
merged 4 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 59 additions & 10 deletions apps/settings/tests/UserMigration/AccountMigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCA\Settings\UserMigration\AccountMigrator;
use OCP\Accounts\IAccountManager;
use OCP\AppFramework\App;
use OCP\IAvatarManager;
use OCP\IUserManager;
use OCP\UserMigration\IExportDestination;
use OCP\UserMigration\IImportSource;
Expand All @@ -45,6 +46,8 @@ class AccountMigratorTest extends TestCase {

private IUserManager $userManager;

private IAvatarManager $avatarManager;

private AccountMigrator $migrator;

/** @var IImportSource|MockObject */
Expand All @@ -58,13 +61,16 @@ class AccountMigratorTest extends TestCase {

private const ASSETS_DIR = __DIR__ . '/assets/';

private const REGEX_ACCOUNT_FILE = '/' . Application::APP_ID . '\/' . '[a-z]+\.json' . '/';
private const REGEX_ACCOUNT_FILE = '/^' . Application::APP_ID . '\/' . '[a-z]+\.json' . '$/';

private const REGEX_AVATAR_FILE = '/^' . Application::APP_ID . '\/' . 'avatar\.(jpg|png)' . '$/';

protected function setUp(): void {
$app = new App(Application::APP_ID);
$container = $app->getContainer();

$this->userManager = $container->get(IUserManager::class);
$this->avatarManager = $container->get(IAvatarManager::class);
$this->migrator = $container->get(AccountMigrator::class);

$this->importSource = $this->createMock(IImportSource::class);
Expand All @@ -74,23 +80,33 @@ protected function setUp(): void {

public function dataImportExportAccount(): array {
return array_map(
fn (string $filename) => [
UUIDUtil::getUUID(),
json_decode(file_get_contents(self::ASSETS_DIR . $filename), true, 512, JSON_THROW_ON_ERROR),
],
array_diff(
scandir(self::ASSETS_DIR),
// Exclude current and parent directories
['.', '..'],
function (string $filename) {
$dataPath = self::ASSETS_DIR . $filename;
$avatarBasename = pathinfo($filename, PATHINFO_FILENAME);
come-nc marked this conversation as resolved.
Show resolved Hide resolved
$avatarPath = self::ASSETS_DIR . (file_exists(self::ASSETS_DIR . "$avatarBasename.jpg") ? "$avatarBasename.jpg" : "$avatarBasename.png");
return [
UUIDUtil::getUUID(),
json_decode(file_get_contents($dataPath), true, 512, JSON_THROW_ON_ERROR),
$avatarPath,
];
},
array_filter(
array_diff(
scandir(self::ASSETS_DIR),
// Exclude current and parent directories
['.', '..'],
),
Pytal marked this conversation as resolved.
Show resolved Hide resolved
fn (string $filename) => pathinfo($filename, PATHINFO_EXTENSION) === 'json',
),
);
}

/**
* @dataProvider dataImportExportAccount
*/
public function testImportExportAccount(string $userId, array $importData): void {
public function testImportExportAccount(string $userId, array $importData, string $avatarPath): void {
$user = $this->userManager->createUser($userId, 'topsecretpassword');
$avatarExt = pathinfo($avatarPath, PATHINFO_EXTENSION);
$exportData = $importData;
// 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;
Expand All @@ -107,14 +123,47 @@ public function testImportExportAccount(string $userId, array $importData): void
->with($this->matchesRegularExpression(self::REGEX_ACCOUNT_FILE))
->willReturn(json_encode($importData));

$this->importSource
->expects($this->once())
->method('getFolderListing')
->with(Application::APP_ID . '/')
->willReturn(["avatar.$avatarExt"]);

$this->importSource
->expects($this->once())
->method('getFileAsStream')
->with($this->matchesRegularExpression(self::REGEX_AVATAR_FILE))
->willReturn(fopen($avatarPath, 'r'));

$this->migrator->import($user, $this->importSource, $this->output);

$importedAvatar = $this->avatarManager->getAvatar($user->getUID());
$this->assertTrue($importedAvatar->isCustomAvatar());

/**
* Avatar images are re-encoded on import therefore JPEG images which use lossy compression cannot be checked for equality
* @see https://github.com/nextcloud/server/blob/9644b7e505dc90a1e683f77ad38dc6dc4e90fa2f/lib/private/legacy/OC_Image.php#L383-L390
*/

if ($avatarExt !== 'jpg') {
$this->assertStringEqualsFile(
$avatarPath,
$importedAvatar->getFile(-1)->getContent(),
);
}

$this->exportDestination
->expects($this->once())
->method('addFileContents')
->with($this->matchesRegularExpression(self::REGEX_ACCOUNT_FILE), json_encode($exportData))
->willReturn(true);

$this->exportDestination
->expects($this->once())
->method('addFileAsStream')
->with($this->matchesRegularExpression(self::REGEX_AVATAR_FILE), $this->isType('resource'))
->willReturn(true);

$this->migrator->export($user, $this->exportDestination, $this->output);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.