2018-04-06 01:05:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use Backend\Widgets\Form;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2019-10-09 22:41:53 +00:00
|
|
|
use October\Tests\Fixtures\Backend\Models\UserFixture;
|
2018-04-06 01:05:31 +00:00
|
|
|
|
|
|
|
|
class FormTestModel extends Model
|
|
|
|
|
{
|
2020-09-11 17:33:44 +00:00
|
|
|
public function modelCustomOptionsMethod()
|
|
|
|
|
{
|
|
|
|
|
return ['model', 'custom', 'options'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getFieldNameOnModelOptionsMethodOptions()
|
|
|
|
|
{
|
|
|
|
|
return ['model', 'field name', 'options method'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getDropdownOptions()
|
|
|
|
|
{
|
|
|
|
|
return ['dropdown', 'options'];
|
|
|
|
|
}
|
2020-11-21 01:45:20 +00:00
|
|
|
|
|
|
|
|
public function staticMethodOptions()
|
|
|
|
|
{
|
|
|
|
|
return ['static', 'method'];
|
|
|
|
|
}
|
2020-09-11 17:33:44 +00:00
|
|
|
}
|
2018-04-06 01:05:31 +00:00
|
|
|
|
2020-09-11 17:33:44 +00:00
|
|
|
class FormHelper
|
|
|
|
|
{
|
|
|
|
|
public static function staticMethodOptions()
|
|
|
|
|
{
|
|
|
|
|
return ['static', 'method'];
|
|
|
|
|
}
|
2018-04-06 01:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-09 22:41:53 +00:00
|
|
|
class FormTest extends PluginTestCase
|
2018-04-06 01:05:31 +00:00
|
|
|
{
|
2019-10-09 22:41:53 +00:00
|
|
|
public function testRestrictedFieldWithUserWithNoPermissions()
|
|
|
|
|
{
|
|
|
|
|
$user = new UserFixture;
|
|
|
|
|
$this->actingAs($user);
|
|
|
|
|
|
|
|
|
|
$form = $this->restrictedFormFixture();
|
|
|
|
|
|
|
|
|
|
$form->render();
|
|
|
|
|
$this->assertNull($form->getField('testRestricted'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRestrictedFieldWithUserWithWrongPermissions()
|
|
|
|
|
{
|
|
|
|
|
$user = new UserFixture;
|
|
|
|
|
$this->actingAs($user->withPermission('test.wrong_permission', true));
|
|
|
|
|
|
|
|
|
|
$form = $this->restrictedFormFixture();
|
|
|
|
|
|
|
|
|
|
$form->render();
|
|
|
|
|
$this->assertNull($form->getField('testRestricted'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRestrictedFieldWithUserWithRightPermissions()
|
|
|
|
|
{
|
|
|
|
|
$user = new UserFixture;
|
|
|
|
|
$this->actingAs($user->withPermission('test.access_field', true));
|
|
|
|
|
|
|
|
|
|
$form = $this->restrictedFormFixture();
|
|
|
|
|
|
|
|
|
|
$form->render();
|
|
|
|
|
$this->assertNotNull($form->getField('testRestricted'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRestrictedFieldWithUserWithRightWildcardPermissions()
|
|
|
|
|
{
|
|
|
|
|
$user = new UserFixture;
|
|
|
|
|
$this->actingAs($user->withPermission('test.access_field', true));
|
|
|
|
|
|
|
|
|
|
$form = new Form(null, [
|
|
|
|
|
'model' => new FormTestModel,
|
|
|
|
|
'arrayName' => 'array',
|
|
|
|
|
'fields' => [
|
|
|
|
|
'testField' => [
|
|
|
|
|
'type' => 'text',
|
|
|
|
|
'label' => 'Test 1'
|
|
|
|
|
],
|
|
|
|
|
'testRestricted' => [
|
|
|
|
|
'type' => 'text',
|
|
|
|
|
'label' => 'Test 2',
|
|
|
|
|
'permission' => 'test.*'
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$form->render();
|
|
|
|
|
$this->assertNotNull($form->getField('testRestricted'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRestrictedFieldWithSuperuser()
|
|
|
|
|
{
|
|
|
|
|
$user = new UserFixture;
|
|
|
|
|
$this->actingAs($user->asSuperUser());
|
|
|
|
|
|
|
|
|
|
$form = $this->restrictedFormFixture();
|
|
|
|
|
|
|
|
|
|
$form->render();
|
|
|
|
|
$this->assertNotNull($form->getField('testRestricted'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRestrictedFieldSinglePermissionWithUserWithWrongPermissions()
|
|
|
|
|
{
|
|
|
|
|
$user = new UserFixture;
|
|
|
|
|
$this->actingAs($user->withPermission('test.wrong_permission', true));
|
|
|
|
|
|
|
|
|
|
$form = $this->restrictedFormFixture(true);
|
|
|
|
|
|
|
|
|
|
$form->render();
|
|
|
|
|
$this->assertNull($form->getField('testRestricted'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRestrictedFieldSinglePermissionWithUserWithRightPermissions()
|
|
|
|
|
{
|
|
|
|
|
$user = new UserFixture;
|
|
|
|
|
$this->actingAs($user->withPermission('test.access_field', true));
|
|
|
|
|
|
|
|
|
|
$form = $this->restrictedFormFixture(true);
|
|
|
|
|
|
|
|
|
|
$form->render();
|
|
|
|
|
$this->assertNotNull($form->getField('testRestricted'));
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-06 01:05:31 +00:00
|
|
|
public function testCheckboxlistTrigger()
|
|
|
|
|
{
|
|
|
|
|
$form = new Form(null, [
|
|
|
|
|
'model' => new FormTestModel,
|
|
|
|
|
'arrayName' => 'array',
|
|
|
|
|
'fields' => [
|
|
|
|
|
'trigger' => [
|
|
|
|
|
'type' => 'checkboxlist',
|
|
|
|
|
'options' => [
|
|
|
|
|
'1' => 'Value One'
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
'triggered' => [
|
|
|
|
|
'type' => 'text',
|
|
|
|
|
'trigger' => [
|
|
|
|
|
'field' => 'trigger[]',
|
|
|
|
|
'action' => 'show',
|
|
|
|
|
'condition' => 'value[1]'
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$form->render();
|
|
|
|
|
|
|
|
|
|
$attributes = $form->getField('triggered')->getAttributes('container', false);
|
|
|
|
|
$this->assertEquals('[name="array[trigger][]"]', array_get($attributes, 'data-trigger'));
|
|
|
|
|
}
|
2019-10-09 22:41:53 +00:00
|
|
|
|
2020-09-11 17:33:44 +00:00
|
|
|
public function testOptionsGeneration()
|
|
|
|
|
{
|
|
|
|
|
$form = new Form(null, [
|
|
|
|
|
'model' => new FormTestModel,
|
|
|
|
|
'arrayName' => 'array',
|
|
|
|
|
'fields' => [
|
|
|
|
|
'static_method_options' => [
|
|
|
|
|
'type' => 'dropdown',
|
2020-11-21 01:45:20 +00:00
|
|
|
'options' => 'FormHelper::staticMethodOptions',
|
2020-09-11 17:33:44 +00:00
|
|
|
'expect' => ['static', 'method'],
|
|
|
|
|
],
|
|
|
|
|
'callable_options' => [
|
|
|
|
|
'type' => 'dropdown',
|
|
|
|
|
'options' => [\FormHelper::class, 'staticMethodOptions'],
|
|
|
|
|
'expect' => ['static', 'method'],
|
|
|
|
|
],
|
|
|
|
|
'model_method_options' => [
|
|
|
|
|
'type' => 'dropdown',
|
|
|
|
|
'options' => 'modelCustomOptionsMethod',
|
|
|
|
|
'expect' => ['model', 'custom', 'options'],
|
|
|
|
|
],
|
|
|
|
|
'defined_options' => [
|
|
|
|
|
'type' => 'dropdown',
|
|
|
|
|
'options' => ['value1', 'value2'],
|
|
|
|
|
'expect' => ['value1', 'value2'],
|
|
|
|
|
],
|
|
|
|
|
'defined_options_key_value' => [
|
|
|
|
|
'type' => 'dropdown',
|
|
|
|
|
'options' => [
|
|
|
|
|
'key1' => 'value1',
|
|
|
|
|
'key2' => 'value2',
|
|
|
|
|
],
|
|
|
|
|
'expect' => [
|
|
|
|
|
'key1' => 'value1',
|
|
|
|
|
'key2' => 'value2',
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
'field_name_on_model_options_method' => [
|
|
|
|
|
'type' => 'dropdown',
|
|
|
|
|
'expect' => ['model', 'field name', 'options method'],
|
|
|
|
|
],
|
|
|
|
|
'get_dropdown_options_method' => [
|
|
|
|
|
'type' => 'dropdown',
|
|
|
|
|
'expect' => ['dropdown', 'options'],
|
|
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$form->render();
|
|
|
|
|
|
|
|
|
|
foreach ($form->getFields() as $name => $field) {
|
|
|
|
|
$this->assertEquals($field->options(), $field->config['expect']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 22:41:53 +00:00
|
|
|
protected function restrictedFormFixture(bool $singlePermission = false)
|
|
|
|
|
{
|
|
|
|
|
return new Form(null, [
|
|
|
|
|
'model' => new FormTestModel,
|
|
|
|
|
'arrayName' => 'array',
|
|
|
|
|
'fields' => [
|
|
|
|
|
'testField' => [
|
|
|
|
|
'type' => 'text',
|
|
|
|
|
'label' => 'Test 1'
|
|
|
|
|
],
|
|
|
|
|
'testRestricted' => [
|
|
|
|
|
'type' => 'text',
|
|
|
|
|
'label' => 'Test 2',
|
|
|
|
|
'permissions' => ($singlePermission) ? 'test.access_field' : [
|
|
|
|
|
'test.access_field'
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
}
|
2018-04-06 01:05:31 +00:00
|
|
|
}
|