2018-05-05 11:03:10 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* FakePrerequisitesTest.php
|
2019-10-02 06:45:03 +02:00
|
|
|
* Copyright (c) 2019 thegrumpydictator@gmail.com
|
2018-05-05 11:03:10 +02:00
|
|
|
*
|
2019-10-02 06:45:03 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-05-05 11:03:10 +02:00
|
|
|
*
|
2019-10-02 06:45:03 +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.
|
2018-05-05 11:03:10 +02:00
|
|
|
*
|
2019-10-02 06:45:03 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-05-05 11:03:10 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:45:03 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-05-05 11:03:10 +02:00
|
|
|
*
|
2019-10-02 06:45:03 +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/>.
|
2018-05-05 11:03:10 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Tests\Unit\Import\Prerequisites;
|
|
|
|
|
|
|
|
|
|
|
|
use FireflyIII\Import\Prerequisites\FakePrerequisites;
|
|
|
|
use FireflyIII\Models\Preference;
|
2019-04-09 15:32:48 +02:00
|
|
|
use Log;
|
2018-05-05 11:03:10 +02:00
|
|
|
use Mockery;
|
|
|
|
use Preferences;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class FakePrerequisitesTest
|
2019-08-17 10:48:28 +02:00
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
|
2018-05-05 11:03:10 +02:00
|
|
|
*/
|
|
|
|
class FakePrerequisitesTest extends TestCase
|
|
|
|
{
|
2018-09-04 09:52:19 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2019-04-09 20:05:20 +02:00
|
|
|
Log::info(sprintf('Now in %s.', get_class($this)));
|
2018-09-04 09:52:19 +02:00
|
|
|
}
|
|
|
|
|
2018-05-05 11:03:10 +02:00
|
|
|
/**
|
|
|
|
* Bad API key length in preferences
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Import\Prerequisites\FakePrerequisites
|
|
|
|
*/
|
|
|
|
public function testGetViewParametersBadLength(): void
|
|
|
|
{
|
|
|
|
// API key should be empty:
|
|
|
|
$apiPref = new Preference;
|
|
|
|
$apiPref->data = 'abc';
|
|
|
|
|
|
|
|
Preferences::shouldReceive('getForUser')
|
|
|
|
->withArgs([Mockery::any(), 'fake_api_key', false])->once()
|
|
|
|
->andReturn($apiPref);
|
|
|
|
|
|
|
|
$object = new FakePrerequisites();
|
|
|
|
$object->setUser($this->user());
|
|
|
|
$result = $object->getViewParameters();
|
|
|
|
$this->assertEquals(['api_key' => ''], $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* No API key in preference.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Import\Prerequisites\FakePrerequisites
|
|
|
|
*/
|
|
|
|
public function testGetViewParametersDataNull(): void
|
|
|
|
{
|
|
|
|
// API key should be empty:
|
|
|
|
$apiPref = new Preference;
|
|
|
|
$apiPref->data = null;
|
|
|
|
|
|
|
|
Preferences::shouldReceive('getForUser')
|
|
|
|
->withArgs([Mockery::any(), 'fake_api_key', false])->once()
|
|
|
|
->andReturn($apiPref);
|
|
|
|
|
|
|
|
$object = new FakePrerequisites();
|
|
|
|
$object->setUser($this->user());
|
|
|
|
$result = $object->getViewParameters();
|
|
|
|
$this->assertEquals(['api_key' => ''], $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Good API key length in preferences
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Import\Prerequisites\FakePrerequisites
|
|
|
|
*/
|
|
|
|
public function testGetViewParametersGoodLength(): void
|
|
|
|
{
|
|
|
|
// API key should be empty:
|
|
|
|
$apiPref = new Preference;
|
|
|
|
$apiPref->data = '123456789012345678901234567890AA';
|
|
|
|
|
|
|
|
Preferences::shouldReceive('getForUser')
|
|
|
|
->withArgs([Mockery::any(), 'fake_api_key', false])->twice()
|
|
|
|
->andReturn($apiPref);
|
|
|
|
|
|
|
|
$object = new FakePrerequisites();
|
|
|
|
$object->setUser($this->user());
|
|
|
|
$result = $object->getViewParameters();
|
|
|
|
$this->assertEquals(['api_key' => '123456789012345678901234567890AA'], $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* No preference at all.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Import\Prerequisites\FakePrerequisites
|
|
|
|
*/
|
|
|
|
public function testGetViewParametersPrefNull(): void
|
|
|
|
{
|
|
|
|
Preferences::shouldReceive('getForUser')
|
|
|
|
->withArgs([Mockery::any(), 'fake_api_key', false])->once()
|
|
|
|
->andReturn(null);
|
|
|
|
|
|
|
|
$object = new FakePrerequisites();
|
|
|
|
$object->setUser($this->user());
|
|
|
|
$result = $object->getViewParameters();
|
|
|
|
$this->assertEquals(['api_key' => ''], $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Also test hasApiKey but that one is covered.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Import\Prerequisites\FakePrerequisites
|
|
|
|
*/
|
|
|
|
public function testIsComplete(): void
|
|
|
|
{
|
|
|
|
// API key should be empty:
|
|
|
|
$apiPref = new Preference;
|
|
|
|
$apiPref->data = null;
|
|
|
|
|
|
|
|
Preferences::shouldReceive('getForUser')
|
|
|
|
->withArgs([Mockery::any(), 'fake_api_key', false])->once()
|
|
|
|
->andReturn($apiPref);
|
|
|
|
|
|
|
|
$object = new FakePrerequisites();
|
|
|
|
$object->setUser($this->user());
|
|
|
|
$this->assertFalse($object->isComplete());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Also test hasApiKey but that one is covered.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Import\Prerequisites\FakePrerequisites
|
|
|
|
*/
|
|
|
|
public function testStorePrerequisitesBad(): void
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'api_key' => 'Hallo',
|
|
|
|
];
|
|
|
|
$object = new FakePrerequisites();
|
|
|
|
$object->setUser($this->user());
|
|
|
|
$messages = $object->storePrerequisites($data);
|
|
|
|
$this->assertCount(1, $messages);
|
|
|
|
$this->assertEquals('API key must be 32 chars.', $messages->first());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Also test hasApiKey but that one is covered.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Import\Prerequisites\FakePrerequisites
|
|
|
|
*/
|
|
|
|
public function testStorePrerequisitesGood(): void
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'api_key' => '123456789012345678901234567890AA',
|
|
|
|
];
|
|
|
|
|
|
|
|
Preferences::shouldReceive('setForUser')->withArgs([Mockery::any(), 'fake_api_key', '123456789012345678901234567890AA'])->once();
|
|
|
|
|
|
|
|
$object = new FakePrerequisites();
|
|
|
|
$object->setUser($this->user());
|
|
|
|
$messages = $object->storePrerequisites($data);
|
|
|
|
$this->assertCount(0, $messages);
|
|
|
|
}
|
|
|
|
|
2018-05-05 16:51:32 +02:00
|
|
|
}
|