Fixed escaping issues in importer

This commit is contained in:
Constantin Graf
2024-10-15 12:34:55 +02:00
committed by Constantin Graf
parent 4c27f1a2de
commit 85f4a3049c
6 changed files with 60 additions and 0 deletions

View File

@@ -47,6 +47,8 @@ class ClockifyTimeEntriesImporter extends DefaultImporter
$reader = Reader::createFromString($data);
$reader->setHeaderOffset(0);
$reader->setDelimiter(',');
$reader->setEnclosure('"');
$reader->setEscape('');
$header = $reader->getHeader();
$this->validateHeader($header);
$records = $reader->getRecords();

View File

@@ -47,6 +47,8 @@ class TogglTimeEntriesImporter extends DefaultImporter
$reader = Reader::createFromString($data);
$reader->setHeaderOffset(0);
$reader->setDelimiter(',');
$reader->setEnclosure('"');
$reader->setEscape('');
$header = $reader->getHeader();
$this->validateHeader($header);
$records = $reader->getRecords();

View File

@@ -0,0 +1,2 @@
"Project","Client","Description","Task","User","Group","Email","Tags","Type","Billable","Invoiced","Invoice ID","Start Date","Start Time","End Date","End Time","Duration (h)","Duration (decimal)","Billable Rate (EUR)","Billable Amount (EUR)","Date of creation"
"Real World Project","Real World Client","\\ 🔥 Special characters ''''''`!@#$%^&*()_+\-=\[\]{};':''\\|,.''<>\/?~ \\\","A giant task","Peter Tester","Group1, Group2","peter.test@email.test","","Regular","Yes","Yes","Invoice100","10/15/2024","11:00:00 AM","10/15/2024","11:30:00 AM","00:30:00","0.50","1000.00","500.00","10/15/2024"
1 Project Client Description Task User Group Email Tags Type Billable Invoiced Invoice ID Start Date Start Time End Date End Time Duration (h) Duration (decimal) Billable Rate (EUR) Billable Amount (EUR) Date of creation
2 Real World Project Real World Client \\ 🔥 Special characters ''''''`!@#$%^&*()_+\-=\[\]{};':''\\|,.''<>\/?~ \\\ A giant task Peter Tester Group1, Group2 peter.test@email.test Regular Yes Yes Invoice100 10/15/2024 11:00:00 AM 10/15/2024 11:30:00 AM 00:30:00 0.50 1000.00 500.00 10/15/2024

View File

@@ -0,0 +1,2 @@
"User","Email","Client","Project","Task","Description","Billable","Start date","Start time","End date","End time","Duration","Tags"
"Peter Tester","peter.test@email.test","Real World Client","Real World Project","A giant task","\\ 🔥 Special characters """"""`!@#$%^&*()_+\-=\[\]{};':""\\|,.''<>\/?~ \\\","No","2024-10-15","12:02:17","2024-10-15","12:02:19","00:00:02",""
1 User Email Client Project Task Description Billable Start date Start time End date End time Duration Tags
2 Peter Tester peter.test@email.test Real World Client Real World Project A giant task \\ 🔥 Special characters """`!@#$%^&*()_+\-=\[\]{};':"\\|,.''<>\/?~ \\\ No 2024-10-15 12:02:17 2024-10-15 12:02:19 00:00:02

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Tests\Unit\Service\Import\Importers;
use App\Models\Organization;
use App\Models\TimeEntry;
use App\Service\Import\Importers\ClockifyTimeEntriesImporter;
use App\Service\Import\Importers\DefaultImporter;
use App\Service\Import\Importers\ImportException;
@@ -42,6 +43,31 @@ class ClockifyTimeEntriesImporterTest extends ImporterTestAbstract
$this->assertSame(1, $report->clientsCreated);
}
public function test_import_of_test_with_special_characters_description_succeeds(): void
{
// Arrange
$organization = Organization::factory()->create();
$timezone = 'Europe/Vienna';
$importer = new ClockifyTimeEntriesImporter;
$importer->init($organization);
// Description: \\ 🔥 Special characters """`!@#$%^&*()_+\-=\[\]{};':"\\|,.''<>\/?~ \\\
$data = Storage::disk('testfiles')->get('clockify_time_entries_import_test_2.csv');
// Act
$importer->importData($data, $timezone);
$report = $importer->getReport();
// Assert
$timeEntry = TimeEntry::first();
$this->assertSame('\\\\ 🔥 Special characters \'\'\'\'\'\'`!@#$%^&*()_+\-=\[\]{};\':\'\'\\\\|,.\'\'<>\/?~ \\\\\\', $timeEntry->description);
$this->assertSame(1, $report->timeEntriesCreated);
$this->assertSame(0, $report->tagsCreated);
$this->assertSame(1, $report->tasksCreated);
$this->assertSame(1, $report->usersCreated);
$this->assertSame(1, $report->projectsCreated);
$this->assertSame(1, $report->clientsCreated);
}
public function test_import_of_test_file_twice_succeeds(): void
{
// Arrange

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Tests\Unit\Service\Import\Importers;
use App\Models\Organization;
use App\Models\TimeEntry;
use App\Service\Import\Importers\DefaultImporter;
use App\Service\Import\Importers\ImportException;
use App\Service\Import\Importers\TogglTimeEntriesImporter;
@@ -47,6 +48,31 @@ class TogglTimeEntriesImporterTest extends ImporterTestAbstract
$this->assertSame(1, $report->clientsCreated);
}
public function test_import_of_test_with_special_characters_description_succeeds(): void
{
// Arrange
$organization = Organization::factory()->create();
$timezone = 'Europe/Vienna';
$importer = new TogglTimeEntriesImporter;
$importer->init($organization);
// Description: \\ 🔥 Special characters """`!@#$%^&*()_+\-=\[\]{};':"\\|,.''<>\/?~ \\\
$data = Storage::disk('testfiles')->get('toggl_time_entries_import_test_2.csv');
// Act
$importer->importData($data, $timezone);
$report = $importer->getReport();
// Assert
$timeEntry = TimeEntry::first();
$this->assertSame('\\\\ 🔥 Special characters """`!@#$%^&*()_+\-=\[\]{};\':"\\\\|,.\'\'<>\/?~ \\\\\\', $timeEntry->description);
$this->assertSame(1, $report->timeEntriesCreated);
$this->assertSame(0, $report->tagsCreated);
$this->assertSame(1, $report->tasksCreated);
$this->assertSame(1, $report->usersCreated);
$this->assertSame(1, $report->projectsCreated);
$this->assertSame(1, $report->clientsCreated);
}
public function test_import_of_test_file_twice_succeeds(): void
{
// Arrange