diff --git a/app/Models/Account.php b/app/Models/Account.php index 77c8a66086..dcea8ec26e 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -60,6 +60,31 @@ class Account extends Model } + /** + * @param array $fields + * + * @return Account|null + */ + public static function firstOrNullEncrypted(array $fields) + { + // everything but the name: + $query = Account::orderBy('id'); + foreach ($fields as $name => $value) { + if ($name != 'name') { + $query->where($name, $value); + } + } + $set = $query->get(['accounts.*']); + /** @var Account $account */ + foreach ($set as $account) { + if ($account->name == $fields['name']) { + return $account; + } + } + + return null; + } + /** * @return \Illuminate\Database\Eloquent\Relations\HasMany */ diff --git a/app/Repositories/Account/AccountRepository.php b/app/Repositories/Account/AccountRepository.php index 9504907c72..456754492e 100644 --- a/app/Repositories/Account/AccountRepository.php +++ b/app/Repositories/Account/AccountRepository.php @@ -443,11 +443,15 @@ class AccountRepository implements AccountRepositoryInterface if (!$newAccount->isValid()) { // does the account already exist? - $existingAccount = Account::where('user_id', $data['user'])->where('account_type_id', $accountType->id)->where('name', $data['name'])->first(); + $searchData = [ + 'user_id' => $data['user'], + 'account_type_id' => $accountType->id, + 'name' => $data['name'] + ]; + $existingAccount = Account::firstOrNullEncrypted($searchData); if (!$existingAccount) { Log::error('Account create error: ' . $newAccount->getErrors()->toJson()); App::abort(500); - } $newAccount = $existingAccount; } diff --git a/tests/factories/all.php b/tests/factories/all.php index 33c2577c03..4b9075a7cd 100644 --- a/tests/factories/all.php +++ b/tests/factories/all.php @@ -61,7 +61,9 @@ FactoryMuffin::define( 'account_type_id' => 'factory|FireflyIII\Models\AccountType', 'name' => 'word', 'active' => 'boolean', - 'encrypted' => 'boolean', + 'encrypted' => function () { + return true; + }, 'virtual_balance' => 0 ] ); diff --git a/tests/repositories/AccountRepositoryTest.php b/tests/repositories/AccountRepositoryTest.php index 43ec9fa1c0..03319f2b65 100644 --- a/tests/repositories/AccountRepositoryTest.php +++ b/tests/repositories/AccountRepositoryTest.php @@ -606,6 +606,9 @@ class AccountRepositoryTest extends TestCase /** * @covers FireflyIII\Repositories\Account\AccountRepository::store + * @covers FireflyIII\Repositories\Account\AccountRepository::storeAccount + * @covers FireflyIII\Repositories\Account\AccountRepository::storeMetadata + * @covers FireflyIII\Repositories\Account\AccountRepository::storeInitialBalance */ public function testStore() { @@ -638,14 +641,127 @@ class AccountRepositoryTest extends TestCase } + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::store + * @covers FireflyIII\Repositories\Account\AccountRepository::storeAccount + * @covers FireflyIII\Repositories\Account\AccountRepository::storeMetadata + * @covers FireflyIII\Repositories\Account\AccountRepository::storeInitialBalance + */ + public function testStoreWithNegativeInitialBalance() + { + $user = FactoryMuffin::create('FireflyIII\User'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $this->be($user); + + $data = [ + 'accountType' => 'expense', + 'user' => $user->id, + 'name' => 'Test account #' . rand(1, 100), + 'active' => true, + 'accountRole' => 'testAccount', + 'openingBalance' => -100, + 'virtualBalance' => 0, + 'openingBalanceCurrency' => $currency->id, + 'openingBalanceDate' => '2015-01-01', + ]; + + + $account = $this->object->store($data); + + $this->assertEquals($data['name'], $account->name); + + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::store + * @covers FireflyIII\Repositories\Account\AccountRepository::storeAccount + * @covers FireflyIII\Repositories\Account\AccountRepository::storeMetadata + * @covers FireflyIII\Repositories\Account\AccountRepository::storeInitialBalance + */ + public function testStoreWithExistingAccount() + { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $currency = FactoryMuffin::create('FireflyIII\Models\TransactionCurrency'); + $this->be($account->user); + + + $data = [ + 'accountType' => 'expense', + 'user' => $account->user->id, + 'name' => $account->name, + 'active' => $account->active, + 'accountRole' => 'testAccount', + 'openingBalance' => 0, + 'virtualBalance' => 0, + 'openingBalanceCurrency' => $currency->id, + 'openingBalanceDate' => '2015-01-01', + ]; + + + $newAccount = $this->object->store($data); + + $this->assertEquals($account->name, $newAccount->name); + $this->assertEquals($account->id, $newAccount->id); + + } + + /** + * @covers FireflyIII\Repositories\Account\AccountRepository::store + * @covers FireflyIII\Repositories\Account\AccountRepository::storeAccount + * @covers FireflyIII\Repositories\Account\AccountRepository::storeMetadata + * @covers FireflyIII\Repositories\Account\AccountRepository::storeInitialBalance + */ + public function testStoreWithInvalidAccountData() + { + $account = FactoryMuffin::create('FireflyIII\Models\Account'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\AccountType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + FactoryMuffin::create('FireflyIII\Models\TransactionType'); + $this->be($account->user); + + + $data = [ + 'accountType' => 'expense', + 'user' => $account->user->id, + 'name' => $account->name, + 'active' => $account->active, + 'accountRole' => 'testAccount', + 'openingBalance' => 0, + 'virtualBalance' => 0, + 'openingBalanceCurrency' => 12, + 'openingBalanceDate' => '2015-01-01', + ]; + + + $newAccount = $this->object->store($data); + + $this->assertEquals($account->name, $newAccount->name); + $this->assertEquals($account->id, $newAccount->id); + + } + /** * @covers FireflyIII\Repositories\Account\AccountRepository::sumOfEverything - * @todo Implement testSumOfEverything(). */ public function testSumOfEverything() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete('This test has not been implemented yet.'); + $user = FactoryMuffin::create('FireflyIII\User'); + $this->be($user); + + $this->assertEquals(0, $this->object->sumOfEverything()); } /**