diff --git a/app/Service/Import/Importers/ClockifyProjectsImporter.php b/app/Service/Import/Importers/ClockifyProjectsImporter.php index 7a697c26..dd3da285 100644 --- a/app/Service/Import/Importers/ClockifyProjectsImporter.php +++ b/app/Service/Import/Importers/ClockifyProjectsImporter.php @@ -54,6 +54,7 @@ class ClockifyProjectsImporter extends DefaultImporter if ($tasksKey !== null && $record[$tasksKey] !== '') { $tasks = explode(', ', $record[$tasksKey]); foreach ($tasks as $task) { + $this->checkTaskNameLength($task); $this->taskImportHelper->getKey([ 'name' => $task, 'project_id' => $projectId, diff --git a/app/Service/Import/Importers/ClockifyTimeEntriesImporter.php b/app/Service/Import/Importers/ClockifyTimeEntriesImporter.php index ef8b1ca9..0cc16847 100644 --- a/app/Service/Import/Importers/ClockifyTimeEntriesImporter.php +++ b/app/Service/Import/Importers/ClockifyTimeEntriesImporter.php @@ -103,6 +103,7 @@ class ClockifyTimeEntriesImporter extends DefaultImporter } $taskId = null; if (! $isBreak && $taskKey !== null && $record[$taskKey] !== '') { + $this->checkTaskNameLength($record[$taskKey]); $taskId = $this->taskImportHelper->getKey([ 'name' => $record[$taskKey], 'project_id' => $projectId, diff --git a/app/Service/Import/Importers/DefaultImporter.php b/app/Service/Import/Importers/DefaultImporter.php index af6c6081..8cda78dd 100644 --- a/app/Service/Import/Importers/DefaultImporter.php +++ b/app/Service/Import/Importers/DefaultImporter.php @@ -21,6 +21,8 @@ use Illuminate\Database\Eloquent\Builder; abstract class DefaultImporter implements ImporterContract { + protected const TASK_NAME_MAX_LENGTH = 500; + protected Organization $organization; /** @@ -181,6 +183,16 @@ abstract class DefaultImporter implements ImporterContract $this->billableRateService = app(BillableRateService::class); } + /** + * @throws ImportException + */ + protected function checkTaskNameLength(string $taskName): void + { + if (strlen($taskName) > self::TASK_NAME_MAX_LENGTH) { + throw new ImportException('Task name ("'.$taskName.'") is too long, maximum length is '.self::TASK_NAME_MAX_LENGTH.' characters'); + } + } + #[\Override] public function getReport(): ReportDto { diff --git a/app/Service/Import/Importers/GenericTimeEntriesImporter.php b/app/Service/Import/Importers/GenericTimeEntriesImporter.php index 6ba1ac83..f5745ac7 100644 --- a/app/Service/Import/Importers/GenericTimeEntriesImporter.php +++ b/app/Service/Import/Importers/GenericTimeEntriesImporter.php @@ -113,6 +113,7 @@ class GenericTimeEntriesImporter extends DefaultImporter } $taskId = null; if ($record['task'] !== '') { + $this->checkTaskNameLength($record['task']); $taskId = $this->taskImportHelper->getKey([ 'name' => $record['task'], 'project_id' => $projectId, diff --git a/app/Service/Import/Importers/HarvestTimeEntriesImporter.php b/app/Service/Import/Importers/HarvestTimeEntriesImporter.php index 1a1aade7..57a66f49 100644 --- a/app/Service/Import/Importers/HarvestTimeEntriesImporter.php +++ b/app/Service/Import/Importers/HarvestTimeEntriesImporter.php @@ -92,6 +92,7 @@ class HarvestTimeEntriesImporter extends DefaultImporter } $taskId = null; if ($record['Task'] !== '') { + $this->checkTaskNameLength($record['Task']); $taskId = $this->taskImportHelper->getKey([ 'name' => $record['Task'], 'project_id' => $projectId, diff --git a/app/Service/Import/Importers/SolidtimeImporter.php b/app/Service/Import/Importers/SolidtimeImporter.php index 677b48bd..f6eade0e 100644 --- a/app/Service/Import/Importers/SolidtimeImporter.php +++ b/app/Service/Import/Importers/SolidtimeImporter.php @@ -199,6 +199,7 @@ class SolidtimeImporter extends DefaultImporter if ($projectId === null) { throw new Exception('Project does not exist'); } + $this->checkTaskNameLength($task['name']); $this->taskImportHelper->getKey([ 'name' => $task['name'], 'project_id' => $projectId, diff --git a/app/Service/Import/Importers/TogglDataImporter.php b/app/Service/Import/Importers/TogglDataImporter.php index ec23dfb7..668d462a 100644 --- a/app/Service/Import/Importers/TogglDataImporter.php +++ b/app/Service/Import/Importers/TogglDataImporter.php @@ -189,6 +189,7 @@ class TogglDataImporter extends DefaultImporter if ($projectId === null) { throw new Exception('Project does not exist'); } + $this->checkTaskNameLength($task->name); $this->taskImportHelper->getKey([ 'name' => $task->name, 'project_id' => $projectId, diff --git a/app/Service/Import/Importers/TogglTimeEntriesImporter.php b/app/Service/Import/Importers/TogglTimeEntriesImporter.php index 95fef716..7b56f89d 100644 --- a/app/Service/Import/Importers/TogglTimeEntriesImporter.php +++ b/app/Service/Import/Importers/TogglTimeEntriesImporter.php @@ -97,6 +97,7 @@ class TogglTimeEntriesImporter extends DefaultImporter } $taskId = null; if ($record['Task'] !== '') { + $this->checkTaskNameLength($record['Task']); $taskId = $this->taskImportHelper->getKey([ 'name' => $record['Task'], 'project_id' => $projectId, diff --git a/tests/Unit/Service/Import/Importers/GenericTimeEntriesImporterTest.php b/tests/Unit/Service/Import/Importers/GenericTimeEntriesImporterTest.php index 65c73eb4..69db995c 100644 --- a/tests/Unit/Service/Import/Importers/GenericTimeEntriesImporterTest.php +++ b/tests/Unit/Service/Import/Importers/GenericTimeEntriesImporterTest.php @@ -66,4 +66,27 @@ class GenericTimeEntriesImporterTest extends ImporterTestAbstract $this->assertSame(0, $report->projectsCreated); $this->assertSame(0, $report->clientsCreated); } + + public function test_import_fails_if_task_name_is_too_long(): void + { + // Arrange + $organization = Organization::factory()->create(); + $timezone = 'Europe/Vienna'; + $importer = new GenericTimeEntriesImporter; + $importer->init($organization); + $taskName = str_repeat('a', 501); + $data = "description,billable,client,project,tags,start,end,task,user_name,user_email\n". + '"Working hard","true","Big Company","Project for Big Company","","2024-03-04T09:23:00Z","2024-03-04T10:23:01Z","'.$taskName.'","Peter Tester","peter.test@email.test"'; + + // Act + try { + $importer->importData($data, $timezone); + } catch (ImportException $e) { + // Assert + $this->assertSame('Task name ("'.$taskName.'") is too long, maximum length is 500 characters', $e->getMessage()); + + return; + } + $this->fail(); + } }