2017-02-12 05:00:11 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* RuleGroupControllerTest.php
|
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* 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
|
2017-12-17 07:42:21 -06:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2017-02-12 05:00:11 -06:00
|
|
|
*/
|
2017-07-07 23:28:44 -05:00
|
|
|
declare(strict_types=1);
|
2017-02-12 05:00:11 -06:00
|
|
|
|
|
|
|
namespace Tests\Feature\Controllers;
|
|
|
|
|
2017-02-12 06:15:23 -06:00
|
|
|
use Carbon\Carbon;
|
2017-03-16 14:46:18 -05:00
|
|
|
use FireflyIII\Jobs\ExecuteRuleGroupOnExistingTransactions;
|
2017-02-12 06:15:23 -06:00
|
|
|
use FireflyIII\Models\RuleGroup;
|
2017-03-05 11:15:38 -06:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
2017-02-12 06:15:23 -06:00
|
|
|
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
|
2018-09-04 02:52:19 -05:00
|
|
|
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
2017-03-05 11:15:38 -06:00
|
|
|
use Illuminate\Support\Collection;
|
2018-03-24 00:08:50 -05:00
|
|
|
use Log;
|
2018-09-04 02:52:19 -05:00
|
|
|
use Mockery;
|
2017-02-12 05:00:11 -06:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
2017-03-05 11:15:38 -06:00
|
|
|
/**
|
|
|
|
* Class RuleGroupControllerTest
|
|
|
|
*
|
2017-08-12 03:27:45 -05:00
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
|
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
2017-03-05 11:15:38 -06:00
|
|
|
*/
|
2017-02-12 05:00:11 -06:00
|
|
|
class RuleGroupControllerTest extends TestCase
|
|
|
|
{
|
2018-03-24 00:08:50 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2018-09-02 13:27:26 -05:00
|
|
|
public function setUp(): void
|
2018-03-24 00:08:50 -05:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2018-09-03 11:52:46 -05:00
|
|
|
Log::info(sprintf('Now in %s.', \get_class($this)));
|
2018-03-24 00:08:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
/**
|
2018-08-09 13:17:15 -05:00
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleGroupController
|
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleGroupController
|
2017-02-12 05:21:44 -06:00
|
|
|
*/
|
2018-05-11 12:58:10 -05:00
|
|
|
public function testCreate(): void
|
2017-02-12 05:21:44 -06:00
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
2018-09-04 02:52:19 -05:00
|
|
|
$userRepos = $this->mock(UserRepositoryInterface::class);
|
2018-04-28 03:27:33 -05:00
|
|
|
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
2018-09-04 02:52:19 -05:00
|
|
|
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
|
2017-03-05 11:15:38 -06:00
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->get(route('rule-groups.create'));
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertSee('<ol class="breadcrumb">');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-09 13:17:15 -05:00
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleGroupController
|
2017-02-12 05:21:44 -06:00
|
|
|
*/
|
2018-05-11 12:58:10 -05:00
|
|
|
public function testDelete(): void
|
2017-02-12 05:21:44 -06:00
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
|
|
|
$repository = $this->mock(RuleGroupRepositoryInterface::class);
|
2018-09-04 02:52:19 -05:00
|
|
|
$userRepos = $this->mock(UserRepositoryInterface::class);
|
2018-04-28 03:27:33 -05:00
|
|
|
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
2017-03-05 11:15:38 -06:00
|
|
|
$repository->shouldReceive('get')->andReturn(new Collection);
|
2018-09-04 02:52:19 -05:00
|
|
|
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
|
2017-03-05 11:15:38 -06:00
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->get(route('rule-groups.delete', [1]));
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertSee('<ol class="breadcrumb">');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-09 13:17:15 -05:00
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleGroupController
|
2017-02-12 05:21:44 -06:00
|
|
|
*/
|
2018-05-11 12:58:10 -05:00
|
|
|
public function testDestroy(): void
|
2017-02-12 05:21:44 -06:00
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
2017-03-16 14:46:18 -05:00
|
|
|
$repository = $this->mock(RuleGroupRepositoryInterface::class);
|
2018-09-04 02:52:19 -05:00
|
|
|
$userRepos = $this->mock(UserRepositoryInterface::class);
|
2018-04-28 03:27:33 -05:00
|
|
|
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
2017-02-12 05:21:44 -06:00
|
|
|
$repository->shouldReceive('destroy');
|
|
|
|
|
2018-09-04 02:52:19 -05:00
|
|
|
|
2017-03-18 14:53:44 -05:00
|
|
|
$this->session(['rule-groups.delete.uri' => 'http://localhost']);
|
2017-02-12 05:21:44 -06:00
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->post(route('rule-groups.destroy', [1]));
|
|
|
|
$response->assertStatus(302);
|
|
|
|
$response->assertSessionHas('success');
|
2017-02-12 05:32:13 -06:00
|
|
|
$response->assertRedirect(route('index'));
|
2017-02-12 05:21:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-09 13:17:15 -05:00
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleGroupController
|
2017-02-12 05:21:44 -06:00
|
|
|
*/
|
2018-05-11 12:58:10 -05:00
|
|
|
public function testDown(): void
|
2017-02-12 05:21:44 -06:00
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
|
|
|
$repository = $this->mock(RuleGroupRepositoryInterface::class);
|
2018-09-04 02:52:19 -05:00
|
|
|
$userRepos = $this->mock(UserRepositoryInterface::class);
|
2018-04-28 03:27:33 -05:00
|
|
|
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
2017-03-05 11:15:38 -06:00
|
|
|
$repository->shouldReceive('moveDown');
|
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->get(route('rule-groups.down', [1]));
|
|
|
|
$response->assertStatus(302);
|
2017-02-12 05:32:13 -06:00
|
|
|
$response->assertRedirect(route('rules.index'));
|
2017-02-12 05:21:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-09 13:17:15 -05:00
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleGroupController
|
2017-02-12 05:21:44 -06:00
|
|
|
*/
|
2018-05-11 12:58:10 -05:00
|
|
|
public function testEdit(): void
|
2017-02-12 05:21:44 -06:00
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
2018-09-04 02:52:19 -05:00
|
|
|
$userRepos = $this->mock(UserRepositoryInterface::class);
|
2018-04-28 03:27:33 -05:00
|
|
|
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
2018-09-04 02:52:19 -05:00
|
|
|
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
|
2017-03-05 11:15:38 -06:00
|
|
|
|
2018-08-07 10:54:37 -05:00
|
|
|
/** @var RuleGroup $ruleGroup */
|
|
|
|
$ruleGroup = $this->user()->ruleGroups()->first();
|
|
|
|
$ruleGroup->description = 'Some description ' . random_int(1, 10000);
|
|
|
|
$ruleGroup->save();
|
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->get(route('rule-groups.edit', [1]));
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertSee('<ol class="breadcrumb">');
|
2018-08-07 10:54:37 -05:00
|
|
|
$response->assertSee($ruleGroup->description);
|
2017-02-12 05:21:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-09 13:17:15 -05:00
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleGroupController
|
2017-02-12 05:21:44 -06:00
|
|
|
*/
|
2018-05-11 12:58:10 -05:00
|
|
|
public function testExecute(): void
|
2017-02-12 05:21:44 -06:00
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
2017-03-16 14:46:18 -05:00
|
|
|
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
2018-09-04 02:52:19 -05:00
|
|
|
$userRepos = $this->mock(UserRepositoryInterface::class);
|
2018-04-28 03:27:33 -05:00
|
|
|
$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
|
2017-03-05 11:15:38 -06:00
|
|
|
$accountRepos->shouldReceive('getAccountsById')->andReturn(new Collection);
|
|
|
|
|
2017-03-16 14:46:18 -05:00
|
|
|
$this->expectsJobs(ExecuteRuleGroupOnExistingTransactions::class);
|
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
$this->session(['first' => new Carbon('2010-01-01')]);
|
|
|
|
$data = [
|
|
|
|
'accounts' => [1],
|
|
|
|
'start_date' => '2010-01-02',
|
|
|
|
'end_date' => '2010-01-02',
|
|
|
|
];
|
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->post(route('rule-groups.execute', [1]), $data);
|
|
|
|
$response->assertStatus(302);
|
|
|
|
$response->assertSessionHas('success');
|
2017-02-12 05:32:13 -06:00
|
|
|
$response->assertRedirect(route('rules.index'));
|
2017-02-12 05:21:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-09 13:17:15 -05:00
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleGroupController
|
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleGroupController
|
2017-02-12 05:21:44 -06:00
|
|
|
*/
|
2018-05-11 12:58:10 -05:00
|
|
|
public function testSelectTransactions(): void
|
2017-02-12 05:21:44 -06:00
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
2017-03-16 14:46:18 -05:00
|
|
|
$accountRepos = $this->mock(AccountRepositoryInterface::class);
|
2018-09-04 02:52:19 -05:00
|
|
|
$userRepos = $this->mock(UserRepositoryInterface::class);
|
2018-04-28 03:27:33 -05:00
|
|
|
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
2017-03-05 11:15:38 -06:00
|
|
|
$accountRepos->shouldReceive('getAccountsByType')->andReturn(new Collection);
|
2018-09-04 02:52:19 -05:00
|
|
|
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
|
2017-03-05 11:15:38 -06:00
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->get(route('rule-groups.select-transactions', [1]));
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertSee('<ol class="breadcrumb">');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-09 13:17:15 -05:00
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleGroupController
|
2018-03-03 10:16:47 -06:00
|
|
|
* @covers \FireflyIII\Http\Requests\RuleGroupFormRequest
|
2017-02-12 05:21:44 -06:00
|
|
|
*/
|
2018-05-11 12:58:10 -05:00
|
|
|
public function testStore(): void
|
2017-02-12 05:21:44 -06:00
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
|
|
|
$repository = $this->mock(RuleGroupRepositoryInterface::class);
|
2018-09-04 02:52:19 -05:00
|
|
|
$userRepos = $this->mock(UserRepositoryInterface::class);
|
2018-04-28 03:27:33 -05:00
|
|
|
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
2017-03-05 11:15:38 -06:00
|
|
|
|
2017-03-18 14:53:44 -05:00
|
|
|
$this->session(['rule-groups.create.uri' => 'http://localhost']);
|
2017-03-16 14:46:18 -05:00
|
|
|
$repository->shouldReceive('store')->andReturn(new RuleGroup);
|
|
|
|
$repository->shouldReceive('find')->andReturn(new RuleGroup);
|
2017-02-12 05:21:44 -06:00
|
|
|
$data = [
|
|
|
|
'title' => 'A',
|
2017-09-12 12:47:29 -05:00
|
|
|
'description' => 'No description',
|
2017-02-12 05:21:44 -06:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->post(route('rule-groups.store', [1]), $data);
|
|
|
|
$response->assertStatus(302);
|
|
|
|
$response->assertSessionHas('success');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-09 13:17:15 -05:00
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleGroupController
|
2017-02-12 05:21:44 -06:00
|
|
|
*/
|
2018-05-11 12:58:10 -05:00
|
|
|
public function testUp(): void
|
2017-02-12 05:21:44 -06:00
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
|
|
|
$repository = $this->mock(RuleGroupRepositoryInterface::class);
|
2018-09-04 02:52:19 -05:00
|
|
|
$userRepos = $this->mock(UserRepositoryInterface::class);
|
|
|
|
|
2018-04-28 03:27:33 -05:00
|
|
|
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
2017-03-05 11:15:38 -06:00
|
|
|
$repository->shouldReceive('moveUp');
|
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->get(route('rule-groups.up', [1]));
|
|
|
|
$response->assertStatus(302);
|
2017-02-12 05:32:13 -06:00
|
|
|
$response->assertRedirect(route('rules.index'));
|
2017-02-12 05:21:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-09 13:17:15 -05:00
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleGroupController
|
2018-03-03 10:16:47 -06:00
|
|
|
* @covers \FireflyIII\Http\Requests\RuleGroupFormRequest
|
2017-02-12 05:21:44 -06:00
|
|
|
*/
|
2018-05-11 12:58:10 -05:00
|
|
|
public function testUpdate(): void
|
2017-02-12 05:21:44 -06:00
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
|
|
|
$repository = $this->mock(RuleGroupRepositoryInterface::class);
|
2018-09-04 02:52:19 -05:00
|
|
|
$userRepos = $this->mock(UserRepositoryInterface::class);
|
2018-04-28 03:27:33 -05:00
|
|
|
$journalRepos->shouldReceive('firstNull')->once()->andReturn(new TransactionJournal);
|
2017-03-05 11:15:38 -06:00
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
$data = [
|
2018-03-03 10:16:47 -06:00
|
|
|
'id' => 1,
|
2017-03-05 04:19:06 -06:00
|
|
|
'title' => 'C',
|
|
|
|
'description' => 'XX',
|
2017-02-12 05:21:44 -06:00
|
|
|
];
|
2017-03-18 14:53:44 -05:00
|
|
|
$this->session(['rule-groups.edit.uri' => 'http://localhost']);
|
2017-02-12 05:21:44 -06:00
|
|
|
|
|
|
|
$repository->shouldReceive('update');
|
2018-03-03 10:16:47 -06:00
|
|
|
$repository->shouldReceive('find')->andReturn(RuleGroup::first());
|
2017-02-12 05:21:44 -06:00
|
|
|
|
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->post(route('rule-groups.update', [1]), $data);
|
|
|
|
$response->assertStatus(302);
|
|
|
|
$response->assertSessionHas('success');
|
|
|
|
}
|
2017-02-16 15:33:32 -06:00
|
|
|
}
|