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

386 lines
16 KiB
PHP
Raw Normal View History

2017-11-26 09:54:09 +01:00
<?php
/**
* LinkControllerTest.php
2020-02-16 13:59:55 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2017-11-26 09:54:09 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-11-26 09:54:09 +01: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-11-26 09:54:09 +01:00
*
* This program is distributed in the hope that it will be useful,
2017-11-26 09:54:09 +01: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-11-26 09:54:09 +01: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-11-26 09:54:09 +01:00
*/
declare(strict_types=1);
namespace Tests\Feature\Controllers\Admin;
2017-11-26 09:54:09 +01:00
use FireflyIII\Models\LinkType;
2018-02-28 15:50:00 +01:00
use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface;
2018-09-03 18:52:46 +02:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2018-02-28 15:50:00 +01:00
use Illuminate\Support\Collection;
2018-02-28 20:18:47 +01:00
use Log;
2018-09-03 18:52:46 +02:00
use Mockery;
use Preferences;
2019-07-24 19:02:41 +02:00
use Tests\TestCase;
2017-11-26 09:54:09 +01:00
/**
* Class LinkControllerTest
2019-07-23 17:33:23 +02:00
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2017-11-26 09:54:09 +01:00
*/
class LinkControllerTest extends TestCase
{
2018-02-28 20:18:47 +01:00
/**
*
*/
2018-09-02 20:27:26 +02:00
public function setUp(): void
2018-02-28 20:18:47 +01:00
{
parent::setUp();
2019-04-09 20:05:20 +02:00
Log::info(sprintf('Now in %s.', get_class($this)));
2018-02-28 20:18:47 +01:00
}
2017-11-26 09:54:09 +01:00
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Admin\LinkController
2017-11-26 09:54:09 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testCreate(): void
2017-11-26 09:54:09 +01:00
{
2018-09-03 18:52:46 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
2019-07-23 17:33:23 +02:00
$this->mock(LinkTypeRepositoryInterface::class);
// mock default session stuff
$this->mockDefaultSession();
2017-11-26 09:54:09 +01:00
2018-09-03 18:52:46 +02:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(false)->atLeast()->once();
2017-11-26 09:54:09 +01:00
$this->be($this->user());
$response = $this->get(route('admin.links.create'));
$response->assertStatus(200);
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Admin\LinkController
2017-11-26 09:54:09 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testDeleteEditable(): void
2017-11-26 09:54:09 +01:00
{
2018-09-03 18:52:46 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
2018-02-28 15:50:00 +01:00
$repository = $this->mock(LinkTypeRepositoryInterface::class);
2017-11-26 09:54:09 +01:00
// create editable link type just in case:
$newType = LinkType::create(['editable' => 1, 'inward' => 'hello', 'outward' => 'bye', 'name' => 'Test type']);
// mock default session stuff
$this->mockDefaultSession();
2017-11-26 09:54:09 +01:00
2018-09-03 18:52:46 +02:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(false)->atLeast()->once();
2017-11-26 09:54:09 +01:00
$linkType = LinkType::where('editable', 1)->first();
2019-07-24 19:02:41 +02:00
$another = LinkType::where('editable', 0)->first();
$repository->shouldReceive('get')->once()->andReturn(new Collection([$linkType, $another]));
2018-02-28 15:50:00 +01:00
$repository->shouldReceive('countJournals')->andReturn(2);
2017-11-26 09:54:09 +01:00
$this->be($this->user());
$response = $this->get(route('admin.links.delete', [$linkType->id]));
$response->assertStatus(200);
$newType->forceDelete();
2017-11-26 09:54:09 +01:00
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Admin\LinkController
2017-11-26 09:54:09 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testDeleteNonEditable(): void
2017-11-26 09:54:09 +01:00
{
2019-07-24 19:02:41 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
2019-07-23 17:33:23 +02:00
$this->mock(LinkTypeRepositoryInterface::class);
2019-07-24 19:02:41 +02:00
$linkType = LinkType::where('editable', 0)->first();
2018-09-03 18:52:46 +02:00
// mock default session stuff
$this->mockDefaultSession();
2018-09-03 18:52:46 +02:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(false)->atLeast()->once();
2017-11-26 09:54:09 +01:00
$this->be($this->user());
$response = $this->get(route('admin.links.delete', [$linkType->id]));
$response->assertStatus(302);
$response->assertSessionHas('error');
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Admin\LinkController
2017-11-26 09:54:09 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testDestroy(): void
2017-11-26 09:54:09 +01:00
{
2018-09-03 18:52:46 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
2018-02-28 15:50:00 +01:00
$repository = $this->mock(LinkTypeRepositoryInterface::class);
2018-09-03 18:52:46 +02:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(false)->atLeast()->once();
// mock default session stuff
$this->mockDefaultSession();
Preferences::shouldReceive('mark')->atLeast()->once();
2017-11-26 09:54:09 +01:00
// create editable link type just in case:
LinkType::create(['editable' => 1, 'inward' => 'hellox', 'outward' => 'byex', 'name' => 'Test typeX']);
$linkType = LinkType::where('editable', 1)->first();
2018-07-01 09:27:22 +02:00
$repository->shouldReceive('findNull')->andReturn($linkType);
2018-02-28 15:50:00 +01:00
$repository->shouldReceive('destroy');
2017-11-26 09:54:09 +01:00
$this->be($this->user());
$this->session(['link_types.delete.uri' => 'http://localhost']);
$response = $this->post(route('admin.links.destroy', [$linkType->id]));
$response->assertStatus(302);
$response->assertSessionHas('success');
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Admin\LinkController
2017-11-26 09:54:09 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testEditEditable(): void
2017-11-26 09:54:09 +01:00
{
2018-09-03 18:52:46 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
2019-07-23 17:33:23 +02:00
$this->mock(LinkTypeRepositoryInterface::class);
2018-09-03 18:52:46 +02:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(false)->atLeast()->once();
// mock default session stuff
$this->mockDefaultSession();
2017-11-26 09:54:09 +01:00
// create editable link type just in case:
LinkType::create(['editable' => 1, 'inward' => 'hello Y', 'outward' => 'bye Y', 'name' => 'Test type Y']);
$linkType = LinkType::where('editable', 1)->first();
$this->be($this->user());
$response = $this->get(route('admin.links.edit', [$linkType->id]));
$response->assertStatus(200);
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Admin\LinkController
2017-11-26 09:54:09 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testEditNonEditable(): void
2017-11-26 09:54:09 +01:00
{
2018-09-03 18:52:46 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
2019-07-23 17:33:23 +02:00
$this->mock(LinkTypeRepositoryInterface::class);
2018-09-03 18:52:46 +02:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(false)->atLeast()->once();
// mock default session stuff
$this->mockDefaultSession();
2017-11-26 09:54:09 +01:00
$linkType = LinkType::where('editable', 0)->first();
$this->be($this->user());
$response = $this->get(route('admin.links.edit', [$linkType->id]));
$response->assertStatus(302);
$response->assertSessionHas('error');
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Admin\LinkController
2017-11-26 09:54:09 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testIndex(): void
2017-11-26 09:54:09 +01:00
{
2018-09-03 18:52:46 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
2019-07-23 17:33:23 +02:00
$this->mock(LinkTypeRepositoryInterface::class);
2018-09-03 18:52:46 +02:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
// mock default session stuff
$this->mockDefaultSession();
//Preferences::shouldReceive('mark')->atLeast()->once();
2018-09-03 18:52:46 +02:00
2018-03-24 06:08:50 +01:00
$linkTypes = LinkType::inRandomOrder()->take(3)->get();
2018-02-28 15:50:00 +01:00
$repository = $this->mock(LinkTypeRepositoryInterface::class);
2018-03-03 14:24:06 +01:00
$repository->shouldReceive('get')->andReturn($linkTypes);
$repository->shouldReceive('countJournals')->andReturn(3);
2017-11-26 09:54:09 +01:00
$this->be($this->user());
$response = $this->get(route('admin.links.index'));
$response->assertStatus(200);
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Admin\LinkController
2017-11-26 09:54:09 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testShow(): void
2017-11-26 09:54:09 +01:00
{
2019-07-24 19:02:41 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$repository = $this->mock(LinkTypeRepositoryInterface::class);
2018-09-03 18:52:46 +02:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$repository->shouldReceive('getJournalLinks')->andReturn(new Collection);
$this->mockDefaultSession();
2018-09-03 18:52:46 +02:00
2017-11-26 09:54:09 +01:00
$linkType = LinkType::first();
$this->be($this->user());
$response = $this->get(route('admin.links.show', [$linkType->id]));
$response->assertStatus(200);
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Admin\LinkController
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\LinkTypeFormRequest
2017-11-26 09:54:09 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testStore(): void
2017-11-26 09:54:09 +01:00
{
2018-09-03 18:52:46 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
2018-02-28 15:50:00 +01:00
$repository = $this->mock(LinkTypeRepositoryInterface::class);
2018-09-03 18:52:46 +02:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(false)->atLeast()->once();
// mock default session stuff
$this->mockDefaultSession();
2018-09-03 18:52:46 +02:00
$data = [
'name' => sprintf('test %d', $this->randomInt()),
'inward' => sprintf('test inward %d', $this->randomInt()),
'outward' => sprintf('test outward %d', $this->randomInt()),
2017-11-26 09:54:09 +01:00
];
2018-02-28 15:50:00 +01:00
$repository->shouldReceive('store')->once()->andReturn(LinkType::first());
2018-07-01 09:27:22 +02:00
$repository->shouldReceive('findNull')->andReturn(LinkType::first());
2018-02-28 15:50:00 +01:00
2017-11-26 09:54:09 +01:00
$this->session(['link_types.create.uri' => 'http://localhost']);
$this->be($this->user());
$response = $this->post(route('admin.links.store'), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Admin\LinkController
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\LinkTypeFormRequest
2017-11-26 09:54:09 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testStoreRedirect(): void
2017-11-26 09:54:09 +01:00
{
2018-09-03 18:52:46 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
2018-02-28 15:50:00 +01:00
$repository = $this->mock(LinkTypeRepositoryInterface::class);
2018-09-03 18:52:46 +02:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(false)->atLeast()->once();
// mock default session stuff
$this->mockDefaultSession();
2018-09-03 18:52:46 +02:00
$data = [
'name' => sprintf('test %d', $this->randomInt()),
'inward' => sprintf('test inward %d', $this->randomInt()),
'outward' => sprintf('test outward %d', $this->randomInt()),
2017-11-26 09:54:09 +01:00
'create_another' => '1',
];
2018-02-28 15:50:00 +01:00
$repository->shouldReceive('store')->once()->andReturn(new LinkType);
2017-11-26 09:54:09 +01:00
$this->session(['link_types.create.uri' => 'http://localhost']);
$this->be($this->user());
$response = $this->post(route('admin.links.store'), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Admin\LinkController
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\LinkTypeFormRequest
2017-11-26 09:54:09 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testUpdate(): void
2017-11-26 09:54:09 +01:00
{
2018-09-03 18:52:46 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
2018-02-28 15:50:00 +01:00
$repository = $this->mock(LinkTypeRepositoryInterface::class);
2017-11-26 09:54:09 +01:00
2018-09-03 18:52:46 +02:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(false)->atLeast()->once();
2018-02-28 15:50:00 +01:00
// create editable link type just in case:
$linkType = LinkType::create(['editable' => 1, 'inward' => 'helloxz', 'outward' => 'bzyex', 'name' => 'Test tyzpeX']);
$repository->shouldReceive('update')->once()->andReturn(new $linkType);
2017-11-26 09:54:09 +01:00
// mock default session stuff
$this->mockDefaultSession();
Preferences::shouldReceive('mark')->atLeast()->once();
2017-11-26 09:54:09 +01:00
$data = [
'name' => sprintf('test %d', $this->randomInt()),
'inward' => sprintf('test inward %d', $this->randomInt()),
'outward' => sprintf('test outward %d', $this->randomInt()),
2017-11-26 09:54:09 +01:00
];
$this->session(['link_types.edit.uri' => 'http://localhost']);
$this->be($this->user());
2018-02-28 15:50:00 +01:00
$response = $this->post(route('admin.links.update', [$linkType->id]), $data);
2017-11-26 09:54:09 +01:00
$response->assertStatus(302);
$response->assertSessionHas('success');
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Admin\LinkController
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\LinkTypeFormRequest
2017-11-26 09:54:09 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testUpdateNonEditable(): void
2017-11-26 09:54:09 +01:00
{
2019-07-24 19:02:41 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
2019-07-23 17:33:23 +02:00
$this->mock(LinkTypeRepositoryInterface::class);
2018-09-03 18:52:46 +02:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(false)->atLeast()->once();
// mock default session stuff
$this->mockDefaultSession();
2018-09-03 18:52:46 +02:00
$linkType = LinkType::where('editable', 0)->first();
2017-11-26 09:54:09 +01:00
$data = [
'name' => sprintf('test %d', $this->randomInt()),
'inward' => sprintf('test inward %d', $this->randomInt()),
'outward' => sprintf('test outward %d', $this->randomInt()),
2017-11-26 09:54:09 +01:00
'return_to_edit' => '1',
];
$this->session(['link_types.edit.uri' => 'http://localhost']);
$this->be($this->user());
$response = $this->post(route('admin.links.update', [$linkType->id]), $data);
$response->assertStatus(302);
$response->assertSessionHas('error');
}
/**
2018-07-01 09:27:22 +02:00
* @covers \FireflyIII\Http\Controllers\Admin\LinkController
2018-03-03 17:16:47 +01:00
* @covers \FireflyIII\Http\Requests\LinkTypeFormRequest
2017-11-26 09:54:09 +01:00
*/
2018-05-11 19:58:10 +02:00
public function testUpdateRedirect(): void
2017-11-26 09:54:09 +01:00
{
2018-09-03 18:52:46 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
2018-02-28 15:50:00 +01:00
$repository = $this->mock(LinkTypeRepositoryInterface::class);
2018-09-03 18:52:46 +02:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'demo'])->andReturn(false)->atLeast()->once();
2017-11-26 09:54:09 +01:00
// create editable link type just in case:
$linkType = LinkType::create(['editable' => 1, 'inward' => 'healox', 'outward' => 'byaex', 'name' => 'Test tyapeX']);
// mock default session stuff
$this->mockDefaultSession();
Preferences::shouldReceive('mark')->atLeast()->once();
2017-11-26 09:54:09 +01:00
$data = [
'name' => sprintf('test %d', $this->randomInt()),
'inward' => sprintf('test inward %d', $this->randomInt()),
'outward' => sprintf('test outward %d', $this->randomInt()),
2017-11-26 09:54:09 +01:00
'return_to_edit' => '1',
];
2018-02-28 15:50:00 +01:00
$repository->shouldReceive('update')->once()->andReturn(new $linkType);
2017-11-26 09:54:09 +01:00
$this->session(['link_types.edit.uri' => 'http://localhost']);
$this->be($this->user());
$response = $this->post(route('admin.links.update', [$linkType->id]), $data);
$response->assertStatus(302);
$response->assertSessionHas('success');
}
}