This commit is contained in:
Jun Pataleta 2024-02-06 12:27:18 +08:00
commit 381b8123be
No known key found for this signature in database
GPG key ID: F83510526D99E2C7
2 changed files with 71 additions and 0 deletions

View file

@ -70,6 +70,33 @@ class profile_field_text extends profile_field_base {
$mform->setType($this->inputname, PARAM_TEXT);
}
/**
* Process the data before it gets saved in database
*
* @param string|null $data
* @param stdClass $datarecord
* @return string|null
*/
public function edit_save_data_preprocess($data, $datarecord) {
if ($data === null) {
return null;
}
return core_text::substr($data, 0, $this->field->param2);
}
/**
* Convert external data (csv file) from value to key for processing later by edit_save_data_preprocess
*
* @param string $data
* @return string|null
*/
public function convert_external_data($data) {
if (core_text::strlen($data) > $this->field->param2) {
return null;
}
return $data;
}
/**
* Return the field type and null properties.
* This will be used for validating the data submitted by a user.

View file

@ -69,5 +69,49 @@ class field_class_test extends \advanced_testcase {
'emoticons_filter' => ['No emoticons filter :-(', 'No emoticons filter :-(']
];
}
/**
* Test preprocess data validation
*/
public function test_edit_save_data_preprocess(): void {
$this->resetAfterTest();
$fielddata = $this->getDataGenerator()->create_custom_profile_field([
'datatype' => 'text',
'name' => 'Test',
'shortname' => 'test',
'param2' => 5, // Max length.
]);
$field = new profile_field_text(0, 0, $fielddata);
$value = $field->edit_save_data_preprocess('ABCDE', new \stdClass());
$this->assertEquals('ABCDE', $value);
// Exceed max length.
$value = $field->edit_save_data_preprocess('ABCDEF', new \stdClass());
$this->assertEquals('ABCDE', $value);
}
/**
* Test external data validation
*/
public function test_convert_external_data(): void {
$this->resetAfterTest();
$fielddata = $this->getDataGenerator()->create_custom_profile_field([
'datatype' => 'text',
'name' => 'Test',
'shortname' => 'test',
'param2' => 5, // Max length.
]);
$field = new profile_field_text(0, 0, $fielddata);
$value = $field->convert_external_data('ABCDE');
$this->assertEquals('ABCDE', $value);
// Exceed max length.
$value = $field->convert_external_data('ABCDEF');
$this->assertNull($value);
}
}