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

190 lines
7.9 KiB
PHP
Raw Normal View History

2017-02-12 18:40:39 +01:00
<?php
/**
* MassControllerTest.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
2017-02-12 18:40:39 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-10-21 08:40:00 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2017-02-12 18:40:39 +01:00
*/
declare(strict_types=1);
2017-02-12 18:40:39 +01:00
namespace Tests\Feature\Controllers\Transaction;
2019-06-29 19:47:31 +02:00
use Amount;
use FireflyIII\Events\UpdatedTransactionGroup;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\TransactionType;
2017-03-04 15:29:20 +01:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2017-03-09 08:19:05 +01:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\Services\Internal\Update\JournalUpdateService;
2017-03-04 15:29:20 +01:00
use Illuminate\Support\Collection;
2018-03-24 06:08:50 +01:00
use Log;
use Mockery;
use Preferences;
2017-02-12 18:40:39 +01:00
use Tests\TestCase;
2017-03-04 15:29:20 +01:00
/**
* Class MassControllerTest
*
2017-08-12 10:27:45 +02:00
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2017-03-04 15:29:20 +01:00
*/
2017-02-12 18:40:39 +01:00
class MassControllerTest extends TestCase
{
2018-03-24 06:08:50 +01:00
/**
*
*/
2018-09-02 20:27:26 +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-02-12 18:40:39 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\MassController
2017-02-12 18:40:39 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testDelete(): void
2017-02-12 18:40:39 +01:00
{
2019-04-09 20:05:20 +02:00
$this->mockDefaultSession();
$withdrawal = $this->getRandomWithdrawal();
$withdrawalArray = $this->getRandomWithdrawalAsArray();
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
$collector = $this->mock(GroupCollectorInterface::class);
$collector->shouldReceive('setTypes')
->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER]])->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('withBudgetInformation')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('withTagInformation')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('withAccountInformation')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('setJournalIds')->withArgs([[$withdrawal->id]])->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('getExtractedJournals')->atLeast()->once()->andReturn([$withdrawalArray]);
2018-02-28 15:50:00 +01:00
Amount::shouldReceive('formatAnything')->atLeast()->once()->andReturn('x');
2018-02-28 15:50:00 +01:00
2017-02-12 18:40:39 +01:00
$this->be($this->user());
$response = $this->get(route('transactions.mass.delete', [$withdrawal->id]));
2017-02-12 18:40:39 +01:00
$response->assertStatus(200);
$response->assertSee('Delete a number of transactions');
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\MassController
2017-02-12 18:40:39 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testDestroy(): void
2017-02-12 18:40:39 +01:00
{
$repository = $this->mockDefaultSession();
$deposit = $this->getRandomDeposit();
2018-02-28 15:50:00 +01:00
$repository->shouldReceive('findNull')->atLeast()->once()->andReturn($deposit);
$repository->shouldReceive('destroyJournal')->atLeast()->once();
Preferences::shouldReceive('mark')->atLeast()->once();
2017-03-04 15:29:20 +01:00
2017-03-18 20:53:44 +01:00
$this->session(['transactions.mass-delete.uri' => 'http://localhost']);
2017-03-04 15:29:20 +01:00
$data = ['confirm_mass_delete' => [$deposit->id],];
2017-02-12 18:40:39 +01:00
$this->be($this->user());
$response = $this->post(route('transactions.mass.destroy'), $data);
$response->assertSessionHas('success');
$response->assertStatus(302);
2017-03-04 15:29:20 +01:00
}
2017-02-12 18:40:39 +01:00
2017-03-04 15:29:20 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\MassController
2017-03-04 15:29:20 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testEdit(): void
2017-03-04 15:29:20 +01:00
{
$withdrawal = $this->getRandomWithdrawal();
$withdrawalArray = $this->getRandomWithdrawalAsArray();
$asset = $this->getRandomAsset();
$budget = $this->getRandomBudget();
$journalRepos = $this->mockDefaultSession();
2018-12-17 07:09:44 +01:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$repository = $this->mock(AccountRepositoryInterface::class);
2018-12-17 07:09:44 +01:00
$budgetRepos = $this->mock(BudgetRepositoryInterface::class);
$collector = $this->mock(GroupCollectorInterface::class);
$collector->shouldReceive('setTypes')
->withArgs([[TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::TRANSFER]])->atLeast()->once()->andReturnSelf();
2018-12-17 07:09:44 +01:00
$collector->shouldReceive('withCategoryInformation')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('withBudgetInformation')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('withTagInformation')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('withAccountInformation')->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('setJournalIds')->withArgs([[$withdrawal->id]])->atLeast()->once()->andReturnSelf();
$collector->shouldReceive('getExtractedJournals')->atLeast()->once()->andReturn([$withdrawalArray]);
$repository->shouldReceive('getAccountsByType')->atLeast()->once()->andReturn(new Collection([$asset]));
$budgetRepos->shouldReceive('getBudgets')->atLeast()->once()->andReturn(new Collection([$budget]));
2018-02-28 15:50:00 +01:00
2018-12-17 07:09:44 +01:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
2017-03-04 15:29:20 +01:00
$this->be($this->user());
$response = $this->get(route('transactions.mass.edit', [$withdrawal->id]));
2017-03-04 15:29:20 +01:00
$response->assertStatus(200);
$response->assertSee('Edit a number of transactions');
// has bread crumb
$response->assertSee('<ol class="breadcrumb">');
}
2017-02-12 18:40:39 +01:00
/**
2018-08-09 20:17:15 +02:00
* @covers \FireflyIII\Http\Controllers\Transaction\MassController
2017-02-12 18:40:39 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testUpdate(): void
2017-02-12 18:40:39 +01:00
{
$deposit = $this->getRandomDeposit();
$repository = $this->mockDefaultSession();
$userRepos = $this->mock(UserRepositoryInterface::class);
$updateService = $this->mock(JournalUpdateService::class);
2019-04-09 20:05:20 +02:00
$this->expectsEvents(UpdatedTransactionGroup::class);
2018-02-28 15:50:00 +01:00
$updateService->shouldReceive('setTransactionJournal')->atLeast()->once();
$updateService->shouldReceive('setData')->atLeast()->once();
$updateService->shouldReceive('update')->atLeast()->once();
Preferences::shouldReceive('mark')->atLeast()->once();
$repository->shouldReceive('findNull')->atLeast()->once()->andReturn($deposit);
2017-03-04 15:29:20 +01:00
2017-03-18 20:53:44 +01:00
$this->session(['transactions.mass-edit.uri' => 'http://localhost']);
2017-02-12 18:40:39 +01:00
$data = [
2019-07-25 14:19:49 +02:00
'journals' => [$deposit->id],
'description' => [$deposit->id => 'Updated salary thing'],
'amount' => [$deposit->id => 1600],
'date' => [$deposit->id => '2014-07-24'],
'source_name' => [$deposit->id => 'Job'],
'destination_id' => [$deposit->id => 1],
'category' => [$deposit->id => 'Salary'],
2017-02-12 18:40:39 +01:00
];
$this->be($this->user());
$response = $this->post(route('transactions.mass.update', [$deposit->id]), $data);
$response->assertSessionHas('success');
$response->assertStatus(302);
}
2017-02-16 22:33:32 +01:00
}