CalendarMigratorTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\CalendarMigrator;
  10. use OCP\AppFramework\App;
  11. use OCP\IUserManager;
  12. use Sabre\VObject\Component as VObjectComponent;
  13. use Sabre\VObject\Component\VCalendar;
  14. use Sabre\VObject\Property as VObjectProperty;
  15. use Sabre\VObject\Reader as VObjectReader;
  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 CalendarMigratorTest extends TestCase {
  24. private IUserManager $userManager;
  25. private CalendarMigrator $migrator;
  26. private OutputInterface $output;
  27. private const ASSETS_DIR = __DIR__ . '/assets/calendars/';
  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(CalendarMigrator::class);
  33. $this->output = $this->createMock(OutputInterface::class);
  34. }
  35. public function dataAssets(): array {
  36. return array_map(
  37. function (string $filename) {
  38. /** @var VCalendar $vCalendar */
  39. $vCalendar = VObjectReader::read(
  40. fopen(self::ASSETS_DIR . $filename, 'r'),
  41. VObjectReader::OPTION_FORGIVING,
  42. );
  43. [$initialCalendarUri, $ext] = explode('.', $filename, 2);
  44. return [UUIDUtil::getUUID(), $filename, $initialCalendarUri, $vCalendar];
  45. },
  46. array_diff(
  47. scandir(self::ASSETS_DIR),
  48. // Exclude current and parent directories
  49. ['.', '..'],
  50. ),
  51. );
  52. }
  53. private function getProperties(VCalendar $vCalendar): array {
  54. return array_map(
  55. fn (VObjectProperty $property) => $property->serialize(),
  56. array_values(array_filter(
  57. $vCalendar->children(),
  58. fn ($child) => $child instanceof VObjectProperty,
  59. )),
  60. );
  61. }
  62. private function getComponents(VCalendar $vCalendar): array {
  63. return array_map(
  64. // Elements of the serialized blob are sorted
  65. fn (VObjectComponent $component) => $component->serialize(),
  66. $vCalendar->getComponents(),
  67. );
  68. }
  69. private function getSanitizedComponents(VCalendar $vCalendar): array {
  70. return array_map(
  71. // Elements of the serialized blob are sorted
  72. fn (VObjectComponent $component) => $this->invokePrivate($this->migrator, 'sanitizeComponent', [$component])->serialize(),
  73. $vCalendar->getComponents(),
  74. );
  75. }
  76. /**
  77. * @dataProvider dataAssets
  78. */
  79. public function testImportExportAsset(string $userId, string $filename, string $initialCalendarUri, VCalendar $importCalendar): void {
  80. $user = $this->userManager->createUser($userId, 'topsecretpassword');
  81. $problems = $importCalendar->validate();
  82. $this->assertEmpty($problems);
  83. $this->invokePrivate($this->migrator, 'importCalendar', [$user, $filename, $initialCalendarUri, $importCalendar, $this->output]);
  84. $calendarExports = $this->invokePrivate($this->migrator, 'getCalendarExports', [$user, $this->output]);
  85. $this->assertCount(1, $calendarExports);
  86. /** @var VCalendar $exportCalendar */
  87. ['vCalendar' => $exportCalendar] = reset($calendarExports);
  88. $this->assertEqualsCanonicalizing(
  89. $this->getProperties($importCalendar),
  90. $this->getProperties($exportCalendar),
  91. );
  92. $this->assertEqualsCanonicalizing(
  93. // Components are expected to be sanitized on import
  94. $this->getSanitizedComponents($importCalendar),
  95. $this->getComponents($exportCalendar),
  96. );
  97. }
  98. }