diff --git a/app/Http/Controllers/Chart/CategoryReportController.php b/app/Http/Controllers/Chart/CategoryReportController.php index 1f581808e5..00e938471e 100644 --- a/app/Http/Controllers/Chart/CategoryReportController.php +++ b/app/Http/Controllers/Chart/CategoryReportController.php @@ -19,6 +19,7 @@ use FireflyIII\Generator\Chart\Basic\GeneratorInterface; use FireflyIII\Generator\Report\Category\MonthReportGenerator; use FireflyIII\Helpers\Chart\MetaPieChartInterface; use FireflyIII\Helpers\Collector\JournalCollectorInterface; +use FireflyIII\Helpers\Filter\TransferFilter; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Models\Category; use FireflyIII\Models\Transaction; diff --git a/tests/Feature/Controllers/CategoryControllerTest.php b/tests/Feature/Controllers/CategoryControllerTest.php index 44ad09445e..4a61fbb9a6 100644 --- a/tests/Feature/Controllers/CategoryControllerTest.php +++ b/tests/Feature/Controllers/CategoryControllerTest.php @@ -13,6 +13,7 @@ namespace Tests\Feature\Controllers; use Carbon\Carbon; use FireflyIII\Helpers\Collector\JournalCollectorInterface; +use FireflyIII\Helpers\Filter\InternalTransferFilter; use FireflyIII\Models\Category; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; @@ -138,7 +139,7 @@ class CategoryControllerTest extends TestCase $collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([], 0, 10)); $collector->shouldReceive('setPage')->andReturnSelf(); - $collector->shouldReceive('disableInternalFilter')->andReturnSelf(); + $collector->shouldReceive('removeFilter')->withArgs([InternalTransferFilter::class])->andReturnSelf(); $collector->shouldReceive('setLimit')->andReturnSelf(); Steam::shouldReceive('positive')->once()->andReturn('1'); @@ -174,7 +175,7 @@ class CategoryControllerTest extends TestCase $collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([], 0, 10)); $collector->shouldReceive('setPage')->andReturnSelf(); - $collector->shouldReceive('disableInternalFilter')->andReturnSelf(); + $collector->shouldReceive('removeFilter')->withArgs([InternalTransferFilter::class])->andReturnSelf(); $collector->shouldReceive('setLimit')->andReturnSelf(); $this->be($this->user()); @@ -208,7 +209,7 @@ class CategoryControllerTest extends TestCase $collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([], 0, 10)); $collector->shouldReceive('setPage')->andReturnSelf(); - $collector->shouldReceive('disableInternalFilter')->andReturnSelf(); + $collector->shouldReceive('removeFilter')->withArgs([InternalTransferFilter::class])->andReturnSelf(); $collector->shouldReceive('setLimit')->andReturnSelf(); Steam::shouldReceive('positive')->once()->andReturn('1'); @@ -249,7 +250,7 @@ class CategoryControllerTest extends TestCase $collector->shouldReceive('setLimit')->andReturnSelf()->once(); $collector->shouldReceive('setAllAssetAccounts')->andReturnSelf()->twice(); $collector->shouldReceive('setRange')->andReturnSelf()->twice(); - $collector->shouldReceive('disableInternalFilter')->andReturnSelf()->twice(); + $collector->shouldReceive('removeFilter')->withArgs([InternalTransferFilter::class])->andReturnSelf()->twice(); $collector->shouldReceive('setTypes')->andReturnSelf()->once(); $collector->shouldReceive('withBudgetInformation')->andReturnSelf()->once(); $collector->shouldReceive('withCategoryInformation')->andReturnSelf()->once(); @@ -287,7 +288,7 @@ class CategoryControllerTest extends TestCase $collector->shouldReceive('withBudgetInformation')->andReturnSelf()->once(); $collector->shouldReceive('withCategoryInformation')->andReturnSelf()->once(); $collector->shouldReceive('withOpposingAccount')->andReturnSelf()->once(); - $collector->shouldReceive('disableInternalFilter')->andReturnSelf()->once(); + $collector->shouldReceive('removeFilter')->withArgs([InternalTransferFilter::class])->andReturnSelf()->once(); $collector->shouldReceive('setCategory')->andReturnSelf()->once(); $collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([$transaction], 0, 10))->once(); @@ -326,7 +327,7 @@ class CategoryControllerTest extends TestCase $collector->shouldReceive('setLimit')->andReturnSelf()->once(); $collector->shouldReceive('setAllAssetAccounts')->andReturnSelf()->twice(); $collector->shouldReceive('setRange')->andReturnSelf()->twice(); - $collector->shouldReceive('disableInternalFilter')->andReturnSelf()->twice(); + $collector->shouldReceive('removeFilter')->withArgs([InternalTransferFilter::class])->andReturnSelf()->twice(); $collector->shouldReceive('setTypes')->andReturnSelf()->once(); $collector->shouldReceive('withBudgetInformation')->andReturnSelf()->once(); $collector->shouldReceive('withCategoryInformation')->andReturnSelf()->once(); @@ -375,7 +376,7 @@ class CategoryControllerTest extends TestCase $collector->shouldReceive('setAllAssetAccounts')->andReturnSelf()->times(4); $collector->shouldReceive('setRange')->andReturnSelf()->times(4); $collector->shouldReceive('setTypes')->andReturnSelf()->times(1); - $collector->shouldReceive('disableInternalFilter')->andReturnSelf()->times(4); + $collector->shouldReceive('removeFilter')->withArgs([InternalTransferFilter::class])->andReturnSelf()->times(4); $collector->shouldReceive('withBudgetInformation')->andReturnSelf()->times(3); $collector->shouldReceive('withCategoryInformation')->andReturnSelf()->times(3); $collector->shouldReceive('withOpposingAccount')->andReturnSelf()->times(4); diff --git a/tests/Feature/Controllers/Chart/BudgetReportControllerTest.php b/tests/Feature/Controllers/Chart/BudgetReportControllerTest.php index 96990fd633..b9f2ec12b5 100644 --- a/tests/Feature/Controllers/Chart/BudgetReportControllerTest.php +++ b/tests/Feature/Controllers/Chart/BudgetReportControllerTest.php @@ -16,6 +16,7 @@ use Carbon\Carbon; use FireflyIII\Generator\Chart\Basic\GeneratorInterface; use FireflyIII\Helpers\Chart\MetaPieChartInterface; use FireflyIII\Helpers\Collector\JournalCollectorInterface; +use FireflyIII\Helpers\Filter\TransferFilter; use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionType; @@ -102,7 +103,7 @@ class BudgetReportControllerTest extends TestCase $collector->shouldReceive('setRange')->andReturnSelf(); $collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL, TransactionType::TRANSFER]])->andReturnSelf(); $collector->shouldReceive('setTypes')->withArgs([[TransactionType::DEPOSIT, TransactionType::TRANSFER]])->andReturnSelf(); - $collector->shouldReceive('disableFilter')->andReturnSelf(); + $collector->shouldReceive('removeFilter')->withArgs([TransferFilter::class])->andReturnSelf(); $collector->shouldReceive('setBudgets')->andReturnSelf(); $collector->shouldReceive('withOpposingAccount')->andReturnSelf(); $collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction])); diff --git a/tests/Feature/Controllers/Chart/CategoryReportControllerTest.php b/tests/Feature/Controllers/Chart/CategoryReportControllerTest.php index f8270c231d..db8fe61071 100644 --- a/tests/Feature/Controllers/Chart/CategoryReportControllerTest.php +++ b/tests/Feature/Controllers/Chart/CategoryReportControllerTest.php @@ -15,6 +15,7 @@ namespace Tests\Feature\Controllers\Chart; use FireflyIII\Generator\Chart\Basic\GeneratorInterface; use FireflyIII\Helpers\Chart\MetaPieChartInterface; use FireflyIII\Helpers\Collector\JournalCollectorInterface; +use FireflyIII\Helpers\Filter\TransferFilter; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionType; use Illuminate\Support\Collection; @@ -129,7 +130,7 @@ class CategoryReportControllerTest extends TestCase $collector->shouldReceive('setRange')->andReturnSelf(); $collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL, TransactionType::TRANSFER]])->andReturnSelf(); $collector->shouldReceive('setTypes')->withArgs([[TransactionType::DEPOSIT, TransactionType::TRANSFER]])->andReturnSelf(); - $collector->shouldReceive('disableFilter')->andReturnSelf(); + $collector->shouldReceive('removeFilter')->withArgs([TransferFilter::class])->andReturnSelf(); $collector->shouldReceive('setCategories')->andReturnSelf(); $collector->shouldReceive('withOpposingAccount')->andReturnSelf(); $collector->shouldReceive('getJournals')->andReturn($transactions); diff --git a/tests/Feature/Controllers/Chart/TagReportControllerTest.php b/tests/Feature/Controllers/Chart/TagReportControllerTest.php index 175955ae41..f86d58fb70 100644 --- a/tests/Feature/Controllers/Chart/TagReportControllerTest.php +++ b/tests/Feature/Controllers/Chart/TagReportControllerTest.php @@ -15,6 +15,7 @@ namespace Tests\Feature\Controllers\Chart; use FireflyIII\Generator\Chart\Basic\GeneratorInterface; use FireflyIII\Helpers\Chart\MetaPieChartInterface; use FireflyIII\Helpers\Collector\JournalCollectorInterface; +use FireflyIII\Helpers\Filter\TransferFilter; use FireflyIII\Models\Tag; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionType; @@ -139,7 +140,7 @@ class TagReportControllerTest extends TestCase $collector->shouldReceive('setRange')->andReturnSelf(); $collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL, TransactionType::TRANSFER]])->andReturnSelf(); $collector->shouldReceive('setTypes')->withArgs([[TransactionType::DEPOSIT, TransactionType::TRANSFER]])->andReturnSelf(); - $collector->shouldReceive('disableFilter')->andReturnSelf(); + $collector->shouldReceive('removeFilter')->withArgs([TransferFilter::class])->andReturnSelf(); $collector->shouldReceive('setTags')->andReturnSelf(); $collector->shouldReceive('withOpposingAccount')->andReturnSelf(); $collector->shouldReceive('getJournals')->andReturn($set); diff --git a/tests/Feature/Controllers/Report/OperationsControllerTest.php b/tests/Feature/Controllers/Report/OperationsControllerTest.php index 58bbcce510..793c7462ab 100644 --- a/tests/Feature/Controllers/Report/OperationsControllerTest.php +++ b/tests/Feature/Controllers/Report/OperationsControllerTest.php @@ -13,6 +13,7 @@ namespace Tests\Feature\Controllers\Report; use FireflyIII\Helpers\Collector\JournalCollectorInterface; +use FireflyIII\Helpers\Filter\InternalTransferFilter; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionType; use Tests\TestCase; @@ -36,7 +37,7 @@ class OperationsControllerTest extends TestCase $collector->shouldReceive('setRange')->andReturnSelf(); $collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL, TransactionType::TRANSFER]])->andReturnSelf(); $collector->shouldReceive('withOpposingAccount')->andReturnSelf(); - $collector->shouldReceive('enableInternalFilter')->andReturnSelf(); + $collector->shouldReceive('addFilter')->withArgs([InternalTransferFilter::class])->andReturnSelf(); $collector->shouldReceive('getJournals')->andReturn($transactions); @@ -57,7 +58,7 @@ class OperationsControllerTest extends TestCase $collector->shouldReceive('setRange')->andReturnSelf(); $collector->shouldReceive('setTypes')->withArgs([[TransactionType::DEPOSIT, TransactionType::TRANSFER]])->andReturnSelf(); $collector->shouldReceive('withOpposingAccount')->andReturnSelf(); - $collector->shouldReceive('enableInternalFilter')->andReturnSelf(); + $collector->shouldReceive('addFilter')->withArgs([InternalTransferFilter::class])->andReturnSelf(); $collector->shouldReceive('getJournals')->andReturn($transactions); $this->be($this->user()); @@ -78,7 +79,7 @@ class OperationsControllerTest extends TestCase $collector->shouldReceive('setTypes')->withArgs([[TransactionType::DEPOSIT, TransactionType::TRANSFER]])->andReturnSelf()->once(); $collector->shouldReceive('setTypes')->withArgs([[TransactionType::WITHDRAWAL, TransactionType::TRANSFER]])->andReturnSelf()->once(); $collector->shouldReceive('withOpposingAccount')->andReturnSelf(); - $collector->shouldReceive('enableInternalFilter')->andReturnSelf(); + $collector->shouldReceive('addFilter')->withArgs([InternalTransferFilter::class])->andReturnSelf(); $collector->shouldReceive('getJournals')->andReturn($transactions); $this->be($this->user()); diff --git a/tests/Feature/Controllers/TransactionControllerTest.php b/tests/Feature/Controllers/TransactionControllerTest.php index ba5a49f93a..0590545c0b 100644 --- a/tests/Feature/Controllers/TransactionControllerTest.php +++ b/tests/Feature/Controllers/TransactionControllerTest.php @@ -13,6 +13,7 @@ namespace Tests\Feature\Controllers; use Carbon\Carbon; use FireflyIII\Helpers\Collector\JournalCollectorInterface; +use FireflyIII\Helpers\Filter\InternalTransferFilter; use FireflyIII\Models\TransactionJournal; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Repositories\Journal\JournalTaskerInterface; @@ -45,7 +46,7 @@ class TransactionControllerTest extends TestCase $collector->shouldReceive('withBudgetInformation')->andReturnSelf(); $collector->shouldReceive('withCategoryInformation')->andReturnSelf(); $collector->shouldReceive('withOpposingAccount')->andReturnSelf(); - $collector->shouldReceive('disableInternalFilter')->andReturnSelf(); + $collector->shouldReceive('removeFilter')->withArgs([InternalTransferFilter::class])->andReturnSelf(); $collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([], 0, 10)); $collector->shouldReceive('getJournals')->andReturn(new Collection); @@ -76,7 +77,7 @@ class TransactionControllerTest extends TestCase $collector->shouldReceive('withBudgetInformation')->andReturnSelf(); $collector->shouldReceive('withCategoryInformation')->andReturnSelf(); $collector->shouldReceive('withOpposingAccount')->andReturnSelf(); - $collector->shouldReceive('disableInternalFilter')->andReturnSelf(); + $collector->shouldReceive('removeFilter')->withArgs([InternalTransferFilter::class])->andReturnSelf(); $collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([], 0, 10)); $collector->shouldReceive('getJournals')->andReturn(new Collection); @@ -106,7 +107,7 @@ class TransactionControllerTest extends TestCase $collector->shouldReceive('withBudgetInformation')->andReturnSelf(); $collector->shouldReceive('withCategoryInformation')->andReturnSelf(); $collector->shouldReceive('withOpposingAccount')->andReturnSelf(); - $collector->shouldReceive('disableInternalFilter')->andReturnSelf(); + $collector->shouldReceive('removeFilter')->withArgs([InternalTransferFilter::class])->andReturnSelf(); $collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([], 0, 10)); $collector->shouldReceive('getJournals')->andReturn(new Collection); @@ -139,7 +140,7 @@ class TransactionControllerTest extends TestCase $collector->shouldReceive('withBudgetInformation')->andReturnSelf(); $collector->shouldReceive('withCategoryInformation')->andReturnSelf(); $collector->shouldReceive('withOpposingAccount')->andReturnSelf(); - $collector->shouldReceive('disableInternalFilter')->andReturnSelf(); + $collector->shouldReceive('removeFilter')->withArgs([InternalTransferFilter::class])->andReturnSelf(); $collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([], 0, 10)); $collector->shouldReceive('getJournals')->andReturn(new Collection); @@ -170,7 +171,7 @@ class TransactionControllerTest extends TestCase $collector->shouldReceive('withBudgetInformation')->andReturnSelf(); $collector->shouldReceive('withCategoryInformation')->andReturnSelf(); $collector->shouldReceive('withOpposingAccount')->andReturnSelf(); - $collector->shouldReceive('disableInternalFilter')->andReturnSelf(); + $collector->shouldReceive('removeFilter')->withArgs([InternalTransferFilter::class])->andReturnSelf(); $collector->shouldReceive('getPaginatedJournals')->andReturn(new LengthAwarePaginator([], 0, 10)); $collector->shouldReceive('getJournals')->andReturn(new Collection); diff --git a/tests/Unit/Helpers/AttachmentHelperTest.php b/tests/Unit/Helpers/AttachmentHelperTest.php new file mode 100644 index 0000000000..47f039f332 --- /dev/null +++ b/tests/Unit/Helpers/AttachmentHelperTest.php @@ -0,0 +1,136 @@ +id)); + $this->assertEquals($helper->getAttachmentLocation($attachment), $path); + } + + /** + * Test invalid mime thing + * + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::__construct + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::saveAttachmentsForModel + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::processFile + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::validateUpload + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::validMime + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::hasFile + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::getMessages + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::getErrors + * + */ + public function testInvalidMime() + { + $journal = TransactionJournal::first(); + $helper = new AttachmentHelper; + $path = resource_path('stubs/csv.csv'); + $file = new UploadedFile($path, 'csv.csv', 'text/plain', filesize($path), null, true); + + $helper->saveAttachmentsForModel($journal, [$file]); + $errors = $helper->getErrors(); + $messages = $helper->getMessages(); + + $this->assertCount(1, $errors); + $this->assertCount(0, $messages); + $this->assertEquals('File "csv.csv" is of type "text/plain" which is not accepted as a new upload.', $errors->first()); + } + + /** + * Test valid file upload. + * + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::__construct + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::saveAttachmentsForModel + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::processFile + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::validateUpload + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::validMime + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::hasFile + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::getMessages + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::getErrors + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::getAttachments + * + */ + public function testSave() + { + Storage::fake('upload'); + + $journal = TransactionJournal::first(); + $helper = new AttachmentHelper; + $path = public_path('apple-touch-icon.png'); + $file = new UploadedFile($path, 'apple-touch-icon.png', 'image/png', filesize($path), null, true); + + $helper->saveAttachmentsForModel($journal, [$file]); + $errors = $helper->getErrors(); + $messages = $helper->getMessages(); + $attachments = $helper->getAttachments(); + + $this->assertCount(0, $errors); + $this->assertCount(1, $messages); + $this->assertEquals('Succesfully uploaded file "apple-touch-icon.png".', $messages->first()); + + // Assert the file was stored... + Storage::disk('upload')->assertExists(sprintf('at-%d.data', $attachments->first()->id)); + } + + /** + * Test double file upload. Needs to be after testSave. + * + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::__construct + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::saveAttachmentsForModel + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::processFile + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::validateUpload + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::validMime + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::hasFile + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::getMessages + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::getErrors + * @covers \FireflyIII\Helpers\Attachments\AttachmentHelper::getAttachments + * + */ + public function testSaveSecond() + { + $journal = TransactionJournal::first(); + $helper = new AttachmentHelper; + $path = public_path('apple-touch-icon.png'); + $file = new UploadedFile($path, 'apple-touch-icon.png', 'image/png', filesize($path), null, true); + + $helper->saveAttachmentsForModel($journal, [$file]); + $errors = $helper->getErrors(); + $messages = $helper->getMessages(); + + $this->assertCount(1, $errors); + $this->assertCount(0, $messages); + $this->assertEquals('Uploaded file "apple-touch-icon.png" is already attached to this object.', $errors->first()); + } + + +} \ No newline at end of file diff --git a/tests/Unit/Helpers/MetaPieChartTest.php b/tests/Unit/Helpers/MetaPieChartTest.php new file mode 100644 index 0000000000..7ab6deb787 --- /dev/null +++ b/tests/Unit/Helpers/MetaPieChartTest.php @@ -0,0 +1,41 @@ +startOfMonth(); + $eom = (new Carbon())->endOfMonth(); + + $helper = new MetaPieChart(); + $helper->setUser($this->user()); + $helper->setStart($som); + $helper->setEnd($eom); + $chart = $helper->generate('income', 'account'); + $this->assertTrue(true); + } + +} \ No newline at end of file