MDL-68706 customfield_textarea: allow field content to be cleared.
authorPaul Holden <paulh@moodle.com>
Wed, 13 May 2020 20:45:58 +0000 (21:45 +0100)
committerPaul Holden <paulh@moodle.com>
Fri, 24 Jul 2020 12:44:41 +0000 (13:44 +0100)
customfield/field/textarea/classes/data_controller.php
customfield/field/textarea/tests/plugin_test.php

index 384918c..03d5656 100644 (file)
@@ -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'],
index ee846ef..a5cc62d 100644 (file)
@@ -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
      */