From fe66d089ad8f6f53a34ba2d93c8262d33017a920 Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 24 Mar 2017 15:01:53 +0100 Subject: [PATCH] Expanded test coverage. --- app/Http/Controllers/Admin/UserController.php | 17 ++-- .../Auth/ForgotPasswordController.php | 2 + app/Http/Controllers/Auth/LoginController.php | 2 + .../Controllers/Auth/PasswordController.php | 2 + .../Controllers/Auth/RegisterController.php | 2 + .../Auth/ResetPasswordController.php | 2 + app/Http/Controllers/TagController.php | 14 +-- .../Controllers/TransactionController.php | 10 +- app/Http/Requests/UserFormRequest.php | 6 +- app/Models/Tag.php | 5 +- .../Journal/JournalRepository.php | 16 +++- .../Journal/JournalRepositoryInterface.php | 10 +- app/Repositories/Tag/TagRepository.php | 69 ++++++++++++- .../Tag/TagRepositoryInterface.php | 17 +++- app/Repositories/User/UserRepository.php | 19 +++- .../User/UserRepositoryInterface.php | 11 ++- app/Support/Models/TagTrait.php | 90 ----------------- .../Controllers/Admin/UserControllerTest.php | 37 ++++++- .../Feature/Controllers/TagControllerTest.php | 96 +++++++++++++++---- .../Controllers/TransactionControllerTest.php | 79 ++++++++++++++- 20 files changed, 357 insertions(+), 149 deletions(-) delete mode 100644 app/Support/Models/TagTrait.php diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index 818c12c2f6..bd83eb13b4 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -9,7 +9,7 @@ * See the LICENSE file for details. */ -declare(strict_types = 1); +declare(strict_types=1); namespace FireflyIII\Http\Controllers\Admin; @@ -18,6 +18,7 @@ use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Requests\UserFormRequest; use FireflyIII\Repositories\User\UserRepositoryInterface; use FireflyIII\User; +use Log; use Preferences; use Session; use View; @@ -129,29 +130,27 @@ class UserController extends Controller * * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ - public function update(UserFormRequest $request, User $user) + public function update(UserFormRequest $request, User $user, UserRepositoryInterface $repository) { + Log::debug('Actually here'); $data = $request->getUserData(); // update password if (strlen($data['password']) > 0) { - $user->password = bcrypt($data['password']); - $user->save(); + $repository->changePassword($user, $data['password']); } - // change blocked status and code: - $user->blocked = $data['blocked']; - $user->blocked_code = $data['blocked_code']; - $user->save(); + $repository->changeStatus($user, $data['blocked'], $data['blocked_code']); Session::flash('success', strval(trans('firefly.updated_user', ['email' => $user->email]))); Preferences::mark(); if (intval($request->get('return_to_edit')) === 1) { - // set value so edit routine will not overwrite URL: + // @codeCoverageIgnoreStart Session::put('users.edit.fromUpdate', true); return redirect(route('admin.users.edit', [$user->id]))->withInput(['return_to_edit' => 1]); + // @codeCoverageIgnoreEnd } // redirect to previous URL. diff --git a/app/Http/Controllers/Auth/ForgotPasswordController.php b/app/Http/Controllers/Auth/ForgotPasswordController.php index 3942c0868b..1f0f162558 100644 --- a/app/Http/Controllers/Auth/ForgotPasswordController.php +++ b/app/Http/Controllers/Auth/ForgotPasswordController.php @@ -19,6 +19,8 @@ use Illuminate\Http\Request; use Password; /** + * @codeCoverageIgnore + * * Class ForgotPasswordController * * @package FireflyIII\Http\Controllers\Auth diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index 0072229023..655559ba39 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -22,6 +22,8 @@ use Illuminate\Http\Request; use Lang; /** + * @codeCoverageIgnore + * * Class LoginController * * @package FireflyIII\Http\Controllers\Auth diff --git a/app/Http/Controllers/Auth/PasswordController.php b/app/Http/Controllers/Auth/PasswordController.php index 06a6fa99b2..2f98b71c3f 100644 --- a/app/Http/Controllers/Auth/PasswordController.php +++ b/app/Http/Controllers/Auth/PasswordController.php @@ -22,6 +22,8 @@ use Illuminate\Support\Facades\Password; /** + * @codeCoverageIgnore + * * Class PasswordController * * @package FireflyIII\Http\Controllers\Auth diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index cd392a925c..042ea85145 100644 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -24,6 +24,8 @@ use Session; use Validator; /** + * @codeCoverageIgnore + * * Class RegisterController * * @package FireflyIII\Http\Controllers\Auth diff --git a/app/Http/Controllers/Auth/ResetPasswordController.php b/app/Http/Controllers/Auth/ResetPasswordController.php index 1d99a0c179..bc9d48f3a3 100644 --- a/app/Http/Controllers/Auth/ResetPasswordController.php +++ b/app/Http/Controllers/Auth/ResetPasswordController.php @@ -16,6 +16,8 @@ use FireflyIII\Http\Controllers\Controller; use Illuminate\Foundation\Auth\ResetsPasswords; /** + * @codeCoverageIgnore + * * Class ResetPasswordController * * @package FireflyIII\Http\Controllers\Auth diff --git a/app/Http/Controllers/TagController.php b/app/Http/Controllers/TagController.php index 813183d1cb..325eac42f2 100644 --- a/app/Http/Controllers/TagController.php +++ b/app/Http/Controllers/TagController.php @@ -9,7 +9,7 @@ * See the LICENSE file for details. */ -declare(strict_types = 1); +declare(strict_types=1); namespace FireflyIII\Http\Controllers; @@ -144,7 +144,7 @@ class TagController extends Controller * * @return View */ - public function edit(Tag $tag) + public function edit(Tag $tag, TagRepositoryInterface $repository) { $subTitle = trans('firefly.edit_tag', ['tag' => $tag->tag]); $subTitleIcon = 'fa-tag'; @@ -158,8 +158,8 @@ class TagController extends Controller /* * Can this tag become another type? */ - $allowAdvance = $tag->tagAllowAdvance(); - $allowToBalancingAct = $tag->tagAllowBalancing(); + $allowAdvance = $repository->tagAllowAdvance($tag); + $allowToBalancingAct = $repository->tagAllowBalancing($tag); // edit tag options: if ($allowAdvance === false) { @@ -318,10 +318,11 @@ class TagController extends Controller Preferences::mark(); if (intval($request->get('create_another')) === 1) { - // set value so create routine will not overwrite URL: + // @codeCoverageIgnoreStart Session::put('tags.create.fromStore', true); return redirect(route('tags.create'))->withInput(); + // @codeCoverageIgnoreEnd } return redirect($this->getPreviousUri('tags.create.uri')); @@ -343,10 +344,11 @@ class TagController extends Controller Preferences::mark(); if (intval($request->get('return_to_edit')) === 1) { - // set value so edit routine will not overwrite URL: + // @codeCoverageIgnoreStart Session::put('tags.edit.fromUpdate', true); return redirect(route('tags.edit', [$tag->id]))->withInput(['return_to_edit' => 1]); + // @codeCoverageIgnoreEnd } // redirect to previous URL. diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index 527b6e84ca..c8369d0a2f 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -9,7 +9,7 @@ * See the LICENSE file for details. */ -declare(strict_types = 1); +declare(strict_types=1); namespace FireflyIII\Http\Controllers; @@ -151,10 +151,9 @@ class TransactionController extends Controller $ids = array_unique($ids); foreach ($ids as $id) { $journal = $repository->find(intval($id)); - if ($journal && $journal->date->format('Y-m-d') == $date->format('Y-m-d')) { - $journal->order = $order; + if ($journal && $journal->date->isSameDay($date)) { + $repository->setOrder($journal, $order); $order++; - $journal->save(); } } } @@ -240,8 +239,6 @@ class TransactionController extends Controller ]; Log::debug(sprintf('What is %s', $what)); switch ($what) { - default: - throw new FireflyException(sprintf('Cannot handle "%s"', $what)); case 'withdrawal': $array['spent'] = $sum; break; @@ -250,7 +247,6 @@ class TransactionController extends Controller break; case 'transfers': case 'transfer': - Log::debug('HERE'); $array['transferred'] = Steam::positive($sum); break; diff --git a/app/Http/Requests/UserFormRequest.php b/app/Http/Requests/UserFormRequest.php index 436e3c9d33..08a7076038 100644 --- a/app/Http/Requests/UserFormRequest.php +++ b/app/Http/Requests/UserFormRequest.php @@ -9,7 +9,7 @@ * See the LICENSE file for details. */ -declare(strict_types = 1); +declare(strict_types=1); namespace FireflyIII\Http\Requests; @@ -37,7 +37,7 @@ class UserFormRequest extends Request { return [ 'email' => $this->string('email'), - 'blocked' => $this->integer('blocked'), + 'blocked' => $this->integer('blocked') === 1, 'blocked_code' => $this->string('blocked_code'), 'password' => $this->string('password'), ]; @@ -50,7 +50,7 @@ class UserFormRequest extends Request { return [ 'id' => 'required|exists:users,id', - 'email' => 'required', + 'email' => 'email|required', 'password' => 'confirmed', 'blocked_code' => 'between:0,30', 'blocked' => 'between:0,1|numeric', diff --git a/app/Models/Tag.php b/app/Models/Tag.php index 3ad3178855..30018b5e87 100644 --- a/app/Models/Tag.php +++ b/app/Models/Tag.php @@ -9,12 +9,11 @@ * See the LICENSE file for details. */ -declare(strict_types = 1); +declare(strict_types=1); namespace FireflyIII\Models; use Crypt; -use FireflyIII\Support\Models\TagTrait; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; @@ -27,7 +26,7 @@ use Watson\Validating\ValidatingTrait; */ class Tag extends Model { - use ValidatingTrait, SoftDeletes, TagTrait; + use ValidatingTrait, SoftDeletes; /** * The attributes that should be casted to native types. diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php index 31ac868afa..2fd7811541 100644 --- a/app/Repositories/Journal/JournalRepository.php +++ b/app/Repositories/Journal/JournalRepository.php @@ -9,7 +9,7 @@ * See the LICENSE file for details. */ -declare(strict_types = 1); +declare(strict_types=1); namespace FireflyIII\Repositories\Journal; @@ -135,6 +135,20 @@ class JournalRepository implements JournalRepositoryInterface return TransactionType::orderBy('type', 'ASC')->get(); } + /** + * @param TransactionJournal $journal + * @param int $order + * + * @return bool + */ + public function setOrder(TransactionJournal $journal, int $order): bool + { + $journal->order = $order; + $journal->save(); + + return true; + } + /** * @param User $user */ diff --git a/app/Repositories/Journal/JournalRepositoryInterface.php b/app/Repositories/Journal/JournalRepositoryInterface.php index f1f5438ad4..c33d47367b 100644 --- a/app/Repositories/Journal/JournalRepositoryInterface.php +++ b/app/Repositories/Journal/JournalRepositoryInterface.php @@ -9,7 +9,7 @@ * See the LICENSE file for details. */ -declare(strict_types = 1); +declare(strict_types=1); namespace FireflyIII\Repositories\Journal; @@ -67,6 +67,14 @@ interface JournalRepositoryInterface */ public function getTransactionTypes(): Collection; + /** + * @param TransactionJournal $journal + * @param int $order + * + * @return bool + */ + public function setOrder(TransactionJournal $journal, int $order): bool; + /** * @param User $user */ diff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.php index 924f0cf77a..ecf0919395 100644 --- a/app/Repositories/Tag/TagRepository.php +++ b/app/Repositories/Tag/TagRepository.php @@ -9,7 +9,7 @@ * See the LICENSE file for details. */ -declare(strict_types = 1); +declare(strict_types=1); namespace FireflyIII\Repositories\Tag; @@ -246,6 +246,73 @@ class TagRepository implements TagRepositoryInterface } + /** + * Can a tag become an advance payment? + * + * @param Tag $tag + * + * @return bool + */ + public function tagAllowAdvance(Tag $tag): bool + { + /* + * If this tag is a balancing act, and it contains transfers, it cannot be + * changed to an advancePayment. + */ + + if ($tag->tagMode == 'balancingAct' || $tag->tagMode == 'nothing') { + foreach ($tag->transactionjournals as $journal) { + if ($journal->isTransfer()) { + return false; + } + } + } + + /* + * If this tag contains more than one expenses, it cannot become an advance payment. + */ + $count = 0; + foreach ($tag->transactionjournals as $journal) { + if ($journal->isWithdrawal()) { + $count++; + } + } + if ($count > 1) { + return false; + } + + return true; + } + + /** + * Can a tag become a balancing act? + * + * @param Tag $tag + * + * @return bool + */ + public function tagAllowBalancing(Tag $tag): bool + { + /* + * If has more than two transactions already, cannot become a balancing act: + */ + if ($tag->transactionjournals->count() > 2) { + return false; + } + + /* + * If any transaction is a deposit, cannot become a balancing act. + */ + foreach ($tag->transactionjournals as $journal) { + if ($journal->isDeposit()) { + return false; + } + } + + return true; + + } + /** * @param Tag $tag * @param array $data diff --git a/app/Repositories/Tag/TagRepositoryInterface.php b/app/Repositories/Tag/TagRepositoryInterface.php index dc7126d108..fc224d19ce 100644 --- a/app/Repositories/Tag/TagRepositoryInterface.php +++ b/app/Repositories/Tag/TagRepositoryInterface.php @@ -9,7 +9,7 @@ * See the LICENSE file for details. */ -declare(strict_types = 1); +declare(strict_types=1); namespace FireflyIII\Repositories\Tag; @@ -27,6 +27,7 @@ use Illuminate\Support\Collection; */ interface TagRepositoryInterface { + /** * This method will connect a journal with a tag. * @@ -125,6 +126,20 @@ interface TagRepositoryInterface */ public function store(array $data): Tag; + /** + * @param Tag $tag + * + * @return bool + */ + public function tagAllowAdvance(Tag $tag): bool; + + /** + * @param Tag $tag + * + * @return bool + */ + public function tagAllowBalancing(Tag $tag): bool; + /** * Update a tag. * diff --git a/app/Repositories/User/UserRepository.php b/app/Repositories/User/UserRepository.php index 8410349efb..ae31df83c9 100644 --- a/app/Repositories/User/UserRepository.php +++ b/app/Repositories/User/UserRepository.php @@ -9,7 +9,7 @@ * See the LICENSE file for details. */ -declare(strict_types = 1); +declare(strict_types=1); namespace FireflyIII\Repositories\User; @@ -66,6 +66,23 @@ class UserRepository implements UserRepositoryInterface return true; } + /** + * @param User $user + * @param bool $isBlocked + * @param string $code + * + * @return bool + */ + public function changeStatus(User $user, bool $isBlocked, string $code): bool + { + // change blocked status and code: + $user->blocked = $isBlocked; + $user->blocked_code = $code; + $user->save(); + + return true; + } + /** * @return int */ diff --git a/app/Repositories/User/UserRepositoryInterface.php b/app/Repositories/User/UserRepositoryInterface.php index 3a380f0190..458cad07e8 100644 --- a/app/Repositories/User/UserRepositoryInterface.php +++ b/app/Repositories/User/UserRepositoryInterface.php @@ -9,7 +9,7 @@ * See the LICENSE file for details. */ -declare(strict_types = 1); +declare(strict_types=1); namespace FireflyIII\Repositories\User; @@ -50,6 +50,15 @@ interface UserRepositoryInterface */ public function changePassword(User $user, string $password); + /** + * @param User $user + * @param bool $isBlocked + * @param string $code + * + * @return bool + */ + public function changeStatus(User $user, bool $isBlocked, string $code): bool; + /** * Returns a count of all users. * diff --git a/app/Support/Models/TagTrait.php b/app/Support/Models/TagTrait.php deleted file mode 100644 index c99a276f92..0000000000 --- a/app/Support/Models/TagTrait.php +++ /dev/null @@ -1,90 +0,0 @@ -tagMode == 'balancingAct' || $this->tagMode == 'nothing') { - foreach ($this->transactionjournals as $journal) { - if ($journal->isTransfer()) { - return false; - } - } - } - - /* - * If this tag contains more than one expenses, it cannot become an advance payment. - */ - $count = 0; - foreach ($this->transactionjournals as $journal) { - if ($journal->isWithdrawal()) { - $count++; - } - } - if ($count > 1) { - return false; - } - - return true; - } - - /** - * Can a tag become a balancing act? - * - * @return bool - */ - public function tagAllowBalancing(): bool - { - /* - * If has more than two transactions already, cannot become a balancing act: - */ - if ($this->transactionjournals->count() > 2) { - return false; - } - - /* - * If any transaction is a deposit, cannot become a balancing act. - */ - foreach ($this->transactionjournals as $journal) { - if ($journal->isDeposit()) { - return false; - } - } - - return true; - - } - -} diff --git a/tests/Feature/Controllers/Admin/UserControllerTest.php b/tests/Feature/Controllers/Admin/UserControllerTest.php index f94977460a..d272442aba 100644 --- a/tests/Feature/Controllers/Admin/UserControllerTest.php +++ b/tests/Feature/Controllers/Admin/UserControllerTest.php @@ -7,14 +7,12 @@ * See the LICENSE file for details. */ -declare(strict_types = 1); +declare(strict_types=1); namespace Tests\Feature\Controllers\Admin; -use FireflyIII\Models\Preference; use FireflyIII\Repositories\User\UserRepositoryInterface; -use FireflyIII\Support\Facades\Preferences; use Illuminate\Support\Collection; use Tests\TestCase; @@ -60,14 +58,43 @@ class UserControllerTest extends TestCase public function testShow() { $repository = $this->mock(UserRepositoryInterface::class); - $repository->shouldReceive('getUserData')->andReturn([]); + $repository->shouldReceive('getUserData')->andReturn( + [ + 'export_jobs_success' => 0, + 'import_jobs_success' => 0, + 'attachments_size' => 0, + ] + ); $this->be($this->user()); - $response = $this->get(route('admin.users.edit', [1])); + $response = $this->get(route('admin.users.show', [1])); $response->assertStatus(200); // has bread crumb $response->assertSee('