2018-05-04 20:21:27 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* JobConfigurationControllerTest.php
|
|
|
|
* Copyright (c) 2018 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;
|
|
|
|
|
|
|
|
use FireflyIII\Import\JobConfiguration\FakeJobConfiguration;
|
|
|
|
use FireflyIII\Models\ImportJob;
|
|
|
|
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
|
2018-09-04 09:52:19 +02:00
|
|
|
use FireflyIII\Repositories\User\UserRepositoryInterface;
|
2018-05-10 20:05:02 +02:00
|
|
|
use Illuminate\Http\UploadedFile;
|
2018-05-04 20:21:27 +02:00
|
|
|
use Illuminate\Support\MessageBag;
|
|
|
|
use Log;
|
|
|
|
use Mockery;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class JobConfigurationControllerTest
|
|
|
|
*/
|
|
|
|
class JobConfigurationControllerTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2018-09-02 20:27:26 +02:00
|
|
|
public function setUp(): void
|
2018-05-04 20:21:27 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2019-04-09 20:05:20 +02:00
|
|
|
Log::info(sprintf('Now in %s.', get_class($this)));
|
2018-05-04 20:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\Import\JobConfigurationController
|
|
|
|
*/
|
|
|
|
public function testIndex(): void
|
|
|
|
{
|
|
|
|
$job = new ImportJob;
|
|
|
|
$job->user_id = $this->user()->id;
|
2018-07-14 16:41:07 +02:00
|
|
|
$job->key = '1Afake_job_' . random_int(1, 10000);
|
2018-05-04 20:21:27 +02:00
|
|
|
$job->status = 'has_prereq';
|
|
|
|
$job->provider = 'fake';
|
|
|
|
$job->file_type = '';
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
// mock repositories and configuration handling classes:
|
|
|
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
|
|
|
$configurator = $this->mock(FakeJobConfiguration::class);
|
2018-09-04 09:52:19 +02:00
|
|
|
$userRepos = $this->mock(UserRepositoryInterface::class);
|
|
|
|
|
|
|
|
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
|
2018-05-04 20:21:27 +02:00
|
|
|
|
|
|
|
// mock calls:
|
2018-05-12 15:50:01 +02:00
|
|
|
$configurator->shouldReceive('setImportJob')->once();
|
2018-05-04 20:21:27 +02:00
|
|
|
$configurator->shouldReceive('configurationComplete')->once()->andReturn(false);
|
|
|
|
$configurator->shouldReceive('getNextView')->once()->andReturn('import.fake.apply-rules');
|
|
|
|
$configurator->shouldReceive('getNextData')->once()
|
|
|
|
->andReturn(['rulesOptions' => [1 => 'Y', 0 => 'N',],]);
|
|
|
|
|
|
|
|
|
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->get(route('import.job.configuration.index', [$job->key]));
|
|
|
|
$response->assertStatus(200);
|
|
|
|
// expect a redirect to prerequisites
|
|
|
|
$response->assertSee('<ol class="breadcrumb">');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\Import\JobConfigurationController
|
|
|
|
*/
|
|
|
|
public function testIndexBadState(): void
|
|
|
|
{
|
|
|
|
$job = new ImportJob;
|
|
|
|
$job->user_id = $this->user()->id;
|
2018-07-14 16:41:07 +02:00
|
|
|
$job->key = '2Bfake_job_' . random_int(1, 10000);
|
2018-05-04 20:21:27 +02:00
|
|
|
$job->status = 'some_bad_state';
|
|
|
|
$job->provider = 'fake';
|
|
|
|
$job->file_type = '';
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
// mock repositories and configuration handling classes:
|
|
|
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
|
|
|
$configurator = $this->mock(FakeJobConfiguration::class);
|
2018-09-04 09:52:19 +02:00
|
|
|
$userRepos = $this->mock(UserRepositoryInterface::class);
|
|
|
|
|
|
|
|
|
2018-05-04 20:21:27 +02:00
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->get(route('import.job.configuration.index', [$job->key]));
|
|
|
|
$response->assertStatus(302);
|
|
|
|
$response->assertRedirect(route('import.index'));
|
|
|
|
$response->assertSessionHas('error', 'To access this page, your import job cannot have status "some_bad_state".');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\Import\JobConfigurationController
|
|
|
|
*/
|
|
|
|
public function testIndexComplete(): void
|
|
|
|
{
|
|
|
|
$job = new ImportJob;
|
|
|
|
$job->user_id = $this->user()->id;
|
2018-07-14 16:41:07 +02:00
|
|
|
$job->key = '3Cfake_job_' . random_int(1, 10000);
|
2018-05-04 20:21:27 +02:00
|
|
|
$job->status = 'has_prereq';
|
|
|
|
$job->provider = 'fake';
|
|
|
|
$job->file_type = '';
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
// mock repositories and configuration handling classes:
|
|
|
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
|
|
|
$configurator = $this->mock(FakeJobConfiguration::class);
|
2018-09-04 09:52:19 +02:00
|
|
|
$userRepos = $this->mock(UserRepositoryInterface::class);
|
2018-05-04 20:21:27 +02:00
|
|
|
|
|
|
|
// mock calls:
|
2018-05-12 15:50:01 +02:00
|
|
|
$configurator->shouldReceive('setImportJob')->once();
|
2018-05-04 20:21:27 +02:00
|
|
|
$configurator->shouldReceive('configurationComplete')->once()->andReturn(true);
|
2018-08-31 21:12:53 +02:00
|
|
|
$repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'ready_to_run']);
|
2018-05-04 20:21:27 +02:00
|
|
|
|
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->get(route('import.job.configuration.index', [$job->key]));
|
|
|
|
$response->assertStatus(302);
|
|
|
|
$response->assertRedirect(route('import.job.status.index', [$job->key]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\Import\JobConfigurationController
|
|
|
|
*/
|
|
|
|
public function testPost(): void
|
|
|
|
{
|
|
|
|
|
|
|
|
$job = new ImportJob;
|
|
|
|
$job->user_id = $this->user()->id;
|
2018-07-14 16:41:07 +02:00
|
|
|
$job->key = '4Dfake_job_' . random_int(1, 10000);
|
2018-05-04 20:21:27 +02:00
|
|
|
$job->status = 'has_prereq';
|
|
|
|
$job->provider = 'fake';
|
|
|
|
$job->file_type = '';
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
$messages = new MessageBag;
|
|
|
|
$messages->add('some', 'srrange message');
|
|
|
|
|
|
|
|
// mock repositories and configuration handling classes:
|
|
|
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
|
|
|
$configurator = $this->mock(FakeJobConfiguration::class);
|
2018-09-04 09:52:19 +02:00
|
|
|
$userRepos = $this->mock(UserRepositoryInterface::class);
|
2018-05-04 20:21:27 +02:00
|
|
|
|
|
|
|
// mock calls:
|
2018-05-12 15:50:01 +02:00
|
|
|
$configurator->shouldReceive('setImportJob')->once();
|
2018-05-04 20:21:27 +02:00
|
|
|
$configurator->shouldReceive('configurationComplete')->once()->andReturn(false);
|
|
|
|
$configurator->shouldReceive('configureJob')->withArgs([[]])->once()->andReturn($messages);
|
|
|
|
|
|
|
|
// call thing.
|
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->post(route('import.job.configuration.post', [$job->key]));
|
|
|
|
$response->assertStatus(302);
|
|
|
|
$response->assertRedirect(route('import.job.configuration.index', [$job->key]));
|
|
|
|
$response->assertSessionHas('warning', $messages->first());
|
|
|
|
}
|
|
|
|
|
2018-05-10 20:05:02 +02:00
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\Import\JobConfigurationController
|
|
|
|
*/
|
|
|
|
public function testPostBadState(): void
|
|
|
|
{
|
|
|
|
|
|
|
|
$job = new ImportJob;
|
|
|
|
$job->user_id = $this->user()->id;
|
2018-07-14 16:41:07 +02:00
|
|
|
$job->key = '5Ffake_job_' . random_int(1, 10000);
|
2018-05-10 20:05:02 +02:00
|
|
|
$job->status = 'some_bad_state';
|
|
|
|
$job->provider = 'fake';
|
|
|
|
$job->file_type = '';
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
$messages = new MessageBag;
|
|
|
|
$messages->add('some', 'srrange message');
|
|
|
|
|
|
|
|
// mock repositories and configuration handling classes:
|
|
|
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
|
|
|
$configurator = $this->mock(FakeJobConfiguration::class);
|
2018-09-04 09:52:19 +02:00
|
|
|
$userRepos = $this->mock(UserRepositoryInterface::class);
|
2018-05-10 20:05:02 +02:00
|
|
|
|
|
|
|
// call thing.
|
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->post(route('import.job.configuration.post', [$job->key]));
|
|
|
|
$response->assertStatus(302);
|
|
|
|
$response->assertRedirect(route('import.index'));
|
|
|
|
$response->assertSessionHas('error', 'To access this page, your import job cannot have status "some_bad_state".');
|
|
|
|
}
|
|
|
|
|
2018-05-04 20:21:27 +02:00
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\Import\JobConfigurationController
|
|
|
|
*/
|
|
|
|
public function testPostComplete(): void
|
|
|
|
{
|
|
|
|
|
|
|
|
$job = new ImportJob;
|
|
|
|
$job->user_id = $this->user()->id;
|
2018-07-14 16:41:07 +02:00
|
|
|
$job->key = '6Efake_job_' . random_int(1, 10000);
|
2018-05-04 20:21:27 +02:00
|
|
|
$job->status = 'has_prereq';
|
|
|
|
$job->provider = 'fake';
|
|
|
|
$job->file_type = '';
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
// mock repositories and configuration handling classes:
|
|
|
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
|
|
|
$configurator = $this->mock(FakeJobConfiguration::class);
|
2018-09-04 09:52:19 +02:00
|
|
|
$userRepos = $this->mock(UserRepositoryInterface::class);
|
|
|
|
|
2018-05-04 20:21:27 +02:00
|
|
|
|
|
|
|
// mock calls:
|
2018-05-12 15:50:01 +02:00
|
|
|
$configurator->shouldReceive('setImportJob')->once();
|
2018-05-04 20:21:27 +02:00
|
|
|
$configurator->shouldReceive('configurationComplete')->once()->andReturn(true);
|
2018-08-31 21:12:53 +02:00
|
|
|
$repository->shouldReceive('setStatus')->withArgs([Mockery::any(), 'ready_to_run']);
|
2018-05-04 20:21:27 +02:00
|
|
|
|
|
|
|
// call thing.
|
|
|
|
$this->be($this->user());
|
|
|
|
$response = $this->post(route('import.job.configuration.post', [$job->key]));
|
|
|
|
$response->assertStatus(302);
|
|
|
|
$response->assertRedirect(route('import.job.status.index', [$job->key]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Http\Controllers\Import\JobConfigurationController
|
|
|
|
*/
|
2018-05-10 20:05:02 +02:00
|
|
|
public function testPostWithUpload(): void
|
2018-05-04 20:21:27 +02:00
|
|
|
{
|
2018-05-10 20:05:02 +02:00
|
|
|
$file = UploadedFile::fake()->image('avatar.jpg');
|
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 = '7Dfake_job_' . random_int(1, 10000);
|
2018-05-10 20:05:02 +02:00
|
|
|
$job->status = 'has_prereq';
|
2018-05-04 20:21:27 +02:00
|
|
|
$job->provider = 'fake';
|
|
|
|
$job->file_type = '';
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
$messages = new MessageBag;
|
|
|
|
$messages->add('some', 'srrange message');
|
|
|
|
|
|
|
|
// mock repositories and configuration handling classes:
|
|
|
|
$repository = $this->mock(ImportJobRepositoryInterface::class);
|
|
|
|
$configurator = $this->mock(FakeJobConfiguration::class);
|
2018-09-04 09:52:19 +02:00
|
|
|
$userRepos = $this->mock(UserRepositoryInterface::class);
|
2018-05-04 20:21:27 +02:00
|
|
|
|
2018-05-10 20:05:02 +02:00
|
|
|
// mock calls:
|
2018-05-12 15:50:01 +02:00
|
|
|
$configurator->shouldReceive('setImportJob')->once();
|
2018-05-10 20:05:02 +02:00
|
|
|
$configurator->shouldReceive('configurationComplete')->once()->andReturn(false);
|
|
|
|
$configurator->shouldReceive('configureJob')->once()->andReturn($messages);
|
|
|
|
$repository->shouldReceive('storeFileUpload')->once()->andReturn(new MessageBag);
|
|
|
|
|
2018-05-04 20:21:27 +02:00
|
|
|
// call thing.
|
|
|
|
$this->be($this->user());
|
2018-05-10 20:05:02 +02:00
|
|
|
$response = $this->post(route('import.job.configuration.post', [$job->key]), ['import_file' => $file]);
|
2018-05-04 20:21:27 +02:00
|
|
|
$response->assertStatus(302);
|
2018-05-10 20:05:02 +02:00
|
|
|
$response->assertRedirect(route('import.job.configuration.index', [$job->key]));
|
|
|
|
$response->assertSessionHas('warning', $messages->first());
|
2018-05-04 20:21:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-05 16:51:32 +02:00
|
|
|
}
|