firefly-iii/tests/Unit/Handlers/Events/VersionCheckEventHandlerTest.php

255 lines
10 KiB
PHP
Raw Normal View History

2018-03-30 15:40:20 -05:00
<?php
/**
* VersionCheckEventHandlerTest.php
2020-02-16 06:59:55 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2018-03-30 15:40:20 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-03-30 15:40:20 -05: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-03-30 15:40:20 -05:00
*
* This program is distributed in the hope that it will be useful,
2018-03-30 15:40:20 -05: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.
2018-03-30 15:40:20 -05: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-03-30 15:40:20 -05:00
*/
declare(strict_types=1);
namespace Tests\Unit\Handlers\Events;
use FireflyConfig;
use FireflyIII\Events\RequestedVersionCheckStatus;
use FireflyIII\Handlers\Events\VersionCheckEventHandler;
use FireflyIII\Models\Configuration;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\Services\Github\Object\Release;
use FireflyIII\Services\Github\Request\UpdateRequest;
2018-08-24 14:14:17 -05:00
use Log;
2018-03-30 15:40:20 -05:00
use Mockery;
use Tests\TestCase;
/**
* Class VersionCheckEventHandlerTest
2019-08-17 03:48:28 -05:00
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2018-03-30 15:40:20 -05:00
*/
class VersionCheckEventHandlerTest extends TestCase
{
/**
*
*/
public function setUp(): void
{
parent::setUp();
2019-04-09 13:05:20 -05:00
Log::info(sprintf('Now in %s.', get_class($this)));
}
2018-03-30 15:40:20 -05:00
/**
2018-08-24 14:14:17 -05:00
* @covers \FireflyIII\Events\RequestedVersionCheckStatus
* @covers \FireflyIII\Handlers\Events\VersionCheckEventHandler
2019-06-21 12:10:02 -05:00
* @covers \FireflyIII\Helpers\Update\UpdateTrait
2018-03-30 15:40:20 -05:00
*/
2018-05-11 12:58:10 -05:00
public function testCheckForUpdatesError(): void
2018-03-30 15:40:20 -05:00
{
$updateConfig = new Configuration;
$updateConfig->data = 1;
$checkConfig = new Configuration;
$checkConfig->data = time() - 604810;
2020-01-05 12:29:28 -06:00
$channelConfig = new Configuration;
$channelConfig->data = 'stable';
$permissionConfig = new Configuration;
$permissionConfig->data = 1;
2019-12-22 00:50:40 -06:00
2018-03-30 15:40:20 -05:00
$event = new RequestedVersionCheckStatus($this->user());
$request = $this->mock(UpdateRequest::class);
$repos = $this->mock(UserRepositoryInterface::class);
$repos->shouldReceive('hasRole')->andReturn(true)->once();
// report on config variables:
FireflyConfig::shouldReceive('get')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig);
FireflyConfig::shouldReceive('set')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig);
2019-12-22 00:50:40 -06:00
FireflyConfig::shouldReceive('get')->withArgs(['update_channel', 'stable'])->once()->andReturn($channelConfig);
2020-01-05 12:29:28 -06:00
FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check', -1])->once()->andReturn($permissionConfig);
2018-03-30 15:40:20 -05:00
// request thing:
2019-12-22 00:50:40 -06:00
//$request->shouldReceive('call')->once()->andThrow(new FireflyException('Errrr'));
//$request->shouldNotReceive('getReleases');
2018-03-30 15:40:20 -05:00
$handler = new VersionCheckEventHandler;
$handler->checkForUpdates($event);
}
/**
* @covers \FireflyIII\Events\RequestedVersionCheckStatus
* @covers \FireflyIII\Handlers\Events\VersionCheckEventHandler
2019-06-21 12:10:02 -05:00
* @covers \FireflyIII\Helpers\Update\UpdateTrait
2018-03-30 15:40:20 -05:00
*/
2018-05-11 12:58:10 -05:00
public function testCheckForUpdatesNewer(): void
2018-03-30 15:40:20 -05:00
{
2020-01-05 12:29:28 -06:00
$updateConfig = new Configuration;
$updateConfig->data = 1;
$checkConfig = new Configuration;
$checkConfig->data = time() - 604800;
$channelConfig = new Configuration;
$channelConfig->data = 'stable';
$permissionConfig = new Configuration;
$permissionConfig->data = 1;
2018-03-30 15:40:20 -05:00
$event = new RequestedVersionCheckStatus($this->user());
$request = $this->mock(UpdateRequest::class);
$repos = $this->mock(UserRepositoryInterface::class);
$repos->shouldReceive('hasRole')->andReturn(true)->once();
// is newer than default return:
$version = config('firefly.version');
$first = new Release(['id' => '1', 'title' => $version . '.1', 'updated' => '2017-05-01', 'content' => '']);
// report on config variables:
FireflyConfig::shouldReceive('get')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig);
FireflyConfig::shouldReceive('set')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig);
2019-12-22 00:50:40 -06:00
FireflyConfig::shouldReceive('get')->withArgs(['update_channel', 'stable'])->once()->andReturn($channelConfig);
2020-01-05 12:29:28 -06:00
FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check', -1])->once()->andReturn($permissionConfig);
2018-03-30 15:40:20 -05:00
// request thing:
2019-12-22 00:50:40 -06:00
//$request->shouldReceive('call')->once();
//$request->shouldReceive('getReleases')->once()->andReturn([$first]);
2018-03-30 15:40:20 -05:00
$handler = new VersionCheckEventHandler;
$handler->checkForUpdates($event);
}
/**
2018-08-24 14:14:17 -05:00
* @covers \FireflyIII\Events\RequestedVersionCheckStatus
* @covers \FireflyIII\Handlers\Events\VersionCheckEventHandler
2019-06-21 12:10:02 -05:00
* @covers \FireflyIII\Helpers\Update\UpdateTrait
*/
public function testCheckForUpdatesSameVersion(): void
{
2020-01-05 12:29:28 -06:00
$updateConfig = new Configuration;
$updateConfig->data = 1;
$checkConfig = new Configuration;
$checkConfig->data = time() - 604800;
$channelConfig = new Configuration;
$channelConfig->data = 'stable';
$permissionConfig = new Configuration;
$permissionConfig->data = 1;
2019-06-21 12:10:02 -05:00
$event = new RequestedVersionCheckStatus($this->user());
$request = $this->mock(UpdateRequest::class);
$repos = $this->mock(UserRepositoryInterface::class);
$repos->shouldReceive('hasRole')->andReturn(true)->once();
// is newer than default return:
$version = config('firefly.version');
$first = new Release(['id' => '1', 'title' => $version, 'updated' => '2017-05-01', 'content' => '']);
// report on config variables:
FireflyConfig::shouldReceive('get')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig);
FireflyConfig::shouldReceive('set')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig);
2019-12-22 00:50:40 -06:00
FireflyConfig::shouldReceive('get')->withArgs(['update_channel', 'stable'])->once()->andReturn($channelConfig);
2020-01-05 12:29:28 -06:00
FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check', -1])->once()->andReturn($permissionConfig);
2019-06-21 12:10:02 -05:00
// request thing:
2019-12-22 00:50:40 -06:00
//$request->shouldReceive('call')->once();
//$request->shouldReceive('getReleases')->once()->andReturn([$first]);
2019-06-21 12:10:02 -05:00
$handler = new VersionCheckEventHandler;
$handler->checkForUpdates($event);
}
/**
* @covers \FireflyIII\Events\RequestedVersionCheckStatus
* @covers \FireflyIII\Handlers\Events\VersionCheckEventHandler
* @covers \FireflyIII\Helpers\Update\UpdateTrait
2018-03-30 15:40:20 -05:00
*/
2018-05-11 12:58:10 -05:00
public function testCheckForUpdatesNoAdmin(): void
2018-03-30 15:40:20 -05:00
{
2020-01-05 12:29:28 -06:00
$updateConfig = new Configuration;
$updateConfig->data = 1;
$checkConfig = new Configuration;
$checkConfig->data = time() - 604800;
$permissionConfig = new Configuration;
$permissionConfig->data = 1;
2018-03-30 15:40:20 -05:00
$event = new RequestedVersionCheckStatus($this->user());
$repos = $this->mock(UserRepositoryInterface::class);
$repos->shouldReceive('hasRole')->andReturn(false)->once();
2020-01-05 12:29:28 -06:00
FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check', -1])->once()->andReturn($permissionConfig);
2018-03-30 15:40:20 -05:00
$handler = new VersionCheckEventHandler;
$handler->checkForUpdates($event);
}
/**
2019-06-21 12:10:02 -05:00
* @covers \FireflyIII\Events\RequestedVersionCheckStatus
* @covers \FireflyIII\Handlers\Events\VersionCheckEventHandler
* @covers \FireflyIII\Helpers\Update\UpdateTrait
2018-03-30 15:40:20 -05:00
*/
2018-05-11 12:58:10 -05:00
public function testCheckForUpdatesNoPermission(): void
2018-03-30 15:40:20 -05:00
{
2020-01-05 12:29:28 -06:00
$updateConfig = new Configuration;
$updateConfig->data = -1;
$checkConfig = new Configuration;
$checkConfig->data = time() - 604800;
$channelConfig = new Configuration;
$channelConfig->data = 'stable';
$permissionConfig = new Configuration;
$permissionConfig->data = 1;
2018-03-30 15:40:20 -05:00
$event = new RequestedVersionCheckStatus($this->user());
$repos = $this->mock(UserRepositoryInterface::class);
$repos->shouldReceive('hasRole')->andReturn(true)->once();
2020-01-05 12:29:28 -06:00
FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check', -1])->once()->andReturn($permissionConfig);
2018-03-30 15:40:20 -05:00
// report on config variables:
FireflyConfig::shouldReceive('get')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig);
FireflyConfig::shouldReceive('set')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig);
2019-12-22 00:50:40 -06:00
FireflyConfig::shouldReceive('get')->withArgs(['update_channel', 'stable'])->once()->andReturn($channelConfig);
2018-03-30 15:40:20 -05:00
$handler = new VersionCheckEventHandler;
$handler->checkForUpdates($event);
}
2018-08-24 14:14:17 -05:00
/**
* @covers \FireflyIII\Events\RequestedVersionCheckStatus
* @covers \FireflyIII\Handlers\Events\VersionCheckEventHandler
2019-06-21 12:10:02 -05:00
* @covers \FireflyIII\Helpers\Update\UpdateTrait
2018-03-30 15:40:20 -05:00
*/
2018-05-11 12:58:10 -05:00
public function testCheckForUpdatesTooRecent(): void
2018-03-30 15:40:20 -05:00
{
$updateConfig = new Configuration;
$updateConfig->data = 1;
$checkConfig = new Configuration;
$checkConfig->data = time() - 800;
2020-01-05 12:29:28 -06:00
$permissionConfig = new Configuration;
$permissionConfig->data = 1;
2018-03-30 15:40:20 -05:00
$event = new RequestedVersionCheckStatus($this->user());
$repos = $this->mock(UserRepositoryInterface::class);
$repos->shouldReceive('hasRole')->andReturn(true)->once();
// report on config variables:
FireflyConfig::shouldReceive('get')->withArgs(['last_update_check', Mockery::any()])->once()->andReturn($checkConfig);
2020-01-05 12:29:28 -06:00
FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check', -1])->once()->andReturn($permissionConfig);
2018-03-30 15:40:20 -05:00
$handler = new VersionCheckEventHandler;
$handler->checkForUpdates($event);
}
2018-05-05 09:51:32 -05:00
}