MDL-75353 mod_data: always return str in data_generate_default_template

From now on, the data_generate_default_template method will always return
a string with the template content or an empty string when there is no
content available (for instance, when the database has no fields).
This method won't return a boolean anymore.
This commit is contained in:
Sara Arjona 2022-08-09 16:04:44 +02:00
parent 17ee072693
commit cb4b6f125b
4 changed files with 93 additions and 7 deletions

View file

@ -606,15 +606,16 @@ class data_field_base { // Base class for Database Field Types (see field/*/
* @param int $recordid the entry record * @param int $recordid the entry record
* @param bool $form print a form instead of data * @param bool $form print a form instead of data
* @param bool $update if the function update the $data object or not * @param bool $update if the function update the $data object or not
* @return bool|string the template content. * @return string the template content or an empty string if no content is available (for instance, when database has no fields).
*/ */
function data_generate_default_template(&$data, $template, $recordid = 0, $form = false, $update = true) { function data_generate_default_template(&$data, $template, $recordid = 0, $form = false, $update = true) {
global $DB; global $DB;
if (!$data && !$template) { if (!$data || !$template) {
return false; return '';
} }
// These templates are empty by default (they have no content).
$defaulttemplates = [ $defaulttemplates = [
'csstemplate', 'csstemplate',
'jstemplate', 'jstemplate',
@ -626,7 +627,8 @@ function data_generate_default_template(&$data, $template, $recordid = 0, $form
return ''; return '';
} }
// get all the fields for that database // Get all the fields for that database.
$str = '';
if ($fields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id')) { if ($fields = $DB->get_records('data_fields', array('dataid'=>$data->id), 'id')) {
$table = new html_table(); $table = new html_table();
@ -675,7 +677,6 @@ function data_generate_default_template(&$data, $template, $recordid = 0, $form
$table->data[] = $row; $table->data[] = $row;
} }
$str = '';
if ($template == 'listtemplate'){ if ($template == 'listtemplate'){
$str .= '##delcheck##'; $str .= '##delcheck##';
$str .= html_writer::empty_tag('br'); $str .= html_writer::empty_tag('br');
@ -695,10 +696,10 @@ function data_generate_default_template(&$data, $template, $recordid = 0, $form
$DB->update_record('data', $newdata); $DB->update_record('data', $newdata);
$data->{$template} = $str; $data->{$template} = $str;
} }
}
return $str; return $str;
} }
}
/** /**
* Build the form elements to manage tags for a record. * Build the form elements to manage tags for a record.

View file

@ -513,6 +513,18 @@ class externallib_test extends externallib_advanced_testcase {
*/ */
public function test_get_entries() { public function test_get_entries() {
global $DB; global $DB;
// Check the behaviour when the database has no entries.
$result = mod_data_external::get_entries($this->database->id);
$result = \external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result);
$this->assertEmpty($result['entries']);
$result = mod_data_external::get_entries($this->database->id, 0, true);
$result = \external_api::clean_returnvalue(mod_data_external::get_entries_returns(), $result);
$this->assertEmpty($result['entries']);
$this->assertEmpty($result['listviewcontents']);
// Add a few fields to the database.
list($entry11, $entry12, $entry13, $entry14, $entry21) = self::populate_database_with_entries(); list($entry11, $entry12, $entry13, $entry14, $entry21) = self::populate_database_with_entries();
// First of all, expect to see only my group entries (not other users in other groups ones). // First of all, expect to see only my group entries (not other users in other groups ones).

View file

@ -24,6 +24,8 @@
*/ */
namespace mod_data; namespace mod_data;
use stdClass;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
global $CFG; global $CFG;
@ -1968,4 +1970,73 @@ class lib_test extends \advanced_testcase {
); );
$generator->create_instance($params); $generator->create_instance($params);
} }
/**
* Test for data_generate_default_template(). This method covers different scenarios for checking when the returned value
* is empty or not, but doesn't check if the content has the expected value when it's not empty.
*
* @covers ::data_generate_default_template
*/
public function test_data_generate_default_template(): void {
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
// Check the result is empty when $data and/or $template are null.
$nullactivity = null;
$result = data_generate_default_template($nullactivity, 'listtemplate', 0, false, false);
$this->assertEmpty($result);
$result = data_generate_default_template($activity, null, 0, false, false);
$this->assertEmpty($result);
$result = data_generate_default_template($nullactivity, null, 0, false, false);
$this->assertEmpty($result);
// Check the result is empty when any of the templates that are empty are given.
$emptytemplates = [
'csstemplate',
'jstemplate',
'listtemplateheader',
'listtemplatefooter',
'rsstitletemplate',
];
foreach ($emptytemplates as $emptytemplate) {
$result = data_generate_default_template($activity, $emptytemplate, 0, false, false);
$this->assertEmpty($result);
}
$templates = [
'listtemplate',
'singletemplate',
'asearchtemplate',
];
// Check the result is empty when the database has no entries.
foreach ($templates as $template) {
$result = data_generate_default_template($activity, $template, 0, false, false);
$this->assertEmpty($result);
$this->assertEmpty($activity->{$template});
}
// Add a field to the activity.
$fieldrecord = new stdClass();
$fieldrecord->name = 'field-1';
$fieldrecord->type = 'text';
$datagenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
$datagenerator->create_field($fieldrecord, $activity);
// Check the result is not empty when the database has no entries.
foreach ($templates as $template) {
$result = data_generate_default_template($activity, $template, 0, false, false);
$this->assertNotEmpty($result);
$this->assertEmpty($activity->{$template});
}
// Check the result is not empty when the database has no entries and the result is saved when $update = true.
foreach ($templates as $template) {
$result = data_generate_default_template($activity, $template, 0, false, true);
$this->assertNotEmpty($result);
$this->assertNotEmpty($activity->{$template});
}
}
} }

View file

@ -15,6 +15,8 @@ information provided here is intended especially for developers.
- is_directory_a_preset - is_directory_a_preset
* mod_data_external::add_entry() function throws an error when trying to add an entry to a database with no field created. * mod_data_external::add_entry() function throws an error when trying to add an entry to a database with no field created.
* data_user_can_add_entry() function returns false for any user if there is no field created on the database. * data_user_can_add_entry() function returns false for any user if there is no field created on the database.
* From now on, the data_generate_default_template method will always return a string with the template content or an empty
string when there is no content available.
=== 3.7 === === 3.7 ===
* External functions get_entries, get_entry and search_entries now return an additional field "tags" containing the entry tags. * External functions get_entries, get_entry and search_entries now return an additional field "tags" containing the entry tags.