PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/filebrowser/tests/file_browser_test.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 248 lines | 133 code | 39 blank | 76 comment | 1 complexity | 7e0d30f7a19e1ad52422cda1f404be0f 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 file browser
  18. *
  19. * @package core_files
  20. * @copyright 2017 Marina Glancy
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. /**
  25. * Unit tests for file browser
  26. *
  27. * @package core_files
  28. * @copyright 2017 Marina Glancy
  29. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30. */
  31. class file_browser_testcase extends advanced_testcase {
  32. /** @var stdClass */
  33. protected $course1;
  34. /** @var stdClass */
  35. protected $course2;
  36. /** @var stdClass */
  37. protected $module1;
  38. /** @var stdClass */
  39. protected $module2;
  40. /** @var stdClass */
  41. protected $course1filerecord;
  42. /** @var stdClass */
  43. protected $teacher;
  44. /** @var stdClass */
  45. protected $teacherrole;
  46. /**
  47. * Set up
  48. */
  49. public function setUp() {
  50. global $DB;
  51. $this->resetAfterTest();
  52. $this->setAdminUser();
  53. $this->getDataGenerator()->create_category(); // Empty category.
  54. $this->course1 = $this->getDataGenerator()->create_course(); // Empty course.
  55. $this->course2 = $this->getDataGenerator()->create_course();
  56. // Add a file to course1 summary.
  57. $coursecontext1 = context_course::instance($this->course1->id);
  58. $this->course1filerecord = array('contextid' => $coursecontext1->id,
  59. 'component' => 'course',
  60. 'filearea' => 'summary',
  61. 'itemid' => '0',
  62. 'filepath' => '/',
  63. 'filename' => 'summaryfile.jpg');
  64. $fs = get_file_storage();
  65. $fs->create_file_from_string($this->course1filerecord, 'IMG');
  66. $this->module1 = $this->getDataGenerator()->create_module('resource', ['course' => $this->course2->id]); // Contains 1 file.
  67. $this->module2 = $this->getDataGenerator()->create_module('assign', ['course' => $this->course2->id]); // Contains no files.
  68. $this->teacher = $this->getDataGenerator()->create_user();
  69. $this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
  70. // Make sure we're testing what should be the default capabilities.
  71. assign_capability('moodle/restore:viewautomatedfilearea', CAP_ALLOW, $this->teacherrole->id, $coursecontext1);
  72. $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course1->id, $this->teacherrole->id);
  73. $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course2->id, $this->teacherrole->id);
  74. $this->setUser($this->teacher);
  75. }
  76. /**
  77. * Test "Server files" from the system context
  78. */
  79. public function test_file_info_context_system() {
  80. // There is one non-empty category child and two category children.
  81. $browser = get_file_browser();
  82. $fileinfo = $browser->get_file_info(context_system::instance());
  83. $this->assertNotEmpty($fileinfo->count_non_empty_children());
  84. $this->assertEquals(1, count($fileinfo->get_non_empty_children()));
  85. $categorychildren = array_filter($fileinfo->get_children(), function($a) {
  86. return $a instanceof file_info_context_coursecat;
  87. });
  88. $this->assertEquals(2, count($categorychildren));
  89. }
  90. /**
  91. * Test "Server files" from the system context, hide Misc category
  92. */
  93. public function test_file_info_context_system_hidden() {
  94. // Hide the course category that contains our two courses. Teacher does not have cap to view hidden categories.
  95. coursecat::get($this->course1->category)->update(['visible' => 0]);
  96. // We should have two non-empty children in system context (courses).
  97. $browser = get_file_browser();
  98. $fileinfo = $browser->get_file_info(context_system::instance());
  99. $this->assertNotEmpty($fileinfo->count_non_empty_children());
  100. $this->assertEquals(2, count($fileinfo->get_non_empty_children()));
  101. // Should be 1 category children (empty category).
  102. $categorychildren = array_filter($fileinfo->get_children(), function($a) {
  103. return $a instanceof file_info_context_coursecat;
  104. });
  105. $this->assertEquals(1, count($categorychildren));
  106. // Should be 2 course children - courses that belonged to hidden subcategory are now direct children of "System".
  107. $coursechildren = array_filter($fileinfo->get_children(), function($a) {
  108. return $a instanceof file_info_context_course;
  109. });
  110. $this->assertEquals(2, count($coursechildren));
  111. }
  112. /**
  113. * Test "Server files" from the course category context
  114. */
  115. public function test_file_info_context_coursecat() {
  116. // There are two non-empty courses.
  117. $browser = get_file_browser();
  118. $fileinfo = $browser->get_file_info(context_coursecat::instance($this->course2->category));
  119. $this->assertNotEmpty($fileinfo->count_non_empty_children());
  120. $this->assertEquals(2, count($fileinfo->get_non_empty_children()));
  121. $coursechildren = array_filter($fileinfo->get_children(), function($a) {
  122. return $a instanceof file_info_context_course;
  123. });
  124. $this->assertEquals(2, count($coursechildren));
  125. }
  126. /**
  127. * Test "Server files" from the course category context, only look for .jpg
  128. */
  129. public function test_file_info_context_coursecat_jpg() {
  130. // There is one non-empty category child and two category children.
  131. $browser = get_file_browser();
  132. $fileinfo = $browser->get_file_info(context_system::instance());
  133. $this->assertNotEmpty($fileinfo->count_non_empty_children(['.jpg']));
  134. $this->assertEquals(1, count($fileinfo->get_non_empty_children(['.jpg'])));
  135. }
  136. /**
  137. * Test "Server files" from the course context (course1)
  138. */
  139. public function test_file_info_context_course_1() {
  140. $browser = get_file_browser();
  141. $fileinfo = $browser->get_file_info(context_course::instance($this->course1->id));
  142. // Fileinfo element has only one non-empty child - "Course summary" file area.
  143. $this->assertNotEmpty($fileinfo->count_non_empty_children());
  144. $nonemptychildren = $fileinfo->get_non_empty_children();
  145. $this->assertEquals(1, count($nonemptychildren));
  146. $child = reset($nonemptychildren);
  147. $this->assertTrue($child instanceof file_info_stored);
  148. $this->assertEquals(['filename' => '.'] + $this->course1filerecord, $child->get_params());
  149. // Filearea "Course summary" has a child that is the actual image file.
  150. $this->assertEquals($this->course1filerecord, $child->get_children()[0]->get_params());
  151. // There are seven course-level file areas available to teachers with default caps and no modules in this course.
  152. $allchildren = $fileinfo->get_children();
  153. $this->assertEquals(7, count($allchildren));
  154. $modulechildren = array_filter($allchildren, function($a) {
  155. return $a instanceof file_info_context_module;
  156. });
  157. $this->assertEquals(0, count($modulechildren));
  158. // Admin can see seven course-level file areas.
  159. $this->setAdminUser();
  160. $fileinfo = $browser->get_file_info(context_course::instance($this->course1->id));
  161. $this->assertEquals(7, count($fileinfo->get_children()));
  162. }
  163. /**
  164. * Test "Server files" from the course context (course1)
  165. */
  166. public function test_file_info_context_course_2() {
  167. // 2. Start from the course level.
  168. $browser = get_file_browser();
  169. $fileinfo = $browser->get_file_info(context_course::instance($this->course2->id));
  170. $this->assertNotEmpty($fileinfo->count_non_empty_children());
  171. $nonemptychildren = $fileinfo->get_non_empty_children();
  172. $this->assertEquals(1, count($nonemptychildren));
  173. $child = reset($nonemptychildren);
  174. $this->assertTrue($child instanceof file_info_context_module);
  175. $this->assertEquals($this->module1->name.' (File)', $child->get_visible_name());
  176. $this->assertEquals(1, count($child->get_non_empty_children()));
  177. $this->assertEquals(1, $child->count_non_empty_children());
  178. $modulechildren = array_filter($fileinfo->get_children(), function($a) {
  179. return $a instanceof file_info_context_module;
  180. });
  181. $this->assertEquals(2, count($modulechildren));
  182. }
  183. /**
  184. * Test "Server files" from the course context (module1)
  185. */
  186. public function test_file_info_context_module_1() {
  187. $module1context = context_module::instance($this->module1->cmid);
  188. $browser = get_file_browser();
  189. $fileinfo = $browser->get_file_info($module1context);
  190. $this->assertEquals($this->module1->name . ' (File)', $fileinfo->get_visible_name());
  191. $this->assertNotEmpty($fileinfo->count_non_empty_children());
  192. $nonemptychildren = $fileinfo->get_non_empty_children();
  193. $this->assertEquals(1, count($nonemptychildren));
  194. $child = reset($nonemptychildren);
  195. $this->assertTrue($child instanceof file_info_stored);
  196. }
  197. /**
  198. * Test "Server files" from the course context (module1)
  199. */
  200. public function test_file_info_context_module_2() {
  201. $module2context = context_module::instance($this->module2->cmid);
  202. $browser = get_file_browser();
  203. $fileinfo = $browser->get_file_info($module2context);
  204. $this->assertEquals($this->module2->name.' (Assignment)', $fileinfo->get_visible_name());
  205. $this->assertEmpty($fileinfo->count_non_empty_children());
  206. $nonemptychildren = $fileinfo->get_non_empty_children();
  207. $this->assertEquals(0, count($nonemptychildren));
  208. }
  209. }