PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/grade/tests/reportlib_test.php

https://bitbucket.org/kudutest/moodlegit
PHP | 197 lines | 107 code | 32 blank | 58 comment | 1 complexity | 1d6d228a0a122cfba9199f06752b36cc 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. * Unit tests for grade/report/lib.php.
  18. *
  19. * @pacakge core_grade
  20. * @category phpunit
  21. * @author Andrew Davis
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. global $CFG;
  26. require_once($CFG->dirroot.'/grade/lib.php');
  27. require_once($CFG->dirroot.'/grade/report/lib.php');
  28. /**
  29. * A test class used to test grade_report, the abstract grade report parent class
  30. */
  31. class grade_report_test extends grade_report {
  32. public function __construct($courseid, $gpr, $context, $user) {
  33. parent::__construct($courseid, $gpr, $context);
  34. $this->user = $user;
  35. }
  36. /**
  37. * A wrapper around blank_hidden_total() to allow test code to call it directly
  38. */
  39. public function blank_hidden_total($courseid, $courseitem, $finalgrade) {
  40. return parent::blank_hidden_total($courseid, $courseitem, $finalgrade);
  41. }
  42. /**
  43. * Implementation of the abstract method process_data()
  44. */
  45. public function process_data($data) {
  46. }
  47. /**
  48. * Implementation of the abstract method process_action()
  49. */
  50. public function process_action($target, $action) {
  51. }
  52. }
  53. /**
  54. * Tests grade_report, the parent class for all grade reports.
  55. */
  56. class gradereportlib_testcase extends advanced_testcase {
  57. /**
  58. * Tests grade_report::blank_hidden_total()
  59. */
  60. public function test_blank_hidden_total() {
  61. global $DB;
  62. $this->resetAfterTest(true);
  63. $student = $this->getDataGenerator()->create_user();
  64. $this->setUser($student);
  65. // Create a course and two activities.
  66. // One activity will be hidden.
  67. $course = $this->getDataGenerator()->create_course();
  68. $coursegradeitem = grade_item::fetch_course_item($course->id);
  69. $coursecontext = context_course::instance($course->id);
  70. $data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
  71. $datacm = get_coursemodule_from_id('data', $data->cmid);
  72. $forum = $this->getDataGenerator()->create_module('forum', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
  73. $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
  74. // Insert student grades for the two activities.
  75. $gi = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'data', 'iteminstance' => $data->id, 'courseid' => $course->id));
  76. $datagrade = 50;
  77. $grade_grade = new grade_grade();
  78. $grade_grade->itemid = $gi->id;
  79. $grade_grade->userid = $student->id;
  80. $grade_grade->rawgrade = $datagrade;
  81. $grade_grade->finalgrade = $datagrade;
  82. $grade_grade->rawgrademax = 100;
  83. $grade_grade->rawgrademin = 0;
  84. $grade_grade->timecreated = time();
  85. $grade_grade->timemodified = time();
  86. $grade_grade->insert();
  87. $gi = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'forum', 'iteminstance' => $forum->id, 'courseid' => $course->id));
  88. $forumgrade = 70;
  89. $grade_grade = new grade_grade();
  90. $grade_grade->itemid = $gi->id;
  91. $grade_grade->userid = $student->id;
  92. $grade_grade->rawgrade = $forumgrade;
  93. $grade_grade->finalgrade = $forumgrade;
  94. $grade_grade->rawgrademax = 100;
  95. $grade_grade->rawgrademin = 0;
  96. $grade_grade->timecreated = time();
  97. $grade_grade->timemodified = time();
  98. $grade_grade->insert();
  99. // Hide the database activity.
  100. set_coursemodule_visible($datacm->id, 0);
  101. $gpr = new grade_plugin_return(array('type' => 'report', 'courseid' => $course->id));
  102. $report = new grade_report_test($course->id, $gpr, $coursecontext, $student);
  103. // Should return the supplied student total grade regardless of hiding.
  104. $report->showtotalsifcontainhidden = GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN;
  105. $this->assertEquals($datagrade + $forumgrade, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade));
  106. // Should blank the student total as course grade depends on a hidden item.
  107. $report->showtotalsifcontainhidden = GRADE_REPORT_HIDE_TOTAL_IF_CONTAINS_HIDDEN;
  108. $this->assertEquals(null, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade));
  109. // Should return the course total minus the hidden database activity grade.
  110. $report->showtotalsifcontainhidden = GRADE_REPORT_SHOW_TOTAL_IF_CONTAINS_HIDDEN;
  111. $this->assertEquals($forumgrade, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade));
  112. // Note: we cannot simply hide modules and call $report->blank_hidden_total() again.
  113. // It stores grades in a static variable so $report->blank_hidden_total() will return incorrect totals
  114. // In practice this isn't a problem. Grade visibility isn't altered mid-request outside of the unit tests.
  115. // Add a second course to test:
  116. // 1) How a course with no visible activities behaves.
  117. // 2) That $report->blank_hidden_total() correctly moves on to the new course.
  118. $course = $this->getDataGenerator()->create_course();
  119. $coursegradeitem = grade_item::fetch_course_item($course->id);
  120. $coursecontext = context_course::instance($course->id);
  121. $data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
  122. $datacm = get_coursemodule_from_id('data', $data->cmid);
  123. $forum = $this->getDataGenerator()->create_module('forum', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
  124. $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
  125. $gi = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'data', 'iteminstance' => $data->id, 'courseid' => $course->id));
  126. $datagrade = 50;
  127. $grade_grade = new grade_grade();
  128. $grade_grade->itemid = $gi->id;
  129. $grade_grade->userid = $student->id;
  130. $grade_grade->rawgrade = $datagrade;
  131. $grade_grade->finalgrade = $datagrade;
  132. $grade_grade->rawgrademax = 100;
  133. $grade_grade->rawgrademin = 0;
  134. $grade_grade->timecreated = time();
  135. $grade_grade->timemodified = time();
  136. $grade_grade->insert();
  137. $gi = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'forum', 'iteminstance' => $forum->id, 'courseid' => $course->id));
  138. $forumgrade = 70;
  139. $grade_grade = new grade_grade();
  140. $grade_grade->itemid = $gi->id;
  141. $grade_grade->userid = $student->id;
  142. $grade_grade->rawgrade = $forumgrade;
  143. $grade_grade->finalgrade = $forumgrade;
  144. $grade_grade->rawgrademax = 100;
  145. $grade_grade->rawgrademin = 0;
  146. $grade_grade->timecreated = time();
  147. $grade_grade->timemodified = time();
  148. $grade_grade->insert();
  149. // Hide both activities.
  150. set_coursemodule_visible($datacm->id, 0);
  151. set_coursemodule_visible($forumcm->id, 0);
  152. $gpr = new grade_plugin_return(array('type' => 'report', 'courseid' => $course->id));
  153. $report = new grade_report_test($course->id, $gpr, $coursecontext, $student);
  154. // Should return the supplied student total grade regardless of hiding.
  155. $report->showtotalsifcontainhidden = GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN;
  156. $this->assertEquals($datagrade + $forumgrade, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade));
  157. // Should blank the student total as course grade depends on a hidden item.
  158. $report->showtotalsifcontainhidden = GRADE_REPORT_HIDE_TOTAL_IF_CONTAINS_HIDDEN;
  159. $this->assertEquals(null, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade));
  160. // Should return the course total minus the hidden activity grades.
  161. // They are both hidden so should return null.
  162. $report->showtotalsifcontainhidden = GRADE_REPORT_SHOW_TOTAL_IF_CONTAINS_HIDDEN;
  163. $this->assertEquals(null, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade));
  164. }
  165. }