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   * Provides the {@link core_form_privacy_provider_testcase} class.
  19   *
  20   * @package     core_form
  21   * @category    test
  22   * @copyright   2018 David Mudrák <david@moodle.com>
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  use core_form\privacy\provider;
  27  use core_privacy\local\request\writer;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  /**
  32   * Unit tests for the privacy API implementation.
  33   *
  34   * @copyright 2018 David Mudrák <david@moodle.com>
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class core_form_privacy_provider_testcase extends \core_privacy\tests\provider_testcase {
  38  
  39      /**
  40       * When no preference exists, there should be no export.
  41       */
  42      public function test_no_preference() {
  43          global $USER;
  44          $this->resetAfterTest();
  45          $this->setAdminUser();
  46  
  47          provider::export_user_preferences($USER->id);
  48          $this->assertFalse(writer::with_context(\context_system::instance())->has_any_data());
  49      }
  50  
  51      /**
  52       * Test that the recently selected filepicker view mode is exported.
  53       *
  54       * @dataProvider data_filemanager_recentviewmode
  55       * @param string $val Value of the preference filemanager_recentviewmode
  56       * @param string $desc Text describing the preference
  57       */
  58      public function test_filemanager_recentviewmode(string $val, string $desc) {
  59          $this->resetAfterTest();
  60  
  61          // Create test user, add some preferences.
  62          $user = $this->getDataGenerator()->create_user();
  63          $this->setUser($user);
  64  
  65          set_user_preference('filemanager_recentviewmode', $val, $user);
  66  
  67          // Switch to admin user (so we can validate preferences of the correct user are being exported).
  68          $this->setAdminUser();
  69  
  70          // Export test users preferences.
  71          provider::export_user_preferences($user->id);
  72          $this->assertTrue(writer::with_context(\context_system::instance())->has_any_data());
  73  
  74          $prefs = writer::with_context(\context_system::instance())->get_user_preferences('core_form');
  75          $this->assertNotEmpty($prefs->filemanager_recentviewmode);
  76          $this->assertNotEmpty($prefs->filemanager_recentviewmode->value);
  77          $this->assertNotEmpty($prefs->filemanager_recentviewmode->description);
  78          $this->assertEquals($val, $prefs->filemanager_recentviewmode->value);
  79          $this->assertStringContainsString($desc, $prefs->filemanager_recentviewmode->description);
  80      }
  81  
  82      /**
  83       * Provides data for the {@link self::test_filemanager_recentviewmode()} method.
  84       *
  85       * @return array
  86       */
  87      public function data_filemanager_recentviewmode() {
  88          return [
  89              'icons' => [
  90                  'val' => '1',
  91                  'desc' => get_string('displayasicons', 'core_repository'),
  92              ],
  93              'tree' => [
  94                  'val' => '2',
  95                  'desc' => get_string('displayastree', 'core_repository'),
  96              ],
  97              'details' => [
  98                  'val' => '3',
  99                  'desc' => get_string('displaydetails', 'core_repository'),
 100              ],
 101              'unknown' => [
 102                  'val' => 'unexpectedvalue_foo_bar',
 103                  'desc' => 'unexpectedvalue_foo_bar',
 104              ],
 105          ];
 106      }
 107  }