Skip to content

Commit

Permalink
MDL-48373 behat: generators for questions and categories
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed Dec 5, 2014
1 parent 6292878 commit 51a0c7c
Showing 1 changed file with 78 additions and 6 deletions.
84 changes: 78 additions & 6 deletions lib/tests/behat/behat_data_generators.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ class behat_data_generators extends behat_base {
'datagenerator' => 'enrol_user',
'required' => array('user', 'course', 'role'),
'switchids' => array('user' => 'userid', 'course' => 'courseid', 'role' => 'roleid')

),
'permission overrides' => array(
'datagenerator' => 'permission_override',
Expand Down Expand Up @@ -156,7 +155,17 @@ class behat_data_generators extends behat_base {
'datagenerator' => 'scale',
'required' => array('name', 'scale'),
'switchids' => array('course' => 'courseid')
)
),
'question categories' => array(
'datagenerator' => 'question_category',
'required' => array('name', 'contextlevel', 'reference'),
'switchids' => array('questioncategory' => 'category')
),
'questions' => array(
'datagenerator' => 'question',
'required' => array('qtype', 'questioncategory', 'name'),
'switchids' => array('questioncategory' => 'category', 'user' => 'createdby')
),
);

/**
Expand Down Expand Up @@ -473,6 +482,49 @@ protected function process_cohort_member($data) {
cohort_add_member($data['cohortid'], $data['userid']);
}

/**
* Create a question category.
*
* @param array $data the row of data from the behat script.
*/
protected function process_question_category($data) {
$context = $this->get_context($data['contextlevel'], $data['reference']);
$data['contextid'] = $context->id;
$this->datagenerator->get_plugin_generator('core_question')->create_question_category($data);
}

/**
* Create a question.
*
* Creating questions relies on the question/type/.../tests/helper.php mechanism.
* We start with test_question_maker::get_question_form_data($data['qtype'], $data['template'])
* and then overlay the values from any other fields of $data that are set.
*
* @param array $data the row of data from the behat script.
*/
protected function process_question($data) {
if (array_key_exists('questiontext', $data)) {
$data['questiontext'] = array(
'text' => $data['questiontext'],
'format' => FORMAT_HTML,
);
}

if (array_key_exists('generalfeedback', $data)) {
$data['generalfeedback'] = array(
'text' => $data['generalfeedback'],
'format' => FORMAT_HTML,
);
}

$which = null;
if (!empty($data['template'])) {
$which = $data['template'];
}

$this->datagenerator->get_plugin_generator('core_question')->create_question($data['qtype'], $which, $data);
}

/**
* Gets the grade category id from the grade category fullname
* @throws Exception
Expand Down Expand Up @@ -616,10 +668,9 @@ protected function get_outcome_id($shortname) {
}

/**
* Gets the course id from its name.
* @throws Exception
* @param string $name
* @return int
* Get the id of a named scale.
* @param string $name the name of the scale.
* @return int the scale id.
*/
protected function get_scale_id($name) {
global $DB;
Expand All @@ -630,6 +681,27 @@ protected function get_scale_id($name) {
return $id;
}

/**
* Get the id of a named question category (must be globally unique).
* Note that 'Top' is a special value, used when setting the parent of another
* category, meaning top-level.
*
* @param string $name the question category name.
* @return int the question category id.
*/
protected function get_questioncategory_id($name) {
global $DB;

if ($name == 'Top') {
return 0;
}

if (!$id = $DB->get_field('question_categories', 'id', array('name' => $name))) {
throw new Exception('The specified question category with name "' . $name . '" does not exist');
}
return $id;
}

/**
* Gets the internal context id from the context reference.
*
Expand Down

0 comments on commit 51a0c7c

Please sign in to comment.