/mod/assign/tests/portfolio_caller_test.php

https://github.com/markn86/moodle · PHP · 255 lines · 158 code · 43 blank · 54 comment · 1 complexity · c13a6f8a88d53f05fce7dcb240a84075 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. * Tests behaviour of the assign_portfolio_caller class.
  18. *
  19. * @package mod_assign
  20. * @category test
  21. * @copyright Brendan Cox <brendan.cox@totaralearning.com>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. global $CFG;
  26. require_once($CFG->dirroot . '/mod/assign/locallib.php');
  27. require_once($CFG->dirroot . '/group/lib.php');
  28. /**
  29. * Class mod_assign_portfolio_caller_testcase
  30. *
  31. * Tests behaviour of the assign_portfolio_caller class.
  32. *
  33. * @copyright Brendan Cox <brendan.cox@totaralearning.com>
  34. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35. */
  36. class mod_assign_portfolio_caller_testcase extends advanced_testcase {
  37. /**
  38. * Test an assignment file is loaded for a user who submitted it.
  39. */
  40. public function test_user_submission_file_is_loaded() {
  41. $this->resetAfterTest(true);
  42. $user = $this->getDataGenerator()->create_user();
  43. $course = $this->getDataGenerator()->create_course();
  44. /* @var mod_assign_generator $assigngenerator */
  45. $assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
  46. $activityrecord = $assigngenerator->create_instance(array('course' => $course->id));
  47. $cm = get_coursemodule_from_instance('assign', $activityrecord->id);
  48. $context = context_module::instance($cm->id);
  49. $assign = new mod_assign_testable_assign($context, $cm, $course);
  50. $submission = $assign->get_user_submission($user->id, true);
  51. $fs = get_file_storage();
  52. $dummy = (object) array(
  53. 'contextid' => $context->id,
  54. 'component' => 'assignsubmission_file',
  55. 'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
  56. 'itemid' => $submission->id,
  57. 'filepath' => '/',
  58. 'filename' => 'myassignmnent.pdf'
  59. );
  60. $file = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
  61. $caller = new assign_portfolio_caller(array('cmid' => $cm->id, 'fileid' => $file->get_id()));
  62. $caller->set('user', $user);
  63. $caller->load_data();
  64. $this->assertEquals($file->get_contenthash(), $caller->get_sha1_file());
  65. // This processes the file either by fileid or by other fields in the file table.
  66. // We should get the same outcome with either approach.
  67. $caller = new assign_portfolio_caller(
  68. array(
  69. 'cmid' => $cm->id,
  70. 'sid' => $submission->id,
  71. 'area' => ASSIGNSUBMISSION_FILE_FILEAREA,
  72. 'component' => 'assignsubmission_file',
  73. )
  74. );
  75. $caller->set('user', $user);
  76. $caller->load_data();
  77. $this->assertEquals($file->get_contenthash(), $caller->get_sha1_file());
  78. }
  79. /**
  80. * Test an assignment file is not loaded for a user that did not submit it.
  81. */
  82. public function test_different_user_submission_file_is_not_loaded() {
  83. $this->resetAfterTest(true);
  84. $user = $this->getDataGenerator()->create_user();
  85. $course = $this->getDataGenerator()->create_course();
  86. /* @var mod_assign_generator $assigngenerator */
  87. $assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
  88. $activityrecord = $assigngenerator->create_instance(array('course' => $course->id));
  89. $cm = get_coursemodule_from_instance('assign', $activityrecord->id);
  90. $context = context_module::instance($cm->id);
  91. $assign = new mod_assign_testable_assign($context, $cm, $course);
  92. $submission = $assign->get_user_submission($user->id, true);
  93. $fs = get_file_storage();
  94. $dummy = (object) array(
  95. 'contextid' => $context->id,
  96. 'component' => 'assignsubmission_file',
  97. 'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
  98. 'itemid' => $submission->id,
  99. 'filepath' => '/',
  100. 'filename' => 'myassignmnent.pdf'
  101. );
  102. $file = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
  103. // Now add second user.
  104. $wronguser = $this->getDataGenerator()->create_user();
  105. $caller = new assign_portfolio_caller(array('cmid' => $cm->id, 'fileid' => $file->get_id()));
  106. $caller->set('user', $wronguser);
  107. $this->expectException(portfolio_caller_exception::class);
  108. $this->expectExceptionMessage('Sorry, the requested file could not be found');
  109. $caller->load_data();
  110. }
  111. /**
  112. * Test an assignment file is loaded for a user who is part of a group that submitted it.
  113. */
  114. public function test_group_submission_file_is_loaded() {
  115. $this->resetAfterTest(true);
  116. $user = $this->getDataGenerator()->create_user();
  117. $course = $this->getDataGenerator()->create_course();
  118. $groupdata = new stdClass();
  119. $groupdata->courseid = $course->id;
  120. $groupdata->name = 'group1';
  121. $groupid = groups_create_group($groupdata);
  122. $this->getDataGenerator()->enrol_user($user->id, $course->id);
  123. groups_add_member($groupid, $user);
  124. /* @var mod_assign_generator $assigngenerator */
  125. $assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
  126. $activityrecord = $assigngenerator->create_instance(array('course' => $course->id));
  127. $cm = get_coursemodule_from_instance('assign', $activityrecord->id);
  128. $context = context_module::instance($cm->id);
  129. $assign = new mod_assign_testable_assign($context, $cm, $course);
  130. $submission = $assign->get_group_submission($user->id, $groupid, true);
  131. $fs = get_file_storage();
  132. $dummy = (object) array(
  133. 'contextid' => $context->id,
  134. 'component' => 'assignsubmission_file',
  135. 'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
  136. 'itemid' => $submission->id,
  137. 'filepath' => '/',
  138. 'filename' => 'myassignmnent.pdf'
  139. );
  140. $file = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
  141. $caller = new assign_portfolio_caller(array('cmid' => $cm->id, 'fileid' => $file->get_id()));
  142. $caller->set('user', $user);
  143. $caller->load_data();
  144. $this->assertEquals($file->get_contenthash(), $caller->get_sha1_file());
  145. // This processes the file either by fileid or by other fields in the file table.
  146. // We should get the same outcome with either approach.
  147. $caller = new assign_portfolio_caller(
  148. array(
  149. 'cmid' => $cm->id,
  150. 'sid' => $submission->id,
  151. 'area' => ASSIGNSUBMISSION_FILE_FILEAREA,
  152. 'component' => 'assignsubmission_file',
  153. )
  154. );
  155. $caller->set('user', $user);
  156. $caller->load_data();
  157. $this->assertEquals($file->get_contenthash(), $caller->get_sha1_file());
  158. }
  159. /**
  160. * Test an assignment file is not loaded for a user who is not part of a group that submitted it.
  161. */
  162. public function test_different_group_submission_file_is_not_loaded() {
  163. $this->resetAfterTest(true);
  164. $user = $this->getDataGenerator()->create_user();
  165. $course = $this->getDataGenerator()->create_course();
  166. $groupdata = new stdClass();
  167. $groupdata->courseid = $course->id;
  168. $groupdata->name = 'group1';
  169. $groupid = groups_create_group($groupdata);
  170. $this->getDataGenerator()->enrol_user($user->id, $course->id);
  171. groups_add_member($groupid, $user);
  172. /* @var mod_assign_generator $assigngenerator */
  173. $assigngenerator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
  174. $activityrecord = $assigngenerator->create_instance(array('course' => $course->id));
  175. $cm = get_coursemodule_from_instance('assign', $activityrecord->id);
  176. $context = context_module::instance($cm->id);
  177. $assign = new mod_assign_testable_assign($context, $cm, $course);
  178. $submission = $assign->get_group_submission($user->id, $groupid,true);
  179. $fs = get_file_storage();
  180. $dummy = (object) array(
  181. 'contextid' => $context->id,
  182. 'component' => 'assignsubmission_file',
  183. 'filearea' => ASSIGNSUBMISSION_FILE_FILEAREA,
  184. 'itemid' => $submission->id,
  185. 'filepath' => '/',
  186. 'filename' => 'myassignmnent.pdf'
  187. );
  188. $file = $fs->create_file_from_string($dummy, 'Content of ' . $dummy->filename);
  189. // Now add second user.
  190. $wronguser = $this->getDataGenerator()->create_user();
  191. // Create a new group for the wrong user.
  192. $groupdata = new stdClass();
  193. $groupdata->courseid = $course->id;
  194. $groupdata->name = 'group2';
  195. $groupid = groups_create_group($groupdata);
  196. $this->getDataGenerator()->enrol_user($wronguser->id, $course->id);
  197. groups_add_member($groupid, $wronguser);
  198. // In the negative test for the user, we loaded the caller via fileid. Switching to the other approach this time.
  199. $caller = new assign_portfolio_caller(
  200. array(
  201. 'cmid' => $cm->id,
  202. 'sid' => $submission->id,
  203. 'area' => ASSIGNSUBMISSION_FILE_FILEAREA,
  204. 'component' => 'assignsubmission_file',
  205. )
  206. );
  207. $caller->set('user', $wronguser);
  208. $this->expectException(portfolio_caller_exception::class);
  209. $this->expectExceptionMessage('Sorry, the requested file could not be found');
  210. $caller->load_data();
  211. }
  212. }