subject = new RdfImporter(); } public function testItFailsWithUnknownStrategyConfigured(): void { $this->currentFile = self::SINGLE_TT_RDF; $form = [ 'uploaded_file' => $this->currentFile ]; $this->switchStrategy('unknown'); $importReport = $this->subject->import( new core_kernel_classes_Class(TestTakerService::CLASS_URI_SUBJECT), $form, self::ADMIN_USER ); self::assertEquals(true, $importReport->containsError()); self::assertCount(1, $importReport->getErrors(true)); self::assertEquals('Data import failed', $importReport->getMessage()); self::assertEquals( 'oat\taoTestTaker\models\RdfImporter configured incorrectly', $importReport->getErrors(true)[0]->getMessage() ); $this->grabImportedUris($importReport); self::assertCount(0, $this->importedUris); } public function testItFailsWithFailOnDuplicateStrategyWhenOneUserInFile(): void { $this->currentFile = self::SINGLE_TT_RDF; $form = [ 'uploaded_file' => $this->currentFile ]; $this->switchStrategy(RdfImporter::OPTION_STRATEGY_FAIL_ON_DUPLICATE); $importReport = $this->subject->import( new core_kernel_classes_Class(TestTakerService::CLASS_URI_SUBJECT), $form, self::ADMIN_USER ); self::assertEquals(false, $importReport->containsError()); $this->grabImportedUris($importReport); self::assertCount(1, $this->importedUris); $importReport = $this->subject->import( new core_kernel_classes_Class(TestTakerService::CLASS_URI_SUBJECT), $form, self::ADMIN_USER ); self::assertEquals(true, $importReport->containsError()); $this->grabImportedUris($importReport); self::assertCount(1, $this->importedUris); } public function testItFailsWithFailOnDuplicateStrategyWhenMultipleUserInFile(): void { $this->currentFile = self::MULTI_TT_RDF; $form = [ 'uploaded_file' => $this->currentFile ]; $this->switchStrategy(RdfImporter::OPTION_STRATEGY_FAIL_ON_DUPLICATE); $importReport = $this->subject->import( new core_kernel_classes_Class(TestTakerService::CLASS_URI_SUBJECT), $form, self::ADMIN_USER ); self::assertEquals(true, $importReport->containsError()); $this->grabImportedUris($importReport); self::assertCount(2, $this->importedUris); } public function testItSkipsWithSkipOnDuplicateStrategyWhenSingleUserInFile(): void { $this->currentFile = self::SINGLE_TT_RDF; $form = [ 'uploaded_file' => $this->currentFile ]; $this->switchStrategy(RdfImporter::OPTION_STRATEGY_SKIP_ON_DUPLICATE); $importReport = $this->subject->import( new core_kernel_classes_Class(TestTakerService::CLASS_URI_SUBJECT), $form, self::ADMIN_USER ); self::assertEquals(false, $importReport->containsError()); $this->grabImportedUris($importReport); self::assertCount(1, $this->importedUris); $importReport = $this->subject->import( new core_kernel_classes_Class(TestTakerService::CLASS_URI_SUBJECT), $form, self::ADMIN_USER ); self::assertEquals(false, $importReport->containsError()); self::assertEquals(true, $importReport->contains(Report::TYPE_WARNING)); self::assertCount(2, $this->getWarningReports($importReport)); $this->grabImportedUris($importReport); self::assertCount(1, $this->importedUris); } public function testItSkipsWithSkipOnDuplicateStrategyWhenMultipleUserInFile(): void { $this->currentFile = self::MULTI_TT_RDF; $form = [ 'uploaded_file' => $this->currentFile ]; $this->switchStrategy(RdfImporter::OPTION_STRATEGY_SKIP_ON_DUPLICATE); $importReport = $this->subject->import( new core_kernel_classes_Class(TestTakerService::CLASS_URI_SUBJECT), $form, self::ADMIN_USER ); self::assertEquals(false, $importReport->containsError()); self::assertEquals(true, $importReport->contains(Report::TYPE_WARNING)); self::assertCount(2, $this->getWarningReports($importReport)); $this->grabImportedUris($importReport); self::assertCount(3, $this->importedUris); $importReport = $this->subject->import( new core_kernel_classes_Class(TestTakerService::CLASS_URI_SUBJECT), $form, self::ADMIN_USER ); self::assertEquals(false, $importReport->containsError()); self::assertEquals(true, $importReport->contains(Report::TYPE_WARNING)); self::assertCount(8, $this->getWarningReports($importReport)); $this->grabImportedUris($importReport); self::assertCount(3, $this->importedUris); } public function testItImportsWithImportOnDuplicateStrategyWhenOneUserInFile(): void { $this->currentFile = self::SINGLE_TT_RDF; $form = [ 'uploaded_file' => $this->currentFile ]; $this->switchStrategy(RdfImporter::OPTION_STRATEGY_IMPORT_ON_DUPLICATE); $importReport = $this->subject->import( new core_kernel_classes_Class(TestTakerService::CLASS_URI_SUBJECT), $form, self::ADMIN_USER ); self::assertEquals(false, $importReport->containsError()); $this->grabImportedUris($importReport); self::assertCount(1, $this->importedUris); $importReport = $this->subject->import( new core_kernel_classes_Class(TestTakerService::CLASS_URI_SUBJECT), $form, self::ADMIN_USER ); self::assertEquals(false, $importReport->containsError()); self::assertEquals(true, $importReport->contains(Report::TYPE_WARNING)); self::assertCount(1, $this->getWarningReports($importReport)); $this->grabImportedUris($importReport); self::assertCount(2, $this->importedUris); } public function testItImportsWithImportOnDuplicateStrategyWhenMultipleUserInFile(): void { $this->currentFile = self::MULTI_TT_RDF; $form = [ 'uploaded_file' => $this->currentFile ]; $this->switchStrategy(RdfImporter::OPTION_STRATEGY_IMPORT_ON_DUPLICATE); $importReport = $this->subject->import( new core_kernel_classes_Class(TestTakerService::CLASS_URI_SUBJECT), $form, self::ADMIN_USER ); self::assertEquals(false, $importReport->containsError()); $this->grabImportedUris($importReport); self::assertCount(4, $this->importedUris); $importReport = $this->subject->import( new core_kernel_classes_Class(TestTakerService::CLASS_URI_SUBJECT), $form, self::ADMIN_USER ); self::assertEquals(false, $importReport->containsError()); self::assertEquals(true, $importReport->contains(Report::TYPE_WARNING)); self::assertCount(4, $this->getWarningReports($importReport)); $this->grabImportedUris($importReport); self::assertCount(8, $this->importedUris); } private function switchStrategy(string $strategy = ''): void { $fileMock = $this->createMock(File::class); $fileMock ->method('getBasename') ->willReturn(basename($this->currentFile)); $fileMock ->method('exists') ->willReturn(true); $fileMock ->method('read') ->willReturn(file_get_contents(__DIR__ . '/../samples/' . $this->currentFile)) ; $directoryMock = $this->createMock(Directory::class); $directoryMock ->method('getFile') ->with($this->currentFile) ->willReturn($fileMock); $uploadServiceMock = $this->createMock(UploadService::class); $uploadServiceMock ->method('getUploadDir') ->willReturn($directoryMock); $eventManagerMock = $this->createMock(EventManager::class); $loggerMock = $this->createMock(LoggerService::class); $serviceLocator = $this->getServiceLocatorMock( [ RdfImporter::CONFIG_ID => new ConfigurationService( [ RdfImporter::OPTION_STRATEGY => $strategy ] ), TestTakerImportEventDispatcher::class => new TestTakerImportEventDispatcher(), UploadService::SERVICE_ID => $uploadServiceMock, EventManager::SERVICE_ID => $eventManagerMock, LoggerService::SERVICE_ID => $loggerMock ] ); $this->subject->setServiceLocator($serviceLocator); } private function grabImportedUris(Report $report): void { if (($reportData = $report->getData()) !== null) { $this->importedUris[] = $reportData->getUri(); } else { /** @var Report $childReport */ foreach ($report->getChildren() as $childReport) { if (($childData = $childReport->getData()) !== null) { $this->importedUris[] = $childData->getUri(); } else { $this->grabImportedUris($childReport); } } } } private function getWarningReports(Report $report): array { $iterator = new \RecursiveIteratorIterator( new \common_report_RecursiveReportIterator($report->getChildren()), \RecursiveIteratorIterator::SELF_FIRST ); $warnings = []; foreach ($iterator as $element) { if ($element->getType() == Report::TYPE_WARNING) { $warnings[] = $element; } } return $warnings; } public function tearDown(): void { parent::tearDown(); $this->cleanImportedUsers(); } private function cleanImportedUsers(): void { foreach ($this->importedUris as $resourceUri) { $user = new core_kernel_classes_Resource($resourceUri); $user->delete(); } } }