diff --git a/mod/data/lib.php b/mod/data/lib.php index 631869b7ab9..e184e152bc6 100644 --- a/mod/data/lib.php +++ b/mod/data/lib.php @@ -606,15 +606,16 @@ class data_field_base { // Base class for Database Field Types (see field/*/ * @param int $recordid the entry record * @param bool $form print a form instead of data * @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) { global $DB; - if (!$data && !$template) { - return false; + if (!$data || !$template) { + return ''; } + // These templates are empty by default (they have no content). $defaulttemplates = [ 'csstemplate', 'jstemplate', @@ -626,7 +627,8 @@ function data_generate_default_template(&$data, $template, $recordid = 0, $form 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')) { $table = new html_table(); @@ -675,7 +677,6 @@ function data_generate_default_template(&$data, $template, $recordid = 0, $form $table->data[] = $row; } - $str = ''; if ($template == 'listtemplate'){ $str .= '##delcheck##'; $str .= html_writer::empty_tag('br'); @@ -695,9 +696,9 @@ function data_generate_default_template(&$data, $template, $recordid = 0, $form $DB->update_record('data', $newdata); $data->{$template} = $str; } - - return $str; } + + return $str; } /** diff --git a/mod/data/tests/externallib_test.php b/mod/data/tests/externallib_test.php index 671c3b9bba2..6198b0c2afa 100644 --- a/mod/data/tests/externallib_test.php +++ b/mod/data/tests/externallib_test.php @@ -513,6 +513,18 @@ class externallib_test extends externallib_advanced_testcase { */ public function test_get_entries() { 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(); // First of all, expect to see only my group entries (not other users in other groups ones). diff --git a/mod/data/tests/lib_test.php b/mod/data/tests/lib_test.php index 9ee50474e7e..84b09dd9026 100644 --- a/mod/data/tests/lib_test.php +++ b/mod/data/tests/lib_test.php @@ -24,6 +24,8 @@ */ namespace mod_data; +use stdClass; + defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -1968,4 +1970,73 @@ class lib_test extends \advanced_testcase { ); $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}); + } + } } diff --git a/mod/data/upgrade.txt b/mod/data/upgrade.txt index f3e4bcabc0f..1241338012c 100644 --- a/mod/data/upgrade.txt +++ b/mod/data/upgrade.txt @@ -15,6 +15,8 @@ information provided here is intended especially for developers. - 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. * 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 === * External functions get_entries, get_entry and search_entries now return an additional field "tags" containing the entry tags.