| 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 | 
            * Base class for unit tests for tool_mobile.
  | 
        
        
            | 
            | 
           18 | 
            *
  | 
        
        
            | 
            | 
           19 | 
            * @package    tool_mobile
  | 
        
        
            | 
            | 
           20 | 
            * @category   test
  | 
        
        
            | 
            | 
           21 | 
            * @copyright  2018 Carlos Escobedo <carlos@moodle.com>
  | 
        
        
            | 
            | 
           22 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           23 | 
            */
  | 
        
        
            | 
            | 
           24 | 
           namespace tool_mobile\privacy;
  | 
        
        
            | 
            | 
           25 | 
              | 
        
        
            | 
            | 
           26 | 
           defined('MOODLE_INTERNAL') || die();
  | 
        
        
            | 
            | 
           27 | 
              | 
        
        
            | 
            | 
           28 | 
           use core_privacy\local\request\writer;
  | 
        
        
            | 
            | 
           29 | 
           use core_privacy\local\request\transform;
  | 
        
        
            | 
            | 
           30 | 
           use core_privacy\local\request\approved_contextlist;
  | 
        
        
            | 
            | 
           31 | 
           use core_privacy\local\request\approved_userlist;
  | 
        
        
            | 
            | 
           32 | 
           use tool_mobile\privacy\provider;
  | 
        
        
            | 
            | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
           /**
  | 
        
        
            | 
            | 
           35 | 
            * Unit tests for the tool_mobile implementation of the privacy API.
  | 
        
        
            | 
            | 
           36 | 
            *
  | 
        
        
            | 
            | 
           37 | 
            * @copyright  2018 Carlos Escobedo <carlos@moodle.com>
  | 
        
        
            | 
            | 
           38 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           39 | 
            */
  | 
        
        
            | 
            | 
           40 | 
           class provider_test extends \core_privacy\tests\provider_testcase {
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
               /**
  | 
        
        
            | 
            | 
           43 | 
                * Basic setup for these tests.
  | 
        
        
            | 
            | 
           44 | 
                */
  | 
        
        
            | 
            | 
           45 | 
               public function setUp(): void {
  | 
        
        
            | 
            | 
           46 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           47 | 
               }
  | 
        
        
            | 
            | 
           48 | 
              | 
        
        
            | 
            | 
           49 | 
               /**
  | 
        
        
            | 
            | 
           50 | 
                * Test to check export_user_preferences.
  | 
        
        
            | 
            | 
           51 | 
                * returns user preferences data.
  | 
        
        
            | 
            | 
           52 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           53 | 
               public function test_export_user_preferences(): void {
  | 
        
        
           | 1 | 
           efrain | 
           54 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           55 | 
                   $expectedtime = time();
  | 
        
        
            | 
            | 
           56 | 
                   set_user_preference('tool_mobile_autologin_request_last', time(), $user);
  | 
        
        
            | 
            | 
           57 | 
                   provider::export_user_preferences($user->id);
  | 
        
        
            | 
            | 
           58 | 
                   $writer = writer::with_context(\context_system::instance());
  | 
        
        
            | 
            | 
           59 | 
                   $prefs = $writer->get_user_preferences('tool_mobile');
  | 
        
        
            | 
            | 
           60 | 
                   $time = transform::datetime($expectedtime);
  | 
        
        
            | 
            | 
           61 | 
                   $this->assertEquals($time, $prefs->tool_mobile_autologin_request_last->value);
  | 
        
        
            | 
            | 
           62 | 
                   $this->assertEquals(get_string('privacy:metadata:preference:tool_mobile_autologin_request_last', 'tool_mobile'),
  | 
        
        
            | 
            | 
           63 | 
                       $prefs->tool_mobile_autologin_request_last->description);
  | 
        
        
            | 
            | 
           64 | 
               }
  | 
        
        
            | 
            | 
           65 | 
              | 
        
        
            | 
            | 
           66 | 
               /**
  | 
        
        
            | 
            | 
           67 | 
                * Test getting the context for the user ID related to this plugin.
  | 
        
        
            | 
            | 
           68 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           69 | 
               public function test_get_contexts_for_userid(): void {
  | 
        
        
           | 1 | 
           efrain | 
           70 | 
                   // Create user and Mobile user keys.
  | 
        
        
            | 
            | 
           71 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           72 | 
                   $context = \context_user::instance($user->id);
  | 
        
        
            | 
            | 
           73 | 
                   $key = get_user_key('tool_mobile', $user->id);
  | 
        
        
            | 
            | 
           74 | 
                   $contextlist = provider::get_contexts_for_userid($user->id);
  | 
        
        
            | 
            | 
           75 | 
                   $this->assertEquals($context->id, $contextlist->current()->id);
  | 
        
        
            | 
            | 
           76 | 
               }
  | 
        
        
            | 
            | 
           77 | 
              | 
        
        
            | 
            | 
           78 | 
               /**
  | 
        
        
            | 
            | 
           79 | 
                * Test getting the users for a context related to this plugin.
  | 
        
        
            | 
            | 
           80 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           81 | 
               public function test_get_users_in_context(): void {
  | 
        
        
           | 1 | 
           efrain | 
           82 | 
                   $component = 'tool_mobile';
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
                   // Create users and Mobile user keys.
  | 
        
        
            | 
            | 
           85 | 
                   $user1 = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           86 | 
                   $user2 = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           87 | 
                   $context1 = \context_user::instance($user1->id);
  | 
        
        
            | 
            | 
           88 | 
                   $context2 = \context_user::instance($user2->id);
  | 
        
        
            | 
            | 
           89 | 
                   $key1 = get_user_key('tool_mobile', $user1->id);
  | 
        
        
            | 
            | 
           90 | 
                   $key2 = get_user_key('tool_mobile/qrlogin', $user1->id);
  | 
        
        
            | 
            | 
           91 | 
                   $key3 = get_user_key('tool_mobile', $user2->id);
  | 
        
        
            | 
            | 
           92 | 
              | 
        
        
            | 
            | 
           93 | 
                   // Ensure only user1 is found in context1.
  | 
        
        
            | 
            | 
           94 | 
                   $userlist = new \core_privacy\local\request\userlist($context1, $component);
  | 
        
        
            | 
            | 
           95 | 
                   provider::get_users_in_context($userlist);
  | 
        
        
            | 
            | 
           96 | 
                   $userids = $userlist->get_userids();
  | 
        
        
            | 
            | 
           97 | 
                   $userid = reset($userids);
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
                   $this->assertCount(1, $userids);
  | 
        
        
            | 
            | 
           100 | 
                   $this->assertEquals($user1->id, $userid);
  | 
        
        
            | 
            | 
           101 | 
               }
  | 
        
        
            | 
            | 
           102 | 
              | 
        
        
            | 
            | 
           103 | 
               /**
  | 
        
        
            | 
            | 
           104 | 
                * Test that data is exported correctly for this plugin.
  | 
        
        
            | 
            | 
           105 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           106 | 
               public function test_export_user_data(): void {
  | 
        
        
           | 1 | 
           efrain | 
           107 | 
                   global $DB;
  | 
        
        
            | 
            | 
           108 | 
                   // Create user and Mobile user keys.
  | 
        
        
            | 
            | 
           109 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           110 | 
                   $context = \context_user::instance($user->id);
  | 
        
        
            | 
            | 
           111 | 
                   $keyvalue = get_user_key('tool_mobile', $user->id);
  | 
        
        
            | 
            | 
           112 | 
                   $key = $DB->get_record('user_private_key', ['value' => $keyvalue]);
  | 
        
        
            | 
            | 
           113 | 
                   // Validate exported data.
  | 
        
        
            | 
            | 
           114 | 
                   $this->setUser($user);
  | 
        
        
            | 
            | 
           115 | 
                   /** @var \core_privacy\tests\request\content_writer $writer */
  | 
        
        
            | 
            | 
           116 | 
                   $writer = writer::with_context($context);
  | 
        
        
            | 
            | 
           117 | 
                   $this->assertFalse($writer->has_any_data());
  | 
        
        
            | 
            | 
           118 | 
                   $this->export_context_data_for_user($user->id, $context, 'tool_mobile');
  | 
        
        
            | 
            | 
           119 | 
                   $userkeydata = $writer->get_related_data([], 'userkeys');
  | 
        
        
            | 
            | 
           120 | 
                   $this->assertCount(1, $userkeydata->keys);
  | 
        
        
            | 
            | 
           121 | 
                   $this->assertEquals($key->script, reset($userkeydata->keys)->script);
  | 
        
        
            | 
            | 
           122 | 
               }
  | 
        
        
            | 
            | 
           123 | 
              | 
        
        
            | 
            | 
           124 | 
               /**
  | 
        
        
            | 
            | 
           125 | 
                * Test for provider::delete_data_for_all_users_in_context().
  | 
        
        
            | 
            | 
           126 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           127 | 
               public function test_delete_data_for_all_users_in_context(): void {
  | 
        
        
           | 1 | 
           efrain | 
           128 | 
                   global $DB;
  | 
        
        
            | 
            | 
           129 | 
                   // Create user and Mobile user keys.
  | 
        
        
            | 
            | 
           130 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           131 | 
                   $context = \context_user::instance($user->id);
  | 
        
        
            | 
            | 
           132 | 
                   $keyvalue = get_user_key('tool_mobile', $user->id);
  | 
        
        
            | 
            | 
           133 | 
                   $key = $DB->get_record('user_private_key', ['value' => $keyvalue]);
  | 
        
        
            | 
            | 
           134 | 
                   // Before deletion, we should have 1 user_private_key.
  | 
        
        
            | 
            | 
           135 | 
                   $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']);
  | 
        
        
            | 
            | 
           136 | 
                   $this->assertEquals(1, $count);
  | 
        
        
            | 
            | 
           137 | 
                   // Delete data.
  | 
        
        
            | 
            | 
           138 | 
                   provider::delete_data_for_all_users_in_context($context);
  | 
        
        
            | 
            | 
           139 | 
                   // After deletion, the user_private_key entries should have been deleted.
  | 
        
        
            | 
            | 
           140 | 
                   $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']);
  | 
        
        
            | 
            | 
           141 | 
                   $this->assertEquals(0, $count);
  | 
        
        
            | 
            | 
           142 | 
               }
  | 
        
        
            | 
            | 
           143 | 
              | 
        
        
            | 
            | 
           144 | 
               /**
  | 
        
        
            | 
            | 
           145 | 
                * Test for provider::delete_data_for_user().
  | 
        
        
            | 
            | 
           146 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           147 | 
               public function test_delete_data_for_user(): void {
  | 
        
        
           | 1 | 
           efrain | 
           148 | 
                   global $DB;
  | 
        
        
            | 
            | 
           149 | 
                   // Create user and Mobile user keys.
  | 
        
        
            | 
            | 
           150 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           151 | 
                   $context = \context_user::instance($user->id);
  | 
        
        
            | 
            | 
           152 | 
                   $keyvalue = get_user_key('tool_mobile', $user->id);
  | 
        
        
            | 
            | 
           153 | 
                   $key = $DB->get_record('user_private_key', ['value' => $keyvalue]);
  | 
        
        
            | 
            | 
           154 | 
                   // Before deletion, we should have 1 user_private_key.
  | 
        
        
            | 
            | 
           155 | 
                   $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']);
  | 
        
        
            | 
            | 
           156 | 
                   $this->assertEquals(1, $count);
  | 
        
        
            | 
            | 
           157 | 
                   // Delete data.
  | 
        
        
            | 
            | 
           158 | 
                   $contextlist = provider::get_contexts_for_userid($user->id);
  | 
        
        
            | 
            | 
           159 | 
                   $approvedcontextlist = new approved_contextlist($user, 'tool_mobile', $contextlist->get_contextids());
  | 
        
        
            | 
            | 
           160 | 
                   provider::delete_data_for_user($approvedcontextlist);
  | 
        
        
            | 
            | 
           161 | 
                   // After deletion, the user_private_key entries should have been deleted.
  | 
        
        
            | 
            | 
           162 | 
                   $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']);
  | 
        
        
            | 
            | 
           163 | 
                   $this->assertEquals(0, $count);
  | 
        
        
            | 
            | 
           164 | 
               }
  | 
        
        
            | 
            | 
           165 | 
              | 
        
        
            | 
            | 
           166 | 
               /**
  | 
        
        
            | 
            | 
           167 | 
                * Test for provider::test_delete_data_for_users().
  | 
        
        
            | 
            | 
           168 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           169 | 
               public function test_delete_data_for_users(): void {
  | 
        
        
           | 1 | 
           efrain | 
           170 | 
                   global $DB;
  | 
        
        
            | 
            | 
           171 | 
                   $component = 'tool_mobile';
  | 
        
        
            | 
            | 
           172 | 
              | 
        
        
            | 
            | 
           173 | 
                   // Create users and Mobile user keys.
  | 
        
        
            | 
            | 
           174 | 
                   $user1 = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           175 | 
                   $user2 = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           176 | 
                   $context1 = \context_user::instance($user1->id);
  | 
        
        
            | 
            | 
           177 | 
                   $context2 = \context_user::instance($user2->id);
  | 
        
        
            | 
            | 
           178 | 
                   $keyvalue1 = get_user_key('tool_mobile', $user1->id);
  | 
        
        
            | 
            | 
           179 | 
                   $keyvalue2 = get_user_key('tool_mobile/qrlogin', $user1->id);
  | 
        
        
            | 
            | 
           180 | 
                   $keyvalue3 = get_user_key('tool_mobile', $user2->id);
  | 
        
        
            | 
            | 
           181 | 
                   $key1 = $DB->get_record('user_private_key', ['value' => $keyvalue1]);
  | 
        
        
            | 
            | 
           182 | 
              | 
        
        
            | 
            | 
           183 | 
                   // Before deletion, we should have 2 user_private_keys for tool_mobile and one for tool_mobile/qrlogin.
  | 
        
        
            | 
            | 
           184 | 
                   $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']);
  | 
        
        
            | 
            | 
           185 | 
                   $this->assertEquals(2, $count);
  | 
        
        
            | 
            | 
           186 | 
                   $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile/qrlogin']);
  | 
        
        
            | 
            | 
           187 | 
                   $this->assertEquals(1, $count);
  | 
        
        
            | 
            | 
           188 | 
              | 
        
        
            | 
            | 
           189 | 
                   // Ensure deleting wrong user in the user context does nothing.
  | 
        
        
            | 
            | 
           190 | 
                   $approveduserids = [$user2->id];
  | 
        
        
            | 
            | 
           191 | 
                   $approvedlist = new approved_userlist($context1, $component, $approveduserids);
  | 
        
        
            | 
            | 
           192 | 
                   provider::delete_data_for_users($approvedlist);
  | 
        
        
            | 
            | 
           193 | 
              | 
        
        
            | 
            | 
           194 | 
                   $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']);
  | 
        
        
            | 
            | 
           195 | 
                   $this->assertEquals(2, $count);
  | 
        
        
            | 
            | 
           196 | 
              | 
        
        
            | 
            | 
           197 | 
                   // Delete for user1 in context1.
  | 
        
        
            | 
            | 
           198 | 
                   $approveduserids = [$user1->id];
  | 
        
        
            | 
            | 
           199 | 
                   $approvedlist = new approved_userlist($context1, $component, $approveduserids);
  | 
        
        
            | 
            | 
           200 | 
                   provider::delete_data_for_users($approvedlist);
  | 
        
        
            | 
            | 
           201 | 
              | 
        
        
            | 
            | 
           202 | 
                   // Ensure only user1's data is deleted, user2's remains.
  | 
        
        
            | 
            | 
           203 | 
                   $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile']);
  | 
        
        
            | 
            | 
           204 | 
                   $this->assertEquals(1, $count);
  | 
        
        
            | 
            | 
           205 | 
                   $count = $DB->count_records('user_private_key', ['script' => 'tool_mobile/qrlogin']);
  | 
        
        
            | 
            | 
           206 | 
                   $this->assertEquals(0, $count);
  | 
        
        
            | 
            | 
           207 | 
              | 
        
        
            | 
            | 
           208 | 
                   $params = ['script' => $component];
  | 
        
        
            | 
            | 
           209 | 
                   $userid = $DB->get_field_select('user_private_key', 'userid', 'script = :script', $params);
  | 
        
        
            | 
            | 
           210 | 
                   $this->assertEquals($user2->id, $userid);
  | 
        
        
            | 
            | 
           211 | 
               }
  | 
        
        
            | 
            | 
           212 | 
           }
  |