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.

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