Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 311 and 403] [Versions 400 and 403]

   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 tool_usertours implementation of the privacy API.
  19   *
  20   * @package    tool_usertours
  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 tool_usertours\privacy;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use core_privacy\local\metadata\collection;
  30  use core_privacy\local\request\writer;
  31  use tool_usertours\tour;
  32  use tool_usertours\privacy\provider;
  33  
  34  /**
  35   * Unit tests for the tool_usertours 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   */
  40  class provider_test extends \core_privacy\tests\provider_testcase {
  41  
  42      /**
  43       * Helper method for creating a tour
  44       *
  45       * @return tour
  46       */
  47      protected function create_test_tour(): tour {
  48          return (new tour())
  49              ->set_name('test_tour')
  50              ->set_description('Test tour')
  51              ->set_enabled(true)
  52              ->set_pathmatch('/')
  53              ->persist();
  54      }
  55  
  56      /**
  57       * Ensure that get_metadata exports valid content.
  58       */
  59      public function test_get_metadata() {
  60          $items = new collection('tool_usertours');
  61          $result = provider::get_metadata($items);
  62          $this->assertSame($items, $result);
  63          $this->assertInstanceOf(collection::class, $result);
  64      }
  65  
  66      /**
  67       * Ensure that export_user_preferences returns no data if the user has completed no tours.
  68       */
  69      public function test_export_user_preferences_no_pref() {
  70          $user = \core_user::get_user_by_username('admin');
  71          provider::export_user_preferences($user->id);
  72  
  73          $writer = writer::with_context(\context_system::instance());
  74  
  75          $this->assertFalse($writer->has_any_data());
  76      }
  77  
  78      /**
  79       * Ensure that export_user_preferences returns request completion data.
  80       */
  81      public function test_export_user_preferences_completed() {
  82          global $DB;
  83  
  84          $this->resetAfterTest();
  85          $this->setAdminUser();
  86  
  87          $tour = $this->create_test_tour();
  88  
  89          $user = \core_user::get_user_by_username('admin');
  90          $tour->mark_user_completed();
  91          provider::export_user_preferences($user->id);
  92  
  93          $writer = writer::with_context(\context_system::instance());
  94  
  95          $this->assertTrue($writer->has_any_data());
  96          $prefs = $writer->get_user_preferences('tool_usertours');
  97  
  98          $this->assertCount(1, (array) $prefs);
  99      }
 100  
 101      /**
 102       * Ensure that export_user_preferences returns request completion data.
 103       */
 104      public function test_export_user_preferences_requested() {
 105          global $DB;
 106  
 107          $this->resetAfterTest();
 108          $this->setAdminUser();
 109  
 110          $tour = $this->create_test_tour();
 111  
 112          $user = \core_user::get_user_by_username('admin');
 113          $tour->mark_user_completed();
 114          $tour->request_user_reset();
 115          provider::export_user_preferences($user->id);
 116  
 117          $writer = writer::with_context(\context_system::instance());
 118  
 119          $this->assertTrue($writer->has_any_data());
 120          $prefs = $writer->get_user_preferences('tool_usertours');
 121  
 122          $this->assertCount(2, (array) $prefs);
 123      }
 124  
 125      /**
 126       * Make sure we are exporting preferences for the correct user
 127       */
 128      public function test_export_user_preferences_correct_user(): void {
 129          $this->resetAfterTest();
 130  
 131          $tour = $this->create_test_tour();
 132  
 133          // Create test user, mark them as having completed the tour.
 134          $user = $this->getDataGenerator()->create_user();
 135          $this->setUser($user);
 136          $tour->mark_user_completed();
 137  
 138          // Switch to admin user, mark them as having reset the tour.
 139          $this->setAdminUser();
 140          $tour->request_user_reset();
 141  
 142          // Export test users preferences.
 143          provider::export_user_preferences($user->id);
 144  
 145          $writer = writer::with_context(\context_system::instance());
 146          $this->assertTrue($writer->has_any_data());
 147  
 148          $prefs = (array)$writer->get_user_preferences('tool_usertours');
 149          $this->assertCount(1, $prefs);
 150  
 151          // We should have received back the "completed tour" preference of the test user.
 152          $this->assertStringStartsWith('You last marked the "' . $tour->get_name() . '" user tour as completed on',
 153              reset($prefs)->description);
 154      }
 155  
 156      /**
 157       * Ensure that export_user_preferences excludes deleted tours.
 158       */
 159      public function test_export_user_preferences_deleted_tour() {
 160          global $DB;
 161  
 162          $this->resetAfterTest();
 163          $this->setAdminUser();
 164  
 165          $tour1 = $this->create_test_tour();
 166          $tour2 = $this->create_test_tour();
 167  
 168          $user = \core_user::get_user_by_username('admin');
 169  
 170          $alltours = $DB->get_records('tool_usertours_tours');
 171  
 172          $tour1->mark_user_completed();
 173  
 174          $tour2->mark_user_completed();
 175          $tour2->remove();
 176  
 177          $writer = writer::with_context(\context_system::instance());
 178  
 179          provider::export_user_preferences($user->id);
 180          $this->assertTrue($writer->has_any_data());
 181  
 182          // We should have one preference.
 183          $prefs = (array)$writer->get_user_preferences('tool_usertours');
 184          $this->assertCount(1, $prefs);
 185  
 186          // The preference should be related to the first tour.
 187          $this->assertStringContainsString($tour1->get_name(), reset($prefs)->description);
 188      }
 189  }