[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/question/type/essay/tests/ -> question_test.php (source)

   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   * Unit tests for the essay question definition class.
  19   *
  20   * @package    qtype
  21   * @subpackage essay
  22   * @copyright  2009 The Open University
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  31  
  32  
  33  /**
  34   * Unit tests for the matching question definition class.
  35   *
  36   * @copyright  2009 The Open University
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class qtype_essay_question_test extends advanced_testcase {
  40      public function test_get_question_summary() {
  41          $essay = test_question_maker::make_an_essay_question();
  42          $essay->questiontext = 'Hello <img src="http://example.com/globe.png" alt="world" />';
  43          $this->assertEquals('Hello [world]', $essay->get_question_summary());
  44      }
  45  
  46      public function test_summarise_response() {
  47          $longstring = str_repeat('0123456789', 50);
  48          $essay = test_question_maker::make_an_essay_question();
  49          $this->assertEquals($longstring, $essay->summarise_response(
  50                  array('answer' => $longstring, 'answerformat' => FORMAT_HTML)));
  51      }
  52  
  53      public function test_is_same_response() {
  54          $essay = test_question_maker::make_an_essay_question();
  55  
  56          $essay->responsetemplate = '';
  57  
  58          $essay->start_attempt(new question_attempt_step(), 1);
  59  
  60          $this->assertTrue($essay->is_same_response(
  61                  array(),
  62                  array('answer' => '')));
  63  
  64          $this->assertTrue($essay->is_same_response(
  65                  array('answer' => ''),
  66                  array('answer' => '')));
  67  
  68          $this->assertTrue($essay->is_same_response(
  69                  array('answer' => ''),
  70                  array()));
  71  
  72          $this->assertFalse($essay->is_same_response(
  73                  array('answer' => 'Hello'),
  74                  array()));
  75  
  76          $this->assertFalse($essay->is_same_response(
  77                  array('answer' => 'Hello'),
  78                  array('answer' => '')));
  79  
  80          $this->assertFalse($essay->is_same_response(
  81                  array('answer' => 0),
  82                  array('answer' => '')));
  83  
  84          $this->assertFalse($essay->is_same_response(
  85                  array('answer' => ''),
  86                  array('answer' => 0)));
  87  
  88          $this->assertFalse($essay->is_same_response(
  89                  array('answer' => '0'),
  90                  array('answer' => '')));
  91  
  92          $this->assertFalse($essay->is_same_response(
  93                  array('answer' => ''),
  94                  array('answer' => '0')));
  95      }
  96  
  97      public function test_is_same_response_with_template() {
  98          $essay = test_question_maker::make_an_essay_question();
  99  
 100          $essay->responsetemplate = 'Once upon a time';
 101  
 102          $essay->start_attempt(new question_attempt_step(), 1);
 103  
 104          $this->assertTrue($essay->is_same_response(
 105                  array(),
 106                  array('answer' => 'Once upon a time')));
 107  
 108          $this->assertTrue($essay->is_same_response(
 109                  array('answer' => ''),
 110                  array('answer' => 'Once upon a time')));
 111  
 112          $this->assertTrue($essay->is_same_response(
 113                  array('answer' => 'Once upon a time'),
 114                  array('answer' => '')));
 115  
 116          $this->assertTrue($essay->is_same_response(
 117                  array('answer' => ''),
 118                  array()));
 119  
 120          $this->assertTrue($essay->is_same_response(
 121                  array('answer' => 'Once upon a time'),
 122                  array()));
 123  
 124          $this->assertFalse($essay->is_same_response(
 125                  array('answer' => 0),
 126                  array('answer' => '')));
 127  
 128          $this->assertFalse($essay->is_same_response(
 129                  array('answer' => ''),
 130                  array('answer' => 0)));
 131  
 132          $this->assertFalse($essay->is_same_response(
 133                  array('answer' => '0'),
 134                  array('answer' => '')));
 135  
 136          $this->assertFalse($essay->is_same_response(
 137                  array('answer' => ''),
 138                  array('answer' => '0')));
 139      }
 140  
 141      public function test_is_complete_response() {
 142          $this->resetAfterTest(true);
 143  
 144          // Create a new logged-in user, so we can test responses with attachments.
 145          $user = $this->getDataGenerator()->create_user();
 146          $this->setUser($user);
 147  
 148          // Create sample attachments to use in testing.
 149          $helper = test_question_maker::get_test_helper('essay');
 150          $attachments = array();
 151          for ($i = 0; $i < 4; ++$i) {
 152              $attachments[$i] = $helper->make_attachments_saver($i);
 153          }
 154  
 155          // Create the essay question under test.
 156          $essay = test_question_maker::make_an_essay_question();
 157          $essay->start_attempt(new question_attempt_step(), 1);
 158  
 159          // Test the "traditional" case, where we must recieve a response from the user.
 160          $essay->responserequired = 1;
 161          $essay->attachmentsrequired = 0;
 162          $essay->responseformat = 'editor';
 163  
 164          // The empty string should be considered an incomplete response, as should a lack of a response.
 165          $this->assertFalse($essay->is_complete_response(array('answer' => '')));
 166          $this->assertFalse($essay->is_complete_response(array()));
 167  
 168          // Any nonempty string should be considered a complete response.
 169          $this->assertTrue($essay->is_complete_response(array('answer' => 'A student response.')));
 170          $this->assertTrue($essay->is_complete_response(array('answer' => '0 times.')));
 171          $this->assertTrue($essay->is_complete_response(array('answer' => '0')));
 172  
 173          // Test the case where two files are required.
 174          $essay->attachmentsrequired = 2;
 175  
 176          // Attaching less than two files should result in an incomplete response.
 177          $this->assertFalse($essay->is_complete_response(array('answer' => 'A')));
 178          $this->assertFalse($essay->is_complete_response(
 179                  array('answer' => 'A', 'attachments' => $attachments[0])));
 180          $this->assertFalse($essay->is_complete_response(
 181                  array('answer' => 'A', 'attachments' => $attachments[1])));
 182  
 183          // Anything without response text should result in an incomplete response.
 184          $this->assertFalse($essay->is_complete_response(
 185                  array('answer' => '', 'attachments' => $attachments[2])));
 186  
 187          // Attaching two or more files should result in a complete response.
 188          $this->assertTrue($essay->is_complete_response(
 189                  array('answer' => 'A', 'attachments' => $attachments[2])));
 190          $this->assertTrue($essay->is_complete_response(
 191                  array('answer' => 'A', 'attachments' => $attachments[3])));
 192  
 193          // Test the case in which two files are required, but the inline
 194          // response is optional.
 195          $essay->responserequired = 0;
 196  
 197          $this->assertFalse($essay->is_complete_response(
 198                  array('answer' => '', 'attachments' => $attachments[1])));
 199  
 200          $this->assertTrue($essay->is_complete_response(
 201                  array('answer' => '', 'attachments' => $attachments[2])));
 202  
 203          // Test the case in which both the response and inline text are optional.
 204          $essay->attachmentsrequired = 0;
 205  
 206          // Providing no answer and no attachment should result in an incomplete
 207          // response.
 208          $this->assertFalse($essay->is_complete_response(
 209                  array('answer' => '')));
 210          $this->assertFalse($essay->is_complete_response(
 211                  array('answer' => '', 'attachments' => $attachments[0])));
 212  
 213          // Providing an answer _or_ an attachment should result in a complete
 214          // response.
 215          $this->assertTrue($essay->is_complete_response(
 216                  array('answer' => '', 'attachments' => $attachments[1])));
 217          $this->assertTrue($essay->is_complete_response(
 218                  array('answer' => 'Answer text.', 'attachments' => $attachments[0])));
 219  
 220          // Test the case in which we're in "no inline response" mode,
 221          // in which the response is not required (as it's not provided).
 222          $essay->reponserequired = 0;
 223          $essay->responseformat = 'noinline';
 224          $essay->attachmensrequired = 1;
 225  
 226          $this->assertFalse($essay->is_complete_response(
 227                  array()));
 228          $this->assertFalse($essay->is_complete_response(
 229                  array('attachments' => $attachments[0])));
 230  
 231          // Providing an attachment should result in a complete response.
 232          $this->assertTrue($essay->is_complete_response(
 233                  array('attachments' => $attachments[1])));
 234  
 235          // Ensure that responserequired is ignored when we're in inline response mode.
 236          $essay->reponserequired = 1;
 237          $this->assertTrue($essay->is_complete_response(
 238                  array('attachments' => $attachments[1])));
 239  
 240      }
 241  
 242  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1