2017-12-23 17:42:07 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PrerequisitesControllerTest.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\Import;
|
|
|
|
|
2018-05-04 20:21:27 +02:00
|
|
|
use FireflyIII\Import\Prerequisites\FakePrerequisites;
|
|
|
|
use FireflyIII\Models\ImportJob;
|
|
|
|
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
2017-12-23 17:42:07 +01:00
|
|
|
use Illuminate\Support\MessageBag;
|
2018-03-24 06:08:50 +01:00
|
|
|
use Log;
|
2018-05-04 20:21:27 +02:00
|
|
|
use Mockery;
|
2017-12-23 17:42:07 +01:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AccountControllerTest
|
|
|
|
*
|
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
|
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
|
|
|
*/
|
|
|
|
class PrerequisitesControllerTest extends TestCase
|
|
|
|
{
|
2018-03-24 06:08:50 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2018-04-28 10:27:33 +02:00
|
|
|
Log::debug(sprintf('Now in %s.', \get_class($this)));
|
2018-03-24 06:08:50 +01:00
|
|
|
}
|
|
|
|
|
2017-12-23 17:42:07 +01:00
|
|
|
/**
|
2018-05-04 20:21:27 +02:00
|
|
|
* @covers \FireflyIII\Http\Controllers\Import\PrerequisitesController
|
2017-12-23 17:42:07 +01:00
|
|
|
*/
|
2018-05-04 20:21:27 +02:00
|
|
|
public function testIndex(): void
|
2017-12-23 17:42:07 +01:00
|
|
|
{
|
2018-05-04 20:21:27 +02:00
|
|
|
$job = new ImportJob;
|
|
|
|
$job->user_id = $this->user()->id;
|
2018-07-14 16:41:07 +02:00
|
|
|
$job->key = 'A_pre_job_' . random_int(1, 10000);
|
2018-05-04 20:21:27 +02:00
|
|
|
$job->status = 'new';
|
|
|
|
$job->provider = 'fake';
|
|
|
|
$job->transactions = [];
|
|
|
|
$job->file_type = '';
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
// mock stuff
|
|
|
|
$prereq = $this->mock(FakePrerequisites::class);
|
|
|
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
|
|
|
|
|
|
|
$prereq->shouldReceive('setUser')->once();
|
|
|
|
$prereq->shouldReceive('isComplete')->once()->andReturn(false);
|
|
|
|
$prereq->shouldReceive('getView')->once()->andReturn('import.fake.prerequisites');
|
|
|
|
$prereq->shouldReceive('getViewParameters')->once()->andReturn(['api_key' => '']);
|
2017-12-23 17:42:07 +01:00
|
|
|
|
2018-05-04 20:21:27 +02:00
|
|
|
|
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->get(route('import.prerequisites.index', ['fake', $job->key]));
|
2017-12-23 17:42:07 +01:00
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-04 20:21:27 +02:00
|
|
|
* @covers \FireflyIII\Http\Controllers\Import\PrerequisitesController
|
2017-12-23 17:42:07 +01:00
|
|
|
*/
|
2018-05-04 20:21:27 +02:00
|
|
|
public function testIndexBadState(): void
|
2017-12-23 17:42:07 +01:00
|
|
|
{
|
2018-05-04 20:21:27 +02:00
|
|
|
$job = new ImportJob;
|
|
|
|
$job->user_id = $this->user()->id;
|
2018-07-14 16:41:07 +02:00
|
|
|
$job->key = 'B_pre_job_' . random_int(1, 10000);
|
2018-05-04 20:21:27 +02:00
|
|
|
$job->status = 'some_Bad_state';
|
|
|
|
$job->provider = 'fake';
|
|
|
|
$job->transactions = [];
|
|
|
|
$job->file_type = '';
|
|
|
|
$job->save();
|
|
|
|
|
2017-12-23 17:42:07 +01:00
|
|
|
$this->be($this->user());
|
2018-05-04 20:21:27 +02:00
|
|
|
$response = $this->get(route('import.prerequisites.index', ['fake', $job->key]));
|
|
|
|
$response->assertStatus(302);
|
|
|
|
$response->assertRedirect(route('import.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\Import\PrerequisitesController
|
|
|
|
*/
|
|
|
|
public function testIndexComplete(): void
|
|
|
|
{
|
|
|
|
$job = new ImportJob;
|
|
|
|
$job->user_id = $this->user()->id;
|
2018-07-14 16:41:07 +02:00
|
|
|
$job->key = 'C_pre_job_' . random_int(1, 10000);
|
2018-05-04 20:21:27 +02:00
|
|
|
$job->status = 'new';
|
|
|
|
$job->provider = 'fake';
|
|
|
|
$job->transactions = [];
|
|
|
|
$job->file_type = '';
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
// mock stuff
|
|
|
|
$prereq = $this->mock(FakePrerequisites::class);
|
|
|
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
|
|
|
|
|
|
|
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'has_prereq']);
|
|
|
|
$prereq->shouldReceive('setUser')->once();
|
|
|
|
$prereq->shouldReceive('isComplete')->once()->andReturn(true);
|
2017-12-23 17:42:07 +01:00
|
|
|
|
2018-05-04 20:21:27 +02:00
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->get(route('import.prerequisites.index', ['fake', $job->key]));
|
2017-12-23 17:42:07 +01:00
|
|
|
$response->assertStatus(302);
|
2018-05-04 20:21:27 +02:00
|
|
|
$response->assertRedirect(route('import.job.configuration.index', [$job->key]));
|
2017-12-23 17:42:07 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-04 20:21:27 +02:00
|
|
|
* Redirects to configuration.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Http\Controllers\Import\PrerequisitesController
|
2017-12-23 17:42:07 +01:00
|
|
|
*/
|
2018-05-04 20:21:27 +02:00
|
|
|
public function testPost(): void
|
2017-12-23 17:42:07 +01:00
|
|
|
{
|
2018-05-04 20:21:27 +02:00
|
|
|
$job = new ImportJob;
|
|
|
|
$job->user_id = $this->user()->id;
|
2018-07-14 16:41:07 +02:00
|
|
|
$job->key = 'D_pre_job_' . random_int(1, 10000);
|
2018-05-04 20:21:27 +02:00
|
|
|
$job->status = 'new';
|
|
|
|
$job->provider = 'fake';
|
|
|
|
$job->transactions = [];
|
|
|
|
$job->file_type = '';
|
|
|
|
$job->save();
|
2017-12-23 17:42:07 +01:00
|
|
|
|
2018-05-04 20:21:27 +02:00
|
|
|
// mock stuff
|
|
|
|
$prereq = $this->mock(FakePrerequisites::class);
|
|
|
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
|
|
|
|
|
|
|
$prereq->shouldReceive('setUser')->once();
|
|
|
|
$prereq->shouldReceive('storePrerequisites')->once()->andReturn(new MessageBag);
|
|
|
|
$repository->shouldReceive('setStatus')->once()->withArgs([Mockery::any(), 'has_prereq']);
|
2017-12-23 17:42:07 +01:00
|
|
|
|
2018-05-04 20:21:27 +02:00
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->post(route('import.prerequisites.post', ['fake', $job->key]));
|
|
|
|
$response->assertStatus(302);
|
|
|
|
$response->assertRedirect(route('import.job.configuration.index', [$job->key]));
|
2017-12-23 17:42:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-04 20:21:27 +02:00
|
|
|
* Bad state gives errors.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Http\Controllers\Import\PrerequisitesController
|
2017-12-23 17:42:07 +01:00
|
|
|
*/
|
2018-05-04 20:21:27 +02:00
|
|
|
public function testPostBadState(): void
|
2017-12-23 17:42:07 +01:00
|
|
|
{
|
2018-05-04 20:21:27 +02:00
|
|
|
$job = new ImportJob;
|
|
|
|
$job->user_id = $this->user()->id;
|
2018-07-14 16:41:07 +02:00
|
|
|
$job->key = 'D_pre_job_' . random_int(1, 10000);
|
2018-05-04 20:21:27 +02:00
|
|
|
$job->status = 'badstate';
|
|
|
|
$job->provider = 'fake';
|
|
|
|
$job->transactions = [];
|
|
|
|
$job->file_type = '';
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
// mock stuff
|
|
|
|
$prereq = $this->mock(FakePrerequisites::class);
|
|
|
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
|
|
|
|
2017-12-23 17:42:07 +01:00
|
|
|
$this->be($this->user());
|
2018-05-04 20:21:27 +02:00
|
|
|
$response = $this->post(route('import.prerequisites.post', ['fake', $job->key]));
|
|
|
|
$response->assertStatus(302);
|
|
|
|
$response->assertRedirect(route('import.index'));
|
|
|
|
$response->assertSessionHas('error', 'To access this page, your import job cannot have status "badstate".');
|
|
|
|
}
|
2017-12-23 17:42:07 +01:00
|
|
|
|
2018-05-04 20:21:27 +02:00
|
|
|
/**
|
|
|
|
* Redirects to index.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Http\Controllers\Import\PrerequisitesController
|
|
|
|
*/
|
|
|
|
public function testPostNoJob(): void
|
|
|
|
{
|
|
|
|
// mock stuff
|
|
|
|
$prereq = $this->mock(FakePrerequisites::class);
|
|
|
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
|
|
|
|
|
|
|
$prereq->shouldReceive('setUser')->once();
|
|
|
|
$prereq->shouldReceive('storePrerequisites')->once()->andReturn(new MessageBag);
|
|
|
|
|
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->post(route('import.prerequisites.post', ['fake']));
|
2017-12-23 17:42:07 +01:00
|
|
|
$response->assertStatus(302);
|
2018-05-04 20:21:27 +02:00
|
|
|
$response->assertRedirect(route('import.index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Error messages? Redirect back
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Http\Controllers\Import\PrerequisitesController
|
|
|
|
*/
|
|
|
|
public function testPostWithMessages(): void
|
|
|
|
{
|
|
|
|
$job = new ImportJob;
|
|
|
|
$job->user_id = $this->user()->id;
|
2018-07-14 16:41:07 +02:00
|
|
|
$job->key = 'D_pre_job_' . random_int(1, 10000);
|
2018-05-04 20:21:27 +02:00
|
|
|
$job->status = 'new';
|
|
|
|
$job->provider = 'fake';
|
|
|
|
$job->transactions = [];
|
|
|
|
$job->file_type = '';
|
|
|
|
$job->save();
|
2017-12-23 17:42:07 +01:00
|
|
|
|
2018-05-04 20:21:27 +02:00
|
|
|
$messages = new MessageBag;
|
|
|
|
$messages->add('some', 'message');
|
|
|
|
|
|
|
|
// mock stuff
|
|
|
|
$prereq = $this->mock(FakePrerequisites::class);
|
|
|
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
|
|
|
|
|
|
|
$prereq->shouldReceive('setUser')->once();
|
|
|
|
$prereq->shouldReceive('storePrerequisites')->once()->andReturn($messages);
|
|
|
|
|
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->post(route('import.prerequisites.post', ['fake', $job->key]));
|
|
|
|
$response->assertStatus(302);
|
|
|
|
$response->assertRedirect(route('import.prerequisites.index', ['fake', $job->key]));
|
|
|
|
$response->assertSessionHas('error', 'message');
|
2017-12-23 17:42:07 +01:00
|
|
|
}
|
|
|
|
}
|