Skip to content

Commit

Permalink
Fixed escaping issues in importer
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Oct 15, 2024
1 parent 1b2bcb9 commit 874ee69
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/Service/Import/Importers/ClockifyTimeEntriesImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function importData(string $data, string $timezone): void
$reader = Reader::createFromString($data);
$reader->setHeaderOffset(0);
$reader->setDelimiter(',');
$reader->setEnclosure('"');
$reader->setEscape('');
$header = $reader->getHeader();
$this->validateHeader($header);
$records = $reader->getRecords();
Expand Down
2 changes: 2 additions & 0 deletions app/Service/Import/Importers/TogglTimeEntriesImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function importData(string $data, string $timezone): void
$reader = Reader::createFromString($data);
$reader->setHeaderOffset(0);
$reader->setDelimiter(',');
$reader->setEnclosure('"');
$reader->setEscape('');
$header = $reader->getHeader();
$this->validateHeader($header);
$records = $reader->getRecords();
Expand Down
2 changes: 2 additions & 0 deletions resources/testfiles/clockify_time_entries_import_test_2.csv
Original file line number Diff line number Diff line change
@@ -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","[email protected]","","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"
2 changes: 2 additions & 0 deletions resources/testfiles/toggl_time_entries_import_test_2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"User","Email","Client","Project","Task","Description","Billable","Start date","Start time","End date","End time","Duration","Tags"
"Peter Tester","[email protected]","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",""
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand Down Expand Up @@ -42,6 +43,31 @@ public function test_import_of_test_file_succeeds(): void
$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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand Down Expand Up @@ -47,6 +48,31 @@ public function test_import_of_test_file_succeeds(): void
$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
Expand Down

0 comments on commit 874ee69

Please sign in to comment.