From e0c8619823aa2633676b9258f5760d44d8a8ced8 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Wed, 8 Jan 2014 17:15:50 -0800 Subject: [PATCH] MDL-44316 test: added function to generate a tag --- lib/testing/generator/data_generator.php | 52 ++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/lib/testing/generator/data_generator.php b/lib/testing/generator/data_generator.php index d9550cbc803..b0efa09f72e 100644 --- a/lib/testing/generator/data_generator.php +++ b/lib/testing/generator/data_generator.php @@ -42,6 +42,7 @@ class testing_data_generator { protected $groupcount = 0; protected $groupingcount = 0; protected $rolecount = 0; + protected $tagcount = 0; /** @var array list of plugin generators */ protected $generators = array(); @@ -744,6 +745,57 @@ EOD; return $newroleid; } + /** + * Create a tag. + * + * @param array|stdClass $record + * @return stdClass the tag record + */ + public function create_tag($record = null) { + global $DB, $USER; + + $this->tagcount++; + $i = $this->tagcount; + + $record = (array) $record; + + if (!isset($record['userid'])) { + $record['userid'] = $USER->id; + } + + if (!isset($record['name'])) { + $record['name'] = 'Tag name ' . $i; + } + + if (!isset($record['rawname'])) { + $record['rawname'] = 'Raw tag name ' . $i; + } + + if (!isset($record['tagtype'])) { + $record['tagtype'] = 'default'; + } + + if (!isset($record['description'])) { + $record['description'] = 'Tag description'; + } + + if (!isset($record['descriptionformat'])) { + $record['descriptionformat'] = FORMAT_MOODLE; + } + + if (!isset($record['flag'])) { + $record['flag'] = 0; + } + + if (!isset($record['timemodified'])) { + $record['timemodified'] = time(); + } + + $id = $DB->insert_record('tag', $record); + + return $DB->get_record('tag', array('id' => $id), '*', MUST_EXIST); + } + /** * Helper method which combines $defaults with the values specified in $record. * If $record is an object, it is converted to an array.