From 9dca130faee47d598cc6f4e6febe337a652e1f4d Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Fri, 11 Sep 2020 11:33:44 -0600 Subject: [PATCH] Add unit tests for the different ways of providing field options --- tests/unit/backend/widgets/FormTest.php | 76 +++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/tests/unit/backend/widgets/FormTest.php b/tests/unit/backend/widgets/FormTest.php index d373a1d86..211fc97a9 100644 --- a/tests/unit/backend/widgets/FormTest.php +++ b/tests/unit/backend/widgets/FormTest.php @@ -6,7 +6,28 @@ use October\Tests\Fixtures\Backend\Models\UserFixture; class FormTestModel extends Model { + public function modelCustomOptionsMethod() + { + return ['model', 'custom', 'options']; + } + public function getFieldNameOnModelOptionsMethodOptions() + { + return ['model', 'field name', 'options method']; + } + + public function getDropdownOptions() + { + return ['dropdown', 'options']; + } +} + +class FormHelper +{ + public static function staticMethodOptions() + { + return ['static', 'method']; + } } class FormTest extends PluginTestCase @@ -131,6 +152,61 @@ class FormTest extends PluginTestCase $this->assertEquals('[name="array[trigger][]"]', array_get($attributes, 'data-trigger')); } + public function testOptionsGeneration() + { + $form = new Form(null, [ + 'model' => new FormTestModel, + 'arrayName' => 'array', + 'fields' => [ + 'static_method_options' => [ + 'type' => 'dropdown', + 'options' => '\FormHelper::staticMethodOptions', + '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']); + } + } + protected function restrictedFormFixture(bool $singlePermission = false) { return new Form(null, [