Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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  /**
  18   * Privacy provider tests.
  19   *
  20   * @package    core_editor
  21   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  use core_privacy\local\metadata\collection;
  26  use core_privacy\local\request\writer;
  27  use core_editor\privacy\provider;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  /**
  32   * Privacy provider tests class.
  33   *
  34   * @package    core_editor
  35   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class core_editor_privacy_provider_testcase extends \core_privacy\tests\provider_testcase {
  39  
  40      /**
  41       * When no preference exists, there should be no export.
  42       */
  43      public function test_no_preference() {
  44          global $USER;
  45          $this->resetAfterTest();
  46          $this->setAdminUser();
  47  
  48          provider::export_user_preferences($USER->id);
  49          $this->assertFalse(writer::with_context(\context_system::instance())->has_any_data());
  50      }
  51  
  52      /**
  53       * When preference exists but is empty, there should be no export.
  54       */
  55      public function test_empty_preference() {
  56          $this->resetAfterTest();
  57  
  58          // Create test user, add some preferences.
  59          $user = $this->getDataGenerator()->create_user();
  60          $this->setUser($user);
  61  
  62          set_user_preference('htmleditor', '', $user);
  63  
  64          // Switch to admin user (so we can validate preferences of the correct user are being exported).
  65          $this->setAdminUser();
  66  
  67          // Export test users preferences.
  68          provider::export_user_preferences($user->id);
  69          $this->assertFalse(writer::with_context(\context_system::instance())->has_any_data());
  70      }
  71  
  72      /**
  73       * When an editor is set, the name of that editor will be reported.
  74       */
  75      public function test_editor_atto() {
  76          $this->resetAfterTest();
  77  
  78          // Create test user, add some preferences.
  79          $user = $this->getDataGenerator()->create_user();
  80          $this->setUser($user);
  81  
  82          set_user_preference('htmleditor', 'atto');
  83  
  84          // Switch to admin user (so we can validate preferences of the correct user are being exported).
  85          $this->setAdminUser();
  86  
  87          // Export test users preferences.
  88          provider::export_user_preferences($user->id);
  89          $this->assertTrue(writer::with_context(\context_system::instance())->has_any_data());
  90  
  91          $prefs = writer::with_context(\context_system::instance())->get_user_preferences('core_editor');
  92          $this->assertNotEmpty($prefs->htmleditor);
  93          $this->assertNotEmpty($prefs->htmleditor->value);
  94          $this->assertNotEmpty($prefs->htmleditor->description);
  95          $this->assertEquals('atto', $prefs->htmleditor->value);
  96  
  97          $this->assertEquals(
  98              get_string(
  99                  'privacy:preference:htmleditor',
 100                  'core_editor',
 101                  get_string('pluginname', "editor_atto")
 102              ),
 103              $prefs->htmleditor->description
 104          );
 105      }
 106  }