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