diff --git a/app/Models/TransactionJournalMeta.php b/app/Models/TransactionJournalMeta.php index e40624dda3..bef13e7fa8 100644 --- a/app/Models/TransactionJournalMeta.php +++ b/app/Models/TransactionJournalMeta.php @@ -30,6 +30,7 @@ use FireflyIII\Models\TransactionJournal; /** * Class TransactionJournalMeta. * @property string $name + * @property int $transaction_journal_id */ class TransactionJournalMeta extends Model { diff --git a/tests/Feature/Controllers/TagControllerTest.php b/tests/Feature/Controllers/TagControllerTest.php index f5e53efe6b..b8aef54bc9 100644 --- a/tests/Feature/Controllers/TagControllerTest.php +++ b/tests/Feature/Controllers/TagControllerTest.php @@ -156,10 +156,10 @@ class TagControllerTest extends TestCase $collector = $this->mock(JournalCollectorInterface::class); $journalRepos = $this->mock(JournalRepositoryInterface::class); $journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal); - $repository->shouldReceive('spentInPeriod')->andReturn('-1')->times(2); + $repository->shouldReceive('spentInPeriod')->andReturn('-1')->times(3); $repository->shouldReceive('firstUseDate')->andReturn(new Carbon)->once(); $repository->shouldReceive('lastUseDate')->andReturn(new Carbon)->once(); - $repository->shouldReceive('earnedInPeriod')->andReturn('1')->times(2); + $repository->shouldReceive('earnedInPeriod')->andReturn('1')->times(3); $repository->shouldReceive('sumsOfTag')->andReturn($amounts)->once(); $collector->shouldReceive('removeFilter')->andReturnSelf()->once(); @@ -225,10 +225,10 @@ class TagControllerTest extends TestCase $collector = $this->mock(JournalCollectorInterface::class); $journalRepos = $this->mock(JournalRepositoryInterface::class); $journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal); - $repository->shouldReceive('spentInPeriod')->andReturn('-1')->times(2); + $repository->shouldReceive('spentInPeriod')->andReturn('-1')->times(3); $repository->shouldReceive('firstUseDate')->andReturn(new Carbon)->once(); $repository->shouldReceive('lastUseDate')->andReturn(new Carbon)->once(); - $repository->shouldReceive('earnedInPeriod')->andReturn('1')->times(2); + $repository->shouldReceive('earnedInPeriod')->andReturn('1')->times(3); $collector->shouldReceive('removeFilter')->andReturnSelf()->once(); $collector->shouldReceive('setAllAssetAccounts')->andReturnSelf()->once(); diff --git a/tests/Unit/Import/Storage/ImportArrayStorageTest.php b/tests/Unit/Import/Storage/ImportArrayStorageTest.php index 791750df6b..21b646e342 100644 --- a/tests/Unit/Import/Storage/ImportArrayStorageTest.php +++ b/tests/Unit/Import/Storage/ImportArrayStorageTest.php @@ -79,6 +79,87 @@ class ImportArrayStorageTest extends TestCase $storage->setImportJob($job); } + /** + * @covers \FireflyIII\Import\Storage\ImportArrayStorage + */ + public function testBasicStoreDoubleTransferWithRules(): void + { + // get a transfer: + /** @var TransactionJournal $transfer */ + $transfer = $this->user()->transactionJournals() + ->inRandomOrder()->where('transaction_type_id', 3) + ->first(); + + // get transfer as a collection, so the compare routine works. + $journalCollector = new JournalCollector(); + $journalCollector->setUser($this->user()); + $journalCollector->setJournals(new Collection([$transfer])); + $transferCollection = $journalCollector->withOpposingAccount()->getJournals(); + + // make fake job + $job = new ImportJob; + $job->user()->associate($this->user()); + $job->key = 'h_storage' . random_int(1, 1000); + $job->status = 'new'; + $job->stage = 'new'; + $job->provider = 'fake'; + $job->file_type = ''; + $job->configuration = ['apply-rules' => true]; + $job->transactions = [$this->singleTransfer(), $this->singleWithdrawal(), $this->basedOnTransfer($transfer)]; + $job->save(); + + + // get some stuff: + $tag = $this->user()->tags()->inRandomOrder()->first(); + $journal = $this->user()->transactionJournals()->inRandomOrder()->first(); + $ruleOne = new Rule; + $ruleOne->stop_processing = false; + $ruleTwo = new Rule; + $ruleTwo->stop_processing = true; + + // mock stuff + $repository = $this->mock(ImportJobRepositoryInterface::class); + $collector = $this->mock(JournalCollectorInterface::class); + $tagRepos = $this->mock(TagRepositoryInterface::class); + $ruleRepos = $this->mock(RuleRepositoryInterface::class); + $journalRepos = $this->mock(JournalRepositoryInterface::class); + + // mock calls: + $collector->shouldReceive('setUser')->once(); + $repository->shouldReceive('setUser')->once(); + $repository->shouldReceive('setStatus')->withAnyArgs(); + $ruleRepos->shouldReceive('setUser')->once(); + $tagRepos->shouldReceive('setUser')->once(); + $tagRepos->shouldReceive('store')->once()->andReturn($tag); + $repository->shouldReceive('setTag')->once(); + $ruleRepos->shouldReceive('getForImport')->andReturn(new Collection([$ruleOne, $ruleTwo])); + $journalRepos->shouldReceive('setUser')->once(); + $journalRepos->shouldReceive('store')->twice()->andReturn($journal); + $journalRepos->shouldReceive('findByHash')->andReturn(null)->times(5); + $repository->shouldReceive('addErrorMessage')->withArgs( + [Mockery::any(), 'Row #2 ("' . $transfer->description . '") could not be imported. Such a transfer already exists.'] + )->once(); + + + // mock collector so it will return some transfers: + $collector->shouldReceive('setAllAssetAccounts')->once()->andReturnSelf(); + $collector->shouldReceive('setTypes')->withArgs([[TransactionType::TRANSFER]])->once()->andReturnSelf(); + $collector->shouldReceive('withOpposingAccount')->once()->andReturnSelf(); + $collector->shouldReceive('ignoreCache')->once()->andReturnSelf(); + $collector->shouldReceive('removeFilter')->withArgs([InternalTransferFilter::class])->once()->andReturnSelf(); + $collector->shouldReceive('getJournals')->andReturn($transferCollection); + + $storage = new ImportArrayStorage; + $storage->setImportJob($job); + $result = new Collection; + try { + $result = $storage->store(); + } catch (FireflyException $e) { + $this->assertTrue(false, $e->getMessage()); + } + $this->assertCount(2, $result); + } + /** * Two withdrawals, one of which is duplicated. * @@ -126,7 +207,7 @@ class ImportArrayStorageTest extends TestCase $ruleRepos->shouldReceive('getForImport')->andReturn(new Collection([$ruleOne, $ruleTwo])); $journalRepos->shouldReceive('setUser')->once(); $journalRepos->shouldReceive('store')->once()->andReturn($journal); - $journalRepos->shouldReceive('findByHash')->andReturn(null, $meta)->twice(); + $journalRepos->shouldReceive('findByHash')->andReturn(null, $meta, null)->times(3); $repository->shouldReceive('addErrorMessage')->once() ->withArgs([Mockery::any(), 'Row #1 ("' . $transactions[1]['description'] . '") could not be imported. It already exists.']); @@ -262,7 +343,7 @@ class ImportArrayStorageTest extends TestCase $ruleRepos->shouldReceive('getForImport')->andReturn(new Collection); $journalRepos->shouldReceive('setUser')->once(); $journalRepos->shouldReceive('store')->once()->andReturn($journal); - $journalRepos->shouldReceive('findByHash')->andReturn(null)->once(); + $journalRepos->shouldReceive('findByHash')->andReturn(null)->times(2); $storage = new ImportArrayStorage; $storage->setImportJob($job); @@ -317,7 +398,7 @@ class ImportArrayStorageTest extends TestCase $ruleRepos->shouldReceive('getForImport')->andReturn(new Collection([$ruleOne, $ruleTwo])); $journalRepos->shouldReceive('setUser')->once(); $journalRepos->shouldReceive('store')->once()->andReturn($journal); - $journalRepos->shouldReceive('findByHash')->andReturn(null)->once(); + $journalRepos->shouldReceive('findByHash')->andReturn(null)->times(2); $storage = new ImportArrayStorage; $storage->setImportJob($job); @@ -384,91 +465,12 @@ class ImportArrayStorageTest extends TestCase $ruleRepos->shouldReceive('getForImport')->andReturn(new Collection([$ruleOne, $ruleTwo])); $journalRepos->shouldReceive('setUser')->once(); $journalRepos->shouldReceive('store')->twice()->andReturn($journal); - $journalRepos->shouldReceive('findByHash')->andReturn(null)->twice(); - - // mock collector so it will return some transfers: - $collector->shouldReceive('setAllAssetAccounts')->once()->andReturnSelf(); - $collector->shouldReceive('setTypes')->withArgs([[TransactionType::TRANSFER]])->once()->andReturnSelf(); - $collector->shouldReceive('withOpposingAccount')->once()->andReturnSelf(); - $collector->shouldReceive('removeFilter')->withArgs([InternalTransferFilter::class])->once()->andReturnSelf(); - $collector->shouldReceive('getJournals')->andReturn($transferCollection); - - $storage = new ImportArrayStorage; - $storage->setImportJob($job); - $result = new Collection; - try { - $result = $storage->store(); - } catch (FireflyException $e) { - $this->assertTrue(false, $e->getMessage()); - } - $this->assertCount(2, $result); - } - - /** - * @covers \FireflyIII\Import\Storage\ImportArrayStorage - */ - public function testBasicStoreDoubleTransferWithRules(): void - { - // get a transfer: - /** @var TransactionJournal $transfer */ - $transfer = $this->user()->transactionJournals() - ->inRandomOrder()->where('transaction_type_id', 3) - ->first(); - - // get transfer as a collection, so the compare routine works. - $journalCollector = new JournalCollector(); - $journalCollector->setUser($this->user()); - $journalCollector->setJournals(new Collection([$transfer])); - $transferCollection = $journalCollector->withOpposingAccount()->getJournals(); - - // make fake job - $job = new ImportJob; - $job->user()->associate($this->user()); - $job->key = 'h_storage' . random_int(1, 1000); - $job->status = 'new'; - $job->stage = 'new'; - $job->provider = 'fake'; - $job->file_type = ''; - $job->configuration = ['apply-rules' => true]; - $job->transactions = [$this->singleTransfer(), $this->singleWithdrawal(), $this->basedOnTransfer($transfer)]; - $job->save(); - - - // get some stuff: - $tag = $this->user()->tags()->inRandomOrder()->first(); - $journal = $this->user()->transactionJournals()->inRandomOrder()->first(); - $ruleOne = new Rule; - $ruleOne->stop_processing = false; - $ruleTwo = new Rule; - $ruleTwo->stop_processing = true; - - // mock stuff - $repository = $this->mock(ImportJobRepositoryInterface::class); - $collector = $this->mock(JournalCollectorInterface::class); - $tagRepos = $this->mock(TagRepositoryInterface::class); - $ruleRepos = $this->mock(RuleRepositoryInterface::class); - $journalRepos = $this->mock(JournalRepositoryInterface::class); - - // mock calls: - $collector->shouldReceive('setUser')->once(); - $repository->shouldReceive('setUser')->once(); - $repository->shouldReceive('setStatus')->withAnyArgs(); - $ruleRepos->shouldReceive('setUser')->once(); - $tagRepos->shouldReceive('setUser')->once(); - $tagRepos->shouldReceive('store')->once()->andReturn($tag); - $repository->shouldReceive('setTag')->once(); - $ruleRepos->shouldReceive('getForImport')->andReturn(new Collection([$ruleOne, $ruleTwo])); - $journalRepos->shouldReceive('setUser')->once(); - $journalRepos->shouldReceive('store')->twice()->andReturn($journal); - $journalRepos->shouldReceive('findByHash')->andReturn(null)->times(3); - $repository->shouldReceive('addErrorMessage')->withArgs( - [Mockery::any(), 'Row #2 ("' . $transfer->description . '") could not be imported. Such a transfer already exists.'] - )->once(); - + $journalRepos->shouldReceive('findByHash')->andReturn(null)->times(4); // mock collector so it will return some transfers: $collector->shouldReceive('setAllAssetAccounts')->once()->andReturnSelf(); $collector->shouldReceive('setTypes')->withArgs([[TransactionType::TRANSFER]])->once()->andReturnSelf(); + $collector->shouldReceive('ignoreCache')->once()->andReturnSelf(); $collector->shouldReceive('withOpposingAccount')->once()->andReturnSelf(); $collector->shouldReceive('removeFilter')->withArgs([InternalTransferFilter::class])->once()->andReturnSelf(); $collector->shouldReceive('getJournals')->andReturn($transferCollection);