From 78b56712fc021a3a4e1badd744b6955c064e7932 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 20 Jan 2016 10:47:29 +0100 Subject: [PATCH] More new tests. --- .travis.yml | 1 + app/Http/Controllers/AttachmentController.php | 21 +-- database/seeds/TestDataSeeder.php | 144 +++++++++++++++++- pu.sh | 5 + .../Controllers/AttachmentControllerTest.php | 65 ++++---- 5 files changed, 190 insertions(+), 46 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5dc0bc70cd..f9ca51f24e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,7 @@ install: - mv -v .env.testing .env - php artisan env - touch storage/upload/at-1.data + - touch storage/upload/at-2.data - touch storage/database/testing.db - php artisan migrate --seed diff --git a/app/Http/Controllers/AttachmentController.php b/app/Http/Controllers/AttachmentController.php index 14b1580d8f..00f351c5e9 100644 --- a/app/Http/Controllers/AttachmentController.php +++ b/app/Http/Controllers/AttachmentController.php @@ -10,6 +10,7 @@ use FireflyIII\Models\Attachment; use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface; use Input; use Preferences; +use Request; use Response; use Session; use URL; @@ -100,17 +101,17 @@ class AttachmentController extends Controller $quoted = sprintf('"%s"', addcslashes(basename($attachment->filename), '"\\')); - header('Content-Description: File Transfer'); - header('Content-Type: application/octet-stream'); - header('Content-Disposition: attachment; filename=' . $quoted); - header('Content-Transfer-Encoding: binary'); - header('Connection: Keep-Alive'); - header('Expires: 0'); - header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); - header('Pragma: public'); - header('Content-Length: ' . $attachment->size); + Request::header('Content-Description: File Transfer'); + Request::header('Content-Type: application/octet-stream'); + Request::header('Content-Disposition: attachment; filename=' . $quoted); + Request::header('Content-Transfer-Encoding: binary'); + Request::header('Connection: Keep-Alive'); + Request::header('Expires: 0'); + Request::header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); + Request::header('Pragma: public'); + Request::header('Content-Length: ' . $attachment->size); - echo Crypt::decrypt(file_get_contents($file)); + return Crypt::decrypt(file_get_contents($file)); } else { abort(404); diff --git a/database/seeds/TestDataSeeder.php b/database/seeds/TestDataSeeder.php index dedc2809d9..6f02bb1755 100644 --- a/database/seeds/TestDataSeeder.php +++ b/database/seeds/TestDataSeeder.php @@ -3,12 +3,15 @@ use Carbon\Carbon; use FireflyIII\Models\Account; use FireflyIII\Models\AccountMeta; +use FireflyIII\Models\Attachment; use FireflyIII\Models\Bill; use FireflyIII\Models\Budget; use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\Category; use FireflyIII\Models\PiggyBank; use FireflyIII\Models\PiggyBankEvent; +use FireflyIII\Models\Transaction; +use FireflyIII\Models\TransactionJournal; use FireflyIII\User; use Illuminate\Database\Seeder; @@ -41,6 +44,15 @@ class TestDataSeeder extends Seeder // create some piggy banks for user #1 $this->createPiggybanks($user); + + // create some expense accounts for user #1 + $this->createExpenseAccounts($user); + + // create some revenue accounts for user #1 + $this->createRevenueAccounts($user); + + // create journal + attachment: + $this->createAttachments($user); } /** @@ -153,12 +165,11 @@ class TestDataSeeder extends Seeder Category::firstOrCreateEncrypted(['name' => 'Car', 'user_id' => $user->id]); } - /** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @param User $user */ - protected function createPiggybanks(User $user) + private function createPiggybanks(User $user) { $account = $this->findAccount($user, 'TestData Savings'); @@ -294,15 +305,15 @@ class TestDataSeeder extends Seeder /** * @param User $user - * @param $name + * @param $name * * @return Account|null */ - protected function findAccount(User $user, $name) + private function findAccount(User $user, $name) { /** @var Account $account */ - foreach (Account::get() as $account) { - if ($account->name == $name && $user->id == $account->user_id) { + foreach ($user->accounts()->get() as $account) { + if ($account->name == $name) { return $account; break; } @@ -310,4 +321,125 @@ class TestDataSeeder extends Seeder return null; } + + /** + * @param User $user + */ + private function createExpenseAccounts(User $user) + { + $expenses = ['Adobe', 'Google', 'Vitens', 'Albert Heijn', 'PLUS', 'Apple', 'Bakker', 'Belastingdienst', 'bol.com', 'Cafe Central', 'conrad.nl', + 'coolblue', 'Shell', + 'DUO', 'Etos', 'FEBO', 'Greenchoice', 'Halfords', 'XS4All', 'iCentre', 'Jumper', 'Land lord']; + foreach ($expenses as $name) { + // create account: + Account::create( + [ + 'user_id' => $user->id, + 'account_type_id' => 4, + 'name' => $name, + 'active' => 1, + 'encrypted' => 1, + ] + ); + } + + } + + /** + * @param User $user + */ + private function createRevenueAccounts(User $user) + { + $revenues = ['Job', 'Belastingdienst', 'Bank', 'KPN', 'Google']; + foreach ($revenues as $name) { + // create account: + Account::create( + [ + 'user_id' => $user->id, + 'account_type_id' => 5, + 'name' => $name, + 'active' => 1, + 'encrypted' => 1, + ] + ); + } + } + + /** + * @param User $user + */ + private function createAttachments(User $user) + { + + $toAccount = $this->findAccount($user, 'TestData Checking Account'); + $fromAccount = $this->findAccount($user, 'Job'); + + $journal = TransactionJournal::create( + [ + 'user_id' => $user->id, + 'transaction_type_id' => 2, + 'transaction_currency_id' => 1, + 'description' => 'Some journal for attachment', + 'completed' => 1, + 'date' => new Carbon, + ] + ); + Transaction::create( + [ + 'account_id' => $fromAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => -100, + + ] + ); + Transaction::create( + [ + 'account_id' => $toAccount->id, + 'transaction_journal_id' => $journal->id, + 'amount' => 100, + + ] + ); + + // and now attachments + $encrypted = Crypt::encrypt('I are secret'); + Attachment::create( + [ + 'attachable_id' => $journal->id, + 'attachable_type' => 'FireflyIII\Models\TransactionJournal', + 'user_id' => $user->id, + 'md5' => md5('Hallo'), + 'filename' => 'empty-file.txt', + 'title' => 'Empty file', + 'description' => 'This file is empty', + 'notes' => 'What notes', + 'mime' => 'text/plain', + 'size' => strlen($encrypted), + 'uploaded' => 1, + ] + ); + + + + // and now attachment. + Attachment::create( + [ + 'attachable_id' => $journal->id, + 'attachable_type' => 'FireflyIII\Models\TransactionJournal', + 'user_id' => $user->id, + 'md5' => md5('Ook hallo'), + 'filename' => 'empty-file-2.txt', + 'title' => 'Empty file 2', + 'description' => 'This file is empty too', + 'notes' => 'What notes do', + 'mime' => 'text/plain', + 'size' => strlen($encrypted), + 'uploaded' => 1, + ] + ); + // echo crypted data to the file. + file_put_contents(storage_path('upload/at-1.data'), $encrypted); + file_put_contents(storage_path('upload/at-2.data'), $encrypted); + + } } diff --git a/pu.sh b/pu.sh index 8f849a617c..a240909d0e 100755 --- a/pu.sh +++ b/pu.sh @@ -6,6 +6,11 @@ cp .env.testing .env # set default phpunit. cp phpunit.default.xml phpunit.xml +# "create" default attachment: +touch storage/upload/at-1.data +touch storage/upload/at-2.data + + # delete test databases: if [ -f storage/database/testing.db ] then diff --git a/tests/acceptance/Controllers/AttachmentControllerTest.php b/tests/acceptance/Controllers/AttachmentControllerTest.php index caa9520859..1175579b18 100644 --- a/tests/acceptance/Controllers/AttachmentControllerTest.php +++ b/tests/acceptance/Controllers/AttachmentControllerTest.php @@ -15,73 +15,78 @@ class AttachmentControllerTest extends TestCase { /** * @covers FireflyIII\Http\Controllers\AttachmentController::delete - * @todo Implement testDelete(). */ public function testDelete() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->be($this->user()); + $response = $this->call('GET', '/attachment/delete/1'); + $this->assertEquals(200, $response->status()); } /** * @covers FireflyIII\Http\Controllers\AttachmentController::destroy - * @todo Implement testDestroy(). */ public function testDestroy() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->be($this->user()); + + $args = [ + '_token' => Session::token(), + ]; + + $this->session(['attachments.delete.url' => 'http://localhost']); + + $response = $this->call('POST', '/attachment/destroy/2', $args); + $this->assertEquals(302, $response->status()); } /** * @covers FireflyIII\Http\Controllers\AttachmentController::download - * @todo Implement testDownload(). */ public function testDownload() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->be($this->user()); + $response = $this->call('GET', '/attachment/download/1'); + $this->assertEquals(200, $response->status()); } /** * @covers FireflyIII\Http\Controllers\AttachmentController::edit - * @todo Implement testEdit(). */ public function testEdit() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->be($this->user()); + $response = $this->call('GET', '/attachment/edit/1'); + $this->assertEquals(200, $response->status()); } /** * @covers FireflyIII\Http\Controllers\AttachmentController::preview - * @todo Implement testPreview(). */ public function testPreview() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->be($this->user()); + $response = $this->call('GET', '/attachment/preview/1'); + $this->assertEquals(200, $response->status()); } /** * @covers FireflyIII\Http\Controllers\AttachmentController::update - * @todo Implement testUpdate(). */ public function testUpdate() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->session(['attachments.edit.url' => 'http://localhost']); + + $args = [ + 'title' => 'New title', + 'description' => 'New descr', + 'notes' => 'New notes', + '_token' => Session::token(), + ]; + $this->be($this->user()); + + $response = $this->call('POST', '/attachment/update/1', $args); + $this->assertEquals(302, $response->status()); + $this->assertSessionHas('success'); } }