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.
   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   * Unit Tests for the request helper.
  19   *
  20   * @package     core_privacy
  21   * @category    test
  22   * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  
  30  use \core_privacy\local\request\helper;
  31  use \core_privacy\local\request\writer;
  32  
  33  /**
  34   * Tests for the \core_privacy API's request helper functionality.
  35   *
  36   * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
  37   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   * @coversDefaultClass \core_privacy\local\request\helper
  39   */
  40  class request_helper_test extends advanced_testcase {
  41      /**
  42       * Test that basic module data is returned.
  43       *
  44       * @covers ::get_context_data
  45       */
  46      public function test_get_context_data_context_module() {
  47          $this->resetAfterTest();
  48  
  49          // Setup.
  50          $course = $this->getDataGenerator()->create_course();
  51          $user = \core_user::get_user_by_username('admin');
  52  
  53          $forum = $this->getDataGenerator()->create_module('forum', [
  54                  'course' => $course->id,
  55              ]);
  56          $context = context_module::instance($forum->cmid);
  57          $modinfo = get_fast_modinfo($course->id);
  58          $cm = $modinfo->cms[$context->instanceid];
  59  
  60          // Fetch the data.
  61          $result = helper::get_context_data($context, $user);
  62          $this->assertInstanceOf('stdClass', $result);
  63  
  64          // Check that the name matches.
  65          $this->assertEquals($cm->get_formatted_name(), $result->name);
  66  
  67          // This plugin supports the intro. Check that it is included and correct.
  68          $formattedintro = format_text($forum->intro, $forum->introformat, [
  69                  'noclean' => true,
  70                  'para' => false,
  71                  'context' => $context,
  72                  'overflowdiv' => true,
  73              ]);
  74          $this->assertEquals($formattedintro, $result->intro);
  75  
  76          // This function should only fetch data. It does not export it.
  77          $this->assertFalse(writer::with_context($context)->has_any_data());
  78      }
  79  
  80      /**
  81       * Test that basic block data is returned.
  82       *
  83       * @covers ::get_context_data
  84       */
  85      public function test_get_context_data_context_block() {
  86          $this->resetAfterTest();
  87  
  88          // Setup.
  89          $block = $this->getDataGenerator()->create_block('online_users');
  90          $context = context_block::instance($block->id);
  91          $user = \core_user::get_user_by_username('admin');
  92  
  93          // Fetch the data.
  94          $data = helper::get_context_data($context, $user);
  95          $this->assertEquals(get_string('pluginname', 'block_online_users'), $data->blocktype);
  96  
  97           // This function should only fetch data. It does not export it.
  98          $this->assertFalse(writer::with_context($context)->has_any_data());
  99      }
 100  
 101      /**
 102       * Test that a course moudle with completion tracking enabled has the completion data returned.
 103       *
 104       * @covers ::get_context_data
 105       */
 106      public function test_get_context_data_context_module_completion() {
 107          $this->resetAfterTest();
 108  
 109          // Create a module and set completion.
 110          $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
 111          $user = $this->getDataGenerator()->create_user();
 112          $this->getDataGenerator()->enrol_user($user->id, $course->id, 'student');
 113          $assign = $this->getDataGenerator()->create_module('assign', ['course' => $course->id, 'completion' => 1]);
 114          $context = context_module::instance($assign->cmid);
 115          $cm = get_coursemodule_from_id('assign', $assign->cmid);
 116  
 117          // Fetch context data.
 118          $contextdata = helper::get_context_data($context, $user);
 119  
 120          // Completion state is zero.
 121          // Check non completion for a user.
 122          $this->assertEquals(0, $contextdata->completion->state);
 123  
 124          // Complete the activity as a user.
 125          $completioninfo = new completion_info($course);
 126          $completioninfo->update_state($cm, COMPLETION_COMPLETE, $user->id);
 127  
 128          // Check that completion is now exported.
 129          $contextdata = helper::get_context_data($context, $user);
 130          $this->assertEquals(1, $contextdata->completion->state);
 131  
 132          // This function should only fetch data. It does not export it.
 133          $this->assertFalse(writer::with_context($context)->has_any_data());
 134      }
 135  
 136      /**
 137       * Test that when there are no files to export for a course module context, nothing is exported.
 138       *
 139       * @covers ::export_context_files
 140       */
 141      public function test_export_context_files_context_module_no_files() {
 142          $this->resetAfterTest();
 143  
 144          // Setup.
 145          $course = $this->getDataGenerator()->create_course();
 146          $user = \core_user::get_user_by_username('admin');
 147  
 148          $forum = $this->getDataGenerator()->create_module('forum', [
 149                  'course' => $course->id,
 150              ]);
 151          $context = context_module::instance($forum->cmid);
 152          $modinfo = get_fast_modinfo($course->id);
 153          $cm = $modinfo->cms[$context->instanceid];
 154  
 155          // Fetch the data.
 156          helper::export_context_files($context, $user);
 157  
 158          // This function should only fetch data. It does not export it.
 159          $this->assertFalse(writer::with_context($context)->has_any_data());
 160      }
 161  
 162      /**
 163       * Test that when there are no files to export for a course context, nothing is exported.
 164       *
 165       * @covers ::export_context_files
 166       */
 167      public function test_export_context_files_context_course_no_files() {
 168          $this->resetAfterTest();
 169  
 170          // Setup.
 171          $course = $this->getDataGenerator()->create_course();
 172          $user = \core_user::get_user_by_username('admin');
 173          $context = context_course::instance($course->id);
 174  
 175          // Fetch the data.
 176          helper::export_context_files($context, $user);
 177  
 178          // This function should only fetch data. It does not export it.
 179          $this->assertFalse(writer::with_context($context)->has_any_data());
 180      }
 181  
 182      /**
 183       * Test that when there are files to export for a course context, the files are exported.
 184       *
 185       * @covers ::export_context_files
 186       */
 187      public function test_export_context_files_context_course_intro_files() {
 188          $this->resetAfterTest();
 189  
 190          // Setup.
 191          $course = $this->getDataGenerator()->create_course();
 192          $user = \core_user::get_user_by_username('admin');
 193          $assign = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]);
 194          $context = context_module::instance($assign->cmid);
 195  
 196          // File details.
 197          $filerecord = array(
 198              'contextid' => $context->id,
 199              'component' => 'mod_assign',
 200              'filearea'  => 'intro',
 201              'itemid'    => 0,
 202              'filepath'  => '/',
 203              'filename'  => 'logo.png',
 204          );
 205  
 206          $content = file_get_contents(__DIR__ . '/fixtures/logo.png');
 207  
 208          // Store the file.
 209          $fs = get_file_storage();
 210          $file = $fs->create_file_from_string($filerecord, $content);
 211  
 212          // Fetch the data.
 213          helper::export_context_files($context, $user);
 214  
 215          // This should have resulted in the file being exported.
 216          $this->assertTrue(writer::with_context($context)->has_any_data());
 217      }
 218  
 219  }