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 core_backup;
  18  
  19  use core_backup_backup_restore_base_testcase;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  require_once ('backup_restore_base_testcase.php');
  25  require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  26  require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  27  
  28  /**
  29   * Backup restore permission tests.
  30   *
  31   * @package   core_backup
  32   * @author    Tomo Tsuyuki <tomotsuyuki@catalyst-au.net>
  33   * @copyright 2023 Catalyst IT Pty Ltd
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class backup_restore_group_test extends core_backup_backup_restore_base_testcase {
  37  
  38      /**
  39       * Test for backup/restore with customfields.
  40       * @covers \backup_groups_structure_step
  41       * @covers \restore_groups_structure_step
  42       */
  43      public function test_backup_restore_group_with_customfields(): void {
  44  
  45          $course1 = self::getDataGenerator()->create_course();
  46          $course2 = self::getDataGenerator()->create_course();
  47  
  48          $groupfieldcategory = self::getDataGenerator()->create_custom_field_category([
  49              'component' => 'core_group',
  50              'area' => 'group',
  51          ]);
  52          $groupcustomfield = self::getDataGenerator()->create_custom_field([
  53              'shortname' => 'testgroupcustomfield1',
  54              'type' => 'text',
  55              'categoryid' => $groupfieldcategory->get('id'),
  56          ]);
  57          $groupingfieldcategory = self::getDataGenerator()->create_custom_field_category([
  58              'component' => 'core_group',
  59              'area' => 'grouping',
  60          ]);
  61          $groupingcustomfield = self::getDataGenerator()->create_custom_field([
  62              'shortname' => 'testgroupingcustomfield1',
  63              'type' => 'text',
  64              'categoryid' => $groupingfieldcategory->get('id'),
  65          ]);
  66  
  67          $group1 = self::getDataGenerator()->create_group([
  68              'courseid' => $course1->id,
  69              'name' => 'Test group 1',
  70              'customfield_testgroupcustomfield1' => 'Custom input for group1',
  71          ]);
  72          $grouping1 = self::getDataGenerator()->create_grouping([
  73              'courseid' => $course1->id,
  74              'name' => 'Test grouping 1',
  75              'customfield_testgroupingcustomfield1' => 'Custom input for grouping1',
  76          ]);
  77  
  78          // Perform backup and restore.
  79          $backupid = $this->perform_backup($course1);
  80          $this->perform_restore($backupid, $course2);
  81  
  82          // Test group.
  83          $groups = groups_get_all_groups($course2->id);
  84          $this->assertCount(1, $groups);
  85          $group = reset($groups);
  86  
  87          // Confirm the group is not same group as original one.
  88          $this->assertNotEquals($group1->id, $group->id);
  89          $this->assertEquals($group1->name, $group->name);
  90  
  91          // Confirm custom field is restored in the new group.
  92          $grouphandler = \core_group\customfield\group_handler::create();
  93          $data = $grouphandler->export_instance_data_object($group->id);
  94          $this->assertSame('Custom input for group1', $data->testgroupcustomfield1);
  95  
  96          // Test grouping.
  97          $groupings = groups_get_all_groupings($course2->id);
  98          $this->assertCount(1, $groupings);
  99          $grouping = reset($groupings);
 100  
 101          // Confirm this is not same grouping as original one.
 102          $this->assertNotEquals($grouping1->id, $grouping->id);
 103  
 104          // Confirm custom field is restored in the new grouping.
 105          $groupinghandler = \core_group\customfield\grouping_handler::create();
 106          $data = $groupinghandler->export_instance_data_object($grouping->id);
 107          $this->assertSame('Custom input for grouping1', $data->testgroupingcustomfield1);
 108      }
 109  }