Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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_data;
  18  
  19  use stdClass;
  20  
  21  /**
  22   * PHPUnit data generator testcase.
  23   *
  24   * @package    mod_data
  25   * @category   phpunit
  26   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   * @coversDefaultClass \mod_data_generator
  29   */
  30  class generator_test extends \advanced_testcase {
  31      /**
  32       * @covers ::create_instance
  33       */
  34      public function test_generator() {
  35          global $DB;
  36  
  37          $this->resetAfterTest(true);
  38  
  39          $this->assertEquals(0, $DB->count_records('data'));
  40  
  41          $course = $this->getDataGenerator()->create_course();
  42  
  43          /** @var mod_data_generator $generator */
  44          $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
  45          $this->assertInstanceOf('mod_data_generator', $generator);
  46          $this->assertEquals('data', $generator->get_modulename());
  47  
  48          $generator->create_instance(['course' => $course->id]);
  49          $generator->create_instance(['course' => $course->id]);
  50          $data = $generator->create_instance(['course' => $course->id]);
  51          $this->assertEquals(3, $DB->count_records('data'));
  52  
  53          $cm = get_coursemodule_from_instance('data', $data->id);
  54          $this->assertEquals($data->id, $cm->instance);
  55          $this->assertEquals('data', $cm->modname);
  56          $this->assertEquals($course->id, $cm->course);
  57  
  58          $context = \context_module::instance($cm->id);
  59          $this->assertEquals($data->cmid, $context->instanceid);
  60  
  61          // Test gradebook integration using low level DB access - DO NOT USE IN PLUGIN CODE!
  62          $data = $generator->create_instance(['course' => $course->id, 'assessed' => 1, 'scale' => 100]);
  63          $gitem = $DB->get_record('grade_items', [
  64              'courseid' => $course->id,
  65              'itemtype' => 'mod',
  66              'itemmodule' => 'data',
  67              'iteminstance' => $data->id,
  68          ]);
  69          $this->assertNotEmpty($gitem);
  70          $this->assertEquals(100, $gitem->grademax);
  71          $this->assertEquals(0, $gitem->grademin);
  72          $this->assertEquals(GRADE_TYPE_VALUE, $gitem->gradetype);
  73      }
  74  
  75      /**
  76       * @covers ::create_field
  77       */
  78      public function test_create_field() {
  79          global $DB;
  80  
  81          $this->resetAfterTest(true);
  82  
  83          $this->setAdminUser();
  84          $this->assertEquals(0, $DB->count_records('data'));
  85  
  86          $course = $this->getDataGenerator()->create_course();
  87  
  88          /** @var mod_data_generator $generator */
  89          $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
  90          $this->assertInstanceOf('mod_data_generator', $generator);
  91          $this->assertEquals('data', $generator->get_modulename());
  92  
  93          $data = $generator->create_instance(['course' => $course->id]);
  94          $this->assertEquals(1, $DB->count_records('data'));
  95  
  96          $cm = get_coursemodule_from_instance('data', $data->id);
  97          $this->assertEquals($data->id, $cm->instance);
  98          $this->assertEquals('data', $cm->modname);
  99          $this->assertEquals($course->id, $cm->course);
 100  
 101          $context = \context_module::instance($cm->id);
 102          $this->assertEquals($data->cmid, $context->instanceid);
 103  
 104          $fieldtypes = ['checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url'];
 105  
 106          $count = 1;
 107  
 108          // Creating test Fields with default parameter values.
 109          foreach ($fieldtypes as $fieldtype) {
 110              // Creating variables dynamically.
 111              $fieldname = 'field-' . $count;
 112              $record = new \stdClass();
 113              $record->name = $fieldname;
 114              $record->type = $fieldtype;
 115  
 116              ${$fieldname} = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_field($record, $data);
 117  
 118              $this->assertInstanceOf('data_field_' . $fieldtype, ${$fieldname});
 119              $count++;
 120          }
 121  
 122          $this->assertEquals(count($fieldtypes), $DB->count_records('data_fields', ['dataid' => $data->id]));
 123      }
 124  
 125      /**
 126       * @covers ::create_entry
 127       */
 128      public function test_create_entry() {
 129          global $DB;
 130  
 131          $this->resetAfterTest(true);
 132  
 133          $this->setAdminUser();
 134          $this->assertEquals(0, $DB->count_records('data'));
 135  
 136          $user1 = $this->getDataGenerator()->create_user();
 137          $course = $this->getDataGenerator()->create_course();
 138          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 139  
 140          $groupa = $this->getDataGenerator()->create_group(['courseid' => $course->id, 'name' => 'groupA']);
 141          $this->getDataGenerator()->create_group_member(['userid' => $user1->id, 'groupid' => $groupa->id]);
 142  
 143          /** @var mod_data_generator $generator */
 144          $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
 145          $this->assertInstanceOf('mod_data_generator', $generator);
 146          $this->assertEquals('data', $generator->get_modulename());
 147  
 148          $data = $generator->create_instance(['course' => $course->id]);
 149          $this->assertEquals(1, $DB->count_records('data'));
 150  
 151          $cm = get_coursemodule_from_instance('data', $data->id);
 152          $this->assertEquals($data->id, $cm->instance);
 153          $this->assertEquals('data', $cm->modname);
 154          $this->assertEquals($course->id, $cm->course);
 155  
 156          $context = \context_module::instance($cm->id);
 157          $this->assertEquals($data->cmid, $context->instanceid);
 158  
 159          $fieldtypes = ['checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url',
 160              'latlong', 'file', 'picture',
 161          ];
 162  
 163          $count = 1;
 164  
 165          // Creating test Fields with default parameter values.
 166          foreach ($fieldtypes as $fieldtype) {
 167              // Creating variables dynamically.
 168              $fieldname = 'field-' . $count;
 169              $record = new \stdClass();
 170              $record->name = $fieldname;
 171              $record->type = $fieldtype;
 172              $record->required = 1;
 173  
 174              $this->getDataGenerator()->get_plugin_generator('mod_data')->create_field($record, $data);
 175              $count++;
 176          }
 177  
 178          $fields = $DB->get_records('data_fields', ['dataid' => $data->id], 'id');
 179  
 180          $contents = [];
 181          $contents[] = ['opt1', 'opt2', 'opt3', 'opt4'];
 182          $contents[] = '01-01-2037'; // It should be lower than 2038, to avoid failing on 32-bit windows.
 183          $contents[] = 'menu1';
 184          $contents[] = ['multimenu1', 'multimenu2', 'multimenu3', 'multimenu4'];
 185          $contents[] = '12345';
 186          $contents[] = 'radioopt1';
 187          $contents[] = 'text for testing';
 188          $contents[] = '<p>text area testing<br /></p>';
 189          $contents[] = ['example.url', 'sampleurl'];
 190          $contents[] = [-31.9489873, 115.8382036]; // Latlong.
 191          $contents[] = 'Filename.pdf'; // File - filename.
 192          $contents[] = ['Cat1234.jpg', 'Cat']; // Picture - filename with alt text.
 193          $count = 0;
 194          $fieldcontents = [];
 195          foreach ($fields as $fieldrecord) {
 196              $fieldcontents[$fieldrecord->id] = $contents[$count++];
 197          }
 198  
 199          $tags = ['Cats', 'mice'];
 200  
 201          $this->setUser($user1);
 202          $datarecordid = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_entry(
 203              $data,
 204              $fieldcontents,
 205              $groupa->id,
 206              $tags
 207          );
 208  
 209          $this->assertEquals(1, $DB->count_records('data_records', ['dataid' => $data->id]));
 210          $this->assertEquals(count($contents), $DB->count_records('data_content', ['recordid' => $datarecordid]));
 211  
 212          $entry = $DB->get_record('data_records', ['id' => $datarecordid]);
 213          $this->assertEquals($entry->groupid, $groupa->id);
 214  
 215          $contents = $DB->get_records('data_content', ['recordid' => $datarecordid], 'id');
 216  
 217          $contentstartid = 0;
 218          $flag = 0;
 219          foreach ($contents as $key => $content) {
 220              if (!$flag++) {
 221                  $contentstartid = $key;
 222              }
 223              $this->assertFalse($content->content == null);
 224          }
 225  
 226          $this->assertEquals($contents[$contentstartid]->content, 'opt1##opt2##opt3##opt4');
 227          $this->assertEquals($contents[++$contentstartid]->content, '2114380800');
 228          $this->assertEquals($contents[++$contentstartid]->content, 'menu1');
 229          $this->assertEquals($contents[++$contentstartid]->content, 'multimenu1##multimenu2##multimenu3##multimenu4');
 230          $this->assertEquals($contents[++$contentstartid]->content, '12345');
 231          $this->assertEquals($contents[++$contentstartid]->content, 'radioopt1');
 232          $this->assertEquals($contents[++$contentstartid]->content, 'text for testing');
 233          $this->assertEquals($contents[++$contentstartid]->content, '<p>text area testing<br /></p>');
 234          $this->assertEquals($contents[$contentstartid]->content1, '1');
 235          $this->assertEquals($contents[++$contentstartid]->content, 'http://example.url');
 236          $this->assertEquals($contents[$contentstartid]->content1, 'sampleurl');
 237          $this->assertEquals(
 238              ['Cats', 'mice'],
 239              array_values(\core_tag_tag::get_item_tags_array('mod_data', 'data_records', $datarecordid))
 240          );
 241      }
 242  
 243      /**
 244       * Test for create_preset().
 245       *
 246       * @dataProvider create_preset_provider
 247       * @covers ::create_preset
 248       * @param stdClass|null $record data for the preset that will be created (like name or description)
 249       */
 250      public function test_create_preset(?stdClass $record) {
 251          global $USER;
 252  
 253          $this->resetAfterTest();
 254          $this->setAdminUser();
 255  
 256          $course = $this->getDataGenerator()->create_course();
 257          $activity = $this->getDataGenerator()->create_module(manager::MODULE, ['course' => $course]);
 258          $cm = get_coursemodule_from_id(manager::MODULE, $activity->cmid, 0, false, MUST_EXIST);
 259          if (!is_null($record) && property_exists($record, 'user')) {
 260              $user = $this->getDataGenerator()->create_and_enrol($course, 'teacher', (object) ['username' => $record->user]);
 261              $record->userid = $user->id;
 262              unset($record->user);
 263          }
 264  
 265          // Check initially there are no saved presets.
 266          $manager = manager::create_from_coursemodule($cm);
 267          $savedpresets = $manager->get_available_saved_presets();
 268          $this->assertEmpty($savedpresets);
 269  
 270          // Create one preset with the configuration in $record.
 271          $plugingenerator = $this->getDataGenerator()->get_plugin_generator('mod_data');
 272          $preset = $plugingenerator->create_preset($activity, $record);
 273          // Check the preset has been saved.
 274          $savedpresets = $manager->get_available_saved_presets();
 275          $this->assertCount(1, $savedpresets);
 276          // Check the preset name has the expected value.
 277          if (is_null($record) || !property_exists($record, 'name')) {
 278              $this->assertStringStartsWith('New preset', $preset->name);
 279          } else {
 280              $this->assertEquals($record->name, $preset->name);
 281          }
 282          // Check the preset description has the expected value.
 283          if (is_null($record) || !property_exists($record, 'description')) {
 284              $this->assertEmpty($preset->description);
 285          } else {
 286              $this->assertEquals($record->description, $preset->description);
 287          }
 288          // Check the preset author has the expected value.
 289          if (is_null($record) || !property_exists($record, 'userid')) {
 290              $this->assertEquals($USER->id, $preset->get_userid());
 291          } else {
 292              $this->assertEquals($record->userid, $preset->get_userid());
 293          }
 294          // Check the file has been updated properly.
 295          $this->assertNotNull($preset->storedfile);
 296      }
 297  
 298      /**
 299       * Data provider for test_create_preset().
 300       *
 301       * @return array
 302       */
 303      public function create_preset_provider(): array {
 304          return [
 305              'Create using the default configuration' => [
 306                  'record' => null,
 307              ],
 308              'Create with a given name but no description' => [
 309                  'record' => (object) [
 310                      'name' => 'World recipes preset',
 311                  ],
 312              ],
 313              'Create with a given description but no name' => [
 314                  'record' => (object) [
 315                      'description' => 'This is a preset to collect the most popular world recipes.',
 316                  ],
 317              ],
 318              'Create with a given name and description' => [
 319                  'record' => (object) [
 320                      'name' => 'World recipes preset',
 321                      'description' => 'This is a preset to collect the most popular world recipes.',
 322                  ],
 323              ],
 324              'Create with a given user but no description or name' => [
 325                  'record' => (object) [
 326                      'user' => 'teacher1',
 327                  ],
 328              ],
 329              'Create with a given name and user but no description' => [
 330                  'record' => (object) [
 331                      'name' => 'World recipes preset',
 332                      'user' => 'teacher1',
 333                  ],
 334              ],
 335              'Create with a given description and user but no name' => [
 336                  'record' => (object) [
 337                      'description' => 'This is a preset to collect the most popular world recipes.',
 338                      'user' => 'teacher1',
 339                  ],
 340              ],
 341              'Create with a given name, description and user' => [
 342                  'record' => (object) [
 343                      'name' => 'World recipes preset',
 344                      'description' => 'This is a preset to collect the most popular world recipes.',
 345                      'user' => 'teacher1',
 346                  ],
 347              ],
 348          ];
 349      }
 350  }