Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This file contains tests that walks essay questions through some attempts.
  19   *
  20   * @package   qtype_essay
  21   * @copyright 2013 The Open University
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  30  
  31  
  32  /**
  33   * Unit tests for the essay question type.
  34   *
  35   * @copyright 2013 The Open University
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class qtype_essay_walkthrough_testcase extends qbehaviour_walkthrough_test_base {
  39  
  40      protected function check_contains_textarea($name, $content = '', $height = 10) {
  41          $fieldname = $this->quba->get_field_prefix($this->slot) . $name;
  42  
  43          $this->assertTag(array('tag' => 'textarea',
  44                  'attributes' => array('cols' => '60', 'rows' => $height,
  45                          'name' => $fieldname)),
  46                  $this->currentoutput);
  47  
  48          if ($content) {
  49              $this->assertRegExp('/' . preg_quote(s($content), '/') . '/', $this->currentoutput);
  50          }
  51      }
  52  
  53      /**
  54       * Helper method: Store a test file with a given name and contents in a
  55       * draft file area.
  56       *
  57       * @param int $usercontextid user context id.
  58       * @param int $draftitemid draft item id.
  59       * @param string $filename filename.
  60       * @param string $contents file contents.
  61       */
  62      protected function save_file_to_draft_area($usercontextid, $draftitemid, $filename, $contents) {
  63          $fs = get_file_storage();
  64  
  65          $filerecord = new stdClass();
  66          $filerecord->contextid = $usercontextid;
  67          $filerecord->component = 'user';
  68          $filerecord->filearea = 'draft';
  69          $filerecord->itemid = $draftitemid;
  70          $filerecord->filepath = '/';
  71          $filerecord->filename = $filename;
  72          $fs->create_file_from_string($filerecord, $contents);
  73      }
  74  
  75      public function test_deferred_feedback_html_editor() {
  76          global $PAGE;
  77  
  78          // The current text editor depends on the users profile setting - so it needs a valid user.
  79          $this->setAdminUser();
  80          // Required to init a text editor.
  81          $PAGE->set_url('/');
  82  
  83          // Create an essay question.
  84          $q = test_question_maker::make_question('essay', 'editor');
  85          $this->start_attempt_at_question($q, 'deferredfeedback', 1);
  86  
  87          $prefix = $this->quba->get_field_prefix($this->slot);
  88          $fieldname = $prefix . 'answer';
  89          $response = '<p>The <b>cat</b> sat on the mat. Then it ate a <b>frog</b>.</p>';
  90  
  91          // Check the initial state.
  92          $this->check_current_state(question_state::$todo);
  93          $this->check_current_mark(null);
  94          $this->render();
  95          $this->check_contains_textarea('answer', '');
  96          $this->check_current_output(
  97                  $this->get_contains_question_text_expectation($q),
  98                  $this->get_does_not_contain_feedback_expectation());
  99          $this->check_step_count(1);
 100  
 101          // Save a response.
 102          $this->quba->process_all_actions(null, array(
 103              'slots'                    => $this->slot,
 104              $fieldname                 => $response,
 105              $fieldname . 'format'      => FORMAT_HTML,
 106              $prefix . ':sequencecheck' => '1',
 107          ));
 108  
 109          // Verify.
 110          $this->check_current_state(question_state::$complete);
 111          $this->check_current_mark(null);
 112          $this->check_step_count(2);
 113          $this->render();
 114          $this->check_contains_textarea('answer', $response);
 115          $this->check_current_output(
 116                  $this->get_contains_question_text_expectation($q),
 117                  $this->get_does_not_contain_feedback_expectation());
 118          $this->check_step_count(2);
 119  
 120          // Finish the attempt.
 121          $this->quba->finish_all_questions();
 122  
 123          // Verify.
 124          $this->check_current_state(question_state::$needsgrading);
 125          $this->check_current_mark(null);
 126          $this->render();
 127          $this->assertRegExp('/' . preg_quote($response, '/') . '/', $this->currentoutput);
 128          $this->check_current_output(
 129                  $this->get_contains_question_text_expectation($q),
 130                  $this->get_contains_general_feedback_expectation($q));
 131      }
 132  
 133      public function test_deferred_feedback_plain_text() {
 134  
 135          // Create an essay question.
 136          $q = test_question_maker::make_question('essay', 'plain');
 137          $this->start_attempt_at_question($q, 'deferredfeedback', 1);
 138  
 139          $prefix = $this->quba->get_field_prefix($this->slot);
 140          $fieldname = $prefix . 'answer';
 141          $response = "x < 1\nx > 0\nFrog & Toad were friends.";
 142  
 143          // Check the initial state.
 144          $this->check_current_state(question_state::$todo);
 145          $this->check_current_mark(null);
 146          $this->render();
 147          $this->check_contains_textarea('answer', '');
 148          $this->check_current_output(
 149                  $this->get_contains_question_text_expectation($q),
 150                  $this->get_does_not_contain_feedback_expectation());
 151          $this->check_step_count(1);
 152  
 153          // Save a response.
 154          $this->quba->process_all_actions(null, array(
 155              'slots'                    => $this->slot,
 156              $fieldname                 => $response,
 157              $fieldname . 'format'      => FORMAT_HTML,
 158              $prefix . ':sequencecheck' => '1',
 159          ));
 160  
 161          // Verify.
 162          $this->check_current_state(question_state::$complete);
 163          $this->check_current_mark(null);
 164          $this->check_step_count(2);
 165          $this->render();
 166          $this->check_contains_textarea('answer', $response);
 167          $this->check_current_output(
 168                  $this->get_contains_question_text_expectation($q),
 169                  $this->get_does_not_contain_feedback_expectation());
 170          $this->check_step_count(2);
 171  
 172          // Finish the attempt.
 173          $this->quba->finish_all_questions();
 174  
 175          // Verify.
 176          $this->check_current_state(question_state::$needsgrading);
 177          $this->check_current_mark(null);
 178          $this->render();
 179          $this->assertRegExp('/' . preg_quote(s($response), '/') . '/', $this->currentoutput);
 180          $this->check_current_output(
 181                  $this->get_contains_question_text_expectation($q),
 182                  $this->get_contains_general_feedback_expectation($q));
 183      }
 184  
 185      public function test_responsetemplate() {
 186          global $PAGE;
 187  
 188          // The current text editor depends on the users profile setting - so it needs a valid user.
 189          $this->setAdminUser();
 190          // Required to init a text editor.
 191          $PAGE->set_url('/');
 192  
 193          // Create an essay question.
 194          $q = test_question_maker::make_question('essay', 'responsetemplate');
 195          $this->start_attempt_at_question($q, 'deferredfeedback', 1);
 196  
 197          $prefix = $this->quba->get_field_prefix($this->slot);
 198          $fieldname = $prefix . 'answer';
 199  
 200          // Check the initial state.
 201          $this->check_current_state(question_state::$todo);
 202          $this->check_current_mark(null);
 203          $this->render();
 204          $this->check_contains_textarea('answer', 'Once upon a time');
 205          $this->check_current_output(
 206                  $this->get_contains_question_text_expectation($q),
 207                  $this->get_does_not_contain_feedback_expectation());
 208          $this->check_step_count(1);
 209  
 210          // Save.
 211          $this->quba->process_all_actions(null, array(
 212              'slots'                    => $this->slot,
 213              $fieldname                 => 'Once upon a time there was a little green frog.',
 214              $fieldname . 'format'      => FORMAT_HTML,
 215              $prefix . ':sequencecheck' => '1',
 216          ));
 217  
 218          // Verify.
 219          $this->check_current_state(question_state::$complete);
 220          $this->check_current_mark(null);
 221          $this->check_step_count(2);
 222          $this->render();
 223          $this->check_contains_textarea('answer', 'Once upon a time there was a little green frog.');
 224          $this->check_current_output(
 225                  $this->get_contains_question_text_expectation($q),
 226                  $this->get_does_not_contain_feedback_expectation());
 227          $this->check_step_count(2);
 228  
 229          // Finish the attempt.
 230          $this->quba->finish_all_questions();
 231  
 232          // Verify.
 233          $this->check_current_state(question_state::$needsgrading);
 234          $this->check_current_mark(null);
 235          $this->render();
 236          $this->assertRegExp('/' . preg_quote(s('Once upon a time there was a little green frog.'), '/') . '/', $this->currentoutput);
 237          $this->check_current_output(
 238                  $this->get_contains_question_text_expectation($q),
 239                  $this->get_contains_general_feedback_expectation($q));
 240      }
 241  
 242      public function test_deferred_feedback_html_editor_with_files_attempt_on_last() {
 243          global $CFG, $USER, $PAGE;
 244  
 245          $this->resetAfterTest(true);
 246          $this->setAdminUser();
 247          // Required to init a text editor.
 248          $PAGE->set_url('/');
 249          $usercontextid = context_user::instance($USER->id)->id;
 250          $fs = get_file_storage();
 251  
 252          // Create an essay question in the DB.
 253          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 254          $cat = $generator->create_question_category();
 255          $question = $generator->create_question('essay', 'editorfilepicker', array('category' => $cat->id));
 256  
 257          // Start attempt at the question.
 258          $q = question_bank::load_question($question->id);
 259          $this->start_attempt_at_question($q, 'deferredfeedback', 1);
 260  
 261          $this->check_current_state(question_state::$todo);
 262          $this->check_current_mark(null);
 263          $this->check_step_count(1);
 264  
 265          // Process a response and check the expected result.
 266          // First we need to get the draft item ids.
 267          $this->render();
 268          if (!preg_match('/env=editor&amp;.*?itemid=(\d+)&amp;/', $this->currentoutput, $matches)) {
 269              throw new coding_exception('Editor draft item id not found.');
 270          }
 271          $editordraftid = $matches[1];
 272          if (!preg_match('/env=filemanager&amp;action=browse&amp;.*?itemid=(\d+)&amp;/', $this->currentoutput, $matches)) {
 273              throw new coding_exception('File manager draft item id not found.');
 274          }
 275          $attachementsdraftid = $matches[1];
 276  
 277          $this->save_file_to_draft_area($usercontextid, $editordraftid, 'smile.txt', ':-)');
 278          $this->save_file_to_draft_area($usercontextid, $attachementsdraftid, 'greeting.txt', 'Hello world!');
 279          $this->process_submission(array(
 280                  'answer' => 'Here is a picture: <img src="' . $CFG->wwwroot .
 281                                  "/draftfile.php/{$usercontextid}/user/draft/{$editordraftid}/smile.txt" .
 282                                  '" alt="smile">.',
 283                  'answerformat' => FORMAT_HTML,
 284                  'answer:itemid' => $editordraftid,
 285                  'attachments' => $attachementsdraftid));
 286  
 287          $this->check_current_state(question_state::$complete);
 288          $this->check_current_mark(null);
 289          $this->check_step_count(2);
 290          $this->save_quba();
 291  
 292          // Save the same response again, and verify no new step is created.
 293          $this->load_quba();
 294  
 295          $this->render();
 296          if (!preg_match('/env=editor&amp;.*?itemid=(\d+)&amp;/', $this->currentoutput, $matches)) {
 297              throw new coding_exception('Editor draft item id not found.');
 298          }
 299          $editordraftid = $matches[1];
 300          if (!preg_match('/env=filemanager&amp;action=browse&amp;.*?itemid=(\d+)&amp;/', $this->currentoutput, $matches)) {
 301              throw new coding_exception('File manager draft item id not found.');
 302          }
 303          $attachementsdraftid = $matches[1];
 304  
 305          $this->process_submission(array(
 306                  'answer' => 'Here is a picture: <img src="' . $CFG->wwwroot .
 307                                  "/draftfile.php/{$usercontextid}/user/draft/{$editordraftid}/smile.txt" .
 308                                  '" alt="smile">.',
 309                  'answerformat' => FORMAT_HTML,
 310                  'answer:itemid' => $editordraftid,
 311                  'attachments' => $attachementsdraftid));
 312  
 313          $this->check_current_state(question_state::$complete);
 314          $this->check_current_mark(null);
 315          $this->check_step_count(2);
 316  
 317          // Now submit all and finish.
 318          $this->finish();
 319          $this->check_current_state(question_state::$needsgrading);
 320          $this->check_current_mark(null);
 321          $this->check_step_count(3);
 322          $this->save_quba();
 323  
 324          // Now start a new attempt based on the old one.
 325          $this->load_quba();
 326          $oldqa = $this->get_question_attempt();
 327  
 328          $q = question_bank::load_question($question->id);
 329          $this->quba = question_engine::make_questions_usage_by_activity('unit_test',
 330                  context_system::instance());
 331          $this->quba->set_preferred_behaviour('deferredfeedback');
 332          $this->slot = $this->quba->add_question($q, 1);
 333          $this->quba->start_question_based_on($this->slot, $oldqa);
 334  
 335          $this->check_current_state(question_state::$complete);
 336          $this->check_current_mark(null);
 337          $this->check_step_count(1);
 338          $this->save_quba();
 339  
 340          // Now save the same response again, and ensure that a new step is not created.
 341          $this->load_quba();
 342  
 343          $this->render();
 344          if (!preg_match('/env=editor&amp;.*?itemid=(\d+)&amp;/', $this->currentoutput, $matches)) {
 345              throw new coding_exception('Editor draft item id not found.');
 346          }
 347          $editordraftid = $matches[1];
 348          if (!preg_match('/env=filemanager&amp;action=browse&amp;.*?itemid=(\d+)&amp;/', $this->currentoutput, $matches)) {
 349              throw new coding_exception('File manager draft item id not found.');
 350          }
 351          $attachementsdraftid = $matches[1];
 352  
 353          $this->process_submission(array(
 354                  'answer' => 'Here is a picture: <img src="' . $CFG->wwwroot .
 355                                  "/draftfile.php/{$usercontextid}/user/draft/{$editordraftid}/smile.txt" .
 356                                  '" alt="smile">.',
 357                  'answerformat' => FORMAT_HTML,
 358                  'answer:itemid' => $editordraftid,
 359                  'attachments' => $attachementsdraftid));
 360  
 361          $this->check_current_state(question_state::$complete);
 362          $this->check_current_mark(null);
 363          $this->check_step_count(1);
 364      }
 365  
 366      public function test_deferred_feedback_html_editor_with_files_attempt_on_last_no_files_uploaded() {
 367          global $CFG, $USER, $PAGE;
 368  
 369          $this->resetAfterTest(true);
 370          $this->setAdminUser();
 371          // Required to init a text editor.
 372          $PAGE->set_url('/');
 373          $usercontextid = context_user::instance($USER->id)->id;
 374          $fs = get_file_storage();
 375  
 376          // Create an essay question in the DB.
 377          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 378          $cat = $generator->create_question_category();
 379          $question = $generator->create_question('essay', 'editorfilepicker', array('category' => $cat->id));
 380  
 381          // Start attempt at the question.
 382          $q = question_bank::load_question($question->id);
 383          $this->start_attempt_at_question($q, 'deferredfeedback', 1);
 384  
 385          $this->check_current_state(question_state::$todo);
 386          $this->check_current_mark(null);
 387          $this->check_step_count(1);
 388  
 389          // Process a response and check the expected result.
 390          // First we need to get the draft item ids.
 391          $this->render();
 392          if (!preg_match('/env=editor&amp;.*?itemid=(\d+)&amp;/', $this->currentoutput, $matches)) {
 393              throw new coding_exception('Editor draft item id not found.');
 394          }
 395          $editordraftid = $matches[1];
 396          if (!preg_match('/env=filemanager&amp;action=browse&amp;.*?itemid=(\d+)&amp;/', $this->currentoutput, $matches)) {
 397              throw new coding_exception('File manager draft item id not found.');
 398          }
 399          $attachementsdraftid = $matches[1];
 400  
 401          $this->process_submission(array(
 402                  'answer' => 'I refuse to draw you a picture, so there!',
 403                  'answerformat' => FORMAT_HTML,
 404                  'answer:itemid' => $editordraftid,
 405                  'attachments' => $attachementsdraftid));
 406  
 407          $this->check_current_state(question_state::$complete);
 408          $this->check_current_mark(null);
 409          $this->check_step_count(2);
 410          $this->save_quba();
 411  
 412          // Now submit all and finish.
 413          $this->finish();
 414          $this->check_current_state(question_state::$needsgrading);
 415          $this->check_current_mark(null);
 416          $this->check_step_count(3);
 417          $this->save_quba();
 418  
 419          // Now start a new attempt based on the old one.
 420          $this->load_quba();
 421          $oldqa = $this->get_question_attempt();
 422  
 423          $q = question_bank::load_question($question->id);
 424          $this->quba = question_engine::make_questions_usage_by_activity('unit_test',
 425                  context_system::instance());
 426          $this->quba->set_preferred_behaviour('deferredfeedback');
 427          $this->slot = $this->quba->add_question($q, 1);
 428          $this->quba->start_question_based_on($this->slot, $oldqa);
 429  
 430          $this->check_current_state(question_state::$complete);
 431          $this->check_current_mark(null);
 432          $this->check_step_count(1);
 433          $this->save_quba();
 434  
 435          // Check the display.
 436          $this->load_quba();
 437          $this->render();
 438          $this->assertRegExp('/I refuse to draw you a picture, so there!/', $this->currentoutput);
 439      }
 440  
 441      public function test_deferred_feedback_plain_attempt_on_last() {
 442          global $CFG, $USER;
 443  
 444          $this->resetAfterTest(true);
 445          $this->setAdminUser();
 446          $usercontextid = context_user::instance($USER->id)->id;
 447  
 448          // Create an essay question in the DB.
 449          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 450          $cat = $generator->create_question_category();
 451          $question = $generator->create_question('essay', 'plain', array('category' => $cat->id));
 452  
 453          // Start attempt at the question.
 454          $q = question_bank::load_question($question->id);
 455          $this->start_attempt_at_question($q, 'deferredfeedback', 1);
 456  
 457          $this->check_current_state(question_state::$todo);
 458          $this->check_current_mark(null);
 459          $this->check_step_count(1);
 460  
 461          // Process a response and check the expected result.
 462  
 463          $this->process_submission(array(
 464              'answer' => 'Once upon a time there was a frog called Freddy. He lived happily ever after.',
 465              'answerformat' => FORMAT_PLAIN,
 466          ));
 467  
 468          $this->check_current_state(question_state::$complete);
 469          $this->check_current_mark(null);
 470          $this->check_step_count(2);
 471          $this->save_quba();
 472  
 473          // Now submit all and finish.
 474          $this->finish();
 475          $this->check_current_state(question_state::$needsgrading);
 476          $this->check_current_mark(null);
 477          $this->check_step_count(3);
 478          $this->save_quba();
 479  
 480          // Now start a new attempt based on the old one.
 481          $this->load_quba();
 482          $oldqa = $this->get_question_attempt();
 483  
 484          $q = question_bank::load_question($question->id);
 485          $this->quba = question_engine::make_questions_usage_by_activity('unit_test',
 486                  context_system::instance());
 487          $this->quba->set_preferred_behaviour('deferredfeedback');
 488          $this->slot = $this->quba->add_question($q, 1);
 489          $this->quba->start_question_based_on($this->slot, $oldqa);
 490  
 491          $this->check_current_state(question_state::$complete);
 492          $this->check_current_mark(null);
 493          $this->check_step_count(1);
 494          $this->save_quba();
 495  
 496          // Check the display.
 497          $this->load_quba();
 498          $this->render();
 499          // Test taht no HTML comment has been added to the response.
 500          $this->assertRegExp('/Once upon a time there was a frog called Freddy. He lived happily ever after.(?!&lt;!--)/', $this->currentoutput);
 501          // Test for the hash of an empty file area.
 502          $this->assertStringNotContainsString('d41d8cd98f00b204e9800998ecf8427e', $this->currentoutput);
 503      }
 504  
 505      public function test_deferred_feedback_html_editor_with_files_attempt_wrong_filetypes() {
 506          global $CFG, $USER, $PAGE;
 507  
 508          $this->resetAfterTest(true);
 509          $this->setAdminUser();
 510          // Required to init a text editor.
 511          $PAGE->set_url('/');
 512          $usercontextid = context_user::instance($USER->id)->id;
 513          $fs = get_file_storage();
 514  
 515          // Create an essay question in the DB.
 516          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 517          $cat = $generator->create_question_category();
 518          $question = $generator->create_question('essay', 'editorfilepicker', array('category' => $cat->id));
 519  
 520          // Start attempt at the question.
 521          $q = question_bank::load_question($question->id);
 522          $q->filetypeslist = '.pdf,.docx';
 523          $this->start_attempt_at_question($q, 'deferredfeedback', 1);
 524  
 525          $this->check_current_state(question_state::$todo);
 526          $this->check_current_mark(null);
 527          $this->check_step_count(1);
 528  
 529          // Process a response and check the expected result.
 530          // First we need to get the draft item ids.
 531          $this->render();
 532          if (!preg_match('/env=editor&amp;.*?itemid=(\d+)&amp;/', $this->currentoutput, $matches)) {
 533              throw new coding_exception('Editor draft item id not found.');
 534          }
 535          $editordraftid = $matches[1];
 536          if (!preg_match('/env=filemanager&amp;action=browse&amp;.*?itemid=(\d+)&amp;/', $this->currentoutput, $matches)) {
 537              throw new coding_exception('File manager draft item id not found.');
 538          }
 539          $attachementsdraftid = $matches[1];
 540  
 541          $this->save_file_to_draft_area($usercontextid, $editordraftid, 'smile.txt', ':-)');
 542          $this->save_file_to_draft_area($usercontextid, $attachementsdraftid, 'greeting.txt', 'Hello world!');
 543          $this->process_submission(array(
 544              'answer' => 'Here is a picture: <img src="' . $CFG->wwwroot .
 545                  "/draftfile.php/{$usercontextid}/user/draft/{$editordraftid}/smile.txt" .
 546                  '" alt="smile">.',
 547              'answerformat' => FORMAT_HTML,
 548              'answer:itemid' => $editordraftid,
 549              'attachments' => $attachementsdraftid));
 550  
 551          $this->check_current_state(question_state::$invalid);
 552          $this->check_current_mark(null);
 553          $this->check_step_count(2);
 554          $this->save_quba();
 555  
 556          // Now submit all and finish.
 557          $this->finish();
 558          $this->check_current_state(question_state::$needsgrading);
 559          $this->check_current_mark(null);
 560          $this->check_step_count(3);
 561          $this->save_quba();
 562      }
 563  
 564      public function test_deferred_feedback_html_editor_with_files_attempt_correct_filetypes() {
 565          global $CFG, $USER, $PAGE;
 566  
 567          $this->resetAfterTest(true);
 568          $this->setAdminUser();
 569          // Required to init a text editor.
 570          $PAGE->set_url('/');
 571          $usercontextid = context_user::instance($USER->id)->id;
 572          $fs = get_file_storage();
 573  
 574          // Create an essay question in the DB.
 575          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 576          $cat = $generator->create_question_category();
 577          $question = $generator->create_question('essay', 'editorfilepicker', array('category' => $cat->id));
 578  
 579          // Start attempt at the question.
 580          $q = question_bank::load_question($question->id);
 581          $q->filetypeslist = '.txt,.docx';
 582          $this->start_attempt_at_question($q, 'deferredfeedback', 1);
 583  
 584          $this->check_current_state(question_state::$todo);
 585          $this->check_current_mark(null);
 586          $this->check_step_count(1);
 587  
 588          // Process a response and check the expected result.
 589          // First we need to get the draft item ids.
 590          $this->render();
 591          if (!preg_match('/env=editor&amp;.*?itemid=(\d+)&amp;/', $this->currentoutput, $matches)) {
 592              throw new coding_exception('Editor draft item id not found.');
 593          }
 594          $editordraftid = $matches[1];
 595          if (!preg_match('/env=filemanager&amp;action=browse&amp;.*?itemid=(\d+)&amp;/', $this->currentoutput, $matches)) {
 596              throw new coding_exception('File manager draft item id not found.');
 597          }
 598          $attachementsdraftid = $matches[1];
 599  
 600          $this->save_file_to_draft_area($usercontextid, $editordraftid, 'smile.txt', ':-)');
 601          $this->save_file_to_draft_area($usercontextid, $attachementsdraftid, 'greeting.txt', 'Hello world!');
 602          $this->process_submission(array(
 603              'answer' => 'Here is a picture: <img src="' . $CFG->wwwroot .
 604                  "/draftfile.php/{$usercontextid}/user/draft/{$editordraftid}/smile.txt" .
 605                  '" alt="smile">.',
 606              'answerformat' => FORMAT_HTML,
 607              'answer:itemid' => $editordraftid,
 608              'attachments' => $attachementsdraftid));
 609  
 610          $this->check_current_state(question_state::$complete);
 611          $this->check_current_mark(null);
 612          $this->check_step_count(2);
 613          $this->save_quba();
 614  
 615          // Now submit all and finish.
 616          $this->finish();
 617          $this->check_current_state(question_state::$needsgrading);
 618          $this->check_current_mark(null);
 619          $this->check_step_count(3);
 620          $this->save_quba();
 621      }
 622  }