Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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 qbank_customfields;
  18  
  19  /**
  20   * Class qbank_customfields_question_handler_testcase
  21   *
  22   * @package     qbank_customfields
  23   * @copyright   2021 Catalyst IT Australia Pty Ltd
  24   * @author      Matt Porritt <mattp@catalyst-au.net>
  25   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class question_handler_test extends \advanced_testcase {
  28  
  29      /**
  30       * Question setup helper method.
  31       *
  32       * @return int The question id.
  33       * @throws coding_exception
  34       */
  35      protected function setup_question(): int {
  36          $category = $this->getDataGenerator()->create_category();
  37          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  38          $context = \context_coursecat::instance($category->id);
  39          $questioncategory = $questiongenerator->create_question_category(['contextid' => $context->id]);
  40          $questiondata = ['category' => $questioncategory->id, 'idnumber' => 'q1'];
  41          $question = $questiongenerator->create_question('shortanswer', null, $questiondata);
  42  
  43          return $question->id;
  44  
  45      }
  46  
  47      /**
  48       * Test custom field data.
  49       */
  50      public function test_get_field_data(): void {
  51          $this->resetAfterTest();
  52          $fieldgenerator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
  53  
  54          $instanceid = $this->setup_question();
  55          $fieldvalue = 'test field text';
  56  
  57          $categorydata = new \stdClass();
  58          $categorydata->component = 'qbank_customfields';
  59          $categorydata->area = 'question';
  60          $categorydata->name = 'test category';
  61  
  62          $customfieldcatid = $fieldgenerator->create_category($categorydata)->get('id');
  63          $field = $fieldgenerator->create_field(['categoryid' => $customfieldcatid, 'type' => 'text', 'shortname' => 'f1']);
  64          $fieldgenerator->add_instance_data($field, $instanceid, $fieldvalue);
  65  
  66          // Get the field data.
  67          $customfieldhandler = customfield\question_handler::create();
  68          $fieldinstancedata = $customfieldhandler->get_field_data($field, $instanceid);
  69  
  70          $this->assertEquals($categorydata->name, $fieldinstancedata->get_field()->get_category()->get('name'));
  71          $this->assertEquals($fieldvalue, $fieldinstancedata->get_value());
  72      }
  73  
  74      /**
  75       * Test getting custom field data for table display.
  76       */
  77      public function test_display_custom_field_table(): void {
  78          $this->resetAfterTest();
  79          $fieldgenerator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
  80  
  81          $instanceid = $this->setup_question();
  82          $fieldvalue = 'test field text';
  83  
  84          $categorydata = new \stdClass();
  85          $categorydata->component = 'qbank_customfields';
  86          $categorydata->area = 'question';
  87          $categorydata->name = 'test category';
  88  
  89          $customfieldcatid = $fieldgenerator->create_category($categorydata)->get('id');
  90          $field = $fieldgenerator->create_field(['categoryid' => $customfieldcatid, 'type' => 'text', 'shortname' => 'f1']);
  91          $fieldgenerator->add_instance_data($field, $instanceid, $fieldvalue);
  92  
  93          // Get the field data.
  94          $customfieldhandler = customfield\question_handler::create();
  95          $fieldinstancedata = $customfieldhandler->get_field_data($field, $instanceid);
  96          $output = $customfieldhandler->display_custom_field_table($fieldinstancedata);
  97  
  98          $this->assertStringContainsString($fieldvalue, $output);
  99      }
 100  
 101      /**
 102       * Test getting categories and field data for a specific instance.
 103       */
 104      public function test_get_categories_fields_data(): void {
 105          $this->resetAfterTest();
 106          $fieldgenerator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
 107  
 108          $instanceid = $this->setup_question();
 109          $field1value = 'first field text';
 110          $field2value = 'second field text';
 111          $field3value = 'third field text';
 112  
 113          $categorydata = new \stdClass();
 114          $categorydata->component = 'qbank_customfields';
 115          $categorydata->area = 'question';
 116          $categorydata->name = 'test category';
 117  
 118          $customfieldcat1id = $fieldgenerator->create_category($categorydata)->get('id');
 119          $categorydata->name = 'test category two';
 120          $customfieldcat2id = $fieldgenerator->create_category($categorydata)->get('id');
 121  
 122          $field1 = $fieldgenerator->create_field(['categoryid' => $customfieldcat1id, 'type' => 'text', 'shortname' => 'f1']);
 123          $fieldgenerator->add_instance_data($field1, $instanceid, $field1value);
 124          $field2 = $fieldgenerator->create_field(['categoryid' => $customfieldcat1id, 'type' => 'text', 'shortname' => 'f2']);
 125          $fieldgenerator->add_instance_data($field2, $instanceid, $field2value);
 126  
 127          $field3 = $fieldgenerator->create_field(['categoryid' => $customfieldcat2id, 'type' => 'text', 'shortname' => 'f3']);
 128          $fieldgenerator->add_instance_data($field3, $instanceid, $field3value);
 129  
 130          // Get the field data.
 131          $customfieldhandler = customfield\question_handler::create();
 132          $outputdata = $customfieldhandler->get_categories_fields_data($instanceid);
 133  
 134          $this->assertEquals($field1value, $outputdata['test category'][0]['value']);
 135          $this->assertEquals($field2value, $outputdata['test category'][1]['value']);
 136          $this->assertEquals($field3value, $outputdata['test category two'][0]['value']);
 137  
 138          // While we're here, lets test the rendering of this data.
 139          $outputhtml = $customfieldhandler->display_custom_categories_fields($outputdata);
 140  
 141          $this->assertStringContainsString($field1value, $outputhtml);
 142          $this->assertStringContainsString($field2value, $outputhtml);
 143          $this->assertStringContainsString($field3value, $outputhtml);
 144      }
 145  }