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

331 lines
14 KiB
PHP
Raw Normal View History

2017-11-26 09:54:09 +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
2017-12-17 14:42:21 +01:00
* along with Firefly III. If not, see <http://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 Tests\TestCase;
2017-11-26 09:54:09 +01:00
/**
* Class LinkControllerTest
*/
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();
2018-09-03 18:52:46 +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);
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:
LinkType::create(['editable' => 1, 'inward' => 'hello', 'outward' => 'bye', 'name' => 'Test type']);
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();
2018-02-28 15:50:00 +01:00
$repository->shouldReceive('get')->once()->andReturn(new Collection([$linkType]));
$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);
}
/**
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
{
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);
$linkType = LinkType::where('editable', 0)->first();
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();
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);
$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::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);
$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', 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);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
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
{
2018-09-03 18:52:46 +02:00
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true)->atLeast()->once();
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();
$data = [
'name' => 'test ' . random_int(1, 10000),
'inward' => 'test inward' . random_int(1, 10000),
'outward' => 'test outward' . random_int(1, 10000),
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();
$data = [
'name' => 'test ' . random_int(1, 10000),
'inward' => 'test inward' . random_int(1, 10000),
'outward' => 'test outward' . random_int(1, 10000),
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
$data = [
'name' => 'test ' . random_int(1, 10000),
'inward' => 'test inward' . random_int(1, 10000),
'outward' => 'test outward' . random_int(1, 10000),
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
{
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();
$linkType = LinkType::where('editable', 0)->first();
2017-11-26 09:54:09 +01:00
$data = [
'name' => 'test ' . random_int(1, 10000),
'inward' => 'test inward' . random_int(1, 10000),
'outward' => 'test outward' . random_int(1, 10000),
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']);
$data = [
'name' => 'test ' . random_int(1, 10000),
'inward' => 'test inward' . random_int(1, 10000),
'outward' => 'test outward' . random_int(1, 10000),
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');
}
}