Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * 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   */
  53  class provider_test extends provider_testcase {
  54  
  55      public function setUp(): void {
  56          $this->resetAfterTest();
  57          $this->preventResetByRollback(); // Logging waits till the transaction gets committed.
  58      }
  59  
  60      public function test_get_contexts_for_userid() {
  61          $admin = \core_user::get_user(2);
  62          $u1 = $this->getDataGenerator()->create_user();
  63          $c1 = $this->getDataGenerator()->create_course();
  64          $c1ctx = \context_course::instance($c1->id);
  65  
  66          $this->enable_logging();
  67          $manager = get_log_manager(true);
  68  
  69          $this->setUser($u1);
  70          $this->assertEmpty(provider::get_contexts_for_userid($u1->id)->get_contextids());
  71          $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
  72          $e->trigger();
  73          $this->assertEquals($c1ctx->id, provider::get_contexts_for_userid($u1->id)->get_contextids()[0]);
  74      }
  75  
  76      public function test_delete_data_for_user() {
  77          global $DB;
  78          $u1 = $this->getDataGenerator()->create_user();
  79          $u2 = $this->getDataGenerator()->create_user();
  80          $c1 = $this->getDataGenerator()->create_course();
  81          $c1ctx = \context_course::instance($c1->id);
  82  
  83          $this->enable_logging();
  84          $manager = get_log_manager(true);
  85  
  86          // User 1 is the author.
  87          $this->setUser($u1);
  88          $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
  89          $e->trigger();
  90          $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
  91          $e->trigger();
  92  
  93          // User 2 is the author.
  94          $this->setUser($u2);
  95          $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
  96          $e->trigger();
  97  
  98          // Confirm data present.
  99          $this->assertTrue($DB->record_exists('logstore_standard_log', ['userid' => $u1->id, 'contextid' => $c1ctx->id]));
 100          $this->assertEquals(2, $DB->count_records('logstore_standard_log', ['userid' => $u1->id]));
 101          $this->assertEquals(1, $DB->count_records('logstore_standard_log', ['userid' => $u2->id]));
 102  
 103          // Delete all the things!
 104          provider::delete_data_for_user(new approved_contextlist($u1, 'logstore_standard', [$c1ctx->id]));
 105          $this->assertFalse($DB->record_exists('logstore_standard_log', ['userid' => $u1->id, 'contextid' => $c1ctx->id]));
 106          $this->assertEquals(0, $DB->count_records('logstore_standard_log', ['userid' => $u1->id]));
 107          $this->assertEquals(1, $DB->count_records('logstore_standard_log', ['userid' => $u2->id]));
 108      }
 109  
 110      public function test_delete_data_for_all_users_in_context() {
 111          global $DB;
 112          $u1 = $this->getDataGenerator()->create_user();
 113          $u2 = $this->getDataGenerator()->create_user();
 114          $c1 = $this->getDataGenerator()->create_course();
 115          $c1ctx = \context_course::instance($c1->id);
 116  
 117          $this->enable_logging();
 118          $manager = get_log_manager(true);
 119  
 120          // User 1 is the author.
 121          $this->setUser($u1);
 122          $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
 123          $e->trigger();
 124          $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
 125          $e->trigger();
 126  
 127          // User 2 is the author.
 128          $this->setUser($u2);
 129          $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx]);
 130          $e->trigger();
 131  
 132          // Confirm data present.
 133          $this->assertTrue($DB->record_exists('logstore_standard_log', ['contextid' => $c1ctx->id]));
 134          $this->assertEquals(2, $DB->count_records('logstore_standard_log', ['userid' => $u1->id]));
 135          $this->assertEquals(1, $DB->count_records('logstore_standard_log', ['userid' => $u2->id]));
 136  
 137          // Delete all the things!
 138          provider::delete_data_for_all_users_in_context($c1ctx);
 139          $this->assertFalse($DB->record_exists('logstore_standard_log', ['contextid' => $c1ctx->id]));
 140          $this->assertEquals(0, $DB->count_records('logstore_standard_log', ['userid' => $u1->id]));
 141          $this->assertEquals(0, $DB->count_records('logstore_standard_log', ['userid' => $u2->id]));
 142      }
 143  
 144      public function test_export_data_for_user() {
 145          $admin = \core_user::get_user(2);
 146          $u1 = $this->getDataGenerator()->create_user();
 147          $c1 = $this->getDataGenerator()->create_course();
 148          $c1ctx = \context_course::instance($c1->id);
 149  
 150          $path = [get_string('privacy:path:logs', 'tool_log'), get_string('pluginname', 'logstore_standard')];
 151          $this->enable_logging();
 152          $manager = get_log_manager(true);
 153  
 154          // User 1 is the author.
 155          $this->setUser($u1);
 156          $e = \logstore_standard\event\unittest_executed::create(['context' => $c1ctx, 'other' => ['i' => 123]]);
 157          $e->trigger();
 158  
 159          // Confirm data present for u1.
 160          provider::export_user_data(new approved_contextlist($u1, 'tool_log', [$c1ctx->id]));
 161          $data = writer::with_context($c1ctx)->get_data($path);
 162          $this->assertCount(1, $data->logs);
 163          $this->assertEquals(transform::yesno(true), $data->logs[0]['author_of_the_action_was_you']);
 164          $this->assertSame(123, $data->logs[0]['other']['i']);
 165      }
 166  
 167      /**
 168       * Enable logging.
 169       *
 170       * @return void
 171       */
 172      protected function enable_logging() {
 173          set_config('enabled_stores', 'logstore_standard', 'tool_log');
 174          set_config('buffersize', 0, 'logstore_standard');
 175          set_config('logguests', 1, 'logstore_standard');
 176      }
 177  }