mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-10-15 17:23:16 +01:00
Add validation for task name length
This commit is contained in:
@@ -54,6 +54,7 @@ class ClockifyProjectsImporter extends DefaultImporter
|
|||||||
if ($tasksKey !== null && $record[$tasksKey] !== '') {
|
if ($tasksKey !== null && $record[$tasksKey] !== '') {
|
||||||
$tasks = explode(', ', $record[$tasksKey]);
|
$tasks = explode(', ', $record[$tasksKey]);
|
||||||
foreach ($tasks as $task) {
|
foreach ($tasks as $task) {
|
||||||
|
$this->checkTaskNameLength($task);
|
||||||
$this->taskImportHelper->getKey([
|
$this->taskImportHelper->getKey([
|
||||||
'name' => $task,
|
'name' => $task,
|
||||||
'project_id' => $projectId,
|
'project_id' => $projectId,
|
||||||
|
|||||||
@@ -103,6 +103,7 @@ class ClockifyTimeEntriesImporter extends DefaultImporter
|
|||||||
}
|
}
|
||||||
$taskId = null;
|
$taskId = null;
|
||||||
if (! $isBreak && $taskKey !== null && $record[$taskKey] !== '') {
|
if (! $isBreak && $taskKey !== null && $record[$taskKey] !== '') {
|
||||||
|
$this->checkTaskNameLength($record[$taskKey]);
|
||||||
$taskId = $this->taskImportHelper->getKey([
|
$taskId = $this->taskImportHelper->getKey([
|
||||||
'name' => $record[$taskKey],
|
'name' => $record[$taskKey],
|
||||||
'project_id' => $projectId,
|
'project_id' => $projectId,
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ use Illuminate\Database\Eloquent\Builder;
|
|||||||
|
|
||||||
abstract class DefaultImporter implements ImporterContract
|
abstract class DefaultImporter implements ImporterContract
|
||||||
{
|
{
|
||||||
|
protected const TASK_NAME_MAX_LENGTH = 500;
|
||||||
|
|
||||||
protected Organization $organization;
|
protected Organization $organization;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -181,6 +183,16 @@ abstract class DefaultImporter implements ImporterContract
|
|||||||
$this->billableRateService = app(BillableRateService::class);
|
$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]
|
#[\Override]
|
||||||
public function getReport(): ReportDto
|
public function getReport(): ReportDto
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -113,6 +113,7 @@ class GenericTimeEntriesImporter extends DefaultImporter
|
|||||||
}
|
}
|
||||||
$taskId = null;
|
$taskId = null;
|
||||||
if ($record['task'] !== '') {
|
if ($record['task'] !== '') {
|
||||||
|
$this->checkTaskNameLength($record['task']);
|
||||||
$taskId = $this->taskImportHelper->getKey([
|
$taskId = $this->taskImportHelper->getKey([
|
||||||
'name' => $record['task'],
|
'name' => $record['task'],
|
||||||
'project_id' => $projectId,
|
'project_id' => $projectId,
|
||||||
|
|||||||
@@ -92,6 +92,7 @@ class HarvestTimeEntriesImporter extends DefaultImporter
|
|||||||
}
|
}
|
||||||
$taskId = null;
|
$taskId = null;
|
||||||
if ($record['Task'] !== '') {
|
if ($record['Task'] !== '') {
|
||||||
|
$this->checkTaskNameLength($record['Task']);
|
||||||
$taskId = $this->taskImportHelper->getKey([
|
$taskId = $this->taskImportHelper->getKey([
|
||||||
'name' => $record['Task'],
|
'name' => $record['Task'],
|
||||||
'project_id' => $projectId,
|
'project_id' => $projectId,
|
||||||
|
|||||||
@@ -199,6 +199,7 @@ class SolidtimeImporter extends DefaultImporter
|
|||||||
if ($projectId === null) {
|
if ($projectId === null) {
|
||||||
throw new Exception('Project does not exist');
|
throw new Exception('Project does not exist');
|
||||||
}
|
}
|
||||||
|
$this->checkTaskNameLength($task['name']);
|
||||||
$this->taskImportHelper->getKey([
|
$this->taskImportHelper->getKey([
|
||||||
'name' => $task['name'],
|
'name' => $task['name'],
|
||||||
'project_id' => $projectId,
|
'project_id' => $projectId,
|
||||||
|
|||||||
@@ -189,6 +189,7 @@ class TogglDataImporter extends DefaultImporter
|
|||||||
if ($projectId === null) {
|
if ($projectId === null) {
|
||||||
throw new Exception('Project does not exist');
|
throw new Exception('Project does not exist');
|
||||||
}
|
}
|
||||||
|
$this->checkTaskNameLength($task->name);
|
||||||
$this->taskImportHelper->getKey([
|
$this->taskImportHelper->getKey([
|
||||||
'name' => $task->name,
|
'name' => $task->name,
|
||||||
'project_id' => $projectId,
|
'project_id' => $projectId,
|
||||||
|
|||||||
@@ -97,6 +97,7 @@ class TogglTimeEntriesImporter extends DefaultImporter
|
|||||||
}
|
}
|
||||||
$taskId = null;
|
$taskId = null;
|
||||||
if ($record['Task'] !== '') {
|
if ($record['Task'] !== '') {
|
||||||
|
$this->checkTaskNameLength($record['Task']);
|
||||||
$taskId = $this->taskImportHelper->getKey([
|
$taskId = $this->taskImportHelper->getKey([
|
||||||
'name' => $record['Task'],
|
'name' => $record['Task'],
|
||||||
'project_id' => $projectId,
|
'project_id' => $projectId,
|
||||||
|
|||||||
@@ -66,4 +66,27 @@ class GenericTimeEntriesImporterTest extends ImporterTestAbstract
|
|||||||
$this->assertSame(0, $report->projectsCreated);
|
$this->assertSame(0, $report->projectsCreated);
|
||||||
$this->assertSame(0, $report->clientsCreated);
|
$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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user