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 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [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_data;
  18  
  19  /**
  20   * PHPUnit data generator testcase.
  21   *
  22   * @package    mod_data
  23   * @category   phpunit
  24   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   * @coversDefaultClass \mod_data_generator
  27   */
  28  class generator_test extends \advanced_testcase {
  29      /**
  30       * @covers ::create_instance
  31       */
  32      public function test_generator() {
  33          global $DB;
  34  
  35          $this->resetAfterTest(true);
  36  
  37          $this->assertEquals(0, $DB->count_records('data'));
  38  
  39          $course = $this->getDataGenerator()->create_course();
  40  
  41          /** @var mod_data_generator $generator */
  42          $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
  43          $this->assertInstanceOf('mod_data_generator', $generator);
  44          $this->assertEquals('data', $generator->get_modulename());
  45  
  46          $generator->create_instance(['course' => $course->id]);
  47          $generator->create_instance(['course' => $course->id]);
  48          $data = $generator->create_instance(['course' => $course->id]);
  49          $this->assertEquals(3, $DB->count_records('data'));
  50  
  51          $cm = get_coursemodule_from_instance('data', $data->id);
  52          $this->assertEquals($data->id, $cm->instance);
  53          $this->assertEquals('data', $cm->modname);
  54          $this->assertEquals($course->id, $cm->course);
  55  
  56          $context = \context_module::instance($cm->id);
  57          $this->assertEquals($data->cmid, $context->instanceid);
  58  
  59          // Test gradebook integration using low level DB access - DO NOT USE IN PLUGIN CODE!
  60          $data = $generator->create_instance(['course' => $course->id, 'assessed' => 1, 'scale' => 100]);
  61          $gitem = $DB->get_record('grade_items', [
  62              'courseid' => $course->id,
  63              'itemtype' => 'mod',
  64              'itemmodule' => 'data',
  65              'iteminstance' => $data->id,
  66          ]);
  67          $this->assertNotEmpty($gitem);
  68          $this->assertEquals(100, $gitem->grademax);
  69          $this->assertEquals(0, $gitem->grademin);
  70          $this->assertEquals(GRADE_TYPE_VALUE, $gitem->gradetype);
  71      }
  72  
  73      /**
  74       * @covers ::create_field
  75       */
  76      public function test_create_field() {
  77          global $DB;
  78  
  79          $this->resetAfterTest(true);
  80  
  81          $this->setAdminUser();
  82          $this->assertEquals(0, $DB->count_records('data'));
  83  
  84          $course = $this->getDataGenerator()->create_course();
  85  
  86          /** @var mod_data_generator $generator */
  87          $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
  88          $this->assertInstanceOf('mod_data_generator', $generator);
  89          $this->assertEquals('data', $generator->get_modulename());
  90  
  91          $data = $generator->create_instance(['course' => $course->id]);
  92          $this->assertEquals(1, $DB->count_records('data'));
  93  
  94          $cm = get_coursemodule_from_instance('data', $data->id);
  95          $this->assertEquals($data->id, $cm->instance);
  96          $this->assertEquals('data', $cm->modname);
  97          $this->assertEquals($course->id, $cm->course);
  98  
  99          $context = \context_module::instance($cm->id);
 100          $this->assertEquals($data->cmid, $context->instanceid);
 101  
 102          $fieldtypes = ['checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url'];
 103  
 104          $count = 1;
 105  
 106          // Creating test Fields with default parameter values.
 107          foreach ($fieldtypes as $fieldtype) {
 108              // Creating variables dynamically.
 109              $fieldname = 'field-' . $count;
 110              $record = new \stdClass();
 111              $record->name = $fieldname;
 112              $record->type = $fieldtype;
 113  
 114              ${$fieldname} = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_field($record, $data);
 115  
 116              $this->assertInstanceOf('data_field_' . $fieldtype, ${$fieldname});
 117              $count++;
 118          }
 119  
 120          $this->assertEquals(count($fieldtypes), $DB->count_records('data_fields', ['dataid' => $data->id]));
 121  
 122          $addtemplate = $DB->get_record('data', ['id' => $data->id], 'addtemplate');
 123          $addtemplate = $addtemplate->addtemplate;
 124  
 125          for ($i = 1; $i < $count; $i++) {
 126              $fieldname = 'field-' . $i;
 127              $position = strpos($addtemplate, '[[' . $fieldname . ']]');
 128              $this->assertIsNumeric($position);
 129              $this->assertGreaterThanOrEqual(0, $position);
 130          }
 131      }
 132  
 133      /**
 134       * @covers ::create_entry
 135       */
 136      public function test_create_entry() {
 137          global $DB;
 138  
 139          $this->resetAfterTest(true);
 140  
 141          $this->setAdminUser();
 142          $this->assertEquals(0, $DB->count_records('data'));
 143  
 144          $user1 = $this->getDataGenerator()->create_user();
 145          $course = $this->getDataGenerator()->create_course();
 146          $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
 147  
 148          $groupa = $this->getDataGenerator()->create_group(['courseid' => $course->id, 'name' => 'groupA']);
 149          $this->getDataGenerator()->create_group_member(['userid' => $user1->id, 'groupid' => $groupa->id]);
 150  
 151          /** @var mod_data_generator $generator */
 152          $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
 153          $this->assertInstanceOf('mod_data_generator', $generator);
 154          $this->assertEquals('data', $generator->get_modulename());
 155  
 156          $data = $generator->create_instance(['course' => $course->id]);
 157          $this->assertEquals(1, $DB->count_records('data'));
 158  
 159          $cm = get_coursemodule_from_instance('data', $data->id);
 160          $this->assertEquals($data->id, $cm->instance);
 161          $this->assertEquals('data', $cm->modname);
 162          $this->assertEquals($course->id, $cm->course);
 163  
 164          $context = \context_module::instance($cm->id);
 165          $this->assertEquals($data->cmid, $context->instanceid);
 166  
 167          $fieldtypes = ['checkbox', 'date', 'menu', 'multimenu', 'number', 'radiobutton', 'text', 'textarea', 'url',
 168              'latlong', 'file', 'picture',
 169          ];
 170  
 171          $count = 1;
 172  
 173          // Creating test Fields with default parameter values.
 174          foreach ($fieldtypes as $fieldtype) {
 175              // Creating variables dynamically.
 176              $fieldname = 'field-' . $count;
 177              $record = new \stdClass();
 178              $record->name = $fieldname;
 179              $record->type = $fieldtype;
 180              $record->required = 1;
 181  
 182              $this->getDataGenerator()->get_plugin_generator('mod_data')->create_field($record, $data);
 183              $count++;
 184          }
 185  
 186          $fields = $DB->get_records('data_fields', ['dataid' => $data->id], 'id');
 187  
 188          $contents = [];
 189          $contents[] = ['opt1', 'opt2', 'opt3', 'opt4'];
 190          $contents[] = '01-01-2037'; // It should be lower than 2038, to avoid failing on 32-bit windows.
 191          $contents[] = 'menu1';
 192          $contents[] = ['multimenu1', 'multimenu2', 'multimenu3', 'multimenu4'];
 193          $contents[] = '12345';
 194          $contents[] = 'radioopt1';
 195          $contents[] = 'text for testing';
 196          $contents[] = '<p>text area testing<br /></p>';
 197          $contents[] = ['example.url', 'sampleurl'];
 198          $contents[] = [-31.9489873, 115.8382036]; // Latlong.
 199          $contents[] = 'Filename.pdf'; // File - filename.
 200          $contents[] = ['Cat1234.jpg', 'Cat']; // Picture - filename with alt text.
 201          $count = 0;
 202          $fieldcontents = [];
 203          foreach ($fields as $fieldrecord) {
 204              $fieldcontents[$fieldrecord->id] = $contents[$count++];
 205          }
 206  
 207          $tags = ['Cats', 'mice'];
 208  
 209          $this->setUser($user1);
 210          $datarecordid = $this->getDataGenerator()->get_plugin_generator('mod_data')->create_entry(
 211              $data,
 212              $fieldcontents,
 213              $groupa->id,
 214              $tags
 215          );
 216  
 217          $this->assertEquals(1, $DB->count_records('data_records', ['dataid' => $data->id]));
 218          $this->assertEquals(count($contents), $DB->count_records('data_content', ['recordid' => $datarecordid]));
 219  
 220          $entry = $DB->get_record('data_records', ['id' => $datarecordid]);
 221          $this->assertEquals($entry->groupid, $groupa->id);
 222  
 223          $contents = $DB->get_records('data_content', ['recordid' => $datarecordid], 'id');
 224  
 225          $contentstartid = 0;
 226          $flag = 0;
 227          foreach ($contents as $key => $content) {
 228              if (!$flag++) {
 229                  $contentstartid = $key;
 230              }
 231              $this->assertFalse($content->content == null);
 232          }
 233  
 234          $this->assertEquals($contents[$contentstartid]->content, 'opt1##opt2##opt3##opt4');
 235          $this->assertEquals($contents[++$contentstartid]->content, '2114380800');
 236          $this->assertEquals($contents[++$contentstartid]->content, 'menu1');
 237          $this->assertEquals($contents[++$contentstartid]->content, 'multimenu1##multimenu2##multimenu3##multimenu4');
 238          $this->assertEquals($contents[++$contentstartid]->content, '12345');
 239          $this->assertEquals($contents[++$contentstartid]->content, 'radioopt1');
 240          $this->assertEquals($contents[++$contentstartid]->content, 'text for testing');
 241          $this->assertEquals($contents[++$contentstartid]->content, '<p>text area testing<br /></p>');
 242          $this->assertEquals($contents[$contentstartid]->content1, '1');
 243          $this->assertEquals($contents[++$contentstartid]->content, 'http://example.url');
 244          $this->assertEquals($contents[$contentstartid]->content1, 'sampleurl');
 245          $this->assertEquals(
 246              ['Cats', 'mice'],
 247              array_values(\core_tag_tag::get_item_tags_array('mod_data', 'data_records', $datarecordid))
 248          );
 249      }
 250  }