1
0

AccountMigratorTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\Tests\UserMigration;
  8. use OCA\Settings\AppInfo\Application;
  9. use OCA\Settings\UserMigration\AccountMigrator;
  10. use OCP\Accounts\IAccountManager;
  11. use OCP\AppFramework\App;
  12. use OCP\IAvatarManager;
  13. use OCP\IUserManager;
  14. use OCP\UserMigration\IExportDestination;
  15. use OCP\UserMigration\IImportSource;
  16. use PHPUnit\Framework\Constraint\JsonMatches;
  17. use PHPUnit\Framework\MockObject\MockObject;
  18. use Sabre\VObject\UUIDUtil;
  19. use Symfony\Component\Console\Output\OutputInterface;
  20. use Test\TestCase;
  21. /**
  22. * @group DB
  23. */
  24. class AccountMigratorTest extends TestCase {
  25. private IUserManager $userManager;
  26. private IAvatarManager $avatarManager;
  27. private AccountMigrator $migrator;
  28. /** @var IImportSource|MockObject */
  29. private $importSource;
  30. /** @var IExportDestination|MockObject */
  31. private $exportDestination;
  32. /** @var OutputInterface|MockObject */
  33. private $output;
  34. private const ASSETS_DIR = __DIR__ . '/assets/';
  35. private const REGEX_ACCOUNT_FILE = '/^' . Application::APP_ID . '\/' . '[a-z]+\.json' . '$/';
  36. private const REGEX_AVATAR_FILE = '/^' . Application::APP_ID . '\/' . 'avatar\.(jpg|png)' . '$/';
  37. private const REGEX_CONFIG_FILE = '/^' . Application::APP_ID . '\/' . '[a-z]+\.json' . '$/';
  38. protected function setUp(): void {
  39. $app = new App(Application::APP_ID);
  40. $container = $app->getContainer();
  41. $this->userManager = $container->get(IUserManager::class);
  42. $this->avatarManager = $container->get(IAvatarManager::class);
  43. $this->migrator = $container->get(AccountMigrator::class);
  44. $this->importSource = $this->createMock(IImportSource::class);
  45. $this->exportDestination = $this->createMock(IExportDestination::class);
  46. $this->output = $this->createMock(OutputInterface::class);
  47. }
  48. public function dataImportExportAccount(): array {
  49. return array_map(
  50. function (string $filename) {
  51. $dataPath = static::ASSETS_DIR . $filename;
  52. // For each account json file there is an avatar image and a config json file with the same basename
  53. $basename = pathinfo($filename, PATHINFO_FILENAME);
  54. $avatarPath = static::ASSETS_DIR . (file_exists(static::ASSETS_DIR . "$basename.jpg") ? "$basename.jpg" : "$basename.png");
  55. $configPath = static::ASSETS_DIR . "$basename-config." . pathinfo($filename, PATHINFO_EXTENSION);
  56. return [
  57. UUIDUtil::getUUID(),
  58. json_decode(file_get_contents($dataPath), true, 512, JSON_THROW_ON_ERROR),
  59. $avatarPath,
  60. json_decode(file_get_contents($configPath), true, 512, JSON_THROW_ON_ERROR),
  61. ];
  62. },
  63. array_filter(
  64. scandir(static::ASSETS_DIR),
  65. fn (string $filename) => pathinfo($filename, PATHINFO_EXTENSION) === 'json' && mb_strpos(pathinfo($filename, PATHINFO_FILENAME), 'config') === false,
  66. ),
  67. );
  68. }
  69. /**
  70. * @dataProvider dataImportExportAccount
  71. */
  72. public function testImportExportAccount(string $userId, array $importData, string $avatarPath, array $importConfig): void {
  73. $user = $this->userManager->createUser($userId, 'topsecretpassword');
  74. $avatarExt = pathinfo($avatarPath, PATHINFO_EXTENSION);
  75. $exportData = $importData;
  76. $exportConfig = $importConfig;
  77. // Verification status of email will be set to in progress on import so we set the export data to reflect that
  78. $exportData[IAccountManager::PROPERTY_EMAIL]['verified'] = IAccountManager::VERIFICATION_IN_PROGRESS;
  79. $this->importSource
  80. ->expects($this->once())
  81. ->method('getMigratorVersion')
  82. ->with($this->migrator->getId())
  83. ->willReturn(1);
  84. $this->importSource
  85. ->expects($this->exactly(2))
  86. ->method('getFileContents')
  87. ->withConsecutive(
  88. [$this->matchesRegularExpression(static::REGEX_ACCOUNT_FILE)],
  89. [$this->matchesRegularExpression(static::REGEX_CONFIG_FILE)],
  90. )
  91. ->willReturnOnConsecutiveCalls(
  92. json_encode($importData),
  93. json_encode($importConfig),
  94. );
  95. $this->importSource
  96. ->expects($this->once())
  97. ->method('getFolderListing')
  98. ->with(Application::APP_ID . '/')
  99. ->willReturn(["avatar.$avatarExt"]);
  100. $this->importSource
  101. ->expects($this->once())
  102. ->method('getFileAsStream')
  103. ->with($this->matchesRegularExpression(static::REGEX_AVATAR_FILE))
  104. ->willReturn(fopen($avatarPath, 'r'));
  105. $this->migrator->import($user, $this->importSource, $this->output);
  106. $importedAvatar = $this->avatarManager->getAvatar($user->getUID());
  107. $this->assertTrue($importedAvatar->isCustomAvatar());
  108. /**
  109. * Avatar images are re-encoded on import therefore JPEG images which use lossy compression cannot be checked for equality
  110. * @see https://github.com/nextcloud/server/blob/9644b7e505dc90a1e683f77ad38dc6dc4e90fa2f/lib/private/legacy/OC_Image.php#L383-L390
  111. */
  112. if ($avatarExt !== 'jpg') {
  113. $this->assertStringEqualsFile(
  114. $avatarPath,
  115. $importedAvatar->getFile(-1)->getContent(),
  116. );
  117. }
  118. $this->exportDestination
  119. ->expects($this->exactly(2))
  120. ->method('addFileContents')
  121. ->withConsecutive(
  122. [$this->matchesRegularExpression(static::REGEX_ACCOUNT_FILE), new JsonMatches(json_encode($exportData))],
  123. [$this->matchesRegularExpression(static::REGEX_CONFIG_FILE), new JsonMatches(json_encode($exportConfig))],
  124. );
  125. $this->exportDestination
  126. ->expects($this->once())
  127. ->method('addFileAsStream')
  128. ->with($this->matchesRegularExpression(static::REGEX_AVATAR_FILE), $this->isType('resource'));
  129. $this->migrator->export($user, $this->exportDestination, $this->output);
  130. }
  131. }