mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-11 05:32:39 +01:00
19080933b6
Shift automatically applies the Laravel coding style - which uses the PSR-2 coding style as a base with some minor additions. You may customize the code style applied by adding a [PHP CS Fixer][1] or [PHP CodeSniffer][2] ruleset to your project root. Feel free to use [Shift's Laravel ruleset][3] to help you get started. For more information on customizing the code style applied by Shift, [watch this short video][4]. [1]: https://github.com/FriendsOfPHP/PHP-CS-Fixer [2]: https://github.com/squizlabs/PHP_CodeSniffer [3]: https://gist.github.com/laravel-shift/cab527923ed2a109dda047b97d53c200 [4]: https://laravelshift.com/videos/shift-code-style
132 lines
4.0 KiB
PHP
132 lines
4.0 KiB
PHP
<?php
|
|
/**
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
*
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
*
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
|
*
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
*/
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* @test
|
|
* @covers App\Utils\Number
|
|
*/
|
|
class CompareCollectionTest extends TestCase
|
|
{
|
|
public function setUp() :void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->map = collect([
|
|
['action' => 'view_client_client_id', 'permission' => 'view_client', 'route' => 'clients.show', 'key' => 'client_id', 'name' => trans('texts.view')],
|
|
['action' => 'edit_client_client_id', 'permission' => 'edit_client', 'route' => 'clients.edit', 'key' => 'client_id', 'name' => trans('texts.edit')],
|
|
['action' => 'create_task_client_id', 'permission' => 'create_task', 'route' => 'task.create', 'key' => 'client_id', 'name' => trans('texts.new_task')],
|
|
['action' => 'create_invoice_client_id', 'permission' => 'create_invoice', 'route' => 'invoice.create', 'key' => 'client_id', 'name' => trans('texts.new_invoice')],
|
|
['action' => 'enter_payment_client_id', 'permission' => 'create_payment', 'route' => 'payment.create', 'key' => 'client_id', 'name' => trans('texts.enter_payment')],
|
|
['action' => 'enter_credit_client_id', 'permission' => 'create_credit', 'route' => 'credit.create', 'key' => 'client_id', 'name' => trans('texts.enter_credit')],
|
|
['action' => 'enter_expense_client_id', 'permission' => 'create_expense', 'route' => 'expense.create', 'key' => 'client_id', 'name' => trans('texts.enter_expense')],
|
|
]);
|
|
|
|
$this->view_permission = ['view_client'];
|
|
|
|
$this->edit_permission = ['view_client', 'edit_client'];
|
|
|
|
$this->is_admin = true;
|
|
|
|
$this->is_not_admin = false;
|
|
}
|
|
|
|
public function testCompareResultOfComparison()
|
|
{
|
|
$this->assertEquals(7, $this->map->count());
|
|
}
|
|
|
|
public function testViewPermission()
|
|
{
|
|
$this->assertEquals(1, $this->checkPermissions($this->view_permission, $this->is_not_admin)->count());
|
|
}
|
|
|
|
public function testViewAndEditPermission()
|
|
{
|
|
$this->assertEquals(2, $this->checkPermissions($this->edit_permission, $this->is_not_admin)->count());
|
|
}
|
|
|
|
public function testAdminPermissions()
|
|
{
|
|
$this->assertEquals(7, $this->checkPermissions($this->view_permission, $this->is_admin)->count());
|
|
}
|
|
|
|
public function testActionViewClientFilter()
|
|
{
|
|
$actions = [
|
|
'view_client_client_id',
|
|
];
|
|
|
|
$this->assertEquals(1, $this->map->whereIn('action', $actions)->count());
|
|
}
|
|
|
|
public function testNoActionClientFilter()
|
|
{
|
|
$actions = [
|
|
'',
|
|
];
|
|
|
|
$this->assertEquals(0, $this->map->whereIn('action', $actions)->count());
|
|
}
|
|
|
|
public function testActionsAndPermissionsFilter()
|
|
{
|
|
$actions = [
|
|
'view_client_client_id',
|
|
|
|
];
|
|
|
|
$this->filterActions($actions);
|
|
|
|
$this->assertEquals(1, $this->checkPermissions($this->view_permission, $this->is_not_admin)->count());
|
|
}
|
|
|
|
public function testActionAndPermissionsFilterFailure()
|
|
{
|
|
$actions = [
|
|
'edit_client_client_id',
|
|
|
|
];
|
|
|
|
$data = $this->filterActions($actions);
|
|
|
|
$this->assertEquals(0, $data->whereIn('permission', $this->view_permission)->count());
|
|
}
|
|
|
|
public function testEditActionAndPermissionsFilter()
|
|
{
|
|
$actions = [
|
|
'edit_client_client_id',
|
|
|
|
];
|
|
|
|
$data = $this->filterActions($actions);
|
|
$this->assertEquals(1, $data->whereIn('permission', $this->edit_permission)->count());
|
|
}
|
|
|
|
public function checkPermissions($permission, $is_admin)
|
|
{
|
|
if ($is_admin === true) {
|
|
return $this->map;
|
|
}
|
|
|
|
return $this->map->whereIn('permission', $permission);
|
|
}
|
|
|
|
public function filterActions($actions)
|
|
{
|
|
return $this->map->whereIn('action', $actions);
|
|
}
|
|
}
|