Fix possible null pointer.

This commit is contained in:
James Cole
2018-03-30 22:40:20 +02:00
parent 08ff3d8ad0
commit 8f3e84df4d
16 changed files with 437 additions and 35 deletions

View File

@@ -181,4 +181,4 @@ class CreateExportTest extends TestCase
$this->assertEquals(1, $output);
}
}
}

View File

@@ -280,4 +280,4 @@ class CreateImportTest extends TestCase
$this->assertEquals(1, $output);
}
}
}

View File

@@ -139,6 +139,41 @@ class AccountFactoryTest extends TestCase
$this->assertEquals('', $account->getMeta('accountRole'));
}
/**
* Create an expense account. This overrules the virtual balance.
* Role should not be set.
*
* @covers \FireflyIII\Factory\AccountFactory
* @covers \FireflyIII\Factory\AccountMetaFactory
* @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait
*/
public function testCreateBasicExpenseFullType()
{
$data = [
'account_type_id' => null,
'accountType' => 'Expense account',
'iban' => null,
'name' => 'Basic expense account #' . random_int(1, 1000),
'virtualBalance' => '1243',
'active' => true,
'accountRole' => 'defaultAsset',
];
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($this->user());
$account = $factory->create($data);
// assert stuff about account:
$this->assertEquals($account->name, $data['name']);
$this->assertEquals(AccountType::EXPENSE, $account->accountType->type);
$this->assertEquals('', $account->iban);
$this->assertTrue($account->active);
$this->assertEquals('0', $account->virtual_balance);
$this->assertEquals('', $account->getMeta('accountRole'));
}
/**
* Submit IB data for asset account.
*

View File

@@ -33,6 +33,29 @@ use Tests\TestCase;
*/
class TransactionCurrencyFactoryTest extends TestCase
{
/**
* @covers \FireflyIII\Factory\TransactionCurrencyFactory
*/
public function testCreate()
{
/** @var TransactionCurrencyFactory $factory */
$factory = app(TransactionCurrencyFactory::class);
$result = $factory->create(['name' => 'OK', 'code' => 'XXA', 'symbol' => 'Z', 'decimal_places' => 2]);
$this->assertNotNull($result);
$this->assertEquals('XXA', $result->code);
}
/**
* @covers \FireflyIII\Factory\TransactionCurrencyFactory
*/
public function testCreateEmpty()
{
/** @var TransactionCurrencyFactory $factory */
$factory = app(TransactionCurrencyFactory::class);
$result = $factory->create(['name' => null, 'code' => null, 'symbol' => null, 'decimal_places' => null]);
$this->assertNull($result);
}
/**
* @covers \FireflyIII\Factory\TransactionCurrencyFactory
*/

View File

@@ -40,8 +40,9 @@ class TransactionJournalMetaFactoryTest extends TestCase
public function testUpdateOrCreateBasic()
{
/** @var TransactionJournal $journal */
$journal = $this->user()->transactionJournals()->where('transaction_type_id', 1)->first();
$set = [
$journal = $this->user()->transactionJournals()->inRandomOrder()->first();
$journal->transactionJournalMeta()->delete();
$set = [
'journal' => $journal,
'name' => 'hello',
'data' => 'bye!',
@@ -60,8 +61,9 @@ class TransactionJournalMetaFactoryTest extends TestCase
public function testUpdateOrCreateDate()
{
/** @var TransactionJournal $journal */
$journal = $this->user()->transactionJournals()->where('transaction_type_id', 2)->first();
$set = [
$journal = $this->user()->transactionJournals()->inRandomOrder()->first();
$journal->transactionJournalMeta()->delete();
$set = [
'journal' => $journal,
'name' => 'hello',
'data' => new Carbon('2012-01-01'),
@@ -101,5 +103,61 @@ class TransactionJournalMetaFactoryTest extends TestCase
$this->assertEquals(0, $journal->transactionJournalMeta()->count());
}
/**
* @covers \FireflyIII\Factory\TransactionJournalMetaFactory
*/
public function testUpdateOrCreateEmpty()
{
/** @var TransactionJournal $journal */
$journal = $this->user()->transactionJournals()->inRandomOrder()->first();
$journal->transactionJournalMeta()->delete();
$set = [
'journal' => $journal,
'name' => 'hello',
'data' => '',
];
/** @var TransactionJournalMetaFactory $factory */
$factory = app(TransactionJournalMetaFactory::class);
$result = $factory->updateOrCreate($set);
$this->assertEquals(0, $journal->transactionJournalMeta()->count());
$this->assertNull($result);
}
/**
* @covers \FireflyIII\Factory\TransactionJournalMetaFactory
*/
public function testUpdateOrCreateExistingEmpty()
{
/** @var TransactionJournal $journal */
$journal = $this->user()->transactionJournals()->inRandomOrder()->first();
$journal->transactionJournalMeta()->delete();
$set = [
'journal' => $journal,
'name' => 'hello',
'data' => 'SomeData',
];
/** @var TransactionJournalMetaFactory $factory */
$factory = app(TransactionJournalMetaFactory::class);
$result = $factory->updateOrCreate($set);
$this->assertEquals(1, $journal->transactionJournalMeta()->count());
$this->assertNotNull($result);
// overrule with empty entry:
$set = [
'journal' => $journal,
'name' => 'hello',
'data' => '',
];
/** @var TransactionJournalMetaFactory $factory */
$factory = app(TransactionJournalMetaFactory::class);
$result = $factory->updateOrCreate($set);
$this->assertEquals(0, $journal->transactionJournalMeta()->count());
$this->assertNull($result);
}
}

View File

@@ -0,0 +1,82 @@
<?php
/**
* AdminEventHandlerTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\Unit\Handlers\Events;
use FireflyIII\Events\AdminRequestedTestMessage;
use FireflyIII\Handlers\Events\AdminEventHandler;
use FireflyIII\Mail\AdminTestMail;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Support\Facades\Mail;
use Mockery;
use Tests\TestCase;
/**
* Class AdminEventHandlerTest
*/
class AdminEventHandlerTest extends TestCase
{
/**
* @covers \FireflyIII\Handlers\Events\AdminEventHandler::sendTestMessage
* @covers \FireflyIII\Events\AdminRequestedTestMessage
*/
public function testSendNoMessage()
{
$repository = $this->mock(UserRepositoryInterface::class);
$event = new AdminRequestedTestMessage($this->user(), '127.0.0.1');
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(false)->once();
$listener = new AdminEventHandler();
$this->assertTrue($listener->sendTestMessage($event));
}
/**
* @covers \FireflyIII\Handlers\Events\AdminEventHandler::sendTestMessage
* @covers \FireflyIII\Events\AdminRequestedTestMessage
*/
public function testSendTestMessage()
{
Mail::fake();
$repository = $this->mock(UserRepositoryInterface::class);
$event = new AdminRequestedTestMessage($this->user(), '127.0.0.1');
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->once();
$listener = new AdminEventHandler();
$this->assertTrue($listener->sendTestMessage($event));
// assert a message was sent.
Mail::assertSent(
AdminTestMail::class, function ($mail) {
return $mail->hasTo('thegrumpydictator@gmail.com') && '127.0.0.1' === $mail->ipAddress;
}
);
}
}

View File

@@ -0,0 +1,172 @@
<?php
/**
* VersionCheckEventHandlerTest.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Tests\Unit\Handlers\Events;
use FireflyConfig;
use FireflyIII\Events\RequestedVersionCheckStatus;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Handlers\Events\VersionCheckEventHandler;
use FireflyIII\Models\Configuration;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\Services\Github\Object\Release;
use FireflyIII\Services\Github\Request\UpdateRequest;
use Mockery;
use Tests\TestCase;
/**
* Class VersionCheckEventHandlerTest
*/
class VersionCheckEventHandlerTest extends TestCase
{
/**
*
*/
public function testCheckForUpdatesError()
{
$updateConfig = new Configuration;
$updateConfig->data = 1;
$checkConfig = new Configuration;
$checkConfig->data = time() - 604810;
$event = new RequestedVersionCheckStatus($this->user());
$request = $this->mock(UpdateRequest::class);
$repos = $this->mock(UserRepositoryInterface::class);
$repos->shouldReceive('hasRole')->andReturn(true)->once();
// report on config variables:
FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check', -1])->once()->andReturn($updateConfig);
FireflyConfig::shouldReceive('get')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig);
// request thing:
$request->shouldReceive('call')->once();
$request->shouldReceive('getReleases')->once()->andThrow(new FireflyException('Errrr'));
$handler = new VersionCheckEventHandler;
$handler->checkForUpdates($event);
}
/**
* @covers \FireflyIII\Events\RequestedVersionCheckStatus
* @covers \FireflyIII\Handlers\Events\VersionCheckEventHandler
*/
public function testCheckForUpdatesNewer()
{
$updateConfig = new Configuration;
$updateConfig->data = 1;
$checkConfig = new Configuration;
$checkConfig->data = time() - 604800;
$event = new RequestedVersionCheckStatus($this->user());
$request = $this->mock(UpdateRequest::class);
$repos = $this->mock(UserRepositoryInterface::class);
$repos->shouldReceive('hasRole')->andReturn(true)->once();
// is newer than default return:
$version = config('firefly.version');
$first = new Release(['id' => '1', 'title' => $version . '.1', 'updated' => '2017-05-01', 'content' => '']);
// report on config variables:
FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check', -1])->once()->andReturn($updateConfig);
FireflyConfig::shouldReceive('get')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig);
FireflyConfig::shouldReceive('set')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig);
// request thing:
$request->shouldReceive('call')->once();
$request->shouldReceive('getReleases')->once()->andReturn([$first]);
$handler = new VersionCheckEventHandler;
$handler->checkForUpdates($event);
}
/**
*
*/
public function testCheckForUpdatesNoAdmin()
{
$updateConfig = new Configuration;
$updateConfig->data = 1;
$checkConfig = new Configuration;
$checkConfig->data = time() - 604800;
$event = new RequestedVersionCheckStatus($this->user());
$repos = $this->mock(UserRepositoryInterface::class);
$repos->shouldReceive('hasRole')->andReturn(false)->once();
$handler = new VersionCheckEventHandler;
$handler->checkForUpdates($event);
}
/**
*
*/
public function testCheckForUpdatesNoPermission()
{
$updateConfig = new Configuration;
$updateConfig->data = -1;
$checkConfig = new Configuration;
$checkConfig->data = time() - 604800;
$event = new RequestedVersionCheckStatus($this->user());
$repos = $this->mock(UserRepositoryInterface::class);
$repos->shouldReceive('hasRole')->andReturn(true)->once();
// report on config variables:
FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check', -1])->once()->andReturn($updateConfig);
FireflyConfig::shouldReceive('get')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig);
$handler = new VersionCheckEventHandler;
$handler->checkForUpdates($event);
}
/**
*
*/
public function testCheckForUpdatesTooRecent()
{
$updateConfig = new Configuration;
$updateConfig->data = 1;
$checkConfig = new Configuration;
$checkConfig->data = time() - 800;
$event = new RequestedVersionCheckStatus($this->user());
$repos = $this->mock(UserRepositoryInterface::class);
$repos->shouldReceive('hasRole')->andReturn(true)->once();
// report on config variables:
FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check', -1])->once()->andReturn($updateConfig);
FireflyConfig::shouldReceive('get')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig);
$handler = new VersionCheckEventHandler;
$handler->checkForUpdates($event);
}
}