PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/mod/assign/tests/custom_completion_test.php

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