Files
firefly-iii/tests/Feature/Controllers/Transaction/LinkControllerTest.php

234 lines
7.9 KiB
PHP
Raw Normal View History

2017-12-23 17:42:07 +01:00
<?php
/**
* LinkControllerTest.php
* Copyright (c) 2017 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\Feature\Controllers\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionJournalLink;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
2019-07-20 06:47:34 +02:00
use Illuminate\Support\Collection;
2018-03-24 06:08:50 +01:00
use Log;
use Mockery;
2019-07-20 06:47:34 +02:00
use Preferences;
2017-12-23 17:42:07 +01:00
use Tests\TestCase;
/**
* Class LinkControllerTest
*/
class LinkControllerTest extends TestCase
{
2018-03-24 06:08:50 +01:00
/**
*
*/
2018-07-20 16:28:54 +02:00
public function setUp(): void
2018-03-24 06:08:50 +01:00
{
parent::setUp();
2019-04-09 20:05:20 +02:00
Log::info(sprintf('Now in %s.', get_class($this)));
2018-03-24 06:08:50 +01:00
}
2017-12-23 17:42:07 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\LinkController
2017-12-23 17:42:07 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testDelete(): void
2019-07-20 06:47:34 +02:00
{
$this->mock(LinkTypeRepositoryInterface::class);
$link = $this->getRandomLink();
$userRepos = $this->mock(UserRepositoryInterface::class);
$this->mockDefaultSession();
2019-04-09 20:05:20 +02:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
2019-07-20 06:47:34 +02:00
$this->be($this->user());
$response = $this->get(route('transactions.link.delete', [$link->id]));
$response->assertStatus(200);
}
2019-07-20 06:47:34 +02:00
/**
* @covers \FireflyIII\Http\Controllers\Transaction\LinkController
*/
public function testModal(): void
{
$journal = $this->getRandomWithdrawal();
$linkRepos = $this->mock(LinkTypeRepositoryInterface::class);
$this->mockDefaultSession();
$linkRepos->shouldReceive('get')->atLeast()->once()->andReturn(new Collection);
2018-02-28 15:50:00 +01:00
2017-12-23 17:42:07 +01:00
$this->be($this->user());
2019-07-20 06:47:34 +02:00
$response = $this->get(route('transactions.link.modal', [$journal->id]));
2017-12-23 17:42:07 +01:00
$response->assertStatus(200);
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\LinkController
2017-12-23 17:42:07 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testDestroy(): void
2019-07-20 06:47:34 +02:00
{
$link = $this->getRandomLink();
$repository = $this->mock(LinkTypeRepositoryInterface::class);
2019-07-20 06:47:34 +02:00
Preferences::shouldReceive('mark')->once();
$this->mockDefaultSession();
2018-02-28 15:50:00 +01:00
2019-07-20 06:47:34 +02:00
$repository->shouldReceive('destroyLink')->atLeast()->once();
2017-12-23 17:42:07 +01:00
$this->be($this->user());
$this->session(['journal_links.delete.uri' => 'http://localhost/']);
2019-07-20 06:47:34 +02:00
$response = $this->post(route('transactions.link.destroy', [$link->id]));
2017-12-23 17:42:07 +01:00
$response->assertStatus(302);
$response->assertSessionHas('success');
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\LinkController
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\JournalLinkRequest
2017-12-23 17:42:07 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testStore(): void
2019-07-20 06:47:34 +02:00
{
$withdrawal = $this->getRandomWithdrawal();
$deposit = $this->getRandomDeposit();
2017-12-23 17:42:07 +01:00
$repository = $this->mock(LinkTypeRepositoryInterface::class);
2019-07-20 06:47:34 +02:00
$journalRepos = $this->mockDefaultSession();
$data = [
2019-07-20 06:47:34 +02:00
'opposing' => $deposit->id,
'link_type' => '1_inward',
2017-12-23 17:42:07 +01:00
];
2019-07-20 06:47:34 +02:00
//$journalRepos->shouldReceive('firstNull')->andReturn(new TransactionJournal);
$journalRepos->shouldReceive('findNull')->andReturn($deposit)->atLeast()->once();
$repository->shouldReceive('findLink')->andReturn(false)->atLeast()->once();
$repository->shouldReceive('storeLink')->andReturn(new TransactionJournalLink)->atLeast()->once();
2017-12-23 17:42:07 +01:00
$this->be($this->user());
2019-07-20 06:47:34 +02:00
$response = $this->post(route('transactions.link.store', [$withdrawal->id]), $data);
2017-12-23 17:42:07 +01:00
$response->assertStatus(302);
2017-12-29 09:05:35 +01:00
$response->assertSessionHas('success');
2019-07-20 06:47:34 +02:00
$response->assertRedirect(route('transactions.show', [$withdrawal->id]));
2017-12-23 17:42:07 +01:00
}
2018-06-02 06:11:13 +02:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\LinkController
2018-06-02 06:11:13 +02:00
* @covers \FireflyIII\Http\Requests\JournalLinkRequest
*/
2018-08-06 19:14:30 +02:00
public function testStoreAlreadyLinked(): void
2019-07-20 06:47:34 +02:00
{
2018-06-02 06:11:13 +02:00
$repository = $this->mock(LinkTypeRepositoryInterface::class);
2019-07-20 06:47:34 +02:00
$journalRepos = $this->mockDefaultSession();
$link = $this->getRandomLink();
$data = [
2019-07-20 06:47:34 +02:00
'opposing' => $link->source_id,
'link_type' => '1_inward',
2018-06-02 06:11:13 +02:00
];
2019-07-20 06:47:34 +02:00
$journalRepos->shouldReceive('findNull')->andReturn(new TransactionJournal)->atLeast()->once();
$repository->shouldReceive('findLink')->andReturn(true)->atLeast()->once();
2018-06-02 06:11:13 +02:00
$this->be($this->user());
2019-07-20 06:47:34 +02:00
$response = $this->post(route('transactions.link.store', [$link->destination_id]), $data);
2018-06-02 06:11:13 +02:00
$response->assertStatus(302);
$response->assertSessionHas('error');
2019-07-20 06:47:34 +02:00
$response->assertRedirect(route('transactions.show', [$link->destination_id]));
2018-06-02 06:11:13 +02:00
}
2017-12-23 17:42:07 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\LinkController
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\JournalLinkRequest
2017-12-23 17:42:07 +01:00
*/
2018-08-06 19:14:30 +02:00
public function testStoreInvalid(): void
2019-07-20 06:47:34 +02:00
{
$this->mock(LinkTypeRepositoryInterface::class);
$journalRepos = $this->mockDefaultSession();
$withdrawal = $this->getRandomWithdrawal();
2019-04-09 20:05:20 +02:00
2018-08-06 19:14:30 +02:00
$data = [
2019-07-20 06:47:34 +02:00
'opposing' => 0,
'link_type' => '1_inward',
2017-12-23 17:42:07 +01:00
];
2019-07-20 06:47:34 +02:00
$journalRepos->shouldReceive('findNull')->andReturn(null)->atLeast()->once();
2017-12-23 17:42:07 +01:00
$this->be($this->user());
2019-07-20 06:47:34 +02:00
$response = $this->post(route('transactions.link.store', [$withdrawal->id]), $data);
2017-12-23 17:42:07 +01:00
$response->assertStatus(302);
2017-12-29 09:05:35 +01:00
$response->assertSessionHas('error');
2019-07-20 06:47:34 +02:00
$response->assertRedirect(route('transactions.show', [$withdrawal->id]));
2017-12-23 17:42:07 +01:00
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\LinkController
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\JournalLinkRequest
2017-12-23 17:42:07 +01:00
*/
2018-08-06 19:14:30 +02:00
public function testStoreSame(): void
2019-07-20 06:47:34 +02:00
{
$withdrawal = $this->getRandomWithdrawal();
2018-08-06 19:14:30 +02:00
$repository = $this->mock(LinkTypeRepositoryInterface::class);
2019-07-20 06:47:34 +02:00
$journalRepos = $this->mockDefaultSession();
$data = [
2019-07-20 06:47:34 +02:00
'link_other' => $withdrawal->id,
2017-12-23 17:42:07 +01:00
'link_type' => '1_inward',
];
2019-07-20 06:47:34 +02:00
$journalRepos->shouldReceive('findNull')->andReturn($withdrawal)->atLeast()->once();
$repository->shouldReceive('findLink')->andReturn(false)->atLeast()->once();
2018-02-28 15:50:00 +01:00
2017-12-23 17:42:07 +01:00
$this->be($this->user());
2019-07-20 06:47:34 +02:00
$response = $this->post(route('transactions.link.store', [$withdrawal->id]), $data);
2018-08-06 19:14:30 +02:00
2017-12-23 17:42:07 +01:00
$response->assertStatus(302);
$response->assertSessionHas('error');
2019-07-20 06:47:34 +02:00
$response->assertRedirect(route('transactions.show', [$withdrawal->id]));
2017-12-23 17:42:07 +01:00
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\LinkController
2017-12-23 17:42:07 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testSwitchLink(): void
2019-07-20 06:47:34 +02:00
{
$link = $this->getRandomLink();
$withdrawal = $this->getRandomWithdrawal();
2018-02-28 15:50:00 +01:00
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$userRepos = $this->mock(UserRepositoryInterface::class);
2019-07-23 17:33:23 +02:00
2017-12-23 17:42:07 +01:00
$repository->shouldReceive('switchLink')->andReturn(false);
$this->be($this->user());
2019-07-20 06:47:34 +02:00
$response = $this->get(route('transactions.link.switch', [$link->id]));
2017-12-23 17:42:07 +01:00
$response->assertStatus(302);
}
2018-03-04 15:14:29 +01:00
}