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 39 and 310]

   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   * Class provider_test
  19   *
  20   * @package     core_customfield
  21   * @copyright   2019 Marina Glancy
  22   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use core_privacy\tests\provider_testcase;
  28  use core_privacy\local\request\approved_contextlist;
  29  use core_privacy\local\request\writer;
  30  use core_customfield\privacy\provider;
  31  
  32  /**
  33   * Class provider_test
  34   *
  35   * @package     core_customfield
  36   * @copyright   2019 Marina Glancy
  37   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class core_customfield_privacy_testcase extends provider_testcase {
  40  
  41      /**
  42       * Generate data.
  43       *
  44       * @return array
  45       */
  46      protected function generate_test_data(): array {
  47          $this->resetAfterTest();
  48  
  49          $generator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
  50          $cfcats[1] = $generator->create_category();
  51          $cfcats[2] = $generator->create_category();
  52          $cffields[11] = $generator->create_field(
  53              ['categoryid' => $cfcats[1]->get('id'), 'type' => 'checkbox']);
  54          $cffields[12] = $generator->create_field(
  55              ['categoryid' => $cfcats[1]->get('id'), 'type' => 'date']);
  56          $cffields[13] = $generator->create_field(
  57              ['categoryid' => $cfcats[1]->get('id'),
  58              'type' => 'select', 'configdata' => ['options' => "a\nb\nc"]]);
  59          $cffields[14] = $generator->create_field(
  60              ['categoryid' => $cfcats[1]->get('id'), 'type' => 'text']);
  61          $cffields[15] = $generator->create_field(
  62              ['categoryid' => $cfcats[1]->get('id'), 'type' => 'textarea']);
  63          $cffields[21] = $generator->create_field(
  64              ['categoryid' => $cfcats[2]->get('id')]);
  65          $cffields[22] = $generator->create_field(
  66              ['categoryid' => $cfcats[2]->get('id')]);
  67  
  68          $courses[1] = $this->getDataGenerator()->create_course();
  69          $courses[2] = $this->getDataGenerator()->create_course();
  70          $courses[3] = $this->getDataGenerator()->create_course();
  71  
  72          $generator->add_instance_data($cffields[11], $courses[1]->id, 1);
  73          $generator->add_instance_data($cffields[12], $courses[1]->id, 1546300800);
  74          $generator->add_instance_data($cffields[13], $courses[1]->id, 2);
  75          $generator->add_instance_data($cffields[14], $courses[1]->id, 'Hello1');
  76          $generator->add_instance_data($cffields[15], $courses[1]->id,
  77              ['text' => '<p>Hi there</p>', 'format' => FORMAT_HTML]);
  78  
  79          $generator->add_instance_data($cffields[21], $courses[1]->id, 'hihi1');
  80  
  81          $generator->add_instance_data($cffields[14], $courses[2]->id, 'Hello2');
  82  
  83          $generator->add_instance_data($cffields[21], $courses[2]->id, 'hihi2');
  84  
  85          $user = $this->getDataGenerator()->create_user();
  86          $this->setUser($user);
  87  
  88          return [
  89              'user' => $user,
  90              'cfcats' => $cfcats,
  91              'cffields' => $cffields,
  92              'courses' => $courses,
  93          ];
  94      }
  95  
  96      /**
  97       * Test for provider::get_metadata()
  98       */
  99      public function test_get_metadata() {
 100          $collection = new \core_privacy\local\metadata\collection('core_customfield');
 101          $collection = provider::get_metadata($collection);
 102          $this->assertNotEmpty($collection);
 103      }
 104  
 105      /**
 106       * Test for provider::get_customfields_data_contexts
 107       */
 108      public function test_get_customfields_data_contexts() {
 109          global $DB;
 110          [
 111              'cffields' => $cffields,
 112              'cfcats' => $cfcats,
 113              'courses' => $courses,
 114          ] = $this->generate_test_data();
 115  
 116          list($sql, $params) = $DB->get_in_or_equal([$courses[1]->id, $courses[2]->id], SQL_PARAMS_NAMED);
 117          $r = provider::get_customfields_data_contexts('core_course', 'course', '=0',
 118              $sql, $params);
 119          $this->assertEqualsCanonicalizing([context_course::instance($courses[1]->id)->id,
 120              context_course::instance($courses[2]->id)->id],
 121              $r->get_contextids());
 122      }
 123  
 124      /**
 125       * Test for provider::get_customfields_configuration_contexts()
 126       */
 127      public function test_get_customfields_configuration_contexts() {
 128          $this->generate_test_data();
 129  
 130          $r = provider::get_customfields_configuration_contexts('core_course', 'course');
 131          $this->assertEquals([context_system::instance()->id], $r->get_contextids());
 132      }
 133  
 134      /**
 135       * Test for provider::export_customfields_data()
 136       */
 137      public function test_export_customfields_data() {
 138          global $USER, $DB;
 139          $this->resetAfterTest();
 140          [
 141              'cffields' => $cffields,
 142              'cfcats' => $cfcats,
 143              'courses' => $courses,
 144          ] = $this->generate_test_data();
 145  
 146          // Hack one of the fields so it has an invalid field type.
 147          $invalidfieldid = $cffields[21]->get('id');
 148          $DB->update_record('customfield_field', ['id' => $invalidfieldid, 'type' => 'invalid']);
 149  
 150          $context = context_course::instance($courses[1]->id);
 151          $contextlist = new approved_contextlist($USER, 'core_customfield', [$context->id]);
 152          provider::export_customfields_data($contextlist, 'core_course', 'course', '=0', '=:i', ['i' => $courses[1]->id]);
 153          /** @var core_privacy\tests\request\content_writer $writer */
 154          $writer = writer::with_context($context);
 155  
 156          // Make sure that all and only data for the course1 was exported.
 157          // There is no way to fetch all data from writer as array so we need to fetch one-by-one for each data id.
 158          $invaldfieldischecked = false;
 159          foreach ($DB->get_records('customfield_data', []) as $dbrecord) {
 160              $data = $writer->get_data(['Custom fields data', $dbrecord->id]);
 161              if ($dbrecord->instanceid == $courses[1]->id) {
 162                  $this->assertEquals($dbrecord->fieldid, $data->fieldid);
 163                  $this->assertNotEmpty($data->fieldtype);
 164                  $this->assertNotEmpty($data->fieldshortname);
 165                  $this->assertNotEmpty($data->fieldname);
 166                  $invaldfieldischecked = $invaldfieldischecked ?: ($data->fieldid == $invalidfieldid);
 167              } else {
 168                  $this->assertEmpty($data);
 169              }
 170          }
 171  
 172          // Make sure field with was checked in this test.
 173          $this->assertTrue($invaldfieldischecked);
 174      }
 175  
 176      /**
 177       * Test for provider::delete_customfields_data()
 178       */
 179      public function test_delete_customfields_data() {
 180          global $USER, $DB;
 181          $this->resetAfterTest();
 182          [
 183              'cffields' => $cffields,
 184              'cfcats' => $cfcats,
 185              'courses' => $courses,
 186          ] = $this->generate_test_data();
 187  
 188          $approvedcontexts = new approved_contextlist($USER, 'core_course', [context_course::instance($courses[1]->id)->id]);
 189          provider::delete_customfields_data($approvedcontexts, 'core_course', 'course');
 190          $this->assertEmpty($DB->get_records('customfield_data', ['instanceid' => $courses[1]->id]));
 191          $this->assertNotEmpty($DB->get_records('customfield_data', ['instanceid' => $courses[2]->id]));
 192      }
 193  
 194      /**
 195       * Test for provider::delete_customfields_configuration()
 196       */
 197      public function test_delete_customfields_configuration() {
 198          global $USER, $DB;
 199          $this->resetAfterTest();
 200          [
 201              'cffields' => $cffields,
 202              'cfcats' => $cfcats,
 203              'courses' => $courses,
 204          ] = $this->generate_test_data();
 205  
 206          // Remember the list of fields in the category 2 before we delete it.
 207          $catid1 = $cfcats[1]->get('id');
 208          $catid2 = $cfcats[2]->get('id');
 209          $fids2 = $DB->get_fieldset_select('customfield_field', 'id', 'categoryid=?', [$catid2]);
 210          $this->assertNotEmpty($fids2);
 211          list($fsql, $fparams) = $DB->get_in_or_equal($fids2, SQL_PARAMS_NAMED);
 212          $this->assertNotEmpty($DB->get_records_select('customfield_data', 'fieldid ' . $fsql, $fparams));
 213  
 214          // A little hack here, modify customfields configuration so they have different itemids.
 215          $DB->update_record('customfield_category', ['id' => $catid2, 'itemid' => 1]);
 216          $contextlist = new approved_contextlist($USER, 'core_course', [context_system::instance()->id]);
 217          provider::delete_customfields_configuration($contextlist, 'core_course', 'course', '=:i', ['i' => 1]);
 218  
 219          // Make sure everything for category $catid2 is gone but present for $catid1.
 220          $this->assertEmpty($DB->get_records('customfield_category', ['id' => $catid2]));
 221          $this->assertEmpty($DB->get_records_select('customfield_field', 'id ' . $fsql, $fparams));
 222          $this->assertEmpty($DB->get_records_select('customfield_data', 'fieldid ' . $fsql, $fparams));
 223  
 224          $this->assertNotEmpty($DB->get_records('customfield_category', ['id' => $catid1]));
 225          $fids1 = $DB->get_fieldset_select('customfield_field', 'id', 'categoryid=?', [$catid1]);
 226          list($fsql1, $fparams1) = $DB->get_in_or_equal($fids1, SQL_PARAMS_NAMED);
 227          $this->assertNotEmpty($DB->get_records_select('customfield_field', 'id ' . $fsql1, $fparams1));
 228          $this->assertNotEmpty($DB->get_records_select('customfield_data', 'fieldid ' . $fsql1, $fparams1));
 229      }
 230  
 231      /**
 232       * Test for provider::delete_customfields_configuration_for_context()
 233       */
 234      public function test_delete_customfields_configuration_for_context() {
 235          global $USER, $DB;
 236          $this->resetAfterTest();
 237          [
 238              'cffields' => $cffields,
 239              'cfcats' => $cfcats,
 240              'courses' => $courses,
 241          ] = $this->generate_test_data();
 242  
 243          // Remember the list of fields in the category 2 before we delete it.
 244          $catid1 = $cfcats[1]->get('id');
 245          $catid2 = $cfcats[2]->get('id');
 246          $fids2 = $DB->get_fieldset_select('customfield_field', 'id', 'categoryid=?', [$catid2]);
 247          $this->assertNotEmpty($fids2);
 248          list($fsql, $fparams) = $DB->get_in_or_equal($fids2, SQL_PARAMS_NAMED);
 249          $this->assertNotEmpty($DB->get_records_select('customfield_data', 'fieldid ' . $fsql, $fparams));
 250  
 251          // A little hack here, modify customfields configuration so they have different contexts.
 252          $context = context_user::instance($USER->id);
 253          $DB->update_record('customfield_category', ['id' => $catid2, 'contextid' => $context->id]);
 254          provider::delete_customfields_configuration_for_context('core_course', 'course', $context);
 255  
 256          // Make sure everything for category $catid2 is gone but present for $catid1.
 257          $this->assertEmpty($DB->get_records('customfield_category', ['id' => $catid2]));
 258          $this->assertEmpty($DB->get_records_select('customfield_field', 'id ' . $fsql, $fparams));
 259          $this->assertEmpty($DB->get_records_select('customfield_data', 'fieldid ' . $fsql, $fparams));
 260  
 261          $this->assertNotEmpty($DB->get_records('customfield_category', ['id' => $catid1]));
 262          $fids1 = $DB->get_fieldset_select('customfield_field', 'id', 'categoryid=?', [$catid1]);
 263          list($fsql1, $fparams1) = $DB->get_in_or_equal($fids1, SQL_PARAMS_NAMED);
 264          $this->assertNotEmpty($DB->get_records_select('customfield_field', 'id ' . $fsql1, $fparams1));
 265          $this->assertNotEmpty($DB->get_records_select('customfield_data', 'fieldid ' . $fsql1, $fparams1));
 266      }
 267  
 268      /**
 269       * Test for provider::delete_customfields_data_for_context()
 270       */
 271      public function test_delete_customfields_data_for_context() {
 272          global $DB;
 273          $this->resetAfterTest();
 274          [
 275              'cffields' => $cffields,
 276              'cfcats' => $cfcats,
 277              'courses' => $courses,
 278          ] = $this->generate_test_data();
 279  
 280          provider::delete_customfields_data_for_context('core_course', 'course',
 281              context_course::instance($courses[1]->id));
 282          $fids2 = $DB->get_fieldset_select('customfield_field', 'id', '1=1', []);
 283          list($fsql, $fparams) = $DB->get_in_or_equal($fids2, SQL_PARAMS_NAMED);
 284          $fparams['course1'] = $courses[1]->id;
 285          $fparams['course2'] = $courses[2]->id;
 286          $this->assertEmpty($DB->get_records_select('customfield_data', 'instanceid = :course1 AND fieldid ' . $fsql, $fparams));
 287          $this->assertNotEmpty($DB->get_records_select('customfield_data', 'instanceid = :course2 AND fieldid ' . $fsql, $fparams));
 288      }
 289  }