| 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 | 
            * Data provider tests.
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * @package    tool_log
  | 
        
        
            | 
            | 
           21 | 
            * @category   test
  | 
        
        
            | 
            | 
           22 | 
            * @copyright  2018 Frédéric Massart
  | 
        
        
            | 
            | 
           23 | 
            * @author     Frédéric Massart <fred@branchup.tech>
  | 
        
        
            | 
            | 
           24 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           25 | 
            */
  | 
        
        
            | 
            | 
           26 | 
           namespace tool_log\privacy;
  | 
        
        
            | 
            | 
           27 | 
              | 
        
        
            | 
            | 
           28 | 
           defined('MOODLE_INTERNAL') || die();
  | 
        
        
            | 
            | 
           29 | 
           global $CFG;
  | 
        
        
            | 
            | 
           30 | 
              | 
        
        
            | 
            | 
           31 | 
           use core_privacy\tests\provider_testcase;
  | 
        
        
            | 
            | 
           32 | 
           use core_privacy\local\request\contextlist;
  | 
        
        
            | 
            | 
           33 | 
           use core_privacy\local\request\approved_contextlist;
  | 
        
        
            | 
            | 
           34 | 
           use core_privacy\local\request\transform;
  | 
        
        
            | 
            | 
           35 | 
           use core_privacy\local\request\writer;
  | 
        
        
            | 
            | 
           36 | 
           use tool_log\privacy\provider;
  | 
        
        
            | 
            | 
           37 | 
              | 
        
        
            | 
            | 
           38 | 
           require_once($CFG->dirroot . '/admin/tool/log/store/standard/tests/fixtures/event.php');
  | 
        
        
            | 
            | 
           39 | 
              | 
        
        
            | 
            | 
           40 | 
           /**
  | 
        
        
            | 
            | 
           41 | 
            * Data provider testcase class.
  | 
        
        
            | 
            | 
           42 | 
            *
  | 
        
        
            | 
            | 
           43 | 
            * We're not testing the full functionality, just that the provider passes the requests
  | 
        
        
            | 
            | 
           44 | 
            * down to at least one of its subplugin. Each subplugin should have tests to cover the
  | 
        
        
            | 
            | 
           45 | 
            * different provider methods in depth.
  | 
        
        
            | 
            | 
           46 | 
            *
  | 
        
        
            | 
            | 
           47 | 
            * @package    tool_log
  | 
        
        
            | 
            | 
           48 | 
            * @category   test
  | 
        
        
            | 
            | 
           49 | 
            * @copyright  2018 Frédéric Massart
  | 
        
        
            | 
            | 
           50 | 
            * @author     Frédéric Massart <fred@branchup.tech>
  | 
        
        
            | 
            | 
           51 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           52 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           53 | 
           final class provider_test extends provider_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           54 | 
              | 
        
        
            | 
            | 
           55 | 
               public function setUp(): void {
  | 
        
        
           | 1441 | 
           ariadna | 
           56 | 
                   parent::setUp();
  | 
        
        
           | 1 | 
           efrain | 
           57 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           58 | 
                   $this->preventResetByRollback(); // Logging waits till the transaction gets committed.
  | 
        
        
            | 
            | 
           59 | 
               }
  | 
        
        
            | 
            | 
           60 | 
              | 
        
        
           | 11 | 
           efrain | 
           61 | 
               public function test_get_contexts_for_userid(): void {
  | 
        
        
           | 1 | 
           efrain | 
           62 | 
                   $admin = \core_user::get_user(2);
  | 
        
        
            | 
            | 
           63 | 
                   $u1 = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           64 | 
                   $c1 = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           65 | 
                   $c1ctx = \context_course::instance($c1->id);
  | 
        
        
            | 
            | 
           66 | 
              | 
        
        
            | 
            | 
           67 | 
                   $this->enable_logging();
  | 
        
        
            | 
            | 
           68 | 
                   $manager = get_log_manager(true);
  | 
        
        
            | 
            | 
           69 | 
              | 
        
        
            | 
            | 
           70 | 
                   $this->setUser($u1);
  | 
        
        
            | 
            | 
           71 | 
                   $this->assertEmpty(provider::get_contexts_for_userid($u1->id)->get_contextids());
  | 
        
        
            | 
            | 
           72 | 
                   $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
  | 
        
        
            | 
            | 
           73 | 
                   $e->trigger();
  | 
        
        
            | 
            | 
           74 | 
                   $this->assertEquals($c1ctx->id, provider::get_contexts_for_userid($u1->id)->get_contextids()[0]);
  | 
        
        
            | 
            | 
           75 | 
               }
  | 
        
        
            | 
            | 
           76 | 
              | 
        
        
           | 11 | 
           efrain | 
           77 | 
               public function test_delete_data_for_user(): void {
  | 
        
        
           | 1 | 
           efrain | 
           78 | 
                   global $DB;
  | 
        
        
            | 
            | 
           79 | 
                   $u1 = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           80 | 
                   $u2 = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           81 | 
                   $c1 = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           82 | 
                   $c1ctx = \context_course::instance($c1->id);
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
                   $this->enable_logging();
  | 
        
        
            | 
            | 
           85 | 
                   $manager = get_log_manager(true);
  | 
        
        
            | 
            | 
           86 | 
              | 
        
        
            | 
            | 
           87 | 
                   // User 1 is the author.
  | 
        
        
            | 
            | 
           88 | 
                   $this->setUser($u1);
  | 
        
        
            | 
            | 
           89 | 
                   $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
  | 
        
        
            | 
            | 
           90 | 
                   $e->trigger();
  | 
        
        
            | 
            | 
           91 | 
                   $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
  | 
        
        
            | 
            | 
           92 | 
                   $e->trigger();
  | 
        
        
            | 
            | 
           93 | 
              | 
        
        
            | 
            | 
           94 | 
                   // User 2 is the author.
  | 
        
        
            | 
            | 
           95 | 
                   $this->setUser($u2);
  | 
        
        
            | 
            | 
           96 | 
                   $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
  | 
        
        
            | 
            | 
           97 | 
                   $e->trigger();
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
                   // Confirm data present.
  | 
        
        
            | 
            | 
           100 | 
                   $this->assertTrue($DB->record_exists('logstore_standard_log', ['userid' => $u1->id, 'contextid' => $c1ctx->id]));
  | 
        
        
            | 
            | 
           101 | 
                   $this->assertEquals(2, $DB->count_records('logstore_standard_log', ['userid' => $u1->id]));
  | 
        
        
            | 
            | 
           102 | 
                   $this->assertEquals(1, $DB->count_records('logstore_standard_log', ['userid' => $u2->id]));
  | 
        
        
            | 
            | 
           103 | 
              | 
        
        
            | 
            | 
           104 | 
                   // Delete all the things!
  | 
        
        
            | 
            | 
           105 | 
                   provider::delete_data_for_user(new approved_contextlist($u1, 'logstore_standard', [$c1ctx->id]));
  | 
        
        
            | 
            | 
           106 | 
                   $this->assertFalse($DB->record_exists('logstore_standard_log', ['userid' => $u1->id, 'contextid' => $c1ctx->id]));
  | 
        
        
            | 
            | 
           107 | 
                   $this->assertEquals(0, $DB->count_records('logstore_standard_log', ['userid' => $u1->id]));
  | 
        
        
            | 
            | 
           108 | 
                   $this->assertEquals(1, $DB->count_records('logstore_standard_log', ['userid' => $u2->id]));
  | 
        
        
            | 
            | 
           109 | 
               }
  | 
        
        
            | 
            | 
           110 | 
              | 
        
        
           | 11 | 
           efrain | 
           111 | 
               public function test_delete_data_for_all_users_in_context(): void {
  | 
        
        
           | 1 | 
           efrain | 
           112 | 
                   global $DB;
  | 
        
        
            | 
            | 
           113 | 
                   $u1 = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           114 | 
                   $u2 = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           115 | 
                   $c1 = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           116 | 
                   $c1ctx = \context_course::instance($c1->id);
  | 
        
        
            | 
            | 
           117 | 
              | 
        
        
            | 
            | 
           118 | 
                   $this->enable_logging();
  | 
        
        
            | 
            | 
           119 | 
                   $manager = get_log_manager(true);
  | 
        
        
            | 
            | 
           120 | 
              | 
        
        
            | 
            | 
           121 | 
                   // User 1 is the author.
  | 
        
        
            | 
            | 
           122 | 
                   $this->setUser($u1);
  | 
        
        
            | 
            | 
           123 | 
                   $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
  | 
        
        
            | 
            | 
           124 | 
                   $e->trigger();
  | 
        
        
            | 
            | 
           125 | 
                   $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
  | 
        
        
            | 
            | 
           126 | 
                   $e->trigger();
  | 
        
        
            | 
            | 
           127 | 
              | 
        
        
            | 
            | 
           128 | 
                   // User 2 is the author.
  | 
        
        
            | 
            | 
           129 | 
                   $this->setUser($u2);
  | 
        
        
            | 
            | 
           130 | 
                   $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
  | 
        
        
            | 
            | 
           131 | 
                   $e->trigger();
  | 
        
        
            | 
            | 
           132 | 
              | 
        
        
            | 
            | 
           133 | 
                   // Confirm data present.
  | 
        
        
            | 
            | 
           134 | 
                   $this->assertTrue($DB->record_exists('logstore_standard_log', ['contextid' => $c1ctx->id]));
  | 
        
        
            | 
            | 
           135 | 
                   $this->assertEquals(2, $DB->count_records('logstore_standard_log', ['userid' => $u1->id]));
  | 
        
        
            | 
            | 
           136 | 
                   $this->assertEquals(1, $DB->count_records('logstore_standard_log', ['userid' => $u2->id]));
  | 
        
        
            | 
            | 
           137 | 
              | 
        
        
            | 
            | 
           138 | 
                   // Delete all the things!
  | 
        
        
            | 
            | 
           139 | 
                   provider::delete_data_for_all_users_in_context($c1ctx);
  | 
        
        
            | 
            | 
           140 | 
                   $this->assertFalse($DB->record_exists('logstore_standard_log', ['contextid' => $c1ctx->id]));
  | 
        
        
            | 
            | 
           141 | 
                   $this->assertEquals(0, $DB->count_records('logstore_standard_log', ['userid' => $u1->id]));
  | 
        
        
            | 
            | 
           142 | 
                   $this->assertEquals(0, $DB->count_records('logstore_standard_log', ['userid' => $u2->id]));
  | 
        
        
            | 
            | 
           143 | 
               }
  | 
        
        
            | 
            | 
           144 | 
              | 
        
        
           | 11 | 
           efrain | 
           145 | 
               public function test_export_data_for_user(): void {
  | 
        
        
           | 1 | 
           efrain | 
           146 | 
                   $admin = \core_user::get_user(2);
  | 
        
        
            | 
            | 
           147 | 
                   $u1 = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           148 | 
                   $c1 = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           149 | 
                   $c1ctx = \context_course::instance($c1->id);
  | 
        
        
            | 
            | 
           150 | 
              | 
        
        
            | 
            | 
           151 | 
                   $path = [get_string('privacy:path:logs', 'tool_log'), get_string('pluginname', 'logstore_standard')];
  | 
        
        
            | 
            | 
           152 | 
                   $this->enable_logging();
  | 
        
        
            | 
            | 
           153 | 
                   $manager = get_log_manager(true);
  | 
        
        
            | 
            | 
           154 | 
              | 
        
        
            | 
            | 
           155 | 
                   // User 1 is the author.
  | 
        
        
            | 
            | 
           156 | 
                   $this->setUser($u1);
  | 
        
        
            | 
            | 
           157 | 
                   $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx, 'other' => ['i' => 123]]);
  | 
        
        
            | 
            | 
           158 | 
                   $e->trigger();
  | 
        
        
            | 
            | 
           159 | 
              | 
        
        
            | 
            | 
           160 | 
                   // Confirm data present for u1.
  | 
        
        
            | 
            | 
           161 | 
                   provider::export_user_data(new approved_contextlist($u1, 'tool_log', [$c1ctx->id]));
  | 
        
        
            | 
            | 
           162 | 
                   $data = writer::with_context($c1ctx)->get_data($path);
  | 
        
        
            | 
            | 
           163 | 
                   $this->assertCount(1, $data->logs);
  | 
        
        
            | 
            | 
           164 | 
                   $this->assertEquals(transform::yesno(true), $data->logs[0]['author_of_the_action_was_you']);
  | 
        
        
            | 
            | 
           165 | 
                   $this->assertSame(123, $data->logs[0]['other']['i']);
  | 
        
        
            | 
            | 
           166 | 
               }
  | 
        
        
            | 
            | 
           167 | 
              | 
        
        
            | 
            | 
           168 | 
               /**
  | 
        
        
            | 
            | 
           169 | 
                * Enable logging.
  | 
        
        
            | 
            | 
           170 | 
                *
  | 
        
        
            | 
            | 
           171 | 
                * @return void
  | 
        
        
            | 
            | 
           172 | 
                */
  | 
        
        
            | 
            | 
           173 | 
               protected function enable_logging() {
  | 
        
        
            | 
            | 
           174 | 
                   set_config('enabled_stores', 'logstore_standard', 'tool_log');
  | 
        
        
            | 
            | 
           175 | 
                   set_config('buffersize', 0, 'logstore_standard');
  | 
        
        
            | 
            | 
           176 | 
                   set_config('logguests', 1, 'logstore_standard');
  | 
        
        
            | 
            | 
           177 | 
               }
  | 
        
        
            | 
            | 
           178 | 
           }
  |