ContactsMigratorTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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\DAV\Tests\integration\UserMigration;
  8. use OCA\DAV\AppInfo\Application;
  9. use OCA\DAV\UserMigration\ContactsMigrator;
  10. use OCP\AppFramework\App;
  11. use OCP\IUserManager;
  12. use Sabre\VObject\Component\VCard;
  13. use Sabre\VObject\Parser\Parser as VObjectParser;
  14. use Sabre\VObject\Property as VObjectProperty;
  15. use Sabre\VObject\Splitter\VCard as VCardSplitter;
  16. use Sabre\VObject\UUIDUtil;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Test\TestCase;
  19. use function scandir;
  20. /**
  21. * @group DB
  22. */
  23. class ContactsMigratorTest extends TestCase {
  24. private IUserManager $userManager;
  25. private ContactsMigrator $migrator;
  26. private OutputInterface $output;
  27. private const ASSETS_DIR = __DIR__ . '/assets/address_books/';
  28. protected function setUp(): void {
  29. $app = new App(Application::APP_ID);
  30. $container = $app->getContainer();
  31. $this->userManager = $container->get(IUserManager::class);
  32. $this->migrator = $container->get(ContactsMigrator::class);
  33. $this->output = $this->createMock(OutputInterface::class);
  34. }
  35. public function dataAssets(): array {
  36. return array_map(
  37. function (string $filename) {
  38. $vCardSplitter = new VCardSplitter(
  39. fopen(self::ASSETS_DIR . $filename, 'r'),
  40. VObjectParser::OPTION_FORGIVING,
  41. );
  42. /** @var VCard[] $vCards */
  43. $vCards = [];
  44. while ($vCard = $vCardSplitter->getNext()) {
  45. $vCards[] = $vCard;
  46. }
  47. [$initialAddressBookUri, $ext] = explode('.', $filename, 2);
  48. $metadata = ['displayName' => ucwords(str_replace('-', ' ', $initialAddressBookUri))];
  49. return [UUIDUtil::getUUID(), $filename, $initialAddressBookUri, $metadata, $vCards];
  50. },
  51. array_diff(
  52. scandir(self::ASSETS_DIR),
  53. // Exclude current and parent directories
  54. ['.', '..'],
  55. ),
  56. );
  57. }
  58. private function getPropertiesChangedOnImport(VCard $vCard): array {
  59. return array_map(
  60. fn (VObjectProperty $property) => $property->serialize(),
  61. array_values(array_filter(
  62. $vCard->children(),
  63. fn (mixed $child) => $child instanceof VObjectProperty && $child->name === 'PRODID',
  64. )),
  65. );
  66. }
  67. private function getProperties(VCard $vCard): array {
  68. return array_map(
  69. fn (VObjectProperty $property) => $property->serialize(),
  70. array_values(array_filter(
  71. $vCard->children(),
  72. fn (mixed $child) => $child instanceof VObjectProperty && $child->name !== 'PRODID',
  73. )),
  74. );
  75. }
  76. /**
  77. * @dataProvider dataAssets
  78. *
  79. * @param array{displayName: string, description?: string} $importMetadata
  80. * @param VCard[] $importCards
  81. */
  82. public function testImportExportAsset(string $userId, string $filename, string $initialAddressBookUri, array $importMetadata, array $importCards): void {
  83. $user = $this->userManager->createUser($userId, 'topsecretpassword');
  84. foreach ($importCards as $importCard) {
  85. $problems = $importCard->validate();
  86. $this->assertEmpty($problems);
  87. }
  88. $this->invokePrivate($this->migrator, 'importAddressBook', [$user, $filename, $initialAddressBookUri, $importMetadata, $importCards, $this->output]);
  89. $addressBookExports = $this->invokePrivate($this->migrator, 'getAddressBookExports', [$user, $this->output]);
  90. $this->assertCount(1, $addressBookExports);
  91. /** @var VCard[] $exportCards */
  92. ['displayName' => $displayName, 'description' => $description, 'vCards' => $exportCards] = reset($addressBookExports);
  93. $exportMetadata = array_filter(['displayName' => $displayName, 'description' => $description]);
  94. $this->assertEquals($importMetadata, $exportMetadata);
  95. $this->assertSameSize($importCards, $exportCards);
  96. $importProperties = [];
  97. $exportProperties = [];
  98. for ($i = 0, $iMax = count($importCards); $i < $iMax; ++$i) {
  99. $importProperties[] = $this->getPropertiesChangedOnImport($importCards[$i]);
  100. $exportProperties[] = $this->getPropertiesChangedOnImport($exportCards[$i]);
  101. }
  102. $this->assertNotEqualsCanonicalizing(
  103. $importProperties,
  104. $exportProperties,
  105. );
  106. $importProperties = [];
  107. $exportProperties = [];
  108. for ($i = 0, $iMax = count($importCards); $i < $iMax; ++$i) {
  109. $importProperties[] = $this->getProperties($importCards[$i]);
  110. $exportProperties[] = $this->getProperties($exportCards[$i]);
  111. }
  112. $this->assertEqualsCanonicalizing(
  113. $importProperties,
  114. $exportProperties,
  115. );
  116. }
  117. }