| 1 | 
           efrain | 
           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 | 
           namespace core_editor\privacy;
  | 
        
        
            | 
            | 
           25 | 
              | 
        
        
            | 
            | 
           26 | 
           use core_privacy\local\metadata\collection;
  | 
        
        
            | 
            | 
           27 | 
           use core_privacy\local\request\writer;
  | 
        
        
            | 
            | 
           28 | 
           use core_editor\privacy\provider;
  | 
        
        
            | 
            | 
           29 | 
              | 
        
        
            | 
            | 
           30 | 
           defined('MOODLE_INTERNAL') || die();
  | 
        
        
            | 
            | 
           31 | 
              | 
        
        
            | 
            | 
           32 | 
           /**
  | 
        
        
            | 
            | 
           33 | 
            * Privacy provider tests class.
  | 
        
        
            | 
            | 
           34 | 
            *
  | 
        
        
            | 
            | 
           35 | 
            * @package    core_editor
  | 
        
        
            | 
            | 
           36 | 
            * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  | 
        
        
            | 
            | 
           37 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           38 | 
            */
  | 
        
        
            | 
            | 
           39 | 
           class provider_test extends \core_privacy\tests\provider_testcase {
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
               /**
  | 
        
        
            | 
            | 
           42 | 
                * When no preference exists, there should be no export.
  | 
        
        
            | 
            | 
           43 | 
                */
  | 
        
        
            | 
            | 
           44 | 
               public function test_no_preference() {
  | 
        
        
            | 
            | 
           45 | 
                   global $USER;
  | 
        
        
            | 
            | 
           46 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           47 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           48 | 
              | 
        
        
            | 
            | 
           49 | 
                   provider::export_user_preferences($USER->id);
  | 
        
        
            | 
            | 
           50 | 
                   /** @var \core_privacy\tests\request\content_writer $writer */
  | 
        
        
            | 
            | 
           51 | 
                   $writer = writer::with_context(\context_system::instance());
  | 
        
        
            | 
            | 
           52 | 
                   $this->assertFalse($writer->has_any_data());
  | 
        
        
            | 
            | 
           53 | 
               }
  | 
        
        
            | 
            | 
           54 | 
              | 
        
        
            | 
            | 
           55 | 
               /**
  | 
        
        
            | 
            | 
           56 | 
                * When preference exists but is empty, there should be no export.
  | 
        
        
            | 
            | 
           57 | 
                */
  | 
        
        
            | 
            | 
           58 | 
               public function test_empty_preference() {
  | 
        
        
            | 
            | 
           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('htmleditor', '', $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 | 
                   /** @var \core_privacy\tests\request\content_writer $writer */
  | 
        
        
            | 
            | 
           73 | 
                   $writer = writer::with_context(\context_system::instance());
  | 
        
        
            | 
            | 
           74 | 
                   $this->assertFalse($writer->has_any_data());
  | 
        
        
            | 
            | 
           75 | 
               }
  | 
        
        
            | 
            | 
           76 | 
              | 
        
        
            | 
            | 
           77 | 
               /**
  | 
        
        
            | 
            | 
           78 | 
                * When an editor is set, the name of that editor will be reported.
  | 
        
        
            | 
            | 
           79 | 
                */
  | 
        
        
            | 
            | 
           80 | 
               public function test_editor_atto() {
  | 
        
        
            | 
            | 
           81 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           82 | 
              | 
        
        
            | 
            | 
           83 | 
                   // Create test user, add some preferences.
  | 
        
        
            | 
            | 
           84 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           85 | 
                   $this->setUser($user);
  | 
        
        
            | 
            | 
           86 | 
              | 
        
        
            | 
            | 
           87 | 
                   set_user_preference('htmleditor', 'atto');
  | 
        
        
            | 
            | 
           88 | 
              | 
        
        
            | 
            | 
           89 | 
                   // Switch to admin user (so we can validate preferences of the correct user are being exported).
  | 
        
        
            | 
            | 
           90 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           91 | 
              | 
        
        
            | 
            | 
           92 | 
                   // Export test users preferences.
  | 
        
        
            | 
            | 
           93 | 
                   provider::export_user_preferences($user->id);
  | 
        
        
            | 
            | 
           94 | 
                   /** @var \core_privacy\tests\request\content_writer $writer */
  | 
        
        
            | 
            | 
           95 | 
                   $writer = writer::with_context(\context_system::instance());
  | 
        
        
            | 
            | 
           96 | 
                   $this->assertTrue($writer->has_any_data());
  | 
        
        
            | 
            | 
           97 | 
              | 
        
        
            | 
            | 
           98 | 
                   $prefs = $writer->get_user_preferences('core_editor');
  | 
        
        
            | 
            | 
           99 | 
                   $this->assertNotEmpty($prefs->htmleditor);
  | 
        
        
            | 
            | 
           100 | 
                   $this->assertNotEmpty($prefs->htmleditor->value);
  | 
        
        
            | 
            | 
           101 | 
                   $this->assertNotEmpty($prefs->htmleditor->description);
  | 
        
        
            | 
            | 
           102 | 
                   $this->assertEquals('atto', $prefs->htmleditor->value);
  | 
        
        
            | 
            | 
           103 | 
              | 
        
        
            | 
            | 
           104 | 
                   $this->assertEquals(
  | 
        
        
            | 
            | 
           105 | 
                       get_string(
  | 
        
        
            | 
            | 
           106 | 
                           'privacy:preference:htmleditor',
  | 
        
        
            | 
            | 
           107 | 
                           'core_editor',
  | 
        
        
            | 
            | 
           108 | 
                           get_string('pluginname', "editor_atto")
  | 
        
        
            | 
            | 
           109 | 
                       ),
  | 
        
        
            | 
            | 
           110 | 
                       $prefs->htmleditor->description
  | 
        
        
            | 
            | 
           111 | 
                   );
  | 
        
        
            | 
            | 
           112 | 
               }
  | 
        
        
            | 
            | 
           113 | 
           }
  |