| 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 | 
            * Unit tests for the block_html implementation of the privacy API.
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * @package    block_html
  | 
        
        
            | 
            | 
           21 | 
            * @category   test
  | 
        
        
            | 
            | 
           22 | 
            * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  | 
        
        
            | 
            | 
           23 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           24 | 
            */
  | 
        
        
            | 
            | 
           25 | 
           namespace block_html\privacy;
  | 
        
        
            | 
            | 
           26 | 
              | 
        
        
            | 
            | 
           27 | 
           defined('MOODLE_INTERNAL') || die();
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
           use core_privacy\local\request\writer;
  | 
        
        
            | 
            | 
           30 | 
           use core_privacy\local\request\approved_contextlist;
  | 
        
        
            | 
            | 
           31 | 
           use core_privacy\local\request\approved_userlist;
  | 
        
        
            | 
            | 
           32 | 
           use block_html\privacy\provider;
  | 
        
        
            | 
            | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
           /**
  | 
        
        
            | 
            | 
           35 | 
            * Unit tests for the block_html implementation of the privacy API.
  | 
        
        
            | 
            | 
           36 | 
            *
  | 
        
        
            | 
            | 
           37 | 
            * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  | 
        
        
            | 
            | 
           38 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           39 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           40 | 
           final class provider_test extends \core_privacy\tests\provider_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           41 | 
               /**
  | 
        
        
            | 
            | 
           42 | 
                * Get the list of standard format options for comparison.
  | 
        
        
            | 
            | 
           43 | 
                *
  | 
        
        
            | 
            | 
           44 | 
                * @return \stdClass
  | 
        
        
            | 
            | 
           45 | 
                */
  | 
        
        
            | 
            | 
           46 | 
               protected function get_format_options() {
  | 
        
        
            | 
            | 
           47 | 
                   return (object) [
  | 
        
        
            | 
            | 
           48 | 
                       'overflowdiv' => true,
  | 
        
        
            | 
            | 
           49 | 
                       'noclean' => true,
  | 
        
        
            | 
            | 
           50 | 
                   ];
  | 
        
        
            | 
            | 
           51 | 
               }
  | 
        
        
            | 
            | 
           52 | 
              | 
        
        
            | 
            | 
           53 | 
               /**
  | 
        
        
            | 
            | 
           54 | 
                * Creates an HTML block on a user.
  | 
        
        
            | 
            | 
           55 | 
                *
  | 
        
        
            | 
            | 
           56 | 
                * @param   string  $title
  | 
        
        
            | 
            | 
           57 | 
                * @param   string  $body
  | 
        
        
            | 
            | 
           58 | 
                * @param   string  $format
  | 
        
        
            | 
            | 
           59 | 
                * @return  \block_instance
  | 
        
        
            | 
            | 
           60 | 
                */
  | 
        
        
            | 
            | 
           61 | 
               protected function create_user_block($title, $body, $format) {
  | 
        
        
            | 
            | 
           62 | 
                   global $USER;
  | 
        
        
            | 
            | 
           63 | 
              | 
        
        
            | 
            | 
           64 | 
                   $configdata = (object) [
  | 
        
        
            | 
            | 
           65 | 
                       'title' => $title,
  | 
        
        
            | 
            | 
           66 | 
                       'text' => [
  | 
        
        
            | 
            | 
           67 | 
                           'itemid' => 19,
  | 
        
        
            | 
            | 
           68 | 
                           'text' => $body,
  | 
        
        
            | 
            | 
           69 | 
                           'format' => $format,
  | 
        
        
            | 
            | 
           70 | 
                       ],
  | 
        
        
            | 
            | 
           71 | 
                   ];
  | 
        
        
            | 
            | 
           72 | 
              | 
        
        
            | 
            | 
           73 | 
                   $this->create_block($this->construct_user_page($USER));
  | 
        
        
            | 
            | 
           74 | 
                   $block = $this->get_last_block_on_page($this->construct_user_page($USER));
  | 
        
        
            | 
            | 
           75 | 
                   $block = block_instance('html', $block->instance);
  | 
        
        
            | 
            | 
           76 | 
                   $block->instance_config_save((object) $configdata);
  | 
        
        
            | 
            | 
           77 | 
              | 
        
        
            | 
            | 
           78 | 
                   return $block;
  | 
        
        
            | 
            | 
           79 | 
               }
  | 
        
        
            | 
            | 
           80 | 
              | 
        
        
            | 
            | 
           81 | 
               /**
  | 
        
        
            | 
            | 
           82 | 
                * Creates an HTML block on a course.
  | 
        
        
            | 
            | 
           83 | 
                *
  | 
        
        
            | 
            | 
           84 | 
                * @param   \stdClass $course
  | 
        
        
            | 
            | 
           85 | 
                * @param   string  $title
  | 
        
        
            | 
            | 
           86 | 
                * @param   string  $body
  | 
        
        
            | 
            | 
           87 | 
                * @param   string  $format
  | 
        
        
            | 
            | 
           88 | 
                * @return  \block_instance
  | 
        
        
            | 
            | 
           89 | 
                */
  | 
        
        
            | 
            | 
           90 | 
               protected function create_course_block($course, $title, $body, $format) {
  | 
        
        
            | 
            | 
           91 | 
                   global $USER;
  | 
        
        
            | 
            | 
           92 | 
              | 
        
        
            | 
            | 
           93 | 
                   $configdata = (object) [
  | 
        
        
            | 
            | 
           94 | 
                       'title' => $title,
  | 
        
        
            | 
            | 
           95 | 
                       'text' => [
  | 
        
        
            | 
            | 
           96 | 
                           'itemid' => 19,
  | 
        
        
            | 
            | 
           97 | 
                           'text' => $body,
  | 
        
        
            | 
            | 
           98 | 
                           'format' => $format,
  | 
        
        
            | 
            | 
           99 | 
                       ],
  | 
        
        
            | 
            | 
           100 | 
                   ];
  | 
        
        
            | 
            | 
           101 | 
              | 
        
        
            | 
            | 
           102 | 
                   $this->create_block($this->construct_course_page($course));
  | 
        
        
            | 
            | 
           103 | 
                   $block = $this->get_last_block_on_page($this->construct_course_page($course));
  | 
        
        
            | 
            | 
           104 | 
                   $block = block_instance('html', $block->instance);
  | 
        
        
            | 
            | 
           105 | 
                   $block->instance_config_save((object) $configdata);
  | 
        
        
            | 
            | 
           106 | 
              | 
        
        
            | 
            | 
           107 | 
                   return $block;
  | 
        
        
            | 
            | 
           108 | 
               }
  | 
        
        
            | 
            | 
           109 | 
              | 
        
        
            | 
            | 
           110 | 
               /**
  | 
        
        
            | 
            | 
           111 | 
                * Creates an HTML block on a page.
  | 
        
        
            | 
            | 
           112 | 
                *
  | 
        
        
            | 
            | 
           113 | 
                * @param \page $page Page
  | 
        
        
            | 
            | 
           114 | 
                */
  | 
        
        
            | 
            | 
           115 | 
               protected function create_block($page) {
  | 
        
        
            | 
            | 
           116 | 
                   $page->blocks->add_block_at_end_of_default_region('html');
  | 
        
        
            | 
            | 
           117 | 
               }
  | 
        
        
            | 
            | 
           118 | 
              | 
        
        
            | 
            | 
           119 | 
               /**
  | 
        
        
            | 
            | 
           120 | 
                * Get the last block on the page.
  | 
        
        
            | 
            | 
           121 | 
                *
  | 
        
        
            | 
            | 
           122 | 
                * @param \page $page Page
  | 
        
        
            | 
            | 
           123 | 
                * @return \block_html Block instance object
  | 
        
        
            | 
            | 
           124 | 
                */
  | 
        
        
            | 
            | 
           125 | 
               protected function get_last_block_on_page($page) {
  | 
        
        
            | 
            | 
           126 | 
                   $blocks = $page->blocks->get_blocks_for_region($page->blocks->get_default_region());
  | 
        
        
            | 
            | 
           127 | 
                   $block = end($blocks);
  | 
        
        
            | 
            | 
           128 | 
              | 
        
        
            | 
            | 
           129 | 
                   return $block;
  | 
        
        
            | 
            | 
           130 | 
               }
  | 
        
        
            | 
            | 
           131 | 
              | 
        
        
            | 
            | 
           132 | 
               /**
  | 
        
        
            | 
            | 
           133 | 
                * Constructs a Page object for the User Dashboard.
  | 
        
        
            | 
            | 
           134 | 
                *
  | 
        
        
            | 
            | 
           135 | 
                * @param   \stdClass       $user User to create Dashboard for.
  | 
        
        
            | 
            | 
           136 | 
                * @return  \moodle_page
  | 
        
        
            | 
            | 
           137 | 
                */
  | 
        
        
            | 
            | 
           138 | 
               protected function construct_user_page(\stdClass $user) {
  | 
        
        
            | 
            | 
           139 | 
                   $page = new \moodle_page();
  | 
        
        
            | 
            | 
           140 | 
                   $page->set_context(\context_user::instance($user->id));
  | 
        
        
            | 
            | 
           141 | 
                   $page->set_pagelayout('mydashboard');
  | 
        
        
            | 
            | 
           142 | 
                   $page->set_pagetype('my-index');
  | 
        
        
            | 
            | 
           143 | 
                   $page->blocks->load_blocks();
  | 
        
        
            | 
            | 
           144 | 
                   return $page;
  | 
        
        
            | 
            | 
           145 | 
               }
  | 
        
        
            | 
            | 
           146 | 
              | 
        
        
            | 
            | 
           147 | 
               /**
  | 
        
        
            | 
            | 
           148 | 
                * Constructs a Page object for the User Dashboard.
  | 
        
        
            | 
            | 
           149 | 
                *
  | 
        
        
            | 
            | 
           150 | 
                * @param   \stdClass       $course Course to create Dashboard for.
  | 
        
        
            | 
            | 
           151 | 
                * @return  \moodle_page
  | 
        
        
            | 
            | 
           152 | 
                */
  | 
        
        
            | 
            | 
           153 | 
               protected function construct_course_page(\stdClass $course) {
  | 
        
        
            | 
            | 
           154 | 
                   $page = new \moodle_page();
  | 
        
        
            | 
            | 
           155 | 
                   $page->set_context(\context_course::instance($course->id));
  | 
        
        
            | 
            | 
           156 | 
                   $page->set_pagelayout('standard');
  | 
        
        
            | 
            | 
           157 | 
                   $page->set_pagetype('course-view');
  | 
        
        
            | 
            | 
           158 | 
                   $page->set_course($course);
  | 
        
        
            | 
            | 
           159 | 
                   $page->blocks->load_blocks();
  | 
        
        
            | 
            | 
           160 | 
                   return $page;
  | 
        
        
            | 
            | 
           161 | 
               }
  | 
        
        
            | 
            | 
           162 | 
              | 
        
        
            | 
            | 
           163 | 
               /**
  | 
        
        
            | 
            | 
           164 | 
                * Test that a block on the dashboard is exported.
  | 
        
        
            | 
            | 
           165 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           166 | 
               public function test_user_block(): void {
  | 
        
        
           | 1 | 
           efrain | 
           167 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           168 | 
              | 
        
        
            | 
            | 
           169 | 
                   $title = 'Example title';
  | 
        
        
            | 
            | 
           170 | 
                   $content = 'Example content';
  | 
        
        
            | 
            | 
           171 | 
                   $format = FORMAT_PLAIN;
  | 
        
        
            | 
            | 
           172 | 
              | 
        
        
            | 
            | 
           173 | 
                   // Test setup.
  | 
        
        
            | 
            | 
           174 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           175 | 
                   $this->setUser($user);
  | 
        
        
            | 
            | 
           176 | 
                   $block = $this->create_user_block($title, $content, $format);
  | 
        
        
            | 
            | 
           177 | 
                   $context = \context_block::instance($block->instance->id);
  | 
        
        
            | 
            | 
           178 | 
              | 
        
        
            | 
            | 
           179 | 
                   // Get the contexts.
  | 
        
        
            | 
            | 
           180 | 
                   $contextlist = provider::get_contexts_for_userid($user->id);
  | 
        
        
            | 
            | 
           181 | 
              | 
        
        
            | 
            | 
           182 | 
                   // Only the user context should be returned.
  | 
        
        
            | 
            | 
           183 | 
                   $this->assertCount(1, $contextlist);
  | 
        
        
            | 
            | 
           184 | 
                   $this->assertEquals($context, $contextlist->current());
  | 
        
        
            | 
            | 
           185 | 
              | 
        
        
            | 
            | 
           186 | 
                   // Export the data.
  | 
        
        
            | 
            | 
           187 | 
                   $this->export_context_data_for_user($user->id, $context, 'block_html');
  | 
        
        
            | 
            | 
           188 | 
                   /** @var \core_privacy\tests\request\content_writer $writer */
  | 
        
        
            | 
            | 
           189 | 
                   $writer = \core_privacy\local\request\writer::with_context($context);
  | 
        
        
            | 
            | 
           190 | 
                   $this->assertTrue($writer->has_any_data());
  | 
        
        
            | 
            | 
           191 | 
              | 
        
        
            | 
            | 
           192 | 
                   // Check the data.
  | 
        
        
            | 
            | 
           193 | 
                   $data = $writer->get_data([]);
  | 
        
        
            | 
            | 
           194 | 
                   $this->assertInstanceOf('stdClass', $data);
  | 
        
        
            | 
            | 
           195 | 
                   $this->assertEquals($title, $data->title);
  | 
        
        
            | 
            | 
           196 | 
                   $this->assertEquals(format_text($content, $format, $this->get_format_options()), $data->content);
  | 
        
        
            | 
            | 
           197 | 
              | 
        
        
            | 
            | 
           198 | 
                   // Delete the context.
  | 
        
        
            | 
            | 
           199 | 
                   provider::delete_data_for_all_users_in_context($context);
  | 
        
        
            | 
            | 
           200 | 
              | 
        
        
            | 
            | 
           201 | 
                   // Re-fetch the contexts - it should no longer be returned.
  | 
        
        
            | 
            | 
           202 | 
                   $contextlist = provider::get_contexts_for_userid($user->id);
  | 
        
        
            | 
            | 
           203 | 
                   $this->assertCount(0, $contextlist);
  | 
        
        
            | 
            | 
           204 | 
               }
  | 
        
        
            | 
            | 
           205 | 
              | 
        
        
            | 
            | 
           206 | 
               /**
  | 
        
        
            | 
            | 
           207 | 
                * Test that a block on the dashboard which is not configured is _not_ exported.
  | 
        
        
            | 
            | 
           208 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           209 | 
               public function test_user_block_unconfigured(): void {
  | 
        
        
           | 1 | 
           efrain | 
           210 | 
                   global $DB;
  | 
        
        
            | 
            | 
           211 | 
              | 
        
        
            | 
            | 
           212 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           213 | 
              | 
        
        
            | 
            | 
           214 | 
                   $title = 'Example title';
  | 
        
        
            | 
            | 
           215 | 
                   $content = 'Example content';
  | 
        
        
            | 
            | 
           216 | 
                   $format = FORMAT_PLAIN;
  | 
        
        
            | 
            | 
           217 | 
              | 
        
        
            | 
            | 
           218 | 
                   // Test setup.
  | 
        
        
            | 
            | 
           219 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           220 | 
                   $this->setUser($user);
  | 
        
        
            | 
            | 
           221 | 
                   $block = $this->create_user_block($title, $content, $format);
  | 
        
        
            | 
            | 
           222 | 
                   $block->instance->configdata = '';
  | 
        
        
            | 
            | 
           223 | 
                   $DB->update_record('block_instances', $block->instance);
  | 
        
        
            | 
            | 
           224 | 
                   $block = block_instance('html', $block->instance);
  | 
        
        
            | 
            | 
           225 | 
              | 
        
        
            | 
            | 
           226 | 
                   $context = \context_block::instance($block->instance->id);
  | 
        
        
            | 
            | 
           227 | 
              | 
        
        
            | 
            | 
           228 | 
                   // Get the contexts.
  | 
        
        
            | 
            | 
           229 | 
                   $contextlist = provider::get_contexts_for_userid($user->id);
  | 
        
        
            | 
            | 
           230 | 
              | 
        
        
            | 
            | 
           231 | 
                   // Only the user context should be returned.
  | 
        
        
            | 
            | 
           232 | 
                   $this->assertCount(1, $contextlist);
  | 
        
        
            | 
            | 
           233 | 
                   $this->assertEquals($context, $contextlist->current());
  | 
        
        
            | 
            | 
           234 | 
              | 
        
        
            | 
            | 
           235 | 
                   // Export the data.
  | 
        
        
            | 
            | 
           236 | 
                   $this->export_context_data_for_user($user->id, $context, 'block_html');
  | 
        
        
            | 
            | 
           237 | 
                   /** @var \core_privacy\tests\request\content_writer $writer */
  | 
        
        
            | 
            | 
           238 | 
                   $writer = \core_privacy\local\request\writer::with_context($context);
  | 
        
        
            | 
            | 
           239 | 
                   $this->assertFalse($writer->has_any_data());
  | 
        
        
            | 
            | 
           240 | 
               }
  | 
        
        
            | 
            | 
           241 | 
              | 
        
        
            | 
            | 
           242 | 
               /**
  | 
        
        
            | 
            | 
           243 | 
                * Test that a block on the dashboard is exported.
  | 
        
        
            | 
            | 
           244 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           245 | 
               public function test_user_multiple_blocks_exported(): void {
  | 
        
        
           | 1 | 
           efrain | 
           246 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           247 | 
              | 
        
        
            | 
            | 
           248 | 
                   $title = 'Example title';
  | 
        
        
            | 
            | 
           249 | 
                   $content = 'Example content';
  | 
        
        
            | 
            | 
           250 | 
                   $format = FORMAT_PLAIN;
  | 
        
        
            | 
            | 
           251 | 
              | 
        
        
            | 
            | 
           252 | 
                   // Test setup.
  | 
        
        
            | 
            | 
           253 | 
                   $blocks = [];
  | 
        
        
            | 
            | 
           254 | 
                   $contexts = [];
  | 
        
        
            | 
            | 
           255 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           256 | 
                   $this->setUser($user);
  | 
        
        
            | 
            | 
           257 | 
              | 
        
        
            | 
            | 
           258 | 
                   $block = $this->create_user_block($title, $content, $format);
  | 
        
        
            | 
            | 
           259 | 
                   $context = \context_block::instance($block->instance->id);
  | 
        
        
            | 
            | 
           260 | 
                   $contexts[$context->id] = $context;
  | 
        
        
            | 
            | 
           261 | 
              | 
        
        
            | 
            | 
           262 | 
                   $block = $this->create_user_block($title, $content, $format);
  | 
        
        
            | 
            | 
           263 | 
                   $context = \context_block::instance($block->instance->id);
  | 
        
        
            | 
            | 
           264 | 
                   $contexts[$context->id] = $context;
  | 
        
        
            | 
            | 
           265 | 
              | 
        
        
            | 
            | 
           266 | 
                   // Get the contexts.
  | 
        
        
            | 
            | 
           267 | 
                   $contextlist = provider::get_contexts_for_userid($user->id);
  | 
        
        
            | 
            | 
           268 | 
              | 
        
        
            | 
            | 
           269 | 
                   // There are now two blocks on the user context.
  | 
        
        
            | 
            | 
           270 | 
                   $this->assertCount(2, $contextlist);
  | 
        
        
            | 
            | 
           271 | 
                   foreach ($contextlist as $context) {
  | 
        
        
            | 
            | 
           272 | 
                       $this->assertTrue(isset($contexts[$context->id]));
  | 
        
        
            | 
            | 
           273 | 
                   }
  | 
        
        
            | 
            | 
           274 | 
              | 
        
        
            | 
            | 
           275 | 
                   // Turn them into an approved_contextlist.
  | 
        
        
            | 
            | 
           276 | 
                   $approvedlist = new approved_contextlist($user, 'block_html', $contextlist->get_contextids());
  | 
        
        
            | 
            | 
           277 | 
              | 
        
        
            | 
            | 
           278 | 
                   // Delete using delete_data_for_user.
  | 
        
        
            | 
            | 
           279 | 
                   provider::delete_data_for_user($approvedlist);
  | 
        
        
            | 
            | 
           280 | 
              | 
        
        
            | 
            | 
           281 | 
                   // Re-fetch the contexts - it should no longer be returned.
  | 
        
        
            | 
            | 
           282 | 
                   $contextlist = provider::get_contexts_for_userid($user->id);
  | 
        
        
            | 
            | 
           283 | 
                   $this->assertCount(0, $contextlist);
  | 
        
        
            | 
            | 
           284 | 
               }
  | 
        
        
            | 
            | 
           285 | 
              | 
        
        
            | 
            | 
           286 | 
               /**
  | 
        
        
            | 
            | 
           287 | 
                * Test that a block on the dashboard is not exported.
  | 
        
        
            | 
            | 
           288 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           289 | 
               public function test_course_blocks_not_exported(): void {
  | 
        
        
           | 1 | 
           efrain | 
           290 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           291 | 
              | 
        
        
            | 
            | 
           292 | 
                   $title = 'Example title';
  | 
        
        
            | 
            | 
           293 | 
                   $content = 'Example content';
  | 
        
        
            | 
            | 
           294 | 
                   $format = FORMAT_PLAIN;
  | 
        
        
            | 
            | 
           295 | 
              | 
        
        
            | 
            | 
           296 | 
                   // Test setup.
  | 
        
        
            | 
            | 
           297 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           298 | 
                   $course = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           299 | 
                   $this->setUser($user);
  | 
        
        
           | 1441 | 
           ariadna | 
           300 | 
                   $this->getDataGenerator()->enrol_user($user->id, $course->id, 'editingteacher');
  | 
        
        
           | 1 | 
           efrain | 
           301 | 
              | 
        
        
            | 
            | 
           302 | 
                   $block = $this->create_course_block($course, $title, $content, $format);
  | 
        
        
            | 
            | 
           303 | 
                   $context = \context_block::instance($block->instance->id);
  | 
        
        
            | 
            | 
           304 | 
              | 
        
        
            | 
            | 
           305 | 
                   // Get the contexts.
  | 
        
        
            | 
            | 
           306 | 
                   $contextlist = provider::get_contexts_for_userid($user->id);
  | 
        
        
            | 
            | 
           307 | 
              | 
        
        
            | 
            | 
           308 | 
                   // No blocks should be returned.
  | 
        
        
            | 
            | 
           309 | 
                   $this->assertCount(0, $contextlist);
  | 
        
        
            | 
            | 
           310 | 
               }
  | 
        
        
            | 
            | 
           311 | 
              | 
        
        
            | 
            | 
           312 | 
               /**
  | 
        
        
            | 
            | 
           313 | 
                * Test that a block on the dashboard is exported.
  | 
        
        
            | 
            | 
           314 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           315 | 
               public function test_mixed_multiple_blocks_exported(): void {
  | 
        
        
           | 1 | 
           efrain | 
           316 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           317 | 
              | 
        
        
            | 
            | 
           318 | 
                   $title = 'Example title';
  | 
        
        
            | 
            | 
           319 | 
                   $content = 'Example content';
  | 
        
        
            | 
            | 
           320 | 
                   $format = FORMAT_PLAIN;
  | 
        
        
            | 
            | 
           321 | 
              | 
        
        
            | 
            | 
           322 | 
                   // Test setup.
  | 
        
        
            | 
            | 
           323 | 
                   $contexts = [];
  | 
        
        
            | 
            | 
           324 | 
              | 
        
        
            | 
            | 
           325 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           326 | 
                   $course = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           327 | 
                   $this->setUser($user);
  | 
        
        
           | 1441 | 
           ariadna | 
           328 | 
                   $this->getDataGenerator()->enrol_user($user->id, $course->id, 'editingteacher');
  | 
        
        
           | 1 | 
           efrain | 
           329 | 
              | 
        
        
            | 
            | 
           330 | 
                   $block = $this->create_course_block($course, $title, $content, $format);
  | 
        
        
            | 
            | 
           331 | 
                   $context = \context_block::instance($block->instance->id);
  | 
        
        
            | 
            | 
           332 | 
              | 
        
        
            | 
            | 
           333 | 
                   $block = $this->create_user_block($title, $content, $format);
  | 
        
        
            | 
            | 
           334 | 
                   $context = \context_block::instance($block->instance->id);
  | 
        
        
            | 
            | 
           335 | 
                   $contexts[$context->id] = $context;
  | 
        
        
            | 
            | 
           336 | 
              | 
        
        
            | 
            | 
           337 | 
                   $block = $this->create_user_block($title, $content, $format);
  | 
        
        
            | 
            | 
           338 | 
                   $context = \context_block::instance($block->instance->id);
  | 
        
        
            | 
            | 
           339 | 
                   $contexts[$context->id] = $context;
  | 
        
        
            | 
            | 
           340 | 
              | 
        
        
            | 
            | 
           341 | 
                   // Get the contexts.
  | 
        
        
            | 
            | 
           342 | 
                   $contextlist = provider::get_contexts_for_userid($user->id);
  | 
        
        
            | 
            | 
           343 | 
              | 
        
        
            | 
            | 
           344 | 
                   // There are now two blocks on the user context.
  | 
        
        
            | 
            | 
           345 | 
                   $this->assertCount(2, $contextlist);
  | 
        
        
            | 
            | 
           346 | 
                   foreach ($contextlist as $context) {
  | 
        
        
            | 
            | 
           347 | 
                       $this->assertTrue(isset($contexts[$context->id]));
  | 
        
        
            | 
            | 
           348 | 
                   }
  | 
        
        
            | 
            | 
           349 | 
               }
  | 
        
        
            | 
            | 
           350 | 
              | 
        
        
            | 
            | 
           351 | 
               /**
  | 
        
        
            | 
            | 
           352 | 
                * Test that only users with a user context HTML block are fetched.
  | 
        
        
            | 
            | 
           353 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           354 | 
               public function test_get_users_in_context(): void {
  | 
        
        
           | 1 | 
           efrain | 
           355 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           356 | 
              | 
        
        
            | 
            | 
           357 | 
                   $component = 'block_html';
  | 
        
        
            | 
            | 
           358 | 
                   $title = 'Block title';
  | 
        
        
            | 
            | 
           359 | 
                   $content = 'Block content';
  | 
        
        
            | 
            | 
           360 | 
                   $blockformat = FORMAT_PLAIN;
  | 
        
        
            | 
            | 
           361 | 
              | 
        
        
            | 
            | 
           362 | 
                   // Create a user with a user context HTML block.
  | 
        
        
            | 
            | 
           363 | 
                   $user1 = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           364 | 
                   $this->setUser($user1);
  | 
        
        
            | 
            | 
           365 | 
              | 
        
        
            | 
            | 
           366 | 
                   $userblock = $this->create_user_block($title, $content, $blockformat);
  | 
        
        
            | 
            | 
           367 | 
                   $usercontext = \context_block::instance($userblock->instance->id);
  | 
        
        
            | 
            | 
           368 | 
              | 
        
        
            | 
            | 
           369 | 
                   // Create a user with a course context HTML block.
  | 
        
        
            | 
            | 
           370 | 
                   $user2 = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           371 | 
                   $this->setUser($user2);
  | 
        
        
            | 
            | 
           372 | 
              | 
        
        
            | 
            | 
           373 | 
                   $course = $this->getDataGenerator()->create_course();
  | 
        
        
           | 1441 | 
           ariadna | 
           374 | 
                   $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
  | 
        
        
           | 1 | 
           efrain | 
           375 | 
                   $courseblock = $this->create_course_block($course, $title, $content, $blockformat);
  | 
        
        
            | 
            | 
           376 | 
                   $coursecontext = \context_block::instance($courseblock->instance->id);
  | 
        
        
            | 
            | 
           377 | 
              | 
        
        
            | 
            | 
           378 | 
                   // Ensure only the user with a user context HTML block is returned.
  | 
        
        
            | 
            | 
           379 | 
                   $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
  | 
        
        
            | 
            | 
           380 | 
                   \block_html\privacy\provider::get_users_in_context($userlist);
  | 
        
        
            | 
            | 
           381 | 
              | 
        
        
            | 
            | 
           382 | 
                   $this->assertCount(1, $userlist);
  | 
        
        
            | 
            | 
           383 | 
              | 
        
        
            | 
            | 
           384 | 
                   $expected = [$user1->id];
  | 
        
        
            | 
            | 
           385 | 
                   $actual = $userlist->get_userids();
  | 
        
        
            | 
            | 
           386 | 
              | 
        
        
            | 
            | 
           387 | 
                   $this->assertEquals($expected, $actual);
  | 
        
        
            | 
            | 
           388 | 
              | 
        
        
            | 
            | 
           389 | 
                   // Ensure passing the course context returns no users.
  | 
        
        
            | 
            | 
           390 | 
                   $userlist = new \core_privacy\local\request\userlist($coursecontext, $component);
  | 
        
        
            | 
            | 
           391 | 
                   \mod_forum\privacy\provider::get_users_in_context($userlist);
  | 
        
        
            | 
            | 
           392 | 
                   $this->assertEmpty($userlist);
  | 
        
        
            | 
            | 
           393 | 
               }
  | 
        
        
            | 
            | 
           394 | 
              | 
        
        
            | 
            | 
           395 | 
               /**
  | 
        
        
            | 
            | 
           396 | 
                * Test that data for users in approved userlist is deleted.
  | 
        
        
            | 
            | 
           397 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           398 | 
               public function test_delete_data_for_users(): void {
  | 
        
        
           | 1 | 
           efrain | 
           399 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           400 | 
              | 
        
        
            | 
            | 
           401 | 
                   $component = 'block_html';
  | 
        
        
            | 
            | 
           402 | 
                   $title = 'Block title';
  | 
        
        
            | 
            | 
           403 | 
                   $content = 'Block content';
  | 
        
        
            | 
            | 
           404 | 
                   $blockformat = FORMAT_PLAIN;
  | 
        
        
            | 
            | 
           405 | 
              | 
        
        
            | 
            | 
           406 | 
                   // Create 2 user swith a user context HTML blocks.
  | 
        
        
            | 
            | 
           407 | 
                   $user1 = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           408 | 
                   $this->setUser($user1);
  | 
        
        
            | 
            | 
           409 | 
              | 
        
        
            | 
            | 
           410 | 
                   $block1 = $this->create_user_block($title, $content, $blockformat);
  | 
        
        
            | 
            | 
           411 | 
                   $context1 = \context_block::instance($block1->instance->id);
  | 
        
        
            | 
            | 
           412 | 
              | 
        
        
            | 
            | 
           413 | 
                   $user2 = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           414 | 
                   $this->setUser($user2);
  | 
        
        
            | 
            | 
           415 | 
                   $block2 = $this->create_user_block($title, $content, $blockformat);
  | 
        
        
            | 
            | 
           416 | 
                   $context2 = \context_block::instance($block2->instance->id);
  | 
        
        
            | 
            | 
           417 | 
              | 
        
        
            | 
            | 
           418 | 
                   // Create and populate the userlists.
  | 
        
        
            | 
            | 
           419 | 
                   $userlist1 = new \core_privacy\local\request\userlist($context1, $component);
  | 
        
        
            | 
            | 
           420 | 
                   \block_html\privacy\provider::get_users_in_context($userlist1);
  | 
        
        
            | 
            | 
           421 | 
                   $userlist2 = new \core_privacy\local\request\userlist($context2, $component);
  | 
        
        
            | 
            | 
           422 | 
                   \block_html\privacy\provider::get_users_in_context($userlist2);
  | 
        
        
            | 
            | 
           423 | 
              | 
        
        
            | 
            | 
           424 | 
                   // Ensure both members are included.
  | 
        
        
            | 
            | 
           425 | 
                   $this->assertCount(1, $userlist1);
  | 
        
        
            | 
            | 
           426 | 
                   $this->assertCount(1, $userlist2);
  | 
        
        
            | 
            | 
           427 | 
              | 
        
        
            | 
            | 
           428 | 
                   // Convert $userlist1 into an approved_contextlist.
  | 
        
        
            | 
            | 
           429 | 
                   $approvedlist = new approved_userlist($context1, 'block_html', $userlist1->get_userids());
  | 
        
        
            | 
            | 
           430 | 
              | 
        
        
            | 
            | 
           431 | 
                   // Delete using delete_data_for_user.
  | 
        
        
            | 
            | 
           432 | 
                   provider::delete_data_for_users($approvedlist);
  | 
        
        
            | 
            | 
           433 | 
              | 
        
        
            | 
            | 
           434 | 
                   // Re-fetch users in the contexts - only the first one should now be empty.
  | 
        
        
            | 
            | 
           435 | 
                   $userlist1 = new \core_privacy\local\request\userlist($context1, $component);
  | 
        
        
            | 
            | 
           436 | 
                   \block_html\privacy\provider::get_users_in_context($userlist1);
  | 
        
        
            | 
            | 
           437 | 
                   $this->assertCount(0, $userlist1);
  | 
        
        
            | 
            | 
           438 | 
              | 
        
        
            | 
            | 
           439 | 
                   $userlist2 = new \core_privacy\local\request\userlist($context2, $component);
  | 
        
        
            | 
            | 
           440 | 
                   \block_html\privacy\provider::get_users_in_context($userlist2);
  | 
        
        
            | 
            | 
           441 | 
                   $this->assertCount(1, $userlist2);
  | 
        
        
            | 
            | 
           442 | 
               }
  | 
        
        
            | 
            | 
           443 | 
           }
  |