From ac7cb0921b4efe997f1602619fc708c40ebb4428 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Wed, 13 May 2020 21:45:58 +0100 Subject: [PATCH] MDL-68706 customfield_textarea: allow field content to be cleared. --- .../textarea/classes/data_controller.php | 2 +- .../field/textarea/tests/plugin_test.php | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/customfield/field/textarea/classes/data_controller.php b/customfield/field/textarea/classes/data_controller.php index 384918c7cec..03d565644c5 100644 --- a/customfield/field/textarea/classes/data_controller.php +++ b/customfield/field/textarea/classes/data_controller.php @@ -96,7 +96,7 @@ class data_controller extends \core_customfield\data_controller { $this->save(); } - if ($fromform['text']) { + if (array_key_exists('text', $fromform)) { $textoptions = $this->value_editor_options(); $data = (object) ['field_editor' => $fromform]; $data = file_postupdate_standard_editor($data, 'field', $textoptions, $textoptions['context'], diff --git a/customfield/field/textarea/tests/plugin_test.php b/customfield/field/textarea/tests/plugin_test.php index ee846eff4ee..a5cc62d3ccb 100644 --- a/customfield/field/textarea/tests/plugin_test.php +++ b/customfield/field/textarea/tests/plugin_test.php @@ -145,6 +145,41 @@ class customfield_textarea_plugin_testcase extends advanced_testcase { $handler->instance_form_save($data); } + /** + * Test that instance form save empties the field content for blank values + */ + public function test_instance_form_save_clear(): void { + global $CFG; + + require_once("{$CFG->dirroot}/customfield/tests/fixtures/test_instance_form.php"); + + $this->setAdminUser(); + + $handler = $this->cfcat->get_handler(); + + // Set our custom field to a known value. + $submitdata = (array) $this->courses[1] + [ + 'customfield_myfield1_editor' => ['text' => 'I can see it in your eyes', 'format' => FORMAT_HTML], + 'customfield_myfield2_editor' => ['text' => 'I can see it in your smile', 'format' => FORMAT_HTML], + ]; + + core_customfield_test_instance_form::mock_submit($submitdata, []); + $form = new core_customfield_test_instance_form('post', ['handler' => $handler, 'instance' => $this->courses[1]]); + $handler->instance_form_save($form->get_data()); + + $this->assertEquals($submitdata['customfield_myfield1_editor']['text'], + core_customfield\data_controller::create($this->cfdata[1]->get('id'))->export_value()); + + // Now empty our non-required field. + $submitdata['customfield_myfield1_editor']['text'] = ''; + + core_customfield_test_instance_form::mock_submit($submitdata, []); + $form = new core_customfield_test_instance_form('post', ['handler' => $handler, 'instance' => $this->courses[1]]); + $handler->instance_form_save($form->get_data()); + + $this->assertEmpty(core_customfield\data_controller::create($this->cfdata[1]->get('id'))->export_value()); + } + /** * Test for data_controller::get_value and export_value */