From 2e4e031e1caf0ce9813e2715df0b110a3cb96d30 Mon Sep 17 00:00:00 2001 From: Mihail Geshoski Date: Fri, 15 May 2020 11:06:07 +0800 Subject: [PATCH] MDL-67812 core_contentbank: Modify behat contentbank content generator Enables the behat contentbank content generator to create content in multiple contexts and optionaly create files on the filesystem --- lib/behat/classes/behat_core_generator.php | 43 +++++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/lib/behat/classes/behat_core_generator.php b/lib/behat/classes/behat_core_generator.php index cbb6905529c..2aa6049770f 100644 --- a/lib/behat/classes/behat_core_generator.php +++ b/lib/behat/classes/behat_core_generator.php @@ -219,8 +219,8 @@ class behat_core_generator extends behat_generator_base { ], 'contentbank content' => [ 'datagenerator' => 'contentbank_content', - 'required' => array('contenttype', 'user', 'contentname'), - 'switchids' => array('course' => 'courseid', 'user' => 'userid') + 'required' => array('contextlevel', 'reference', 'contenttype', 'user', 'contentname'), + 'switchids' => array('user' => 'userid') ], ]; } @@ -823,27 +823,50 @@ class behat_core_generator extends behat_generator_base { } /** - * Create content in the given context's content bank + * Create content in the given context's content bank. * * @param array $data * @return void */ protected function process_contentbank_content(array $data) { - if (empty($data['contextid'])) { - if (empty($data['courseid'])) { - throw new Exception('contentbank_content requires the field course or contextid to be specified'); - } - $context = context_course::instance($data['courseid']); - } else { - $context = context::instance_by_id($data['contextid']); + global $CFG; + + if (empty($data['contextlevel'])) { + throw new Exception('contentbank_content requires the field contextlevel to be specified'); } + + if (!isset($data['reference'])) { + throw new Exception('contentbank_content requires the field reference to be specified'); + } + + if (empty($data['contenttype'])) { + throw new Exception('contentbank_content requires the field contenttype to be specified'); + } + $contenttypeclass = "\\".$data['contenttype']."\\contenttype"; if (class_exists($contenttypeclass)) { + $context = $this->get_context($data['contextlevel'], $data['reference']); $contenttype = new $contenttypeclass($context); $record = new stdClass(); $record->usercreated = $data['userid']; $record->name = $data['contentname']; $content = $contenttype->create_content($record); + + if (!empty($data['filepath'])) { + $fs = get_file_storage(); + $filerecord = array( + 'component' => 'contentbank', + 'filearea' => 'public', + 'contextid' => $context->id, + 'userid' => $data['userid'], + 'itemid' => $content->get_id(), + 'filename' => $data['contentname'], + 'filepath' => '/' + ); + $fs->create_file_from_pathname($filerecord, $CFG->dirroot . $data['filepath']); + } + } else { + throw new Exception('The specified "' . $data['contenttype'] . '" contenttype does not exist'); } } }