2019-01-07 12:30:28 +01:00
|
|
|
<?php
|
2020-09-14 13:11:46 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-09-14 13:11:46 +02:00
|
|
|
*
|
2022-06-21 11:57:17 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-09-14 13:11:46 +02:00
|
|
|
*/
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-01-07 12:30:28 +01:00
|
|
|
namespace Tests\Unit;
|
|
|
|
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2019-08-28 04:36:53 +02:00
|
|
|
* @covers App\Utils\Number
|
2019-01-07 12:30:28 +01:00
|
|
|
*/
|
|
|
|
class CompareCollectionTest extends TestCase
|
|
|
|
{
|
2022-06-21 12:00:57 +02:00
|
|
|
protected function setUp() :void
|
2019-01-07 12:30:28 +01:00
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
parent::setUp();
|
2019-01-07 12:30:28 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->map = collect([
|
2022-06-21 11:57:17 +02:00
|
|
|
['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')],
|
|
|
|
]);
|
2019-01-07 12:30:28 +01:00
|
|
|
|
|
|
|
$this->view_permission = ['view_client'];
|
|
|
|
|
|
|
|
$this->edit_permission = ['view_client', 'edit_client'];
|
|
|
|
|
|
|
|
$this->is_admin = true;
|
|
|
|
|
|
|
|
$this->is_not_admin = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCompareResultOfComparison()
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->assertEquals(7, $this->map->count());
|
2019-01-07 12:30:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 = [
|
2020-09-06 11:38:10 +02:00
|
|
|
'view_client_client_id',
|
2019-01-07 12:30:28 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals(1, $this->map->whereIn('action', $actions)->count());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNoActionClientFilter()
|
|
|
|
{
|
|
|
|
$actions = [
|
2020-09-06 11:38:10 +02:00
|
|
|
'',
|
2019-01-07 12:30:28 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals(0, $this->map->whereIn('action', $actions)->count());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testActionsAndPermissionsFilter()
|
|
|
|
{
|
|
|
|
$actions = [
|
2020-09-06 11:38:10 +02:00
|
|
|
'view_client_client_id',
|
2019-01-07 12:30:28 +01:00
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->filterActions($actions);
|
|
|
|
|
|
|
|
$this->assertEquals(1, $this->checkPermissions($this->view_permission, $this->is_not_admin)->count());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testActionAndPermissionsFilterFailure()
|
|
|
|
{
|
|
|
|
$actions = [
|
2020-09-06 11:38:10 +02:00
|
|
|
'edit_client_client_id',
|
2019-01-07 12:30:28 +01:00
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
$data = $this->filterActions($actions);
|
|
|
|
|
|
|
|
$this->assertEquals(0, $data->whereIn('permission', $this->view_permission)->count());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEditActionAndPermissionsFilter()
|
|
|
|
{
|
|
|
|
$actions = [
|
2020-09-06 11:38:10 +02:00
|
|
|
'edit_client_client_id',
|
2019-01-07 12:30:28 +01:00
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
$data = $this->filterActions($actions);
|
|
|
|
$this->assertEquals(1, $data->whereIn('permission', $this->edit_permission)->count());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function checkPermissions($permission, $is_admin)
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($is_admin === true) {
|
2019-01-07 12:30:28 +01:00
|
|
|
return $this->map;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2019-01-07 12:30:28 +01:00
|
|
|
|
|
|
|
return $this->map->whereIn('permission', $permission);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function filterActions($actions)
|
|
|
|
{
|
|
|
|
return $this->map->whereIn('action', $actions);
|
|
|
|
}
|
|
|
|
}
|