/mod/forum/tests/h5p_canedit_test.php

https://github.com/sbourget/moodle · PHP · 204 lines · 138 code · 16 blank · 50 comment · 4 complexity · 65f21f068f122edfb4026232a8890807 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. declare(strict_types = 1);
  17. namespace mod_forum\h5p;
  18. use stdClass;
  19. /**
  20. * Test class covering the H5P canedit class.
  21. *
  22. * @package mod_forum
  23. * @copyright 2021 Sara Arjona <sara@moodle.com>
  24. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25. * @coversDefaultClass \mod_forum\h5p\canedit
  26. */
  27. class h5p_canedit_test extends \advanced_testcase {
  28. /**
  29. * Test the behaviour of can_edit_content().
  30. *
  31. * @covers ::can_edit_content
  32. * @dataProvider can_edit_content_provider
  33. *
  34. * @param string $currentuser User who will call the method.
  35. * @param string $fileauthor Author of the file to check.
  36. * @param string $filecomponent Component of the file to check.
  37. * @param bool $expected Expected result after calling the can_edit_content method.
  38. * @param string $filearea Area of the file to check.
  39. *
  40. * @return void
  41. */
  42. public function test_can_edit_content(string $currentuser, string $fileauthor, string $filecomponent, bool $expected,
  43. $filearea = 'unittest'): void {
  44. global $USER, $DB;
  45. $this->setRunTestInSeparateProcess(true);
  46. $this->resetAfterTest();
  47. // Create course.
  48. $course = $this->getDataGenerator()->create_course();
  49. $context = \context_course::instance($course->id);
  50. // Create some users.
  51. $this->setAdminUser();
  52. $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
  53. $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  54. $users = [
  55. 'admin' => $USER,
  56. 'teacher' => $teacher,
  57. 'student' => $student,
  58. ];
  59. // Set current user.
  60. if ($currentuser !== 'admin') {
  61. $this->setUser($users[$currentuser]);
  62. }
  63. $itemid = rand();
  64. if ($filearea === 'post') {
  65. // Create a forum and add a discussion.
  66. $forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id]);
  67. $record = new stdClass();
  68. $record->course = $course->id;
  69. $record->userid = $users[$fileauthor]->id;
  70. $record->forum = $forum->id;
  71. $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
  72. $post = $DB->get_record('forum_posts', ['discussion' => $discussion->id]);
  73. $itemid = $post->id;
  74. }
  75. // Create the file.
  76. $filename = 'greeting-card-887.h5p';
  77. $path = __DIR__ . '/../../../h5p/tests/fixtures/' . $filename;
  78. if ($filecomponent === 'contentbank') {
  79. $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
  80. $contents = $generator->generate_contentbank_data(
  81. 'contenttype_h5p',
  82. 1,
  83. (int)$users[$fileauthor]->id,
  84. $context,
  85. true,
  86. $path
  87. );
  88. $content = array_shift($contents);
  89. $file = $content->get_file();
  90. } else {
  91. $filerecord = [
  92. 'contextid' => $context->id,
  93. 'component' => $filecomponent,
  94. 'filearea' => $filearea,
  95. 'itemid' => $itemid,
  96. 'filepath' => '/',
  97. 'filename' => basename($path),
  98. 'userid' => $users[$fileauthor]->id,
  99. ];
  100. $fs = get_file_storage();
  101. $file = $fs->create_file_from_pathname($filerecord, $path);
  102. }
  103. // Check if the currentuser can edit the file.
  104. $result = \mod_forum\h5p\canedit::can_edit_content($file);
  105. $this->assertEquals($expected, $result);
  106. }
  107. /**
  108. * Data provider for test_can_edit_content().
  109. *
  110. * @return array
  111. */
  112. public function can_edit_content_provider(): array {
  113. return [
  114. // Component = mod_forum.
  115. 'mod_forum: Admin user is author' => [
  116. 'currentuser' => 'admin',
  117. 'fileauthor' => 'admin',
  118. 'filecomponent' => 'mod_forum',
  119. 'expected' => true,
  120. ],
  121. 'mod_forum: Admin user, teacher is author' => [
  122. 'currentuser' => 'admin',
  123. 'fileauthor' => 'teacher',
  124. 'filecomponent' => 'mod_forum',
  125. 'expected' => true,
  126. ],
  127. 'mod_forum: Teacher user, admin is author' => [
  128. 'currentuser' => 'teacher',
  129. 'fileauthor' => 'admin',
  130. 'filecomponent' => 'mod_forum',
  131. 'expected' => true,
  132. ],
  133. 'mod_forum: Student user, teacher is author' => [
  134. 'currentuser' => 'student',
  135. 'fileauthor' => 'teacher',
  136. 'filecomponent' => 'mod_forum',
  137. 'expected' => false,
  138. ],
  139. 'mod_forum/post: Admin user is author' => [
  140. 'currentuser' => 'admin',
  141. 'fileauthor' => 'admin',
  142. 'filecomponent' => 'mod_forum',
  143. 'expected' => true,
  144. 'filearea' => 'post',
  145. ],
  146. 'mod_forum/post: Teacher user, admin is author' => [
  147. 'currentuser' => 'teacher',
  148. 'fileauthor' => 'admin',
  149. 'filecomponent' => 'mod_forum',
  150. 'expected' => true,
  151. 'filearea' => 'post',
  152. ],
  153. 'mod_forum/post: Student user, teacher is author' => [
  154. 'currentuser' => 'student',
  155. 'fileauthor' => 'teacher',
  156. 'filecomponent' => 'mod_forum',
  157. 'expected' => false,
  158. 'filearea' => 'post',
  159. ],
  160. // Component <> mod_forum.
  161. 'mod_page: Admin user is author' => [
  162. 'currentuser' => 'admin',
  163. 'fileauthor' => 'admin',
  164. 'filecomponent' => 'mod_page',
  165. 'expected' => false,
  166. ],
  167. // Unexisting components.
  168. 'Unexisting component' => [
  169. 'currentuser' => 'admin',
  170. 'fileauthor' => 'admin',
  171. 'filecomponent' => 'unexisting_component',
  172. 'expected' => false,
  173. ],
  174. 'Unexisting module activity' => [
  175. 'currentuser' => 'admin',
  176. 'fileauthor' => 'admin',
  177. 'filecomponent' => 'mod_unexisting',
  178. 'expected' => false,
  179. ],
  180. 'Unexisting block' => [
  181. 'currentuser' => 'admin',
  182. 'fileauthor' => 'admin',
  183. 'filecomponent' => 'block_unexisting',
  184. 'expected' => false,
  185. ],
  186. ];
  187. }
  188. }