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]

   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   * Genarator tests.
  19   *
  20   * @package    mod_feedback
  21   * @copyright  2013 Ankit Agarwal
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  global $CFG;
  26  
  27  /**
  28   * Genarator tests class.
  29   *
  30   * @package    mod_feedback
  31   * @copyright  2013 Ankit Agarwal
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class mod_feedback_generator_testcase extends advanced_testcase {
  35  
  36      public function test_create_instance() {
  37          global $DB;
  38          $this->resetAfterTest();
  39          $this->setAdminUser();
  40  
  41          $course = $this->getDataGenerator()->create_course();
  42  
  43          $this->assertFalse($DB->record_exists('feedback', array('course' => $course->id)));
  44          $feedback = $this->getDataGenerator()->create_module('feedback', array('course' => $course->id));
  45          $this->assertEquals(1, $DB->count_records('feedback', array('course' => $course->id)));
  46          $this->assertTrue($DB->record_exists('feedback', array('course' => $course->id)));
  47          $this->assertTrue($DB->record_exists('feedback', array('id' => $feedback->id)));
  48  
  49          $params = array('course' => $course->id, 'name' => 'One more feedback');
  50          $feedback = $this->getDataGenerator()->create_module('feedback', $params);
  51          $this->assertEquals(2, $DB->count_records('feedback', array('course' => $course->id)));
  52          $this->assertEquals('One more feedback', $DB->get_field_select('feedback', 'name', 'id = :id',
  53                  array('id' => $feedback->id)));
  54      }
  55  
  56      public function test_create_item_info() {
  57          global $DB;
  58          $this->resetAfterTest();
  59          $this->setAdminUser();
  60  
  61          $course = $this->getDataGenerator()->create_course();
  62          $feedback = $this->getDataGenerator()->create_module('feedback', array('course' => $course));
  63          $feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback');
  64  
  65          $item1 = $feedbackgenerator->create_item_info($feedback);
  66          $item2 = $feedbackgenerator->create_item_info($feedback, array('name' => 'Custom name'));
  67          $records = $DB->get_records('feedback_item', array('feedback' => $feedback->id), 'id');
  68          $this->assertCount(2, $records);
  69          $this->assertEquals($item1->id, $records[$item1->id]->id);
  70          $this->assertEquals($item2->id, $records[$item2->id]->id);
  71          $this->assertEquals('Custom name', $records[$item2->id]->name);
  72          $this->assertEquals('info', $records[$item1->id]->typ);
  73      }
  74  
  75      public function test_create_item_label() {
  76          global $DB;
  77          $this->resetAfterTest();
  78          $this->setAdminUser();
  79  
  80          $course = $this->getDataGenerator()->create_course();
  81          $feedback = $this->getDataGenerator()->create_module('feedback', array('course' => $course));
  82          $feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback');
  83  
  84          $item1 = $feedbackgenerator->create_item_label($feedback);
  85  
  86          $editor = array(
  87              'text' => "Custom editor",
  88              'format' => FORMAT_HTML,
  89              'itemid' => 0
  90          );
  91          $item2 = $feedbackgenerator->create_item_label($feedback, array('presentation_editor' => $editor));
  92          $records = $DB->get_records('feedback_item', array('feedback' => $feedback->id), 'id');
  93          $this->assertCount(2, $records);
  94          $this->assertEquals($item1->id, $records[$item1->id]->id);
  95          $this->assertEquals($item2->id, $records[$item2->id]->id);
  96          $this->assertEquals($editor['text'], $records[$item2->id]->presentation);
  97          $this->assertEquals('label', $records[$item1->id]->typ);
  98      }
  99  
 100      public function test_create_item_multichoice() {
 101          global $DB;
 102          $this->resetAfterTest();
 103          $this->setAdminUser();
 104  
 105          $course = $this->getDataGenerator()->create_course();
 106          $feedback = $this->getDataGenerator()->create_module('feedback', array('course' => $course));
 107          $feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback');
 108  
 109          $item1 = $feedbackgenerator->create_item_multichoice($feedback);
 110          $item2 = $feedbackgenerator->create_item_multichoice($feedback, array('values' => "1\n2\n3\n4\n5", 'horizontal' => 1));
 111          $records = $DB->get_records('feedback_item', array('feedback' => $feedback->id), 'id');
 112          $this->assertCount(2, $records);
 113          $this->assertEquals($item1->id, $records[$item1->id]->id);
 114          $this->assertEquals($item2->id, $records[$item2->id]->id);
 115          $this->assertEquals('r>>>>>a|b|c|d|e', $records[$item1->id]->presentation);
 116          $this->assertEquals('r>>>>>1|2|3|4|5<<<<<1', $records[$item2->id]->presentation);
 117          $this->assertEquals('multichoice', $records[$item1->id]->typ);
 118      }
 119  
 120      public function test_create_item_multichoicerated() {
 121          global $DB;
 122          $this->resetAfterTest();
 123          $this->setAdminUser();
 124  
 125          $course = $this->getDataGenerator()->create_course();
 126          $feedback = $this->getDataGenerator()->create_module('feedback', array('course' => $course));
 127          $feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback');
 128  
 129          $item1 = $feedbackgenerator->create_item_multichoicerated($feedback);
 130          $item2 = $feedbackgenerator->create_item_multichoicerated($feedback, array(
 131                      'values' => "0/1\n1/2\n2/3\n3/4\n4/5", 'horizontal' => 1));
 132          $records = $DB->get_records('feedback_item', array('feedback' => $feedback->id), 'id');
 133          $this->assertCount(2, $records);
 134          $this->assertEquals($item1->id, $records[$item1->id]->id);
 135          $this->assertEquals($item2->id, $records[$item2->id]->id);
 136          $this->assertEquals('r>>>>>0####a|1####b|2####c|3####d|4####e', $records[$item1->id]->presentation);
 137          $this->assertEquals('r>>>>>0####1|1####2|2####3|3####4|4####5<<<<<1', $records[$item2->id]->presentation);
 138          $this->assertEquals('multichoicerated', $records[$item1->id]->typ);
 139      }
 140  
 141      public function test_create_item_numeric() {
 142          global $DB;
 143          $this->resetAfterTest();
 144          $this->setAdminUser();
 145  
 146          $course = $this->getDataGenerator()->create_course();
 147          $feedback = $this->getDataGenerator()->create_module('feedback', array('course' => $course));
 148          $feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback');
 149  
 150          $item1 = $feedbackgenerator->create_item_numeric($feedback);
 151          $item2 = $feedbackgenerator->create_item_numeric($feedback, array('rangefrom' => '0', 'rangeto' => '10'));
 152          $records = $DB->get_records('feedback_item', array('feedback' => $feedback->id), 'id');
 153          $this->assertCount(2, $records);
 154          $this->assertEquals($item1->id, $records[$item1->id]->id);
 155          $this->assertEquals($item2->id, $records[$item2->id]->id);
 156          $this->assertEquals('-|-', $records[$item1->id]->presentation);
 157          $this->assertEquals('0|10', $records[$item2->id]->presentation);
 158          $this->assertEquals('numeric', $records[$item1->id]->typ);
 159      }
 160  
 161      public function test_create_item_textarea() {
 162          global $DB;
 163          $this->resetAfterTest();
 164          $this->setAdminUser();
 165  
 166          $course = $this->getDataGenerator()->create_course();
 167          $feedback = $this->getDataGenerator()->create_module('feedback', array('course' => $course));
 168          $feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback');
 169  
 170          $item1 = $feedbackgenerator->create_item_textarea($feedback);
 171          $item2 = $feedbackgenerator->create_item_textarea($feedback, array('itemwidth' => '20', 'itemheight' => '10'));
 172          $records = $DB->get_records('feedback_item', array('feedback' => $feedback->id), 'id');
 173          $this->assertCount(2, $records);
 174          $this->assertEquals($item1->id, $records[$item1->id]->id);
 175          $this->assertEquals($item2->id, $records[$item2->id]->id);
 176          $this->assertEquals('40|20', $records[$item1->id]->presentation);
 177          $this->assertEquals('20|10', $records[$item2->id]->presentation);
 178          $this->assertEquals('textarea', $records[$item1->id]->typ);
 179      }
 180  
 181      public function test_create_item_textfield() {
 182          global $DB;
 183          $this->resetAfterTest();
 184          $this->setAdminUser();
 185  
 186          $course = $this->getDataGenerator()->create_course();
 187          $feedback = $this->getDataGenerator()->create_module('feedback', array('course' => $course));
 188          $feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback');
 189  
 190          $item1 = $feedbackgenerator->create_item_textfield($feedback);
 191          $item2 = $feedbackgenerator->create_item_textfield($feedback, array('itemsize' => '20', 'itemmaxlength' => '10'));
 192          $records = $DB->get_records('feedback_item', array('feedback' => $feedback->id), 'id');
 193          $this->assertCount(2, $records);
 194          $this->assertEquals($item1->id, $records[$item1->id]->id);
 195          $this->assertEquals($item2->id, $records[$item2->id]->id);
 196          $this->assertEquals('20|30', $records[$item1->id]->presentation);
 197          $this->assertEquals('20|10', $records[$item2->id]->presentation);
 198          $this->assertEquals('textfield', $records[$item1->id]->typ);
 199      }
 200  
 201      public function test_create_item_pagebreak() {
 202          global $DB;
 203          $this->resetAfterTest();
 204          $this->setAdminUser();
 205  
 206          $course = $this->getDataGenerator()->create_course();
 207          $feedback = $this->getDataGenerator()->create_module('feedback', array('course' => $course));
 208          $feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback');
 209  
 210          // Try to create a page break in an empty feedback (no items).
 211          $feedbackgenerator->create_item_pagebreak($feedback);
 212          $records = $DB->get_records('feedback_item', array('feedback' => $feedback->id), 'id');
 213          $this->assertCount(0, $records);
 214  
 215          // Create at least one item before the pagebreak.
 216          $feedbackgenerator->create_item_textfield($feedback);
 217  
 218          // Now, create one pagebreak.
 219          $item1 = $feedbackgenerator->create_item_pagebreak($feedback);
 220          $records = $DB->get_records('feedback_item', array('feedback' => $feedback->id), 'id');
 221          $this->assertCount(2, $records);
 222  
 223          // This pagebreak won't be created (there is already one in the last position).
 224          $item2 = $feedbackgenerator->create_item_pagebreak($feedback);
 225          $this->assertFalse($item2);
 226          $records = $DB->get_records('feedback_item', array('feedback' => $feedback->id), 'id');
 227          $this->assertCount(2, $records);
 228      }
 229  }
 230