2017-02-12 05:00:11 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* RuleControllerTest.php
|
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
|
|
|
* This software may be modified and distributed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types = 1);
|
|
|
|
|
|
|
|
namespace Tests\Feature\Controllers;
|
|
|
|
|
2017-02-12 06:15:23 -06:00
|
|
|
use FireflyIII\Models\Rule;
|
2017-03-05 11:15:38 -06:00
|
|
|
use FireflyIII\Models\RuleGroup;
|
|
|
|
use FireflyIII\Models\TransactionJournal;
|
|
|
|
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
2017-02-12 06:15:23 -06:00
|
|
|
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
|
2017-03-05 11:15:38 -06:00
|
|
|
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
|
|
|
|
use Illuminate\Support\Collection;
|
2017-02-12 05:00:11 -06:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
2017-03-05 11:15:38 -06:00
|
|
|
/**
|
|
|
|
* Class RuleControllerTest
|
|
|
|
*
|
|
|
|
* @package Tests\Feature\Controllers
|
|
|
|
*/
|
2017-02-12 05:00:11 -06:00
|
|
|
class RuleControllerTest extends TestCase
|
|
|
|
{
|
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleController::create
|
2017-03-05 11:15:38 -06:00
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleController::getPreviousTriggers
|
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleController::getPreviousActions
|
2017-02-12 05:21:44 -06:00
|
|
|
*/
|
|
|
|
public function testCreate()
|
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
|
|
|
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
|
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->get(route('rules.create', [1]));
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertSee('<ol class="breadcrumb">');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleController::delete
|
|
|
|
*/
|
|
|
|
public function testDelete()
|
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
|
|
|
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
|
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->get(route('rules.delete', [1]));
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertSee('<ol class="breadcrumb">');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleController::destroy
|
|
|
|
*/
|
|
|
|
public function testDestroy()
|
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$repository = $this->mock(RuleRepositoryInterface::class);
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
|
|
|
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
|
2017-02-12 05:21:44 -06:00
|
|
|
$repository->shouldReceive('destroy');
|
|
|
|
|
|
|
|
$this->session(['rules.delete.url' => 'http://localhost']);
|
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->post(route('rules.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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleController::down
|
|
|
|
*/
|
|
|
|
public function testDown()
|
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$repository = $this->mock(RuleRepositoryInterface::class);
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
|
|
|
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
|
|
|
|
$repository->shouldReceive('moveDown');
|
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->get(route('rules.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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleController::edit
|
|
|
|
*/
|
|
|
|
public function testEdit()
|
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$repository = $this->mock(RuleRepositoryInterface::class);
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
|
|
|
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
|
|
|
|
$repository->shouldReceive('getPrimaryTrigger')->andReturn(new Rule);
|
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->get(route('rules.edit', [1]));
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertSee('<ol class="breadcrumb">');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleController::index
|
2017-02-17 13:14:38 -06:00
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleController::__construct
|
2017-02-12 05:21:44 -06:00
|
|
|
*/
|
|
|
|
public function testIndex()
|
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$repository = $this->mock(RuleRepositoryInterface::class);
|
|
|
|
$ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
|
|
|
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
|
|
|
|
$ruleGroupRepos->shouldReceive('count')->andReturn(0);
|
|
|
|
$ruleGroupRepos->shouldReceive('store');
|
|
|
|
$repository->shouldReceive('getFirstRuleGroup')->andReturn(new RuleGroup);
|
|
|
|
$ruleGroupRepos->shouldReceive('getRuleGroupsWithRules')->andReturn(new Collection);
|
|
|
|
|
|
|
|
$repository->shouldReceive('count')->andReturn(0);
|
|
|
|
$repository->shouldReceive('store');
|
|
|
|
|
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->get(route('rules.index'));
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertSee('<ol class="breadcrumb">');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleController::reorderRuleActions
|
|
|
|
*/
|
|
|
|
public function testReorderRuleActions()
|
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
|
|
|
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
|
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
$data = [
|
|
|
|
'triggers' => [1, 2, 3],
|
|
|
|
];
|
|
|
|
|
|
|
|
$repository = $this->mock(RuleRepositoryInterface::class);
|
|
|
|
$repository->shouldReceive('reorderRuleActions');
|
|
|
|
|
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->post(route('rules.reorder-actions', [1]), $data);
|
|
|
|
$response->assertStatus(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleController::reorderRuleTriggers
|
|
|
|
*/
|
|
|
|
public function testReorderRuleTriggers()
|
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
|
|
|
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
|
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
$data = [
|
|
|
|
'triggers' => [1, 2, 3],
|
|
|
|
];
|
|
|
|
|
|
|
|
$repository = $this->mock(RuleRepositoryInterface::class);
|
|
|
|
$repository->shouldReceive('reorderRuleTriggers');
|
|
|
|
|
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->post(route('rules.reorder-triggers', [1]), $data);
|
|
|
|
$response->assertStatus(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleController::store
|
|
|
|
*/
|
|
|
|
public function testStore()
|
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
|
|
|
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
|
|
|
|
$ruleGroupRepos->shouldReceive('find')->andReturn(new RuleGroup)->once();
|
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
$this->session(['rules.create.url' => 'http://localhost']);
|
|
|
|
$data = [
|
|
|
|
'rule_group_id' => 1,
|
|
|
|
'active' => 1,
|
|
|
|
'title' => 'A',
|
|
|
|
'trigger' => 'store-journal',
|
|
|
|
'description' => 'D',
|
|
|
|
'rule-trigger' => [
|
|
|
|
1 => 'from_account_starts',
|
|
|
|
],
|
|
|
|
'rule-trigger-value' => [
|
|
|
|
1 => 'B',
|
|
|
|
],
|
|
|
|
'rule-action' => [
|
|
|
|
1 => 'set_category',
|
|
|
|
],
|
|
|
|
'rule-action-value' => [
|
|
|
|
1 => 'C',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$repository = $this->mock(RuleRepositoryInterface::class);
|
|
|
|
$repository->shouldReceive('store')->andReturn(new Rule);
|
|
|
|
|
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->post(route('rules.store', [1]), $data);
|
|
|
|
$response->assertStatus(302);
|
|
|
|
$response->assertSessionHas('success');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This actually hits an error and not the actually code but OK.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleController::testTriggers
|
|
|
|
*/
|
|
|
|
public function testTestTriggers()
|
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
|
|
|
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
|
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->get(route('rules.test-triggers', [1]));
|
|
|
|
$response->assertStatus(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleController::up
|
|
|
|
*/
|
|
|
|
public function testUp()
|
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$repository = $this->mock(RuleRepositoryInterface::class);
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
|
|
|
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
|
|
|
|
$repository->shouldReceive('moveUp');
|
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->get(route('rules.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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\RuleController::update
|
|
|
|
*/
|
|
|
|
public function testUpdate()
|
|
|
|
{
|
2017-03-05 11:15:38 -06:00
|
|
|
// mock stuff
|
|
|
|
$ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
|
|
|
$journalRepos->shouldReceive('first')->once()->andReturn(new TransactionJournal);
|
|
|
|
$ruleGroupRepos->shouldReceive('find')->andReturn(new RuleGroup)->once();
|
|
|
|
|
2017-02-12 05:21:44 -06:00
|
|
|
$data = [
|
|
|
|
'rule_group_id' => 1,
|
|
|
|
'title' => 'Your first default rule',
|
|
|
|
'trigger' => 'store-journal',
|
|
|
|
'active' => 1,
|
|
|
|
'description' => 'This rule is an example. You can safely delete it.',
|
|
|
|
'rule-trigger' => [
|
|
|
|
1 => 'description_is',
|
|
|
|
],
|
|
|
|
'rule-trigger-value' => [
|
|
|
|
1 => 'something',
|
|
|
|
],
|
|
|
|
'rule-action' => [
|
|
|
|
1 => 'prepend_description',
|
|
|
|
],
|
|
|
|
'rule-action-value' => [
|
|
|
|
1 => 'Bla bla',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
$this->session(['rules.edit.url' => 'http://localhost']);
|
|
|
|
|
|
|
|
$repository = $this->mock(RuleRepositoryInterface::class);
|
|
|
|
$repository->shouldReceive('update');
|
|
|
|
|
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->post(route('rules.update', [1]), $data);
|
|
|
|
$response->assertStatus(302);
|
|
|
|
$response->assertSessionHas('success');
|
|
|
|
}
|
|
|
|
|
2017-02-16 15:33:32 -06:00
|
|
|
}
|