actingAsForApi(User::factory()->create()); $this->importFileResponse(['import' => 44])->assertForbidden(); } #[Test] public function import_location(): void { $importFileBuilder = ImportFileBuilder::new(); $row = $importFileBuilder->firstRow(); $import = Import::factory()->locations()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $this->actingAsForApi(User::factory()->superuser()->create()); $this->importFileResponse(['import' => $import->id, 'send-welcome' => 0]) ->assertOk() ->assertExactJson([ 'payload' => null, 'status' => 'success', 'messages' => ['redirect_url' => route('locations.index')], ]); $newLocation = Location::query() ->where('name', $row['name']) ->sole(); $this->assertEquals($row['name'], $newLocation->name); } #[Test] public function will_ignore_unknown_columns_when_file_contains_unknown_columns(): void { $row = ImportFileBuilder::new()->definition(); $row['unknownColumnInCsvFile'] = 'foo'; $importFileBuilder = new ImportFileBuilder([$row]); $this->actingAsForApi(User::factory()->superuser()->create()); $import = Import::factory()->locations()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $this->importFileResponse(['import' => $import->id])->assertOk(); } #[Test] public function when_required_columns_are_missing_in_import_file(): void { $importFileBuilder = ImportFileBuilder::new(['name' => '']); $import = Import::factory()->locations()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $this->actingAsForApi(User::factory()->superuser()->create()); $this->importFileResponse(['import' => $import->id]) ->assertInternalServerError() ->assertExactJson([ 'status' => 'import-errors', 'payload' => null, 'messages' => [ '' => [ 'Location ""' => [ 'name' => ['The name field is required.'], ], ], ], ]); $newLocation = Location::query() ->where('name', $importFileBuilder->firstRow()['name']) ->get(); $this->assertCount(0, $newLocation); } #[Test] public function update_location_from_import(): void { $location = Location::factory()->create()->refresh(); $importFileBuilder = ImportFileBuilder::new(['name' => $location->name, 'phone' => $location->phone]); $row = $importFileBuilder->firstRow(); $import = Import::factory()->locations()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]); $this->actingAsForApi(User::factory()->superuser()->create()); $this->importFileResponse(['import' => $import->id, 'import-update' => true])->assertOk(); $updatedLocation = Location::query()->find($location->id); $updatedAttributes = [ 'name', 'phone', ]; $this->assertEquals($row['name'], $updatedLocation->name); $this->assertEquals( Arr::except($location->attributesToArray(), array_merge($updatedAttributes, $location->getDates())), Arr::except($updatedLocation->attributesToArray(), array_merge($updatedAttributes, $location->getDates())), ); } #[Test] public function update_mode_logs_location_update_in_actionlog(): void { $this->actingAsForApi(User::factory()->superuser()->create()); $initialFile = ImportFileBuilder::new(); $initialRow = $initialFile->firstRow(); $initialImport = Import::factory()->locations()->create([ 'file_path' => $initialFile->saveToImportsDirectory(), ]); $this->importFileResponse(['import' => $initialImport->id])->assertOk(); $location = Location::query()->where('name', $initialRow['name'])->sole(); $updatedRow = array_merge($initialRow, [ 'notes' => 'Importer update notes', ]); $updateFile = new ImportFileBuilder([$updatedRow]); $updateImport = Import::factory()->locations()->create([ 'file_path' => $updateFile->saveToImportsDirectory(), ]); $this->importFileResponse([ 'import' => $updateImport->id, 'import-update' => true, ])->assertOk(); $location->refresh(); $this->assertEquals($updatedRow['notes'], $location->notes); $updateLog = ActionLog::query() ->where('item_type', Location::class) ->where('item_id', $location->id) ->where('action_type', 'update') ->latest('id') ->first(); $this->assertNotNull($updateLog, 'Expected an update action log entry after location importer update mode.'); } }