diff --git a/app/Support/Import/Configuration/File/ConfigureUploadHandler.php b/app/Support/Import/Configuration/File/ConfigureUploadHandler.php index 553423aa07..640f39833a 100644 --- a/app/Support/Import/Configuration/File/ConfigureUploadHandler.php +++ b/app/Support/Import/Configuration/File/ConfigureUploadHandler.php @@ -66,14 +66,13 @@ class ConfigureUploadHandler implements ConfigurationInterface // collect specifics. foreach (config('csv.import_specifics') as $name => $className) { $specifics[$name] = [ - 'name' => $className::getName(), - 'description' => $className::getDescription(), + 'name' => trans($className::getName()), + 'description' => trans($className::getDescription()), ]; } $data = [ 'accounts' => [], - 'specifix' => [], 'delimiters' => $delimiters, 'specifics' => $specifics, ]; diff --git a/tests/Unit/Support/Import/Configuration/File/ConfigureUploadHandlerTest.php b/tests/Unit/Support/Import/Configuration/File/ConfigureUploadHandlerTest.php new file mode 100644 index 0000000000..baab7a6b62 --- /dev/null +++ b/tests/Unit/Support/Import/Configuration/File/ConfigureUploadHandlerTest.php @@ -0,0 +1,185 @@ +. + */ + +declare(strict_types=1); + +namespace Tests\Unit\Support\Import\Configuration\File; + + +use FireflyIII\Models\ImportJob; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; +use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface; +use FireflyIII\Support\Import\Configuration\File\ConfigureUploadHandler; +use Mockery; +use Tests\TestCase; + +/** + * Class ConfigureUploadHandlerTest + */ +class ConfigureUploadHandlerTest extends TestCase +{ + /** + * @covers \FireflyIII\Support\Import\Configuration\File\ConfigureUploadHandler + */ + public function testConfigureJobAccount(): void + { + $job = new ImportJob; + $job->user_id = $this->user()->id; + $job->key = 'upload-B' . random_int(1, 1000); + $job->status = 'new'; + $job->stage = 'new'; + $job->provider = 'fake'; + $job->file_type = ''; + $job->configuration = []; + $job->save(); + + + $data = [ + 'csv_import_account' => '1', + 'csv_delimiter' => ',', + 'has_headers' => '1', + 'date_format' => 'Y-m-d', + 'apply_rules' => '1', + 'specifics' => ['IngDescription'], + ]; + $expectedConfig = [ + 'has-headers' => true, + 'date-format' => 'Y-m-d', + 'delimiter' => ',', + 'apply-rules' => true, + 'specifics' => [ + 'IngDescription' => 1, + ], + 'import-account' => 1, + ]; + + $repository = $this->mock(ImportJobRepositoryInterface::class); + $accountRepos = $this->mock(AccountRepositoryInterface::class); + $repository->shouldReceive('setUser')->once(); + $accountRepos->shouldReceive('setUser')->once(); + $accountRepos->shouldReceive('findNull')->once()->withArgs([1])->andReturn($this->user()->accounts()->first()); + $repository->shouldReceive('setConfiguration')->once()->withArgs([Mockery::any(), $expectedConfig]); + $repository->shouldReceive('setStage')->once()->withArgs([Mockery::any(), 'roles']); + + $handler = new ConfigureUploadHandler; + $handler->setJob($job); + $result = $handler->configureJob($data); + $this->assertCount(0, $result); + } + + /** + * @covers \FireflyIII\Support\Import\Configuration\File\ConfigureUploadHandler + */ + public function testConfigureJobNoAccount(): void + { + $job = new ImportJob; + $job->user_id = $this->user()->id; + $job->key = 'upload-B' . random_int(1, 1000); + $job->status = 'new'; + $job->stage = 'new'; + $job->provider = 'fake'; + $job->file_type = ''; + $job->configuration = []; + $job->save(); + + + $data = [ + 'csv_import_account' => '1', + 'csv_delimiter' => ',', + 'has_headers' => '1', + 'date_format' => 'Y-m-d', + 'apply_rules' => '1', + 'specifics' => ['IngDescription'], + ]; + $expectedConfig = [ + 'has-headers' => true, + 'date-format' => 'Y-m-d', + 'delimiter' => ',', + 'apply-rules' => true, + 'specifics' => [ + 'IngDescription' => 1, + ], + ]; + + $repository = $this->mock(ImportJobRepositoryInterface::class); + $accountRepos = $this->mock(AccountRepositoryInterface::class); + $repository->shouldReceive('setUser')->once(); + $accountRepos->shouldReceive('setUser')->once(); + $accountRepos->shouldReceive('findNull')->once()->withArgs([1])->andReturn(null); + $repository->shouldReceive('setConfiguration')->once()->withArgs([Mockery::any(), $expectedConfig]); + + $handler = new ConfigureUploadHandler; + $handler->setJob($job); + $result = $handler->configureJob($data); + $this->assertCount(1, $result); + $this->assertEquals('You have selected an invalid account to import into.', $result->get('account')[0]); + } + + /** + * @covers \FireflyIII\Support\Import\Configuration\File\ConfigureUploadHandler + */ + public function testGetNextData(): void + { + $job = new ImportJob; + $job->user_id = $this->user()->id; + $job->key = 'upload-A' . random_int(1, 1000); + $job->status = 'new'; + $job->stage = 'new'; + $job->provider = 'fake'; + $job->file_type = ''; + $job->configuration = []; + $job->save(); + + $repository = $this->mock(ImportJobRepositoryInterface::class); + $repository->shouldReceive('setUser'); + $repository->shouldReceive('setConfiguration')->once()->withArgs([Mockery::any(), ['date-format' => 'Ymd']]); + + $handler = new ConfigureUploadHandler; + $handler->setJob($job); + $result = $handler->getNextData(); + $expected = [ + 'accounts' => [], + 'delimiters' => [], + ]; + // not much to compare, really. + $this->assertEquals($expected['accounts'], $result['accounts']); + } + + /** + * @covers \FireflyIII\Support\Import\Configuration\File\ConfigureUploadHandler + */ + public function testGetSpecifics(): void + { + $array = [ + 'specifics' => [ + 'IngDescription', 'BadFakeNewsThing', + ], + ]; + $expected = [ + 'IngDescription' => 1, + ]; + + $handler = new ConfigureUploadHandler; + $result = $handler->getSpecifics($array); + $this->assertEquals($expected, $result); + } + +} \ No newline at end of file