AccountMigratorTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2022 Christopher Ng <chrng8@gmail.com>
  5. *
  6. * @author Christopher Ng <chrng8@gmail.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Settings\Tests\UserMigration;
  25. use OCA\Settings\AppInfo\Application;
  26. use OCA\Settings\UserMigration\AccountMigrator;
  27. use OCP\Accounts\IAccountManager;
  28. use OCP\AppFramework\App;
  29. use OCP\IAvatarManager;
  30. use OCP\IUserManager;
  31. use OCP\UserMigration\IExportDestination;
  32. use OCP\UserMigration\IImportSource;
  33. use PHPUnit\Framework\Constraint\JsonMatches;
  34. use PHPUnit\Framework\MockObject\MockObject;
  35. use Sabre\VObject\UUIDUtil;
  36. use Symfony\Component\Console\Output\OutputInterface;
  37. use Test\TestCase;
  38. /**
  39. * @group DB
  40. */
  41. class AccountMigratorTest extends TestCase {
  42. private IUserManager $userManager;
  43. private IAvatarManager $avatarManager;
  44. private AccountMigrator $migrator;
  45. /** @var IImportSource|MockObject */
  46. private $importSource;
  47. /** @var IExportDestination|MockObject */
  48. private $exportDestination;
  49. /** @var OutputInterface|MockObject */
  50. private $output;
  51. private const ASSETS_DIR = __DIR__ . '/assets/';
  52. private const REGEX_ACCOUNT_FILE = '/^' . Application::APP_ID . '\/' . '[a-z]+\.json' . '$/';
  53. private const REGEX_AVATAR_FILE = '/^' . Application::APP_ID . '\/' . 'avatar\.(jpg|png)' . '$/';
  54. private const REGEX_CONFIG_FILE = '/^' . Application::APP_ID . '\/' . '[a-z]+\.json' . '$/';
  55. protected function setUp(): void {
  56. $app = new App(Application::APP_ID);
  57. $container = $app->getContainer();
  58. $this->userManager = $container->get(IUserManager::class);
  59. $this->avatarManager = $container->get(IAvatarManager::class);
  60. $this->migrator = $container->get(AccountMigrator::class);
  61. $this->importSource = $this->createMock(IImportSource::class);
  62. $this->exportDestination = $this->createMock(IExportDestination::class);
  63. $this->output = $this->createMock(OutputInterface::class);
  64. }
  65. public function dataImportExportAccount(): array {
  66. return array_map(
  67. function (string $filename) {
  68. $dataPath = static::ASSETS_DIR . $filename;
  69. // For each account json file there is an avatar image and a config json file with the same basename
  70. $basename = pathinfo($filename, PATHINFO_FILENAME);
  71. $avatarPath = static::ASSETS_DIR . (file_exists(static::ASSETS_DIR . "$basename.jpg") ? "$basename.jpg" : "$basename.png");
  72. $configPath = static::ASSETS_DIR . "$basename-config." . pathinfo($filename, PATHINFO_EXTENSION);
  73. return [
  74. UUIDUtil::getUUID(),
  75. json_decode(file_get_contents($dataPath), true, 512, JSON_THROW_ON_ERROR),
  76. $avatarPath,
  77. json_decode(file_get_contents($configPath), true, 512, JSON_THROW_ON_ERROR),
  78. ];
  79. },
  80. array_filter(
  81. scandir(static::ASSETS_DIR),
  82. fn (string $filename) => pathinfo($filename, PATHINFO_EXTENSION) === 'json' && mb_strpos(pathinfo($filename, PATHINFO_FILENAME), 'config') === false,
  83. ),
  84. );
  85. }
  86. /**
  87. * @dataProvider dataImportExportAccount
  88. */
  89. public function testImportExportAccount(string $userId, array $importData, string $avatarPath, array $importConfig): void {
  90. $user = $this->userManager->createUser($userId, 'topsecretpassword');
  91. $avatarExt = pathinfo($avatarPath, PATHINFO_EXTENSION);
  92. $exportData = $importData;
  93. $exportConfig = $importConfig;
  94. // Verification status of email will be set to in progress on import so we set the export data to reflect that
  95. $exportData[IAccountManager::PROPERTY_EMAIL]['verified'] = IAccountManager::VERIFICATION_IN_PROGRESS;
  96. $this->importSource
  97. ->expects($this->once())
  98. ->method('getMigratorVersion')
  99. ->with($this->migrator->getId())
  100. ->willReturn(1);
  101. $this->importSource
  102. ->expects($this->exactly(2))
  103. ->method('getFileContents')
  104. ->withConsecutive(
  105. [$this->matchesRegularExpression(static::REGEX_ACCOUNT_FILE)],
  106. [$this->matchesRegularExpression(static::REGEX_CONFIG_FILE)],
  107. )
  108. ->willReturnOnConsecutiveCalls(
  109. json_encode($importData),
  110. json_encode($importConfig),
  111. );
  112. $this->importSource
  113. ->expects($this->once())
  114. ->method('getFolderListing')
  115. ->with(Application::APP_ID . '/')
  116. ->willReturn(["avatar.$avatarExt"]);
  117. $this->importSource
  118. ->expects($this->once())
  119. ->method('getFileAsStream')
  120. ->with($this->matchesRegularExpression(static::REGEX_AVATAR_FILE))
  121. ->willReturn(fopen($avatarPath, 'r'));
  122. $this->migrator->import($user, $this->importSource, $this->output);
  123. $importedAvatar = $this->avatarManager->getAvatar($user->getUID());
  124. $this->assertTrue($importedAvatar->isCustomAvatar());
  125. /**
  126. * Avatar images are re-encoded on import therefore JPEG images which use lossy compression cannot be checked for equality
  127. * @see https://github.com/nextcloud/server/blob/9644b7e505dc90a1e683f77ad38dc6dc4e90fa2f/lib/private/legacy/OC_Image.php#L383-L390
  128. */
  129. if ($avatarExt !== 'jpg') {
  130. $this->assertStringEqualsFile(
  131. $avatarPath,
  132. $importedAvatar->getFile(-1)->getContent(),
  133. );
  134. }
  135. $this->exportDestination
  136. ->expects($this->exactly(2))
  137. ->method('addFileContents')
  138. ->withConsecutive(
  139. [$this->matchesRegularExpression(static::REGEX_ACCOUNT_FILE), new JsonMatches(json_encode($exportData))],
  140. [$this->matchesRegularExpression(static::REGEX_CONFIG_FILE), new JsonMatches(json_encode($exportConfig))],
  141. );
  142. $this->exportDestination
  143. ->expects($this->once())
  144. ->method('addFileAsStream')
  145. ->with($this->matchesRegularExpression(static::REGEX_AVATAR_FILE), $this->isType('resource'));
  146. $this->migrator->export($user, $this->exportDestination, $this->output);
  147. }
  148. }