ContactsMigratorTest.php 4.7 KB

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