Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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   * Contains unit tests for core_completion/activity_custom_completion.
  19   *
  20   * @package   mod_assign
  21   * @copyright Simey Lameze <simey@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  declare(strict_types=1);
  26  
  27  namespace mod_assign;
  28  
  29  use advanced_testcase;
  30  use cm_info;
  31  use coding_exception;
  32  use mod_assign\completion\custom_completion;
  33  use moodle_exception;
  34  
  35  defined('MOODLE_INTERNAL') || die();
  36  
  37  global $CFG;
  38  require_once($CFG->libdir . '/completionlib.php');
  39  require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
  40  /**
  41   * Class for unit testing mod_assign/activity_custom_completion.
  42   *
  43   * @package   mod_assign
  44   * @copyright Simey Lameze <simey@moodle.com>
  45   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  46   */
  47  class custom_completion_test extends advanced_testcase {
  48  
  49      // Use the generator helper.
  50      use \mod_assign_test_generator;
  51  
  52      /**
  53       * Data provider for get_state().
  54       *
  55       * @return array[]
  56       */
  57      public function get_state_provider(): array {
  58          return [
  59              'Undefined rule' => [
  60                  'somenonexistentrule', COMPLETION_DISABLED, false, null, coding_exception::class
  61              ],
  62              'Rule not available' => [
  63                  'completionsubmit', COMPLETION_DISABLED, false, null, moodle_exception::class
  64              ],
  65              'Rule available, user has not submitted' => [
  66                  'completionsubmit', COMPLETION_ENABLED, false, COMPLETION_INCOMPLETE, null
  67              ],
  68              'Rule available, user has submitted' => [
  69                  'completionsubmit', COMPLETION_ENABLED, true, COMPLETION_COMPLETE, null
  70              ],
  71          ];
  72      }
  73  
  74      /**
  75       * Test for get_state().
  76       *
  77       * @dataProvider get_state_provider
  78       * @param string $rule The custom completion rule.
  79       * @param int $available Whether this rule is available.
  80       * @param bool $submitted
  81       * @param int|null $status Expected status.
  82       * @param string|null $exception Expected exception.
  83       */
  84      public function test_get_state(string $rule, int $available, ?bool $submitted, ?int $status, ?string $exception) {
  85          if (!is_null($exception)) {
  86              $this->expectException($exception);
  87          }
  88  
  89          $this->resetAfterTest();
  90  
  91          $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
  92          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  93          $assign = $this->create_instance($course, ['completion' => COMPLETION_TRACKING_AUTOMATIC, $rule => $available]);
  94  
  95          // Submit the assignment as the student.
  96          $this->setUser($student);
  97          if ($submitted == true) {
  98              $this->add_submission($student, $assign);
  99              $this->submit_for_grading($student, $assign);
 100          }
 101          $cm = cm_info::create($assign->get_course_module());
 102  
 103          $customcompletion = new custom_completion($cm, (int)$student->id);
 104          $this->assertEquals($status, $customcompletion->get_state($rule));
 105      }
 106  
 107      /**
 108       * Test for get_state().
 109       *
 110       * @dataProvider get_state_provider
 111       * @param string $rule The custom completion rule.
 112       * @param int $available Whether this rule is available.
 113       * @param bool $submitted
 114       * @param int|null $status Expected status.
 115       * @param string|null $exception Expected exception.
 116       */
 117      public function test_get_state_group(string $rule, int $available, ?bool $submitted, ?int $status, ?string $exception) {
 118          if (!is_null($exception)) {
 119              $this->expectException($exception);
 120          }
 121  
 122          $this->resetAfterTest();
 123  
 124          $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
 125          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
 126          $assign = $this->create_instance($course, ['completion' => COMPLETION_TRACKING_AUTOMATIC, $rule => $available,
 127                  'teamsubmission' => 1]);
 128  
 129          // Submit the assignment as the student.
 130          $this->setUser($student);
 131          if ($submitted == true) {
 132              $this->add_submission($student, $assign);
 133              $this->submit_for_grading($student, $assign);
 134          }
 135          $cm = cm_info::create($assign->get_course_module());
 136  
 137          $customcompletion = new custom_completion($cm, (int)$student->id);
 138          $this->assertEquals($status, $customcompletion->get_state($rule));
 139      }
 140  
 141  
 142      /**
 143       * Test for get_defined_custom_rules().
 144       */
 145      public function test_get_defined_custom_rules() {
 146          $rules = custom_completion::get_defined_custom_rules();
 147          $this->assertCount(1, $rules);
 148          $this->assertEquals('completionsubmit', reset($rules));
 149      }
 150  
 151      /**
 152       * Test for get_defined_custom_rule_descriptions().
 153       */
 154      public function test_get_custom_rule_descriptions() {
 155          $this->resetAfterTest();
 156          // Get defined custom rules.
 157          $rules = custom_completion::get_defined_custom_rules();
 158          // Get custom rule descriptions.
 159          $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
 160          $assign = $this->create_instance($course, [
 161              'submissiondrafts' => 0,
 162              'completionusegrade' => 1
 163          ]);
 164  
 165          $cm = cm_info::create($assign->get_course_module());
 166          $customcompletion = new custom_completion($cm, 1);
 167          $ruledescriptions = $customcompletion->get_custom_rule_descriptions();
 168  
 169          // Confirm that defined rules and rule descriptions are consistent with each other.
 170          $this->assertEquals(count($rules), count($ruledescriptions));
 171          foreach ($rules as $rule) {
 172              $this->assertArrayHasKey($rule, $ruledescriptions);
 173          }
 174      }
 175  
 176      /**
 177       * Test for is_defined().
 178       */
 179      public function test_is_defined() {
 180          $this->resetAfterTest();
 181          $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
 182          $assign = $this->create_instance($course, [
 183              'submissiondrafts' => 0,
 184              'completionsubmit' => 1
 185          ]);
 186  
 187          $cm = cm_info::create($assign->get_course_module());
 188  
 189          $customcompletion = new custom_completion($cm, 1);
 190  
 191          // Rule is defined.
 192          $this->assertTrue($customcompletion->is_defined('completionsubmit'));
 193  
 194          // Undefined rule.
 195          $this->assertFalse($customcompletion->is_defined('somerandomrule'));
 196      }
 197  
 198      /**
 199       * Data provider for test_get_available_custom_rules().
 200       *
 201       * @return array[]
 202       */
 203      public function get_available_custom_rules_provider(): array {
 204          return [
 205              'Completion submit available' => [
 206                  COMPLETION_ENABLED, ['completionsubmit']
 207              ],
 208              'Completion submit not available' => [
 209                  COMPLETION_DISABLED, []
 210              ],
 211          ];
 212      }
 213  
 214      /**
 215       * Test for get_available_custom_rules().
 216       *
 217       * @dataProvider get_available_custom_rules_provider
 218       * @param int $status
 219       * @param array $expected
 220       */
 221      public function test_get_available_custom_rules(int $status, array $expected) {
 222          $this->resetAfterTest();
 223          $course = $this->getDataGenerator()->create_course(['enablecompletion' => $status]);
 224  
 225          $params = [];
 226          if ($status == COMPLETION_ENABLED ) {
 227              $params = [
 228                  'completion' => COMPLETION_TRACKING_AUTOMATIC,
 229                  'completionsubmit' => 1
 230              ];
 231          }
 232  
 233          $assign = $this->create_instance($course, $params);
 234          $cm = cm_info::create($assign->get_course_module());
 235  
 236          $customcompletion = new custom_completion($cm, 1);
 237          $this->assertEquals($expected, $customcompletion->get_available_custom_rules());
 238      }
 239  }