PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/tests/grouplib_test.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 841 lines | 504 code | 155 blank | 182 comment | 6 complexity | 668e0015e673c1a6a57c3a042f6325bb MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  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 groups subsystems.
  18. *
  19. * @package core_group
  20. * @category phpunit
  21. * @copyright 2007 onwards Martin Dougiamas (http://dougiamas.com)
  22. * @author Andrew Nicols
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. /**
  27. * Unit tests for lib/grouplib.php
  28. * @group core_group
  29. */
  30. class core_grouplib_testcase extends advanced_testcase {
  31. public function test_groups_get_group_by_idnumber() {
  32. $this->resetAfterTest(true);
  33. $generator = $this->getDataGenerator();
  34. // Create a course category and course.
  35. $cat = $generator->create_category(array('parent' => 0));
  36. $course = $generator->create_course(array('category' => $cat->id));
  37. $idnumber1 = 'idnumber1';
  38. $idnumber2 = 'idnumber2';
  39. /*
  40. * Test with an empty and a null idnumber.
  41. */
  42. // An empty idnumber should always return a false value.
  43. $this->assertFalse(groups_get_group_by_idnumber($course->id, ''));
  44. $this->assertFalse(groups_get_group_by_idnumber($course->id, null));
  45. // Even when a group exists which also has an empty idnumber.
  46. $generator->create_group(array('courseid' => $course->id));
  47. $this->assertFalse(groups_get_group_by_idnumber($course->id, ''));
  48. $this->assertFalse(groups_get_group_by_idnumber($course->id, null));
  49. /*
  50. * Test with a valid idnumber.
  51. */
  52. // There is no matching idnumber at present.
  53. $this->assertFalse(groups_get_group_by_idnumber($course->id, $idnumber1));
  54. // We should now have a valid group returned by the idnumber search.
  55. $group = $generator->create_group(array('courseid' => $course->id, 'idnumber' => $idnumber1));
  56. $this->assertEquals($group, groups_get_group_by_idnumber($course->id, $idnumber1));
  57. // An empty idnumber should still return false.
  58. $this->assertFalse(groups_get_group_by_idnumber($course->id, ''));
  59. $this->assertFalse(groups_get_group_by_idnumber($course->id, null));
  60. /*
  61. * Test with another idnumber.
  62. */
  63. // There is no matching idnumber at present.
  64. $this->assertFalse(groups_get_group_by_idnumber($course->id, $idnumber2));
  65. // We should now have a valid group returned by the idnumber search.
  66. $group = $generator->create_group(array('courseid' => $course->id, 'idnumber' => $idnumber2));
  67. $this->assertEquals($group, groups_get_group_by_idnumber($course->id, $idnumber2));
  68. /*
  69. * Group idnumbers are unique within a course so test that we don't
  70. * retrieve groups for the first course.
  71. */
  72. // Create a second course.
  73. $course = $generator->create_course(array('category' => $cat->id));
  74. // An empty idnumber should always return a false value.
  75. $this->assertFalse(groups_get_group_by_idnumber($course->id, ''));
  76. $this->assertFalse(groups_get_group_by_idnumber($course->id, null));
  77. // Our existing idnumbers shouldn't be returned here as we're in a different course.
  78. $this->assertFalse(groups_get_group_by_idnumber($course->id, $idnumber1));
  79. $this->assertFalse(groups_get_group_by_idnumber($course->id, $idnumber2));
  80. // We should be able to reuse the idnumbers again since this is a different course.
  81. $group = $generator->create_group(array('courseid' => $course->id, 'idnumber' => $idnumber1));
  82. $this->assertEquals($group, groups_get_group_by_idnumber($course->id, $idnumber1));
  83. $group = $generator->create_group(array('courseid' => $course->id, 'idnumber' => $idnumber2));
  84. $this->assertEquals($group, groups_get_group_by_idnumber($course->id, $idnumber2));
  85. }
  86. public function test_groups_get_grouping_by_idnumber() {
  87. $this->resetAfterTest(true);
  88. $generator = $this->getDataGenerator();
  89. // Create a course category and course.
  90. $cat = $generator->create_category(array('parent' => 0));
  91. $course = $generator->create_course(array('category' => $cat->id));
  92. $idnumber1 = 'idnumber1';
  93. $idnumber2 = 'idnumber2';
  94. /*
  95. * Test with an empty and a null idnumber.
  96. */
  97. // An empty idnumber should always return a false value.
  98. $this->assertFalse(groups_get_grouping_by_idnumber($course->id, ''));
  99. $this->assertFalse(groups_get_grouping_by_idnumber($course->id, null));
  100. // Even when a grouping exists which also has an empty idnumber.
  101. $generator->create_grouping(array('courseid' => $course->id));
  102. $this->assertFalse(groups_get_grouping_by_idnumber($course->id, ''));
  103. $this->assertFalse(groups_get_grouping_by_idnumber($course->id, null));
  104. /*
  105. * Test with a valid idnumber
  106. */
  107. // There is no matching idnumber at present.
  108. $this->assertFalse(groups_get_grouping_by_idnumber($course->id, $idnumber1));
  109. // We should now have a valid group returned by the idnumber search.
  110. $grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber1));
  111. $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id, $idnumber1));
  112. // An empty idnumber should still return false.
  113. $this->assertFalse(groups_get_grouping_by_idnumber($course->id, ''));
  114. $this->assertFalse(groups_get_grouping_by_idnumber($course->id, null));
  115. /*
  116. * Test with another idnumber.
  117. */
  118. // There is no matching idnumber at present.
  119. $this->assertFalse(groups_get_grouping_by_idnumber($course->id, $idnumber2));
  120. // We should now have a valid grouping returned by the idnumber search.
  121. $grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber2));
  122. $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id, $idnumber2));
  123. /*
  124. * Grouping idnumbers are unique within a course so test that we don't
  125. * retrieve groupings for the first course.
  126. */
  127. // Create a second course.
  128. $course = $generator->create_course(array('category' => $cat->id));
  129. // An empty idnumber should always return a false value.
  130. $this->assertFalse(groups_get_grouping_by_idnumber($course->id, ''));
  131. $this->assertFalse(groups_get_grouping_by_idnumber($course->id, null));
  132. // Our existing idnumbers shouldn't be returned here as we're in a different course.
  133. $this->assertFalse(groups_get_grouping_by_idnumber($course->id, $idnumber1));
  134. $this->assertFalse(groups_get_grouping_by_idnumber($course->id, $idnumber2));
  135. // We should be able to reuse the idnumbers again since this is a different course.
  136. $grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber1));
  137. $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id, $idnumber1));
  138. $grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber2));
  139. $this->assertEquals($grouping, groups_get_grouping_by_idnumber($course->id, $idnumber2));
  140. }
  141. public function test_groups_get_group_by_name() {
  142. $this->resetAfterTest(true);
  143. $generator = $this->getDataGenerator();
  144. // Create a course category and course.
  145. $cat = $generator->create_category(array('parent' => 0));
  146. $course = $generator->create_course(array('category' => $cat->id));
  147. $name1 = 'Name 1';
  148. $name2 = 'Name 2';
  149. // Test with an empty and a null idnumber.
  150. $this->assertFalse(groups_get_group_by_name($course->id, ''));
  151. $this->assertFalse(groups_get_group_by_name($course->id, null));
  152. // Even when a group exists.
  153. $generator->create_group(array('courseid' => $course->id));
  154. $this->assertFalse(groups_get_group_by_name($course->id, ''));
  155. $this->assertFalse(groups_get_group_by_name($course->id, null));
  156. // Test with a valid name, but one that doesn't exist yet.
  157. $this->assertFalse(groups_get_group_by_name($course->id, $name1));
  158. $this->assertFalse(groups_get_group_by_name($course->id, $name2));
  159. // We should now have a valid group returned by the name search.
  160. $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => $name1));
  161. $this->assertEquals($group1->id, groups_get_group_by_name($course->id, $name1));
  162. $this->assertFalse(groups_get_group_by_name($course->id, $name2));
  163. // We should now have a two valid groups returned by the name search.
  164. $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => $name2));
  165. $this->assertEquals($group1->id, groups_get_group_by_name($course->id, $name1));
  166. $this->assertEquals($group2->id, groups_get_group_by_name($course->id, $name2));
  167. // Delete a group.
  168. $this->assertTrue(groups_delete_group($group1));
  169. $this->assertFalse(groups_get_group_by_name($course->id, $name1));
  170. $this->assertEquals($group2->id, groups_get_group_by_name($course->id, $name2));
  171. /*
  172. * Group idnumbers are unique within a course so test that we don't
  173. * retrieve groups for the first course.
  174. */
  175. // Create a second course.
  176. $course = $generator->create_course(array('category' => $cat->id));
  177. // An empty name should always return a false value.
  178. $this->assertFalse(groups_get_group_by_name($course->id, ''));
  179. $this->assertFalse(groups_get_group_by_name($course->id, null));
  180. // Our existing names shouldn't be returned here as we're in a different course.
  181. $this->assertFalse(groups_get_group_by_name($course->id, $name1));
  182. $this->assertFalse(groups_get_group_by_name($course->id, $name2));
  183. // We should be able to reuse the idnumbers again since this is a different course.
  184. $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => $name1));
  185. $this->assertEquals($group1->id, groups_get_group_by_name($course->id, $name1));
  186. $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => $name2));
  187. $this->assertEquals($group2->id, groups_get_group_by_name($course->id, $name2));
  188. }
  189. public function test_groups_get_grouping() {
  190. $this->resetAfterTest(true);
  191. $generator = $this->getDataGenerator();
  192. // Create a course category and course.
  193. $cat = $generator->create_category(array('parent' => 0));
  194. $course = $generator->create_course(array('category' => $cat->id));
  195. $name1 = 'Grouping 1';
  196. $name2 = 'Grouping 2';
  197. // Test with an empty and a null idnumber.
  198. $this->assertFalse(groups_get_grouping_by_name($course->id, ''));
  199. $this->assertFalse(groups_get_grouping_by_name($course->id, null));
  200. // Even when a group exists.
  201. $generator->create_group(array('courseid' => $course->id));
  202. $this->assertFalse(groups_get_grouping_by_name($course->id, ''));
  203. $this->assertFalse(groups_get_grouping_by_name($course->id, null));
  204. // Test with a valid name, but one that doesn't exist yet.
  205. $this->assertFalse(groups_get_grouping_by_name($course->id, $name1));
  206. $this->assertFalse(groups_get_grouping_by_name($course->id, $name2));
  207. // We should now have a valid group returned by the name search.
  208. $group1 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name1));
  209. $this->assertEquals($group1->id, groups_get_grouping_by_name($course->id, $name1));
  210. $this->assertFalse(groups_get_grouping_by_name($course->id, $name2));
  211. // We should now have a two valid groups returned by the name search.
  212. $group2 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name2));
  213. $this->assertEquals($group1->id, groups_get_grouping_by_name($course->id, $name1));
  214. $this->assertEquals($group2->id, groups_get_grouping_by_name($course->id, $name2));
  215. // Delete a group.
  216. $this->assertTrue(groups_delete_grouping($group1));
  217. $this->assertFalse(groups_get_grouping_by_name($course->id, $name1));
  218. $this->assertEquals($group2->id, groups_get_grouping_by_name($course->id, $name2));
  219. /*
  220. * Group idnumbers are unique within a course so test that we don't
  221. * retrieve groups for the first course.
  222. */
  223. // Create a second course.
  224. $course = $generator->create_course(array('category' => $cat->id));
  225. // An empty name should always return a false value.
  226. $this->assertFalse(groups_get_grouping_by_name($course->id, ''));
  227. $this->assertFalse(groups_get_grouping_by_name($course->id, null));
  228. // Our existing names shouldn't be returned here as we're in a different course.
  229. $this->assertFalse(groups_get_grouping_by_name($course->id, $name1));
  230. $this->assertFalse(groups_get_grouping_by_name($course->id, $name2));
  231. // We should be able to reuse the idnumbers again since this is a different course.
  232. $group1 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name1));
  233. $this->assertEquals($group1->id, groups_get_grouping_by_name($course->id, $name1));
  234. $group2 = $generator->create_grouping(array('courseid' => $course->id, 'name' => $name2));
  235. $this->assertEquals($group2->id, groups_get_grouping_by_name($course->id, $name2));
  236. }
  237. public function test_groups_get_course_data() {
  238. $this->resetAfterTest(true);
  239. $generator = $this->getDataGenerator();
  240. // Create a course category and course.
  241. $cat = $generator->create_category(array('parent' => 0));
  242. $course = $generator->create_course(array('category' => $cat->id));
  243. $grouping1 = $generator->create_grouping(array('courseid' => $course->id, 'name' => 'Grouping 1'));
  244. $grouping2 = $generator->create_grouping(array('courseid' => $course->id, 'name' => 'Grouping 2'));
  245. $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 1'));
  246. $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 2'));
  247. $group3 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 3'));
  248. $group4 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 4'));
  249. // Assign the groups to groupings.
  250. $this->assertTrue(groups_assign_grouping($grouping1->id, $group1->id));
  251. $this->assertTrue(groups_assign_grouping($grouping1->id, $group2->id));
  252. $this->assertTrue(groups_assign_grouping($grouping2->id, $group3->id));
  253. $this->assertTrue(groups_assign_grouping($grouping2->id, $group4->id));
  254. // Get the data.
  255. $data = groups_get_course_data($course->id);
  256. $this->assertInstanceOf('stdClass', $data);
  257. $this->assertObjectHasAttribute('groups', $data);
  258. $this->assertObjectHasAttribute('groupings', $data);
  259. $this->assertObjectHasAttribute('mappings', $data);
  260. // Test we have the expected items returns.
  261. $this->assertCount(4, $data->groups);
  262. $this->assertCount(2, $data->groupings);
  263. $this->assertCount(4, $data->mappings);
  264. // Check we have the expected groups.
  265. $this->assertArrayHasKey($group1->id, $data->groups);
  266. $this->assertArrayHasKey($group2->id, $data->groups);
  267. $this->assertArrayHasKey($group3->id, $data->groups);
  268. $this->assertArrayHasKey($group4->id, $data->groups);
  269. // Test a group-id is mapped correctly.
  270. $this->assertSame($group3->name, $data->groups[$group3->id]->name);
  271. // Check we have the expected number of groupings.
  272. $this->assertContains($grouping1->id, array_keys($data->groupings));
  273. $this->assertContains($grouping2->id, array_keys($data->groupings));
  274. // Test a grouping-id is mapped correctly.
  275. $this->assertEquals($grouping2->name, $data->groupings[$grouping2->id]->name);
  276. // Test that all of the mappings are correct.
  277. $grouping1maps = 0;
  278. $grouping2maps = 0;
  279. $group1maps = 0;
  280. $group2maps = 0;
  281. $group3maps = 0;
  282. $group4maps = 0;
  283. foreach ($data->mappings as $mapping) {
  284. if ($mapping->groupingid === $grouping1->id) {
  285. $grouping1maps++;
  286. $this->assertContains($mapping->groupid, array($group1->id, $group2->id));
  287. } else if ($mapping->groupingid === $grouping2->id) {
  288. $grouping2maps++;
  289. $this->assertContains($mapping->groupid, array($group3->id, $group4->id));
  290. } else {
  291. $this->fail('Unexpected groupingid');
  292. }
  293. switch ($mapping->groupid) {
  294. case $group1->id : $group1maps++; break;
  295. case $group2->id : $group2maps++; break;
  296. case $group3->id : $group3maps++; break;
  297. case $group4->id : $group4maps++; break;
  298. }
  299. }
  300. $this->assertEquals(2, $grouping1maps);
  301. $this->assertEquals(2, $grouping2maps);
  302. $this->assertEquals(1, $group1maps);
  303. $this->assertEquals(1, $group2maps);
  304. $this->assertEquals(1, $group3maps);
  305. $this->assertEquals(1, $group4maps);
  306. // Test the groups_get_all_groups which uses this functionality.
  307. $groups = groups_get_all_groups($course->id);
  308. $groupkeys = array_keys($groups);
  309. $this->assertCount(4, $groups);
  310. $this->assertContains($group1->id, $groupkeys);
  311. $this->assertContains($group2->id, $groupkeys);
  312. $this->assertContains($group3->id, $groupkeys);
  313. $this->assertContains($group4->id, $groupkeys);
  314. $groups = groups_get_all_groups($course->id, null, $grouping1->id);
  315. $groupkeys = array_keys($groups);
  316. $this->assertCount(2, $groups);
  317. $this->assertContains($group1->id, $groupkeys);
  318. $this->assertContains($group2->id, $groupkeys);
  319. $this->assertNotContains($group3->id, $groupkeys);
  320. $this->assertNotContains($group4->id, $groupkeys);
  321. $groups = groups_get_all_groups($course->id, null, $grouping2->id);
  322. $groupkeys = array_keys($groups);
  323. $this->assertCount(2, $groups);
  324. $this->assertNotContains($group1->id, $groupkeys);
  325. $this->assertNotContains($group2->id, $groupkeys);
  326. $this->assertContains($group3->id, $groupkeys);
  327. $this->assertContains($group4->id, $groupkeys);
  328. // Test this function using an alternate column for the result index
  329. $groups = groups_get_all_groups($course->id, null, $grouping2->id, 'g.name, g.id');
  330. $groupkeys = array_keys($groups);
  331. $this->assertCount(2, $groups);
  332. $this->assertNotContains($group3->id, $groupkeys);
  333. $this->assertContains($group3->name, $groupkeys);
  334. $this->assertEquals($group3->id, $groups[$group3->name]->id);
  335. }
  336. /**
  337. * Tests for groups_group_visible.
  338. */
  339. public function test_groups_group_visible() {
  340. global $CFG, $DB;
  341. $generator = $this->getDataGenerator();
  342. $this->resetAfterTest();
  343. $this->setAdminUser();
  344. // Create a course category, course and groups.
  345. $cat = $generator->create_category(array('parent' => 0));
  346. $course = $generator->create_course(array('category' => $cat->id));
  347. $coursecontext = context_course::instance($course->id);
  348. $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 1'));
  349. $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 2'));
  350. $group3 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 3'));
  351. $group4 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 4'));
  352. // Create cm.
  353. $assign = $generator->create_module("assign", array('course' => $course->id));
  354. $cm = get_coursemodule_from_instance("assign", $assign->id);
  355. // Create users.
  356. $user1 = $generator->create_user();
  357. $user2 = $generator->create_user();
  358. $user3 = $generator->create_user();
  359. // Enrol users into the course.
  360. $generator->enrol_user($user1->id, $course->id);
  361. $generator->enrol_user($user2->id, $course->id);
  362. // Assign groups.
  363. groups_add_member($group1, $user2);
  364. // Give capability at course level to the user to access all groups.
  365. $role = $DB->get_field("role", "id", array("shortname" => "manager"));
  366. $generator->enrol_user($user3->id, $course->id, $role);
  367. // Make sure the user has the capability.
  368. assign_capability('moodle/site:accessallgroups', CAP_ALLOW, $role, $coursecontext->id);
  369. // No groups , not forced.
  370. $result = groups_group_visible($group1->id, $course, null, $user1->id);
  371. $this->assertTrue($result);
  372. $result = groups_group_visible(0, $course, null, $user1->id);
  373. $this->assertTrue($result); // Requesting all groups.
  374. $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
  375. $this->assertTrue($result); // Cm with no groups.
  376. $cm->groupmode = SEPARATEGROUPS;
  377. $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
  378. $this->assertFalse($result); // Cm with separate groups.
  379. $result = groups_group_visible($group1->id, $course, $cm, $user2->id);
  380. $this->assertTrue($result); // Cm with separate groups.
  381. $cm->groupmode = VISIBLEGROUPS;
  382. $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
  383. $this->assertTrue($result); // Cm with visible groups.
  384. // No groups, forced.
  385. $course->groupmode = NOGROUPS;
  386. $course->groupmodeforce = true;
  387. update_course($course);
  388. $result = groups_group_visible($group1->id, $course, null, $user1->id);
  389. $this->assertTrue($result);
  390. $result = groups_group_visible(0, $course, null, $user1->id);
  391. $this->assertTrue($result); // Requesting all groups.
  392. $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
  393. $this->assertTrue($result); // Cm with no groups.
  394. $cm->groupmode = SEPARATEGROUPS;
  395. $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
  396. $this->assertTrue($result); // Cm with separate groups.
  397. $result = groups_group_visible($group1->id, $course, $cm, $user2->id);
  398. $this->assertTrue($result); // Cm with separate groups.
  399. $cm->groupmode = SEPARATEGROUPS;
  400. $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
  401. $this->assertTrue($result); // Cm with visible groups.
  402. // Visible groups, forced.
  403. $course->groupmode = VISIBLEGROUPS;
  404. $course->groupmodeforce = true;
  405. update_course($course);
  406. $result = groups_group_visible($group1->id, $course, null, $user1->id);
  407. $this->assertTrue($result);
  408. $result = groups_group_visible(0, $course, null, $user1->id);
  409. $this->assertTrue($result); // Requesting all groups.
  410. $cm->groupmode = NOGROUPS;
  411. $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
  412. $this->assertTrue($result); // Cm with no groups.
  413. $cm->groupmode = SEPARATEGROUPS;
  414. $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
  415. $this->assertTrue($result); // Cm with separate groups.
  416. $result = groups_group_visible($group1->id, $course, $cm, $user2->id);
  417. $this->assertTrue($result); // Cm with separate groups.
  418. $cm->groupmode = VISIBLEGROUPS;
  419. $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
  420. $this->assertTrue($result); // Cm with visible groups.
  421. // Visible groups, not forced.
  422. $course->groupmode = VISIBLEGROUPS;
  423. $course->groupmodeforce = false;
  424. update_course($course);
  425. $result = groups_group_visible($group1->id, $course, null, $user1->id);
  426. $this->assertTrue($result);
  427. $result = groups_group_visible(0, $course, null, $user1->id);
  428. $this->assertTrue($result); // Requesting all groups.
  429. $cm->groupmode = NOGROUPS;
  430. $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
  431. $this->assertTrue($result); // Cm with no groups.
  432. $cm->groupmode = SEPARATEGROUPS;
  433. $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
  434. $this->assertFalse($result); // Cm with separate groups.
  435. $result = groups_group_visible($group1->id, $course, $cm, $user2->id);
  436. $this->assertTrue($result); // Cm with separate groups.
  437. $cm->groupmode = VISIBLEGROUPS;
  438. $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
  439. $this->assertTrue($result); // Cm with visible groups.
  440. // Separate groups, forced.
  441. $course->groupmode = SEPARATEGROUPS;
  442. $course->groupmodeforce = true;
  443. update_course($course);
  444. $result = groups_group_visible($group1->id, $course, null, $user1->id);
  445. $this->assertFalse($result);
  446. $result = groups_group_visible($group1->id, $course, null, $user2->id);
  447. $this->assertTrue($result);
  448. $result = groups_group_visible(0, $course, null, $user2->id);
  449. $this->assertFalse($result); // Requesting all groups.
  450. $result = groups_group_visible(0, $course, null, $user3->id);
  451. $this->assertTrue($result); // Requesting all groups.
  452. $result = groups_group_visible($group1->id, $course, null, $user3->id);
  453. $this->assertTrue($result); // Make sure user with access to all groups can see any group.
  454. $cm->groupmode = NOGROUPS;
  455. $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
  456. $this->assertFalse($result); // Cm with no groups.
  457. $cm->groupmode = SEPARATEGROUPS;
  458. $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
  459. $this->assertFalse($result); // Cm with separate groups.
  460. $result = groups_group_visible($group1->id, $course, $cm, $user2->id);
  461. $this->assertTrue($result); // Cm with separate groups.
  462. $result = groups_group_visible($group1->id, $course, $cm, $user3->id);
  463. $this->assertTrue($result); // Make sure user with access to all groups can see any group.
  464. $cm->groupmode = VISIBLEGROUPS;
  465. $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
  466. $this->assertFalse($result); // Cm with visible groups.
  467. // Separate groups, not forced.
  468. $course->groupmode = SEPARATEGROUPS;
  469. $course->groupmodeforce = false;
  470. update_course($course);
  471. $result = groups_group_visible($group1->id, $course, null, $user1->id);
  472. $this->assertFalse($result);
  473. $result = groups_group_visible($group1->id, $course, null, $user2->id);
  474. $this->assertTrue($result);
  475. $result = groups_group_visible(0, $course, null, $user2->id);
  476. $this->assertFalse($result); // Requesting all groups.
  477. $result = groups_group_visible(0, $course, null, $user3->id);
  478. $this->assertTrue($result); // Requesting all groups.
  479. $cm->groupmode = NOGROUPS;
  480. $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
  481. $this->assertTrue($result); // Cm with no groups.
  482. $cm->groupmode = SEPARATEGROUPS;
  483. $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
  484. $this->assertFalse($result); // Cm with separate groups.
  485. $result = groups_group_visible($group1->id, $course, $cm, $user2->id);
  486. $this->assertTrue($result); // Cm with separate groups.
  487. $cm->groupmode = VISIBLEGROUPS;
  488. $result = groups_group_visible($group1->id, $course, $cm, $user1->id);
  489. $this->assertTrue($result); // Cm with visible groups.
  490. }
  491. function test_groups_get_groupmode() {
  492. global $DB;
  493. $generator = $this->getDataGenerator();
  494. $this->resetAfterTest();
  495. $this->setAdminUser();
  496. // Create a course with no groups forcing.
  497. $course1 = $generator->create_course();
  498. // Create cm1 with no groups, cm1 with visible groups, cm2 with separate groups and cm3 with visible groups.
  499. $assign1 = $generator->create_module("assign", array('course' => $course1->id));
  500. $assign2 = $generator->create_module("assign", array('course' => $course1->id),
  501. array('groupmode' => SEPARATEGROUPS));
  502. $assign3 = $generator->create_module("assign", array('course' => $course1->id),
  503. array('groupmode' => VISIBLEGROUPS));
  504. // Request data for tests.
  505. $cm1 = get_coursemodule_from_instance("assign", $assign1->id);
  506. $cm2 = get_coursemodule_from_instance("assign", $assign2->id);
  507. $cm3 = get_coursemodule_from_instance("assign", $assign3->id);
  508. $modinfo = get_fast_modinfo($course1->id);
  509. // Assert that any method of getting activity groupmode returns the correct result.
  510. $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($cm1));
  511. $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($cm1, $course1));
  512. $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm1->id]));
  513. $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2));
  514. $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2, $course1));
  515. $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm2->id]));
  516. $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($cm3));
  517. $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($cm3, $course1));
  518. $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm3->id]));
  519. // Update the course set the groupmode SEPARATEGROUPS but not forced.
  520. update_course((object)array('id' => $course1->id, 'groupmode' => SEPARATEGROUPS));
  521. // Re-request the data from DB.
  522. $course1 = $DB->get_record('course', array('id' => $course1->id));
  523. $modinfo = get_fast_modinfo($course1->id);
  524. // Existing activities are not changed.
  525. $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($cm1));
  526. $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($cm1, $course1));
  527. $this->assertEquals(NOGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm1->id]));
  528. $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2));
  529. $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2, $course1));
  530. $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm2->id]));
  531. $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($cm3));
  532. $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($cm3, $course1));
  533. $this->assertEquals(VISIBLEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm3->id]));
  534. // Update the course set the groupmode SEPARATEGROUPS and forced.
  535. update_course((object)array('id' => $course1->id, 'groupmode' => SEPARATEGROUPS, 'groupmodeforce' => true));
  536. // Re-request the data from DB.
  537. $course1 = $DB->get_record('course', array('id' => $course1->id));
  538. $modinfo = get_fast_modinfo($course1->id);
  539. // Make sure all activities have separate groups mode now.
  540. $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm1));
  541. $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm1, $course1));
  542. $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm1->id]));
  543. $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2));
  544. $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm2, $course1));
  545. $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm2->id]));
  546. $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm3));
  547. $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($cm3, $course1));
  548. $this->assertEquals(SEPARATEGROUPS, groups_get_activity_groupmode($modinfo->cms[$cm3->id]));
  549. }
  550. /**
  551. * Tests for groups_allgroups_course_menu() .
  552. */
  553. public function test_groups_allgroups_course_menu() {
  554. global $SESSION;
  555. $this->resetAfterTest();
  556. // Generate data.
  557. $course = $this->getDataGenerator()->create_course();
  558. $record = new stdClass();
  559. $record->courseid = $course->id;
  560. $group1 = $this->getDataGenerator()->create_group($record);
  561. $group2 = $this->getDataGenerator()->create_group($record);
  562. $user = $this->getDataGenerator()->create_user();
  563. $this->getDataGenerator()->enrol_user($user->id, $course->id);
  564. $this->setUser($user);
  565. $html = groups_allgroups_course_menu($course, 'someurl.php');
  566. // Since user is not a part of this group and doesn't have accessallgroups permission,
  567. // the html should be empty.
  568. $this->assertEmpty($html);
  569. groups_add_member($group1->id, $user);
  570. // Now user can access one of the group. We can't assert an exact match here because of random ids generated by yui. So do
  571. // partial match to see if all groups are listed or not.
  572. $html = groups_allgroups_course_menu($course, 'someurl.php');
  573. $this->assertContains(format_string($group1->name), $html);
  574. $this->assertNotContains(format_string($group2->name), $html);
  575. $this->setAdminUser();
  576. // Now user can access everything.
  577. $html = groups_allgroups_course_menu($course, 'someurl.php');
  578. $this->assertContains(format_string($group1->name), $html);
  579. $this->assertContains(format_string($group2->name), $html);
  580. // Make sure separate groups mode, doesn't change anything.
  581. $course->groupmode = SEPARATEGROUPS;
  582. update_course($course);
  583. $html = groups_allgroups_course_menu($course, 'someurl.php');
  584. $this->assertContains(format_string($group1->name), $html);
  585. $this->assertContains(format_string($group2->name), $html);
  586. // Make sure Visible groups mode, doesn't change anything.
  587. $course->groupmode = VISIBLEGROUPS;
  588. update_course($course);
  589. $html = groups_allgroups_course_menu($course, 'someurl.php');
  590. $this->assertContains(format_string($group1->name), $html);
  591. $this->assertContains(format_string($group2->name), $html);
  592. // Let us test activegroup changes now.
  593. $this->setUser($user);
  594. $SESSION->activegroup[$course->id][VISIBLEGROUPS][$course->defaultgroupingid] = 5;
  595. groups_allgroups_course_menu($course, 'someurl.php', false); // Do not update session.
  596. $this->assertSame(5, $SESSION->activegroup[$course->id][VISIBLEGROUPS][$course->defaultgroupingid]);
  597. groups_allgroups_course_menu($course, 'someurl.php', true, $group1->id); // Update session.
  598. $this->assertSame($group1->id, $SESSION->activegroup[$course->id][VISIBLEGROUPS][$course->defaultgroupingid]);
  599. // Try to update session with an invalid groupid. It should not accept the invalid id.
  600. groups_allgroups_course_menu($course, 'someurl.php', true, 256);
  601. $this->assertEquals($group1->id, $SESSION->activegroup[$course->id][VISIBLEGROUPS][$course->defaultgroupingid]);
  602. }
  603. /**
  604. * This unit test checks that groups_get_all_groups returns groups in
  605. * alphabetical order even if they are in a grouping.
  606. */
  607. public function test_groups_ordering() {
  608. $generator = $this->getDataGenerator();
  609. $this->resetAfterTest();
  610. // Create a course category and course.
  611. $cat = $generator->create_category(array('parent' => 0));
  612. $course = $generator->create_course(array('category' => $cat->id));
  613. $grouping = $generator->create_grouping(array('courseid' => $course->id, 'name' => 'Grouping'));
  614. // Create groups in reverse order.
  615. $group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 2'));
  616. $group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'Group 1'));
  617. // Assign the groups to the grouping in reverse order.
  618. $this->assertTrue(groups_assign_grouping($grouping->id, $group2->id));
  619. $this->assertTrue(groups_assign_grouping($grouping->id, $group1->id));
  620. // Get all groups and check they are alphabetical.
  621. $groups = array_values(groups_get_all_groups($course->id, 0));
  622. $this->assertEquals('Group 1', $groups[0]->name);
  623. $this->assertEquals('Group 2', $groups[1]->name);
  624. // Now check the same is true when accessed by grouping.
  625. $groups = array_values(groups_get_all_groups($course->id, 0, $grouping->id));
  626. $this->assertEquals('Group 1', $groups[0]->name);
  627. $this->assertEquals('Group 2', $groups[1]->name);
  628. }
  629. /**
  630. * Tests for groups_get_user_groups() method.
  631. */
  632. public function test_groups_get_user_groups() {
  633. $this->resetAfterTest(true);
  634. $generator = $this->getDataGenerator();
  635. // Create courses.
  636. $course1 = $generator->create_course();
  637. $course2 = $generator->create_course();
  638. // Create users.
  639. $user1 = $generator->create_user();
  640. $user2 = $generator->create_user();
  641. $user3 = $generator->create_user();
  642. // Enrol users.
  643. $generator->enrol_user($user1->id, $course1->id);
  644. $generator->enrol_user($user1->id, $course2->id);
  645. $generator->enrol_user($user2->id, $course2->id);
  646. $generator->enrol_user($user3->id, $course2->id);
  647. // Create groups.
  648. $group1 = $generator->create_group(array('courseid' => $course1->id));
  649. $group2 = $generator->create_group(array('courseid' => $course2->id));
  650. $group3 = $generator->create_group(array('courseid' => $course2->id));
  651. // Assign users to groups.
  652. $this->assertTrue($generator->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id)));
  653. $this->assertTrue($generator->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id)));
  654. // Get user groups.
  655. $usergroups1 = groups_get_user_groups($course1->id, $user1->id);
  656. $usergroups2 = groups_get_user_groups($course2->id, $user2->id);;
  657. // Assert return data.
  658. $this->assertEquals($group1->id, $usergroups1[0][0]);
  659. $this->assertEquals($group2->id, $usergroups2[0][0]);
  660. // Now, test with groupings.
  661. $grouping1 = $generator->create_grouping(array('courseid' => $course1->id));
  662. $grouping2 = $generator->create_grouping(array('courseid' => $course2->id));
  663. // Assign the groups to grouping.
  664. groups_assign_grouping($grouping1->id, $group1->id);
  665. groups_assign_grouping($grouping2->id, $group2->id);
  666. groups_assign_grouping($grouping2->id, $group3->id);
  667. // Test with grouping.
  668. $usergroups1 = groups_get_user_groups($course1->id, $user1->id);
  669. $usergroups2 = groups_get_user_groups($course2->id, $user2->id);
  670. $this->assertArrayHasKey($grouping1->id, $usergroups1);
  671. $this->assertArrayHasKey($grouping2->id, $usergroups2);
  672. // Test user without a group.
  673. $usergroups1 = groups_get_user_groups($course2->id, $user3->id);
  674. $this->assertCount(0, $usergroups1[0]);
  675. // Test with userid = 0.
  676. $usergroups1 = groups_get_user_groups($course1->id, 0);
  677. $usergroups2 = groups_get_user_groups($course2->id, 0);
  678. $this->assertCount(0, $usergroups1[0]);
  679. $this->assertCount(0, $usergroups2[0]);
  680. // Test with courseid = 0.
  681. $usergroups1 = groups_get_user_groups(0, $user1->id);
  682. $usergroups2 = groups_get_user_groups(0, $user2->id);
  683. $this->assertCount(0, $usergroups1[0]);
  684. $this->assertCount(0, $usergroups2[0]);
  685. }
  686. }