This commit is contained in:
Sara Arjona 2024-03-13 12:18:46 +01:00
commit 6256d7c238
No known key found for this signature in database
2 changed files with 12 additions and 4 deletions

View File

@ -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();

View File

@ -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' => '<br />', '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('<div class="text_to_html">Value1</div>', $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());