Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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   * Unit Tests for the request helper.
  19   *
  20   * @package     core_completion
  21   * @category    test
  22   * @copyright   2018 Adrian Greeve <adriangreeve.com>
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  
  30  require_once($CFG->dirroot . '/completion/tests/fixtures/completion_creation.php');
  31  
  32  /**
  33   * Tests for the \core_completion API's provider functionality.
  34   *
  35   * @copyright   2018 Adrian Greeve <adriangreeve.com>
  36   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class core_completion_privacy_test extends \core_privacy\tests\provider_testcase {
  39  
  40      use completion_creation;
  41  
  42      /**
  43       * Test joining course completion data to an sql statement.
  44       */
  45      public function test_get_course_completion_join_sql() {
  46          global $DB;
  47          $this->resetAfterTest();
  48          $user = $this->getDataGenerator()->create_user();
  49          $this->create_course_completion();
  50          $this->complete_course($user, false);
  51  
  52          list($join, $where, $params) = \core_completion\privacy\provider::get_course_completion_join_sql($user->id, 'comp', 'c.id');
  53          $sql = "SELECT DISTINCT c.id
  54                  FROM {course} c
  55                  {$join}
  56                  WHERE {$where}";
  57          $records = $DB->get_records_sql($sql, $params);
  58          $data = array_shift($records);
  59          $this->assertEquals($this->course->id, $data->id);
  60      }
  61  
  62      /**
  63       * Test fetching users' course completion by context and adding to a userlist.
  64       */
  65      public function test_add_course_completion_users_to_userlist() {
  66          $this->resetAfterTest();
  67  
  68          $user1 = $this->getDataGenerator()->create_user();
  69          $user2 = $this->getDataGenerator()->create_user();
  70          $user3 = $this->getDataGenerator()->create_user();
  71  
  72          // User1 and user2 complete course.
  73          $this->create_course_completion();
  74          $this->complete_course($user1);
  75          $this->complete_course($user2);
  76  
  77          // User3 is enrolled but has not completed course.
  78          $this->getDataGenerator()->enrol_user($user3->id, $this->course->id, 'student');
  79  
  80          $userlist = new \core_privacy\local\request\userlist($this->coursecontext, 'test');
  81          \core_completion\privacy\provider::add_course_completion_users_to_userlist($userlist);
  82  
  83          // Ensure only users that have course completion are returned.
  84          $expected = [$user1->id, $user2->id];
  85          $actual = $userlist->get_userids();
  86          sort($expected);
  87          sort($actual);
  88          $this->assertCount(2, $actual);
  89          $this->assertEquals($expected, $actual);
  90      }
  91  
  92      /**
  93       * Test getting course completion information.
  94       */
  95      public function test_get_course_completion_info() {
  96          $this->resetAfterTest();
  97          $user = $this->getDataGenerator()->create_user();
  98          $this->create_course_completion();
  99          $this->complete_course($user);
 100          $coursecompletion = \core_completion\privacy\provider::get_course_completion_info($user, $this->course);
 101          $this->assertEquals('In progress', $coursecompletion['status']);
 102          $this->assertCount(2, $coursecompletion['criteria']);
 103      }
 104  
 105      /**
 106       * Test getting activity completion information.
 107       */
 108      public function test_get_activity_completion_info() {
 109          $this->resetAfterTest();
 110          $user = $this->getDataGenerator()->create_user();
 111          $this->create_course_completion();
 112          $this->complete_course($user);
 113          $activitycompletion = \core_completion\privacy\provider::get_activity_completion_info($user, $this->course,
 114                  $this->cm);
 115          $this->assertEquals($user->id, $activitycompletion->userid);
 116          $this->assertEquals($this->cm->id, $activitycompletion->coursemoduleid);
 117          $this->assertEquals(1, $activitycompletion->completionstate);
 118      }
 119  
 120      /**
 121       * Test deleting activity completion information for a user.
 122       */
 123      public function test_delete_completion_activity_user() {
 124          $this->resetAfterTest();
 125          $user = $this->getDataGenerator()->create_user();
 126          $this->create_course_completion();
 127          $this->complete_course($user);
 128          \core_completion\privacy\provider::delete_completion($user, null, $this->cm->id);
 129          $activitycompletion = \core_completion\privacy\provider::get_activity_completion_info($user, $this->course,
 130                  $this->cm);
 131          $this->assertEquals(0, $activitycompletion->completionstate);
 132      }
 133  
 134      /**
 135       * Test deleting course completion information.
 136       */
 137      public function test_delete_completion_course() {
 138          $this->resetAfterTest();
 139          $user = $this->getDataGenerator()->create_user();
 140          $this->create_course_completion();
 141          $this->complete_course($user);
 142          \core_completion\privacy\provider::delete_completion(null, $this->course->id);
 143          $coursecompletion = \core_completion\privacy\provider::get_course_completion_info($user, $this->course);
 144          foreach ($coursecompletion['criteria'] as $criterion) {
 145              $this->assertEquals('No', $criterion['completed']);
 146          }
 147      }
 148  
 149      /**
 150       * Test deleting course completion information by approved userlist.
 151       */
 152      public function test_delete_completion_by_approved_userlist() {
 153          $this->resetAfterTest();
 154          $user1 = $this->getDataGenerator()->create_user();
 155          $user2 = $this->getDataGenerator()->create_user();
 156          $user3 = $this->getDataGenerator()->create_user();
 157          $user4 = $this->getDataGenerator()->create_user();
 158  
 159          $this->create_course_completion();
 160          $this->complete_course($user1);
 161          $this->complete_course($user2);
 162          $this->complete_course($user3);
 163          $this->complete_course($user4);
 164  
 165          // Prepare approved userlist (context/component are irrelevant for this test).
 166          $approveduserids = [$user1->id, $user3->id];
 167          $userlist = new \core_privacy\local\request\approved_userlist($this->coursecontext, 'completion', $approveduserids);
 168  
 169          // Test deleting activity completion information only affects approved userlist.
 170          \core_completion\privacy\provider::delete_completion_by_approved_userlist(
 171                  $userlist, null, $this->cm->id);
 172          $activitycompletion1 = \core_completion\privacy\provider::get_activity_completion_info($user1, $this->course,
 173                  $this->cm);
 174          $this->assertEquals(0, $activitycompletion1->completionstate);
 175          $activitycompletion2 = \core_completion\privacy\provider::get_activity_completion_info($user2, $this->course,
 176                  $this->cm);
 177          $this->assertNotEquals(0, $activitycompletion2->completionstate);
 178          $activitycompletion3 = \core_completion\privacy\provider::get_activity_completion_info($user3, $this->course,
 179                  $this->cm);
 180          $this->assertEquals(0, $activitycompletion3->completionstate);
 181          $activitycompletion4 = \core_completion\privacy\provider::get_activity_completion_info($user4, $this->course,
 182                  $this->cm);
 183          $this->assertNotEquals(0, $activitycompletion4->completionstate);
 184  
 185          // Prepare different approved userlist (context/component are irrelevant for this test).
 186          $approveduserids = [$user2->id, $user4->id];
 187          $userlist = new \core_privacy\local\request\approved_userlist($this->coursecontext, 'completion', $approveduserids);
 188  
 189          // Test deleting course completion information only affects approved userlist.
 190          \core_completion\privacy\provider::delete_completion_by_approved_userlist($userlist, $this->course->id);
 191  
 192          $coursecompletion1 = \core_completion\privacy\provider::get_course_completion_info($user1, $this->course);
 193          $hasno = array_search('No', $coursecompletion1['criteria'], true);
 194          $this->assertFalse($hasno);
 195          $coursecompletion2 = \core_completion\privacy\provider::get_course_completion_info($user2, $this->course);
 196          $hasyes = array_search('Yes', $coursecompletion2['criteria'], true);
 197          $this->assertFalse($hasyes);
 198          $coursecompletion3 = \core_completion\privacy\provider::get_course_completion_info($user3, $this->course);
 199          $hasno = array_search('No', $coursecompletion3['criteria'], true);
 200          $this->assertFalse($hasno);
 201          $coursecompletion4 = \core_completion\privacy\provider::get_course_completion_info($user4, $this->course);
 202          $hasyes = array_search('Yes', $coursecompletion4['criteria'], true);
 203          $this->assertFalse($hasyes);
 204      }
 205  
 206      /**
 207       * Test getting course completion information with completion disabled.
 208       */
 209      public function test_get_course_completion_info_completion_disabled() {
 210          $this->resetAfterTest();
 211  
 212          $user = $this->getDataGenerator()->create_user();
 213  
 214          $course = $this->getDataGenerator()->create_course(['enablecompletion' => 0]);
 215  
 216          $this->getDataGenerator()->enrol_user($user->id, $course->id, 'student');
 217  
 218          $coursecompletion = \core_completion\privacy\provider::get_course_completion_info($user, $course);
 219  
 220          $this->assertTrue(is_array($coursecompletion));
 221          $this->assertEmpty($coursecompletion);
 222      }
 223  }