Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

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