CalendarMigratorTest.php 4.2 KB

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