mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-12-06 17:52:16 +00:00
Re-importing the same data duplicated all time entries, because only clients, projects, tasks, tags and users were matched with existing records. Time entries are now matched by member, start, end, project, task and description. Matches are counted, so identical time entries within one import are still kept. Fixes #894
257 lines
10 KiB
PHP
257 lines
10 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Service\Import\Importers;
|
|
|
|
use App\Models\Member;
|
|
use App\Models\Organization;
|
|
use App\Models\TimeEntry;
|
|
use App\Models\User;
|
|
use App\Service\Import\Importers\DefaultImporter;
|
|
use App\Service\Import\Importers\GenericTimeEntriesImporter;
|
|
use App\Service\Import\Importers\ImportException;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
#[CoversClass(GenericTimeEntriesImporter::class)]
|
|
#[CoversClass(ImportException::class)]
|
|
#[CoversClass(DefaultImporter::class)]
|
|
class GenericTimeEntriesImporterTest extends ImporterTestAbstract
|
|
{
|
|
public function test_import_of_test_file_succeeds(): void
|
|
{
|
|
// Arrange
|
|
$organization = Organization::factory()->create();
|
|
$timezone = 'Europe/Vienna';
|
|
$importer = new GenericTimeEntriesImporter;
|
|
$importer->init($organization);
|
|
$data = Storage::disk('testfiles')->get('generic_time_entries_import_test_1.csv');
|
|
|
|
// Act
|
|
$importer->importData($data, $timezone);
|
|
$report = $importer->getReport();
|
|
|
|
// Assert
|
|
$testScenario = $this->checkTestScenarioAfterImportExcludingTimeEntries();
|
|
$this->checkTimeEntries($testScenario);
|
|
$this->assertSame(2, $report->timeEntriesCreated);
|
|
$this->assertSame(2, $report->tagsCreated);
|
|
$this->assertSame(1, $report->tasksCreated);
|
|
$this->assertSame(1, $report->usersCreated);
|
|
$this->assertSame(2, $report->projectsCreated);
|
|
$this->assertSame(1, $report->clientsCreated);
|
|
}
|
|
|
|
public function test_import_of_test_file_twice_skips_existing_time_entries(): void
|
|
{
|
|
// Arrange
|
|
$organization = Organization::factory()->create();
|
|
$timezone = 'Europe/Vienna';
|
|
$importer = new GenericTimeEntriesImporter;
|
|
$importer->init($organization);
|
|
$data = Storage::disk('testfiles')->get('generic_time_entries_import_test_1.csv');
|
|
$importer->importData($data, $timezone);
|
|
$importer = new GenericTimeEntriesImporter;
|
|
$importer->init($organization);
|
|
|
|
// Act
|
|
$importer->importData($data, $timezone);
|
|
$report = $importer->getReport();
|
|
|
|
// Assert
|
|
$testScenario = $this->checkTestScenarioAfterImportExcludingTimeEntries();
|
|
$this->checkTimeEntries($testScenario);
|
|
$this->assertSame(0, $report->timeEntriesCreated);
|
|
$this->assertSame(2, $report->timeEntriesSkipped);
|
|
$this->assertSame(0, $report->tagsCreated);
|
|
$this->assertSame(0, $report->tasksCreated);
|
|
$this->assertSame(0, $report->usersCreated);
|
|
$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();
|
|
}
|
|
|
|
public static function invalidUserEmailProvider(): array
|
|
{
|
|
return [
|
|
'empty' => ['', ''],
|
|
'whitespace' => [' ', ' '],
|
|
'surrounding whitespace' => [' ivanna@example.com ', ' ivanna@example.com '],
|
|
'invalid' => ['not-an-email', 'not-an-email'],
|
|
];
|
|
}
|
|
|
|
public function test_import_accepts_unicode_user_email(): void
|
|
{
|
|
// Arrange
|
|
$organization = Organization::factory()->create();
|
|
$timezone = 'Europe/Vienna';
|
|
$importer = new GenericTimeEntriesImporter;
|
|
$importer->init($organization);
|
|
$data = "description,billable,client,project,tags,start,end,task,user_name,user_email\n".
|
|
'"Working hard","true","","","","2024-03-04T09:23:00Z","2024-03-04T10:23:00Z","","Jörg","jörg@example.com"';
|
|
|
|
// Act
|
|
$importer->importData($data, $timezone);
|
|
|
|
// Assert
|
|
$this->assertSame(1, User::query()->where('email', '=', 'jörg@example.com')->count());
|
|
$this->assertSame(1, TimeEntry::query()->count());
|
|
}
|
|
|
|
#[DataProvider('invalidUserEmailProvider')]
|
|
public function test_import_fails_if_user_email_is_invalid(string $userEmail, string $expectedValue): void
|
|
{
|
|
// Arrange
|
|
$organization = Organization::factory()->create();
|
|
$timezone = 'Europe/Vienna';
|
|
$importer = new GenericTimeEntriesImporter;
|
|
$importer->init($organization);
|
|
$data = "description,billable,client,project,tags,start,end,task,user_name,user_email\n".
|
|
'"Working hard","true","","","","2024-03-04T09:23:00Z","2024-03-04T10:23:00Z","","Ivanna","'.$userEmail.'"';
|
|
|
|
// Act
|
|
try {
|
|
$importer->importData($data, $timezone);
|
|
} catch (ImportException $e) {
|
|
// Assert
|
|
$this->assertSame('Value of user_email ("'.$expectedValue.'") is not a valid email address', $e->getMessage());
|
|
$this->assertSame(0, User::query()->where('name', '=', 'Ivanna')->count());
|
|
|
|
return;
|
|
}
|
|
$this->fail();
|
|
}
|
|
|
|
public function test_import_fails_if_user_email_is_missing_in_row(): void
|
|
{
|
|
// Arrange
|
|
$organization = Organization::factory()->create();
|
|
$timezone = 'Europe/Vienna';
|
|
$importer = new GenericTimeEntriesImporter;
|
|
$importer->init($organization);
|
|
$data = "description,billable,client,project,tags,start,end,task,user_name,user_email\n".
|
|
'"Working hard","true","","","","2024-03-04T09:23:00Z","2024-03-04T10:23:00Z","","Ivanna"';
|
|
|
|
// Act
|
|
try {
|
|
$importer->importData($data, $timezone);
|
|
} catch (ImportException $e) {
|
|
// Assert
|
|
$this->assertSame('Value of user_email ("") is not a valid email address', $e->getMessage());
|
|
|
|
return;
|
|
}
|
|
$this->fail();
|
|
}
|
|
|
|
public function test_import_fails_if_end_is_before_start(): void
|
|
{
|
|
// Arrange
|
|
$organization = Organization::factory()->create();
|
|
$timezone = 'Europe/Vienna';
|
|
$importer = new GenericTimeEntriesImporter;
|
|
$importer->init($organization);
|
|
$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-04T10:23:00Z","2024-03-04T09:23:00Z","","Peter Tester","peter.test@email.test"';
|
|
|
|
// Act
|
|
try {
|
|
$importer->importData($data, $timezone);
|
|
} catch (ImportException $e) {
|
|
// Assert
|
|
$this->assertSame('Value of end ("2024-03-04T09:23:00Z") is before start ("2024-03-04T10:23:00Z")', $e->getMessage());
|
|
$this->assertSame(0, TimeEntry::query()->count());
|
|
|
|
return;
|
|
}
|
|
$this->fail();
|
|
}
|
|
|
|
public function test_import_does_not_set_client_on_time_entry_without_project(): void
|
|
{
|
|
// Arrange
|
|
$organization = Organization::factory()->create();
|
|
$timezone = 'Europe/Vienna';
|
|
$importer = new GenericTimeEntriesImporter;
|
|
$importer->init($organization);
|
|
$data = "description,billable,client,project,tags,start,end,task,user_name,user_email\n".
|
|
'"Working hard","false","Big Company","","","2024-03-04T09:23:00Z","2024-03-04T10:23:00Z","","Peter Tester","peter.test@email.test"';
|
|
|
|
// Act
|
|
$importer->importData($data, $timezone);
|
|
|
|
// Assert
|
|
$timeEntry = TimeEntry::query()->sole();
|
|
$this->assertNull($timeEntry->project_id);
|
|
$this->assertNull($timeEntry->client_id);
|
|
}
|
|
|
|
public function test_import_drops_billable_flags_and_rates_if_organization_can_not_use_billable_rates(): void
|
|
{
|
|
// Arrange
|
|
$this->actAsOrganizationWithoutBillableRates();
|
|
$organization = Organization::factory()->create();
|
|
$timezone = 'Europe/Vienna';
|
|
$importer = new GenericTimeEntriesImporter;
|
|
$importer->init($organization);
|
|
$data = Storage::disk('testfiles')->get('generic_time_entries_import_test_1.csv');
|
|
|
|
// Act
|
|
$importer->importData($data, $timezone);
|
|
|
|
// Assert
|
|
$timeEntries = TimeEntry::query()->whereBelongsTo($organization, 'organization')->get();
|
|
$this->assertCount(2, $timeEntries);
|
|
foreach ($timeEntries as $timeEntry) {
|
|
$this->assertFalse($timeEntry->billable);
|
|
$this->assertNull($timeEntry->billable_rate);
|
|
}
|
|
}
|
|
|
|
public function test_import_lowercases_user_email_and_matches_existing_user(): void
|
|
{
|
|
// Arrange
|
|
$organization = Organization::factory()->create();
|
|
$user = User::factory()->create(['email' => 'ivanna@example.com']);
|
|
Member::factory()->forOrganization($organization)->forUser($user)->create();
|
|
$timezone = 'Europe/Vienna';
|
|
$importer = new GenericTimeEntriesImporter;
|
|
$importer->init($organization);
|
|
$data = "description,billable,client,project,tags,start,end,task,user_name,user_email\n".
|
|
'"Working hard","true","","","","2024-03-04T09:23:00Z","2024-03-04T10:23:00Z","","Ivanna","Ivanna@Example.com"';
|
|
|
|
// Act
|
|
$importer->importData($data, $timezone);
|
|
|
|
// Assert
|
|
$this->assertSame(1, User::query()->whereRaw('lower(email) = ?', ['ivanna@example.com'])->count());
|
|
$this->assertSame(0, User::query()->where('email', '=', 'Ivanna@Example.com')->count());
|
|
$this->assertSame(1, TimeEntry::query()->where('user_id', '=', $user->getKey())->count());
|
|
}
|
|
}
|