diff --git a/customfield/field/textarea/classes/data_controller.php b/customfield/field/textarea/classes/data_controller.php index 2e0eee71cfa..f133cf1b4cb 100644 --- a/customfield/field/textarea/classes/data_controller.php +++ b/customfield/field/textarea/classes/data_controller.php @@ -153,7 +153,7 @@ class data_controller extends \core_customfield\data_controller { /** * Checks if the value is empty, overriding the base method to ensure it's the "text" element of our value being compared * - * @param mixed $value + * @param string|string[] $value * @return bool */ protected function is_empty($value): bool { @@ -232,6 +232,9 @@ class data_controller extends \core_customfield\data_controller { require_once($CFG->libdir . '/filelib.php'); $value = $this->get_value(); + if ($this->is_empty($value)) { + return null; + } if ($dataid = $this->get('id')) { $context = $this->get_context(); diff --git a/customfield/field/textarea/tests/plugin_test.php b/customfield/field/textarea/tests/plugin_test.php index bedef623054..d24e24ec344 100644 --- a/customfield/field/textarea/tests/plugin_test.php +++ b/customfield/field/textarea/tests/plugin_test.php @@ -28,10 +28,12 @@ use context_system; * @package customfield_textarea * @copyright 2019 Marina Glancy * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @covers \customfield_textarea\field_controller + * @covers \customfield_textarea\data_controller */ class plugin_test extends \advanced_testcase { - /** @var stdClass[] */ + /** @var \stdClass[] */ private $courses = []; /** @var \core_customfield\category_controller */ private $cfcat; @@ -64,7 +66,7 @@ class plugin_test extends \advanced_testcase { $this->cfdata[1] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[1]->id, ['text' => 'Value1', 'format' => FORMAT_MOODLE]); $this->cfdata[2] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[2]->id, - ['text' => 'Value2', 'format' => FORMAT_MOODLE]); + ['text' => '
', 'format' => FORMAT_MOODLE]); $this->setUser($this->getDataGenerator()->create_user()); } @@ -173,7 +175,7 @@ class plugin_test extends \advanced_testcase { $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()); + $this->assertNull(\core_customfield\data_controller::create($this->cfdata[1]->get('id'))->export_value()); } /** @@ -183,6 +185,9 @@ class plugin_test extends \advanced_testcase { $this->assertEquals('Value1', $this->cfdata[1]->get_value()); $this->assertEquals('
Value1
', $this->cfdata[1]->export_value()); + // Field with empty data. + $this->assertNull($this->cfdata[2]->export_value()); + // Field without data but with a default value. $d = \core_customfield\data_controller::create(0, null, $this->cfields[3]); $this->assertEquals('Value3', $d->get_value());