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   * Unit tests for the block_myoverview implementation of the privacy API.
  18   *
  19   * @package    block_myoverview
  20   * @category   test
  21   * @copyright  2018 Peter Dias <peter@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace block_myoverview\privacy;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use core_privacy\local\request\writer;
  29  use block_myoverview\privacy\provider;
  30  /**
  31   * Unit tests for the block_myoverview implementation of the privacy API.
  32   *
  33   * @copyright  2018 Peter Dias <peter@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class provider_test extends \core_privacy\tests\provider_testcase {
  37      /**
  38       * Ensure that export_user_preferences returns no data if the user has not visited the myoverview block.
  39       */
  40      public function test_export_user_preferences_no_pref() {
  41          $this->resetAfterTest();
  42          $user = $this->getDataGenerator()->create_user();
  43          provider::export_user_preferences($user->id);
  44          $writer = writer::with_context(\context_system::instance());
  45          $this->assertFalse($writer->has_any_data());
  46      }
  47  
  48      /**
  49       * Test the export_user_preferences given different inputs
  50       *
  51       * @param string $type The name of the user preference to get/set
  52       * @param string $value The value you are storing
  53       *
  54       * @dataProvider user_preference_provider
  55       */
  56      public function test_export_user_preferences($type, $value, $expected) {
  57          $this->resetAfterTest();
  58          $user = $this->getDataGenerator()->create_user();
  59          set_user_preference($type, $value, $user);
  60          provider::export_user_preferences($user->id);
  61          $writer = writer::with_context(\context_system::instance());
  62          $blockpreferences = $writer->get_user_preferences('block_myoverview');
  63          if (!$expected) {
  64              $expected = get_string($value, 'block_myoverview');
  65          }
  66          $this->assertEquals($expected, $blockpreferences->{$type}->value);
  67      }
  68  
  69      /**
  70       * Create an array of valid user preferences for the myoverview block.
  71       *
  72       * @return array Array of valid user preferences.
  73       */
  74      public function user_preference_provider() {
  75          return array(
  76              array('block_myoverview_user_sort_preference', 'lastaccessed', ''),
  77              array('block_myoverview_user_sort_preference', 'title', ''),
  78              array('block_myoverview_user_sort_preference', 'shortname', ''),
  79              array('block_myoverview_user_grouping_preference', 'allincludinghidden', ''),
  80              array('block_myoverview_user_grouping_preference', 'all', ''),
  81              array('block_myoverview_user_grouping_preference', 'inprogress', ''),
  82              array('block_myoverview_user_grouping_preference', 'future', ''),
  83              array('block_myoverview_user_grouping_preference', 'past', ''),
  84              array('block_myoverview_user_grouping_preference', 'hidden', ''),
  85              array('block_myoverview_user_grouping_preference', 'favourites', ''),
  86              array('block_myoverview_user_view_preference', 'card', ''),
  87              array('block_myoverview_user_view_preference', 'list', ''),
  88              array('block_myoverview_user_view_preference', 'summary', ''),
  89              array('block_myoverview_user_paging_preference', 12, 12)
  90          );
  91      }
  92  
  93      public function test_export_user_preferences_with_hidden_courses() {
  94          $this->resetAfterTest();
  95          $user = $this->getDataGenerator()->create_user();
  96          $name = "block_myoverview_hidden_course_1";
  97  
  98          set_user_preference($name, 1, $user);
  99          provider::export_user_preferences($user->id);
 100          $writer = writer::with_context(\context_system::instance());
 101          $blockpreferences = $writer->get_user_preferences('block_myoverview');
 102  
 103          $this->assertEquals(
 104              get_string("privacy:request:preference:set", 'block_myoverview', (object) [
 105                  'name' => $name,
 106                  'value' => 1,
 107              ]),
 108              $blockpreferences->{$name}->description
 109          );
 110      }
 111  }