2012-09-16 20:17:16 +02:00
|
|
|
<?php
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2022-02-05 10:30:50 +01:00
|
|
|
namespace core_cohort;
|
2012-09-16 20:17:16 +02:00
|
|
|
|
2023-03-07 13:28:02 +11:00
|
|
|
use core_cohort\customfield\cohort_handler;
|
|
|
|
use core_customfield\data_controller;
|
|
|
|
|
2012-09-16 20:17:16 +02:00
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
require_once("$CFG->dirroot/cohort/lib.php");
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cohort library tests.
|
|
|
|
*
|
|
|
|
* @package core_cohort
|
|
|
|
* @category phpunit
|
|
|
|
* @copyright 2012 Petr Skoda {@link http://skodak.org}
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2024-10-15 07:45:57 +08:00
|
|
|
final class lib_test extends \advanced_testcase {
|
2012-09-16 20:17:16 +02:00
|
|
|
|
2023-03-07 13:28:02 +11:00
|
|
|
/**
|
|
|
|
* Create Cohort custom field for testing.
|
|
|
|
*
|
|
|
|
* @return \core_customfield\field_controller
|
|
|
|
*/
|
|
|
|
protected function create_cohort_custom_field(): \core_customfield\field_controller {
|
|
|
|
$fieldcategory = self::getDataGenerator()->create_custom_field_category([
|
|
|
|
'component' => 'core_cohort',
|
|
|
|
'area' => 'cohort',
|
|
|
|
'name' => 'Other fields',
|
|
|
|
]);
|
|
|
|
|
|
|
|
return self::getDataGenerator()->create_custom_field([
|
|
|
|
'shortname' => 'testfield1',
|
|
|
|
'name' => 'Custom field',
|
|
|
|
'type' => 'text',
|
|
|
|
'categoryid' => $fieldcategory->get('id'),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_cohort_add_cohort(): void {
|
2012-09-16 20:17:16 +02:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->resetAfterTest();
|
2023-03-07 13:28:02 +11:00
|
|
|
$this->setAdminUser();
|
|
|
|
|
|
|
|
$this->create_cohort_custom_field();
|
2012-09-16 20:17:16 +02:00
|
|
|
|
2022-02-05 10:30:50 +01:00
|
|
|
$cohort = new \stdClass();
|
|
|
|
$cohort->contextid = \context_system::instance()->id;
|
2012-09-16 20:17:16 +02:00
|
|
|
$cohort->name = 'test cohort';
|
|
|
|
$cohort->idnumber = 'testid';
|
|
|
|
$cohort->description = 'test cohort desc';
|
|
|
|
$cohort->descriptionformat = FORMAT_HTML;
|
2023-03-07 13:28:02 +11:00
|
|
|
$cohort->customfield_testfield1 = 'Test value 1';
|
2012-09-16 20:17:16 +02:00
|
|
|
|
|
|
|
$id = cohort_add_cohort($cohort);
|
|
|
|
$this->assertNotEmpty($id);
|
|
|
|
|
|
|
|
$newcohort = $DB->get_record('cohort', array('id'=>$id));
|
|
|
|
$this->assertEquals($cohort->contextid, $newcohort->contextid);
|
|
|
|
$this->assertSame($cohort->name, $newcohort->name);
|
|
|
|
$this->assertSame($cohort->description, $newcohort->description);
|
|
|
|
$this->assertEquals($cohort->descriptionformat, $newcohort->descriptionformat);
|
|
|
|
$this->assertNotEmpty($newcohort->timecreated);
|
|
|
|
$this->assertSame($newcohort->component, '');
|
2018-01-23 14:54:18 +01:00
|
|
|
$this->assertSame($newcohort->theme, '');
|
2012-09-16 20:17:16 +02:00
|
|
|
$this->assertSame($newcohort->timecreated, $newcohort->timemodified);
|
2023-03-07 13:28:02 +11:00
|
|
|
|
|
|
|
$handler = cohort_handler::create();
|
|
|
|
$customfieldsdata = $handler->export_instance_data_object($id);
|
|
|
|
$this->assertEquals('Test value 1', $customfieldsdata->testfield1);
|
2013-07-15 12:47:01 +08:00
|
|
|
}
|
|
|
|
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_cohort_add_cohort_missing_name(): void {
|
2022-02-05 10:30:50 +01:00
|
|
|
$cohort = new \stdClass();
|
|
|
|
$cohort->contextid = \context_system::instance()->id;
|
2013-07-15 12:47:01 +08:00
|
|
|
$cohort->name = null;
|
|
|
|
$cohort->idnumber = 'testid';
|
|
|
|
$cohort->description = 'test cohort desc';
|
|
|
|
$cohort->descriptionformat = FORMAT_HTML;
|
|
|
|
|
2022-02-05 10:30:50 +01:00
|
|
|
$this->expectException(\coding_exception::class);
|
2020-08-03 01:19:50 +02:00
|
|
|
$this->expectExceptionMessage('Missing cohort name in cohort_add_cohort()');
|
2013-07-15 12:47:01 +08:00
|
|
|
cohort_add_cohort($cohort);
|
|
|
|
}
|
|
|
|
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_cohort_add_cohort_event(): void {
|
2013-07-15 12:47:01 +08:00
|
|
|
$this->resetAfterTest();
|
|
|
|
|
|
|
|
// Setup cohort data structure.
|
2022-02-05 10:30:50 +01:00
|
|
|
$cohort = new \stdClass();
|
|
|
|
$cohort->contextid = \context_system::instance()->id;
|
2013-07-15 12:47:01 +08:00
|
|
|
$cohort->name = 'test cohort';
|
|
|
|
$cohort->idnumber = 'testid';
|
|
|
|
$cohort->description = 'test cohort desc';
|
|
|
|
$cohort->descriptionformat = FORMAT_HTML;
|
2012-09-16 20:17:16 +02:00
|
|
|
|
2013-07-15 12:47:01 +08:00
|
|
|
// Catch Events.
|
|
|
|
$sink = $this->redirectEvents();
|
|
|
|
|
|
|
|
// Perform the add operation.
|
|
|
|
$id = cohort_add_cohort($cohort);
|
|
|
|
|
|
|
|
// Capture the event.
|
|
|
|
$events = $sink->get_events();
|
|
|
|
$sink->close();
|
|
|
|
|
|
|
|
// Validate the event.
|
|
|
|
$this->assertCount(1, $events);
|
|
|
|
$event = $events[0];
|
|
|
|
$this->assertInstanceOf('\core\event\cohort_created', $event);
|
|
|
|
$this->assertEquals('cohort', $event->objecttable);
|
|
|
|
$this->assertEquals($id, $event->objectid);
|
|
|
|
$this->assertEquals($cohort->contextid, $event->contextid);
|
2022-02-05 10:30:50 +01:00
|
|
|
$url = new \moodle_url('/cohort/index.php', array('contextid' => $event->contextid));
|
2014-04-01 14:51:05 +08:00
|
|
|
$this->assertEquals($url, $event->get_url());
|
2013-08-13 17:26:35 +08:00
|
|
|
$this->assertEquals($cohort, $event->get_record_snapshot('cohort', $id));
|
2014-01-21 17:04:37 +08:00
|
|
|
$this->assertEventContextNotUsed($event);
|
2012-09-16 20:17:16 +02:00
|
|
|
}
|
|
|
|
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_cohort_update_cohort(): void {
|
2012-09-16 20:17:16 +02:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->resetAfterTest();
|
2023-03-07 13:28:02 +11:00
|
|
|
$this->setAdminUser();
|
|
|
|
|
|
|
|
$this->create_cohort_custom_field();
|
2012-09-16 20:17:16 +02:00
|
|
|
|
2022-02-05 10:30:50 +01:00
|
|
|
$cohort = new \stdClass();
|
|
|
|
$cohort->contextid = \context_system::instance()->id;
|
2012-09-16 20:17:16 +02:00
|
|
|
$cohort->name = 'test cohort';
|
|
|
|
$cohort->idnumber = 'testid';
|
|
|
|
$cohort->description = 'test cohort desc';
|
|
|
|
$cohort->descriptionformat = FORMAT_HTML;
|
2023-03-07 13:28:02 +11:00
|
|
|
$cohort->customfield_testfield1 = 'Test value 1';
|
|
|
|
|
2012-09-16 20:17:16 +02:00
|
|
|
$id = cohort_add_cohort($cohort);
|
|
|
|
$this->assertNotEmpty($id);
|
|
|
|
$DB->set_field('cohort', 'timecreated', $cohort->timecreated - 10, array('id'=>$id));
|
|
|
|
$DB->set_field('cohort', 'timemodified', $cohort->timemodified - 10, array('id'=>$id));
|
|
|
|
$cohort = $DB->get_record('cohort', array('id'=>$id));
|
|
|
|
|
|
|
|
$cohort->name = 'test cohort 2';
|
2023-03-07 13:28:02 +11:00
|
|
|
$cohort->customfield_testfield1 = 'Test value updated';
|
|
|
|
|
2012-09-16 20:17:16 +02:00
|
|
|
cohort_update_cohort($cohort);
|
|
|
|
|
|
|
|
$newcohort = $DB->get_record('cohort', array('id'=>$id));
|
|
|
|
|
|
|
|
$this->assertSame($cohort->contextid, $newcohort->contextid);
|
|
|
|
$this->assertSame($cohort->name, $newcohort->name);
|
|
|
|
$this->assertSame($cohort->description, $newcohort->description);
|
|
|
|
$this->assertSame($cohort->descriptionformat, $newcohort->descriptionformat);
|
|
|
|
$this->assertSame($cohort->timecreated, $newcohort->timecreated);
|
|
|
|
$this->assertSame($cohort->component, $newcohort->component);
|
2018-01-23 14:54:18 +01:00
|
|
|
$this->assertSame($newcohort->theme, '');
|
2012-09-16 20:17:16 +02:00
|
|
|
$this->assertGreaterThan($newcohort->timecreated, $newcohort->timemodified);
|
|
|
|
$this->assertLessThanOrEqual(time(), $newcohort->timemodified);
|
2023-03-07 13:28:02 +11:00
|
|
|
|
|
|
|
$handler = cohort_handler::create();
|
|
|
|
$customfieldsdata = $handler->export_instance_data_object($id);
|
|
|
|
$this->assertEquals('Test value updated', $customfieldsdata->testfield1);
|
2012-09-16 20:17:16 +02:00
|
|
|
}
|
|
|
|
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_cohort_update_cohort_event(): void {
|
2013-07-15 12:47:01 +08:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->resetAfterTest();
|
|
|
|
|
|
|
|
// Setup the cohort data structure.
|
2022-02-05 10:30:50 +01:00
|
|
|
$cohort = new \stdClass();
|
|
|
|
$cohort->contextid = \context_system::instance()->id;
|
2013-07-15 12:47:01 +08:00
|
|
|
$cohort->name = 'test cohort';
|
|
|
|
$cohort->idnumber = 'testid';
|
|
|
|
$cohort->description = 'test cohort desc';
|
|
|
|
$cohort->descriptionformat = FORMAT_HTML;
|
2018-01-23 14:54:18 +01:00
|
|
|
$cohort->theme = '';
|
2013-07-15 12:47:01 +08:00
|
|
|
$id = cohort_add_cohort($cohort);
|
|
|
|
$this->assertNotEmpty($id);
|
|
|
|
|
|
|
|
$cohort->name = 'test cohort 2';
|
|
|
|
|
|
|
|
// Catch Events.
|
|
|
|
$sink = $this->redirectEvents();
|
|
|
|
|
|
|
|
// Peform the update.
|
|
|
|
cohort_update_cohort($cohort);
|
2018-01-23 14:54:18 +01:00
|
|
|
// Add again theme property to the cohort object for comparing it to the event snapshop.
|
|
|
|
$cohort->theme = '';
|
2013-07-15 12:47:01 +08:00
|
|
|
|
|
|
|
$events = $sink->get_events();
|
|
|
|
$sink->close();
|
|
|
|
|
|
|
|
// Validate the event.
|
|
|
|
$this->assertCount(1, $events);
|
|
|
|
$event = $events[0];
|
|
|
|
$updatedcohort = $DB->get_record('cohort', array('id'=>$id));
|
|
|
|
$this->assertInstanceOf('\core\event\cohort_updated', $event);
|
|
|
|
$this->assertEquals('cohort', $event->objecttable);
|
|
|
|
$this->assertEquals($updatedcohort->id, $event->objectid);
|
|
|
|
$this->assertEquals($updatedcohort->contextid, $event->contextid);
|
2022-02-05 10:30:50 +01:00
|
|
|
$url = new \moodle_url('/cohort/edit.php', array('id' => $event->objectid));
|
2014-04-01 14:51:05 +08:00
|
|
|
$this->assertEquals($url, $event->get_url());
|
2013-08-13 17:26:35 +08:00
|
|
|
$this->assertEquals($cohort, $event->get_record_snapshot('cohort', $id));
|
2014-01-21 17:04:37 +08:00
|
|
|
$this->assertEventContextNotUsed($event);
|
2013-07-15 12:47:01 +08:00
|
|
|
}
|
|
|
|
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_cohort_delete_cohort(): void {
|
2012-09-16 20:17:16 +02:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->resetAfterTest();
|
2023-03-07 13:28:02 +11:00
|
|
|
$this->setAdminUser();
|
2012-09-16 20:17:16 +02:00
|
|
|
|
2023-03-07 13:28:02 +11:00
|
|
|
$field = $this->create_cohort_custom_field();
|
|
|
|
|
|
|
|
$cohort = $this->getDataGenerator()->create_cohort(['customfield_testfield1' => 'Test value 1']);
|
|
|
|
$this->assertTrue($DB->record_exists('customfield_data', ['instanceid' => $cohort->id, 'fieldid' => $field->get('id')]));
|
2012-09-16 20:17:16 +02:00
|
|
|
|
|
|
|
cohort_delete_cohort($cohort);
|
|
|
|
|
|
|
|
$this->assertFalse($DB->record_exists('cohort', array('id'=>$cohort->id)));
|
2023-03-07 13:28:02 +11:00
|
|
|
$this->assertFalse($DB->record_exists('customfield_data', ['instanceid' => $cohort->id, 'fieldid' => $field->get('id')]));
|
2012-09-16 20:17:16 +02:00
|
|
|
}
|
|
|
|
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_cohort_delete_cohort_event(): void {
|
2013-07-15 12:47:01 +08:00
|
|
|
|
|
|
|
$this->resetAfterTest();
|
|
|
|
|
|
|
|
$cohort = $this->getDataGenerator()->create_cohort();
|
|
|
|
|
|
|
|
// Capture the events.
|
|
|
|
$sink = $this->redirectEvents();
|
|
|
|
|
|
|
|
// Perform the delete.
|
|
|
|
cohort_delete_cohort($cohort);
|
|
|
|
|
|
|
|
$events = $sink->get_events();
|
|
|
|
$sink->close();
|
|
|
|
|
|
|
|
// Validate the event structure.
|
|
|
|
$this->assertCount(1, $events);
|
|
|
|
$event = $events[0];
|
|
|
|
$this->assertInstanceOf('\core\event\cohort_deleted', $event);
|
|
|
|
$this->assertEquals('cohort', $event->objecttable);
|
|
|
|
$this->assertEquals($cohort->id, $event->objectid);
|
2022-02-05 10:30:50 +01:00
|
|
|
$url = new \moodle_url('/cohort/index.php', array('contextid' => $event->contextid));
|
2014-04-01 14:51:05 +08:00
|
|
|
$this->assertEquals($url, $event->get_url());
|
2013-08-13 17:26:35 +08:00
|
|
|
$this->assertEquals($cohort, $event->get_record_snapshot('cohort', $cohort->id));
|
2014-01-21 17:04:37 +08:00
|
|
|
$this->assertEventContextNotUsed($event);
|
2013-07-15 12:47:01 +08:00
|
|
|
}
|
|
|
|
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_cohort_delete_category(): void {
|
2012-09-16 20:17:16 +02:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->resetAfterTest();
|
|
|
|
|
|
|
|
$category = $this->getDataGenerator()->create_category();
|
|
|
|
|
2022-02-05 10:30:50 +01:00
|
|
|
$cohort = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($category->id)->id));
|
2012-09-16 20:17:16 +02:00
|
|
|
|
|
|
|
cohort_delete_category($category);
|
|
|
|
|
|
|
|
$this->assertTrue($DB->record_exists('cohort', array('id'=>$cohort->id)));
|
|
|
|
$newcohort = $DB->get_record('cohort', array('id'=>$cohort->id));
|
2022-02-05 10:30:50 +01:00
|
|
|
$this->assertEquals(\context_system::instance()->id, $newcohort->contextid);
|
2012-09-16 20:17:16 +02:00
|
|
|
}
|
|
|
|
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_cohort_add_member(): void {
|
2012-09-16 20:17:16 +02:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->resetAfterTest();
|
|
|
|
|
|
|
|
$cohort = $this->getDataGenerator()->create_cohort();
|
|
|
|
$user = $this->getDataGenerator()->create_user();
|
|
|
|
|
|
|
|
$this->assertFalse($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id)));
|
|
|
|
cohort_add_member($cohort->id, $user->id);
|
|
|
|
$this->assertTrue($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id)));
|
|
|
|
}
|
|
|
|
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_cohort_add_member_event(): void {
|
2013-07-15 12:47:01 +08:00
|
|
|
global $USER;
|
2013-08-13 17:26:35 +08:00
|
|
|
$this->resetAfterTest();
|
2013-07-15 12:47:01 +08:00
|
|
|
|
|
|
|
// Setup the data.
|
|
|
|
$cohort = $this->getDataGenerator()->create_cohort();
|
|
|
|
$user = $this->getDataGenerator()->create_user();
|
|
|
|
|
|
|
|
// Capture the events.
|
|
|
|
$sink = $this->redirectEvents();
|
|
|
|
|
|
|
|
// Peform the add member operation.
|
|
|
|
cohort_add_member($cohort->id, $user->id);
|
|
|
|
|
|
|
|
$events = $sink->get_events();
|
|
|
|
$sink->close();
|
|
|
|
|
|
|
|
// Validate the event.
|
|
|
|
$this->assertCount(1, $events);
|
|
|
|
$event = $events[0];
|
|
|
|
$this->assertInstanceOf('\core\event\cohort_member_added', $event);
|
|
|
|
$this->assertEquals('cohort', $event->objecttable);
|
|
|
|
$this->assertEquals($cohort->id, $event->objectid);
|
|
|
|
$this->assertEquals($user->id, $event->relateduserid);
|
|
|
|
$this->assertEquals($USER->id, $event->userid);
|
2022-02-05 10:30:50 +01:00
|
|
|
$url = new \moodle_url('/cohort/assign.php', array('id' => $event->objectid));
|
2014-04-01 14:51:05 +08:00
|
|
|
$this->assertEquals($url, $event->get_url());
|
2014-01-21 17:04:37 +08:00
|
|
|
$this->assertEventContextNotUsed($event);
|
2013-07-15 12:47:01 +08:00
|
|
|
}
|
|
|
|
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_cohort_remove_member(): void {
|
2012-09-16 20:17:16 +02:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->resetAfterTest();
|
|
|
|
|
|
|
|
$cohort = $this->getDataGenerator()->create_cohort();
|
|
|
|
$user = $this->getDataGenerator()->create_user();
|
|
|
|
|
|
|
|
cohort_add_member($cohort->id, $user->id);
|
|
|
|
$this->assertTrue($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id)));
|
|
|
|
|
|
|
|
cohort_remove_member($cohort->id, $user->id);
|
|
|
|
$this->assertFalse($DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$user->id)));
|
|
|
|
}
|
|
|
|
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_cohort_remove_member_event(): void {
|
2013-07-15 12:47:01 +08:00
|
|
|
global $USER;
|
2013-08-13 17:26:35 +08:00
|
|
|
$this->resetAfterTest();
|
2013-07-15 12:47:01 +08:00
|
|
|
|
|
|
|
// Setup the data.
|
|
|
|
$cohort = $this->getDataGenerator()->create_cohort();
|
|
|
|
$user = $this->getDataGenerator()->create_user();
|
|
|
|
cohort_add_member($cohort->id, $user->id);
|
|
|
|
|
|
|
|
// Capture the events.
|
|
|
|
$sink = $this->redirectEvents();
|
|
|
|
|
|
|
|
// Peform the remove operation.
|
|
|
|
cohort_remove_member($cohort->id, $user->id);
|
|
|
|
$events = $sink->get_events();
|
|
|
|
$sink->close();
|
|
|
|
|
|
|
|
// Validate the event.
|
|
|
|
$this->assertCount(1, $events);
|
|
|
|
$event = $events[0];
|
|
|
|
$this->assertInstanceOf('\core\event\cohort_member_removed', $event);
|
|
|
|
$this->assertEquals('cohort', $event->objecttable);
|
|
|
|
$this->assertEquals($cohort->id, $event->objectid);
|
|
|
|
$this->assertEquals($user->id, $event->relateduserid);
|
|
|
|
$this->assertEquals($USER->id, $event->userid);
|
2022-02-05 10:30:50 +01:00
|
|
|
$url = new \moodle_url('/cohort/assign.php', array('id' => $event->objectid));
|
2014-04-01 14:51:05 +08:00
|
|
|
$this->assertEquals($url, $event->get_url());
|
2014-01-21 17:04:37 +08:00
|
|
|
$this->assertEventContextNotUsed($event);
|
2013-07-15 12:47:01 +08:00
|
|
|
}
|
|
|
|
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_cohort_is_member(): void {
|
2012-09-16 20:17:16 +02:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->resetAfterTest();
|
|
|
|
|
|
|
|
$cohort = $this->getDataGenerator()->create_cohort();
|
|
|
|
$user = $this->getDataGenerator()->create_user();
|
|
|
|
|
|
|
|
$this->assertFalse(cohort_is_member($cohort->id, $user->id));
|
|
|
|
cohort_add_member($cohort->id, $user->id);
|
|
|
|
$this->assertTrue(cohort_is_member($cohort->id, $user->id));
|
|
|
|
}
|
2012-09-16 21:07:53 +02:00
|
|
|
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_cohort_get_cohorts(): void {
|
2012-09-16 21:24:49 +02:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->resetAfterTest();
|
|
|
|
|
|
|
|
$category1 = $this->getDataGenerator()->create_category();
|
|
|
|
$category2 = $this->getDataGenerator()->create_category();
|
|
|
|
|
2022-02-05 10:30:50 +01:00
|
|
|
$cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($category1->id)->id, 'name'=>'aaagrrryyy', 'idnumber'=>'','description'=>''));
|
|
|
|
$cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($category1->id)->id, 'name'=>'bbb', 'idnumber'=>'', 'description'=>'yyybrrr'));
|
|
|
|
$cohort3 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($category1->id)->id, 'name'=>'ccc', 'idnumber'=>'xxarrrghyyy', 'description'=>'po_us'));
|
|
|
|
$cohort4 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_system::instance()->id));
|
2012-09-16 21:24:49 +02:00
|
|
|
|
2022-02-05 10:30:50 +01:00
|
|
|
$result = cohort_get_cohorts(\context_coursecat::instance($category2->id)->id);
|
2012-09-16 21:24:49 +02:00
|
|
|
$this->assertEquals(0, $result['totalcohorts']);
|
|
|
|
$this->assertEquals(0, count($result['cohorts']));
|
2012-09-16 21:56:27 +02:00
|
|
|
$this->assertEquals(0, $result['allcohorts']);
|
2012-09-16 21:24:49 +02:00
|
|
|
|
2022-02-05 10:30:50 +01:00
|
|
|
$result = cohort_get_cohorts(\context_coursecat::instance($category1->id)->id);
|
2012-09-16 21:24:49 +02:00
|
|
|
$this->assertEquals(3, $result['totalcohorts']);
|
|
|
|
$this->assertEquals(array($cohort1->id=>$cohort1, $cohort2->id=>$cohort2, $cohort3->id=>$cohort3), $result['cohorts']);
|
2012-09-16 21:56:27 +02:00
|
|
|
$this->assertEquals(3, $result['allcohorts']);
|
2012-09-16 21:24:49 +02:00
|
|
|
|
2022-02-05 10:30:50 +01:00
|
|
|
$result = cohort_get_cohorts(\context_coursecat::instance($category1->id)->id, 0, 100, 'arrrgh');
|
2012-09-16 21:24:49 +02:00
|
|
|
$this->assertEquals(1, $result['totalcohorts']);
|
|
|
|
$this->assertEquals(array($cohort3->id=>$cohort3), $result['cohorts']);
|
2012-09-16 21:56:27 +02:00
|
|
|
$this->assertEquals(3, $result['allcohorts']);
|
2012-09-16 21:24:49 +02:00
|
|
|
|
2022-02-05 10:30:50 +01:00
|
|
|
$result = cohort_get_cohorts(\context_coursecat::instance($category1->id)->id, 0, 100, 'brrr');
|
2012-09-16 21:24:49 +02:00
|
|
|
$this->assertEquals(1, $result['totalcohorts']);
|
|
|
|
$this->assertEquals(array($cohort2->id=>$cohort2), $result['cohorts']);
|
2012-09-16 21:56:27 +02:00
|
|
|
$this->assertEquals(3, $result['allcohorts']);
|
2012-09-16 21:24:49 +02:00
|
|
|
|
2022-02-05 10:30:50 +01:00
|
|
|
$result = cohort_get_cohorts(\context_coursecat::instance($category1->id)->id, 0, 100, 'grrr');
|
2012-09-16 21:24:49 +02:00
|
|
|
$this->assertEquals(1, $result['totalcohorts']);
|
|
|
|
$this->assertEquals(array($cohort1->id=>$cohort1), $result['cohorts']);
|
2012-09-16 21:56:27 +02:00
|
|
|
$this->assertEquals(3, $result['allcohorts']);
|
2012-09-16 21:24:49 +02:00
|
|
|
|
2022-02-05 10:30:50 +01:00
|
|
|
$result = cohort_get_cohorts(\context_coursecat::instance($category1->id)->id, 1, 1, 'yyy');
|
2012-09-16 21:24:49 +02:00
|
|
|
$this->assertEquals(3, $result['totalcohorts']);
|
|
|
|
$this->assertEquals(array($cohort2->id=>$cohort2), $result['cohorts']);
|
2012-09-16 21:56:27 +02:00
|
|
|
$this->assertEquals(3, $result['allcohorts']);
|
2012-09-16 22:32:33 +02:00
|
|
|
|
2022-02-05 10:30:50 +01:00
|
|
|
$result = cohort_get_cohorts(\context_coursecat::instance($category1->id)->id, 0, 100, 'po_us');
|
2012-09-16 22:32:33 +02:00
|
|
|
$this->assertEquals(1, $result['totalcohorts']);
|
|
|
|
$this->assertEquals(array($cohort3->id=>$cohort3), $result['cohorts']);
|
|
|
|
$this->assertEquals(3, $result['allcohorts']);
|
|
|
|
|
2022-02-05 10:30:50 +01:00
|
|
|
$result = cohort_get_cohorts(\context_coursecat::instance($category1->id)->id, 0, 100, 'pokus');
|
2012-09-16 22:32:33 +02:00
|
|
|
$this->assertEquals(0, $result['totalcohorts']);
|
|
|
|
$this->assertEquals(array(), $result['cohorts']);
|
|
|
|
$this->assertEquals(3, $result['allcohorts']);
|
2014-07-22 17:05:06 +08:00
|
|
|
|
2022-02-05 10:30:50 +01:00
|
|
|
$result = cohort_get_cohorts(\context_system::instance()->id);
|
2014-07-22 17:05:06 +08:00
|
|
|
$this->assertEquals(1, $result['totalcohorts']);
|
|
|
|
$this->assertEquals(array($cohort4->id=>$cohort4), $result['cohorts']);
|
|
|
|
$this->assertEquals(1, $result['allcohorts']);
|
|
|
|
}
|
|
|
|
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_cohort_get_all_cohorts(): void {
|
2014-07-22 17:05:06 +08:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->resetAfterTest();
|
|
|
|
|
|
|
|
$category1 = $this->getDataGenerator()->create_category();
|
|
|
|
$category2 = $this->getDataGenerator()->create_category();
|
|
|
|
|
2022-02-05 10:30:50 +01:00
|
|
|
$cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($category1->id)->id, 'name'=>'aaagrrryyy', 'idnumber'=>'','description'=>''));
|
|
|
|
$cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($category1->id)->id, 'name'=>'bbb', 'idnumber'=>'', 'description'=>'yyybrrr'));
|
|
|
|
$cohort3 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($category2->id)->id, 'name'=>'ccc', 'idnumber'=>'xxarrrghyyy', 'description'=>'po_us'));
|
|
|
|
$cohort4 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_system::instance()->id));
|
2014-07-22 17:05:06 +08:00
|
|
|
|
|
|
|
// Get list of all cohorts as admin.
|
|
|
|
$this->setAdminUser();
|
|
|
|
|
|
|
|
$result = cohort_get_all_cohorts(0, 100, '');
|
|
|
|
$this->assertEquals(4, $result['totalcohorts']);
|
|
|
|
$this->assertEquals(array($cohort1->id=>$cohort1, $cohort2->id=>$cohort2, $cohort3->id=>$cohort3, $cohort4->id=>$cohort4), $result['cohorts']);
|
|
|
|
$this->assertEquals(4, $result['allcohorts']);
|
|
|
|
|
|
|
|
$result = cohort_get_all_cohorts(0, 100, 'grrr');
|
|
|
|
$this->assertEquals(1, $result['totalcohorts']);
|
|
|
|
$this->assertEquals(array($cohort1->id=>$cohort1), $result['cohorts']);
|
|
|
|
$this->assertEquals(4, $result['allcohorts']);
|
|
|
|
|
|
|
|
// Get list of all cohorts as manager who has capability everywhere.
|
|
|
|
$user = $this->getDataGenerator()->create_user();
|
|
|
|
$managerrole = $DB->get_record('role', array('shortname' => 'manager'));
|
2022-02-05 10:30:50 +01:00
|
|
|
role_assign($managerrole->id, $user->id, \context_system::instance()->id);
|
2014-07-22 17:05:06 +08:00
|
|
|
$this->setUser($user);
|
|
|
|
|
|
|
|
$result = cohort_get_all_cohorts(0, 100, '');
|
|
|
|
$this->assertEquals(4, $result['totalcohorts']);
|
|
|
|
$this->assertEquals(array($cohort1->id=>$cohort1, $cohort2->id=>$cohort2, $cohort3->id=>$cohort3, $cohort4->id=>$cohort4), $result['cohorts']);
|
|
|
|
$this->assertEquals(4, $result['allcohorts']);
|
|
|
|
|
|
|
|
$result = cohort_get_all_cohorts(0, 100, 'grrr');
|
|
|
|
$this->assertEquals(1, $result['totalcohorts']);
|
|
|
|
$this->assertEquals(array($cohort1->id=>$cohort1), $result['cohorts']);
|
|
|
|
$this->assertEquals(4, $result['allcohorts']);
|
|
|
|
|
|
|
|
// Get list of all cohorts as manager who has capability everywhere except category2.
|
2022-02-05 10:30:50 +01:00
|
|
|
$context2 = \context_coursecat::instance($category2->id);
|
2014-07-22 17:05:06 +08:00
|
|
|
role_change_permission($managerrole->id, $context2, 'moodle/cohort:view', CAP_PROHIBIT);
|
|
|
|
role_change_permission($managerrole->id, $context2, 'moodle/cohort:manage', CAP_PROHIBIT);
|
|
|
|
$this->assertFalse(has_any_capability(array('moodle/cohort:view', 'moodle/cohort:manage'), $context2));
|
|
|
|
|
|
|
|
$result = cohort_get_all_cohorts(0, 100, '');
|
|
|
|
$this->assertEquals(3, $result['totalcohorts']);
|
|
|
|
$this->assertEquals(array($cohort1->id=>$cohort1, $cohort2->id=>$cohort2, $cohort4->id=>$cohort4), $result['cohorts']);
|
|
|
|
$this->assertEquals(3, $result['allcohorts']);
|
|
|
|
|
|
|
|
$result = cohort_get_all_cohorts(0, 100, 'grrr');
|
|
|
|
$this->assertEquals(1, $result['totalcohorts']);
|
|
|
|
$this->assertEquals(array($cohort1->id=>$cohort1), $result['cohorts']);
|
|
|
|
$this->assertEquals(3, $result['allcohorts']);
|
|
|
|
|
2022-02-05 10:30:50 +01:00
|
|
|
$result = cohort_get_cohorts(\context_coursecat::instance($category1->id)->id, 1, 1, 'yyy');
|
2014-07-22 17:05:06 +08:00
|
|
|
$this->assertEquals(2, $result['totalcohorts']);
|
|
|
|
$this->assertEquals(array($cohort2->id=>$cohort2), $result['cohorts']);
|
|
|
|
$this->assertEquals(2, $result['allcohorts']);
|
2012-09-16 21:24:49 +02:00
|
|
|
}
|
2014-07-29 17:44:25 +08:00
|
|
|
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_cohort_get_available_cohorts(): void {
|
2014-07-29 17:44:25 +08:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->resetAfterTest();
|
|
|
|
|
|
|
|
$category1 = $this->getDataGenerator()->create_category();
|
|
|
|
$category2 = $this->getDataGenerator()->create_category();
|
|
|
|
|
|
|
|
$course1 = $this->getDataGenerator()->create_course(array('category' => $category1->id));
|
|
|
|
$course2 = $this->getDataGenerator()->create_course(array('category' => $category2->id));
|
|
|
|
|
2022-02-05 10:30:50 +01:00
|
|
|
$category1ctx = \context_coursecat::instance($category1->id);
|
|
|
|
$category2ctx = \context_coursecat::instance($category2->id);
|
|
|
|
$course1ctx = \context_course::instance(($course1->id));
|
|
|
|
$course2ctx = \context_course::instance(($course2->id));
|
|
|
|
$systemctx = \context_system::instance();
|
2014-07-29 17:44:25 +08:00
|
|
|
|
|
|
|
$cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>$category1ctx->id, 'name'=>'aaagrrryyy', 'idnumber'=>'','description'=>''));
|
|
|
|
$cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>$category1ctx->id, 'name'=>'bbb', 'idnumber'=>'', 'description'=>'yyybrrr', 'visible'=>0));
|
|
|
|
$cohort3 = $this->getDataGenerator()->create_cohort(array('contextid'=>$category2ctx->id, 'name'=>'ccc', 'idnumber'=>'xxarrrghyyy', 'description'=>'po_us'));
|
2014-10-06 09:50:29 +08:00
|
|
|
$cohort4 = $this->getDataGenerator()->create_cohort(array('contextid'=>$systemctx->id, 'name' => 'ddd'));
|
|
|
|
$cohort5 = $this->getDataGenerator()->create_cohort(array('contextid'=>$systemctx->id, 'visible'=>0, 'name' => 'eee'));
|
2014-07-29 17:44:25 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
Structure of generated course categories, courses and cohort:
|
|
|
|
|
|
|
|
system
|
|
|
|
-cohort4 (visible, has 3 members)
|
|
|
|
-cohort5 (not visible, no members)
|
|
|
|
category1
|
|
|
|
-cohort1 (visible, no members)
|
|
|
|
-cohort2 (not visible, has 1 member)
|
|
|
|
course1
|
|
|
|
category2
|
|
|
|
-cohort3 (visible, has 2 member)
|
|
|
|
course2
|
|
|
|
|
|
|
|
In this test we call cohort_get_available_cohorts() for users with different roles
|
|
|
|
and with different paramteres ($withmembers, $search, $offset, $limit) to make sure we go
|
|
|
|
through all possible options of SQL query.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Admin can see visible and invisible cohorts defined in above contexts.
|
|
|
|
$this->setAdminUser();
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 0, '');
|
|
|
|
$this->assertEquals(array($cohort1->id, $cohort2->id, $cohort4->id, $cohort5->id), array_keys($result));
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 2, '');
|
|
|
|
$this->assertEquals(array($cohort1->id, $cohort2->id), array_keys($result));
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 1, 2, '');
|
|
|
|
$this->assertEquals(array($cohort2->id, $cohort4->id), array_keys($result));
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 100, 'yyy');
|
|
|
|
$this->assertEquals(array($cohort1->id, $cohort2->id), array_keys($result));
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course2ctx, COHORT_ALL, 0, 0, '');
|
|
|
|
$this->assertEquals(array($cohort3->id, $cohort4->id, $cohort5->id), array_keys($result));
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY);
|
|
|
|
$this->assertEmpty($result);
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course2ctx, COHORT_WITH_MEMBERS_ONLY);
|
|
|
|
$this->assertEmpty($result);
|
|
|
|
|
|
|
|
// Get list of available cohorts as a teacher in the course.
|
|
|
|
$user1 = $this->getDataGenerator()->create_user();
|
|
|
|
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
|
|
|
|
role_assign($teacherrole->id, $user1->id, $course1ctx->id);
|
|
|
|
role_assign($teacherrole->id, $user1->id, $course2ctx->id);
|
|
|
|
$this->setUser($user1);
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 0, '');
|
|
|
|
$this->assertEquals(array($cohort1->id, $cohort4->id), array_keys($result));
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 1, '');
|
|
|
|
$this->assertEquals(array($cohort1->id), array_keys($result));
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 1, 1, '');
|
|
|
|
$this->assertEquals(array($cohort4->id), array_keys($result));
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 100, 'yyy');
|
|
|
|
$this->assertEquals(array($cohort1->id), array_keys($result));
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course2ctx, COHORT_ALL, 0, 0, '');
|
|
|
|
$this->assertEquals(array($cohort3->id, $cohort4->id), array_keys($result));
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY);
|
|
|
|
$this->assertEmpty($result);
|
|
|
|
|
|
|
|
// Now add members to cohorts.
|
|
|
|
$user2 = $this->getDataGenerator()->create_user();
|
|
|
|
$user3 = $this->getDataGenerator()->create_user();
|
|
|
|
$user4 = $this->getDataGenerator()->create_user();
|
|
|
|
$user5 = $this->getDataGenerator()->create_user();
|
|
|
|
$user6 = $this->getDataGenerator()->create_user();
|
|
|
|
cohort_add_member($cohort2->id, $user3->id);
|
|
|
|
cohort_add_member($cohort3->id, $user2->id);
|
|
|
|
cohort_add_member($cohort3->id, $user3->id);
|
|
|
|
cohort_add_member($cohort4->id, $user4->id);
|
|
|
|
cohort_add_member($cohort4->id, $user5->id);
|
|
|
|
cohort_add_member($cohort4->id, $user6->id);
|
|
|
|
|
|
|
|
// Check filtering non-empty cohorts as admin.
|
|
|
|
$this->setAdminUser();
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, '');
|
|
|
|
$this->assertEquals(array($cohort2->id, $cohort4->id), array_keys($result));
|
|
|
|
$this->assertEquals(1, $result[$cohort2->id]->memberscnt);
|
|
|
|
$this->assertEquals(3, $result[$cohort4->id]->memberscnt);
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course2ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, '');
|
|
|
|
$this->assertEquals(array($cohort3->id, $cohort4->id), array_keys($result));
|
|
|
|
$this->assertEquals(2, $result[$cohort3->id]->memberscnt);
|
|
|
|
$this->assertEquals(3, $result[$cohort4->id]->memberscnt);
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, 'yyy');
|
|
|
|
$this->assertEquals(array($cohort2->id), array_keys($result));
|
|
|
|
$this->assertEquals(1, $result[$cohort2->id]->memberscnt);
|
|
|
|
|
|
|
|
// Check filtering non-empty cohorts as teacher.
|
|
|
|
$this->setUser($user1);
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, '');
|
|
|
|
$this->assertEquals(array($cohort4->id), array_keys($result));
|
|
|
|
$this->assertEquals(3, $result[$cohort4->id]->memberscnt);
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course2ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, '');
|
|
|
|
$this->assertEquals(array($cohort3->id, $cohort4->id), array_keys($result));
|
|
|
|
$this->assertEquals(2, $result[$cohort3->id]->memberscnt);
|
|
|
|
$this->assertEquals(3, $result[$cohort4->id]->memberscnt);
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_MEMBERS_ONLY, 0, 0, 'yyy');
|
|
|
|
$this->assertEmpty($result);
|
|
|
|
|
|
|
|
// Enrol users.
|
|
|
|
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
|
$this->getDataGenerator()->enrol_user($user2->id, $course1->id, $studentrole->id);
|
|
|
|
$this->getDataGenerator()->enrol_user($user3->id, $course1->id, $studentrole->id);
|
|
|
|
$this->getDataGenerator()->enrol_user($user5->id, $course1->id, $studentrole->id);
|
|
|
|
$this->getDataGenerator()->enrol_user($user6->id, $course1->id, $studentrole->id);
|
|
|
|
$this->getDataGenerator()->enrol_user($user3->id, $course2->id, $studentrole->id);
|
|
|
|
$this->getDataGenerator()->enrol_user($user4->id, $course2->id, $studentrole->id);
|
|
|
|
$this->getDataGenerator()->enrol_user($user5->id, $course2->id, $studentrole->id);
|
|
|
|
$this->getDataGenerator()->enrol_user($user6->id, $course2->id, $studentrole->id);
|
|
|
|
|
|
|
|
// Check cohorts with enrolments as admin.
|
|
|
|
$this->setAdminUser();
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_ENROLLED_MEMBERS_ONLY, 0, 0, '');
|
|
|
|
$this->assertEquals(array($cohort2->id, $cohort4->id), array_keys($result));
|
|
|
|
$this->assertEquals(1, $result[$cohort2->id]->enrolledcnt);
|
|
|
|
$this->assertEquals(2, $result[$cohort4->id]->enrolledcnt);
|
|
|
|
$this->assertEquals(1, $result[$cohort2->id]->memberscnt);
|
|
|
|
$this->assertEquals(3, $result[$cohort4->id]->memberscnt);
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course2ctx, COHORT_WITH_ENROLLED_MEMBERS_ONLY, 0, 0, '');
|
|
|
|
$this->assertEquals(array($cohort3->id, $cohort4->id), array_keys($result));
|
|
|
|
$this->assertEquals(1, $result[$cohort3->id]->enrolledcnt);
|
|
|
|
$this->assertEquals(3, $result[$cohort4->id]->enrolledcnt);
|
|
|
|
$this->assertEquals(2, $result[$cohort3->id]->memberscnt);
|
|
|
|
$this->assertEquals(3, $result[$cohort4->id]->memberscnt);
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_ENROLLED_MEMBERS_ONLY, 0, 0, 'yyy');
|
|
|
|
$this->assertEquals(array($cohort2->id), array_keys($result));
|
|
|
|
$this->assertEquals(1, $result[$cohort2->id]->enrolledcnt);
|
|
|
|
$this->assertEquals(1, $result[$cohort2->id]->memberscnt);
|
|
|
|
|
|
|
|
$result = cohort_get_available_cohorts($course1ctx, COHORT_WITH_NOTENROLLED_MEMBERS_ONLY, 0, 0, '');
|
|
|
|
$this->assertEquals(array($cohort4->id), array_keys($result));
|
|
|
|
$this->assertEquals(2, $result[$cohort4->id]->enrolledcnt);
|
|
|
|
$this->assertEquals(3, $result[$cohort4->id]->memberscnt);
|
|
|
|
|
|
|
|
// Assign user1 additional 'manager' role in the category context. He can now see hidden cohort in category1
|
|
|
|
// but still can not see hidden category in system.
|
|
|
|
$managerrole = $DB->get_record('role', array('shortname' => 'manager'));
|
2022-02-05 10:30:50 +01:00
|
|
|
role_assign($managerrole->id, $user1->id, \context_coursecat::instance($category1->id));
|
2014-07-29 17:44:25 +08:00
|
|
|
$this->setUser($user1);
|
|
|
|
$result = cohort_get_available_cohorts($course1ctx, COHORT_ALL, 0, 0, '');
|
|
|
|
$this->assertEquals(array($cohort1->id, $cohort2->id, $cohort4->id), array_keys($result));
|
|
|
|
}
|
2018-01-23 14:54:18 +01:00
|
|
|
|
2023-03-07 13:28:02 +11:00
|
|
|
/**
|
|
|
|
* Test that all get functions return custom fields data.
|
|
|
|
*
|
|
|
|
* @covers \cohort_get_cohort, \cohort_get_cohorts, \cohort_get_all_cohorts
|
|
|
|
* @covers \cohort_get_available_cohorts, \cohort_get_user_cohorts
|
|
|
|
*/
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_get_functions_return_custom_fields(): void {
|
2023-03-07 13:28:02 +11:00
|
|
|
$this->resetAfterTest();
|
|
|
|
$this->setAdminUser();
|
|
|
|
|
|
|
|
$user = self::getDataGenerator()->create_user();
|
|
|
|
$course = self::getDataGenerator()->create_course();
|
|
|
|
$coursectx = \context_course::instance(($course->id));
|
|
|
|
|
|
|
|
$this->create_cohort_custom_field();
|
|
|
|
|
|
|
|
$cohort1 = $this->getDataGenerator()->create_cohort(['customfield_testfield1' => 'Test value 1']);
|
|
|
|
$cohort2 = $this->getDataGenerator()->create_cohort();
|
|
|
|
|
|
|
|
// Test cohort_get_cohort.
|
2023-06-20 12:28:39 +08:00
|
|
|
$result = cohort_get_cohort($cohort1->id, $coursectx, true);
|
2024-03-19 11:11:59 +01:00
|
|
|
$this->assertObjectHasProperty('customfields', $result);
|
2023-03-07 13:28:02 +11:00
|
|
|
$this->assertCount(1, $result->customfields);
|
|
|
|
$field = reset($result->customfields);
|
|
|
|
$this->assertInstanceOf(data_controller::class, $field);
|
|
|
|
$this->assertEquals('testfield1', $field->get_field()->get('shortname'));
|
|
|
|
$this->assertEquals('Test value 1', $field->get_value());
|
|
|
|
|
|
|
|
// Test custom fields are not returned if not needed.
|
2023-06-20 12:28:39 +08:00
|
|
|
$result = cohort_get_cohort($cohort1->id, $coursectx);
|
2024-03-19 11:11:59 +01:00
|
|
|
$this->assertObjectNotHasProperty('customfields', $result);
|
2023-03-07 13:28:02 +11:00
|
|
|
|
|
|
|
// Test cohort_get_cohorts.
|
|
|
|
$result = cohort_get_cohorts(\context_system::instance()->id, 0, 25, '', true);
|
|
|
|
$this->assertEquals(2, $result['totalcohorts']);
|
|
|
|
$this->assertEquals(2, $result['allcohorts']);
|
|
|
|
foreach ($result['cohorts'] as $cohort) {
|
2024-03-19 11:11:59 +01:00
|
|
|
$this->assertObjectHasProperty('customfields', $cohort);
|
2023-03-07 13:28:02 +11:00
|
|
|
$this->assertCount(1, $cohort->customfields);
|
|
|
|
$field = reset($cohort->customfields);
|
|
|
|
$this->assertInstanceOf(data_controller::class, $field);
|
|
|
|
$this->assertEquals('testfield1', $field->get_field()->get('shortname'));
|
|
|
|
|
|
|
|
if ($cohort->id == $cohort1->id ) {
|
|
|
|
$this->assertEquals('Test value 1', $field->get_value());
|
|
|
|
} else {
|
|
|
|
$this->assertEquals('', $field->get_value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test custom fields are not returned if not needed.
|
|
|
|
$result = cohort_get_cohorts(\context_system::instance()->id, 0, 25, '');
|
|
|
|
$this->assertEquals(2, $result['totalcohorts']);
|
|
|
|
$this->assertEquals(2, $result['allcohorts']);
|
|
|
|
foreach ($result['cohorts'] as $cohort) {
|
2024-03-19 11:11:59 +01:00
|
|
|
$this->assertObjectNotHasProperty('customfields', $cohort);
|
2023-03-07 13:28:02 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test test_cohort_get_all_cohorts.
|
|
|
|
$result = cohort_get_all_cohorts(0, 100, '', true);
|
|
|
|
$this->assertEquals(2, $result['totalcohorts']);
|
|
|
|
$this->assertEquals(2, $result['allcohorts']);
|
|
|
|
foreach ($result['cohorts'] as $cohort) {
|
2024-03-19 11:11:59 +01:00
|
|
|
$this->assertObjectHasProperty('customfields', $cohort);
|
2023-03-07 13:28:02 +11:00
|
|
|
$this->assertCount(1, $cohort->customfields);
|
|
|
|
$field = reset($cohort->customfields);
|
|
|
|
$this->assertInstanceOf(data_controller::class, $field);
|
|
|
|
$this->assertEquals('testfield1', $field->get_field()->get('shortname'));
|
|
|
|
|
|
|
|
if ($cohort->id == $cohort1->id ) {
|
|
|
|
$this->assertEquals('Test value 1', $field->get_value());
|
|
|
|
} else {
|
|
|
|
$this->assertEquals('', $field->get_value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test custom fields are not returned if not needed.
|
|
|
|
$result = cohort_get_all_cohorts(0, 100, '');
|
|
|
|
$this->assertEquals(2, $result['totalcohorts']);
|
|
|
|
$this->assertEquals(2, $result['allcohorts']);
|
|
|
|
foreach ($result['cohorts'] as $cohort) {
|
2024-03-19 11:11:59 +01:00
|
|
|
$this->assertObjectNotHasProperty('customfields', $cohort);
|
2023-03-07 13:28:02 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test cohort_get_available_cohorts.
|
|
|
|
$result = cohort_get_available_cohorts($coursectx, COHORT_ALL, 0, 25, '', true);
|
|
|
|
$this->assertCount(2, $result);
|
|
|
|
foreach ($result as $cohort) {
|
2024-03-19 11:11:59 +01:00
|
|
|
$this->assertObjectHasProperty('customfields', $cohort);
|
2023-03-07 13:28:02 +11:00
|
|
|
$this->assertCount(1, $cohort->customfields);
|
|
|
|
$field = reset($cohort->customfields);
|
|
|
|
$this->assertInstanceOf(data_controller::class, $field);
|
|
|
|
$this->assertEquals('testfield1', $field->get_field()->get('shortname'));
|
|
|
|
|
|
|
|
if ($cohort->id == $cohort1->id ) {
|
|
|
|
$this->assertEquals('Test value 1', $field->get_value());
|
|
|
|
} else {
|
|
|
|
$this->assertEquals('', $field->get_value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test custom fields are not returned if not needed.
|
|
|
|
$result = cohort_get_available_cohorts($coursectx, COHORT_ALL, 0, 25, '');
|
|
|
|
$this->assertCount(2, $result);
|
|
|
|
foreach ($result as $cohort) {
|
2024-03-19 11:11:59 +01:00
|
|
|
$this->assertObjectNotHasProperty('customfields', $cohort);
|
2023-03-07 13:28:02 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test cohort_get_user_cohorts.
|
|
|
|
cohort_add_member($cohort1->id, $user->id);
|
|
|
|
cohort_add_member($cohort2->id, $user->id);
|
|
|
|
|
|
|
|
$result = cohort_get_user_cohorts($user->id, true);
|
|
|
|
$this->assertCount(2, $result);
|
|
|
|
foreach ($result as $cohort) {
|
2024-03-19 11:11:59 +01:00
|
|
|
$this->assertObjectHasProperty('customfields', $cohort);
|
2023-03-07 13:28:02 +11:00
|
|
|
$this->assertCount(1, $cohort->customfields);
|
|
|
|
$field = reset($cohort->customfields);
|
|
|
|
$this->assertInstanceOf(data_controller::class, $field);
|
|
|
|
$this->assertEquals('testfield1', $field->get_field()->get('shortname'));
|
|
|
|
|
|
|
|
if ($cohort->id == $cohort1->id ) {
|
|
|
|
$this->assertEquals('Test value 1', $field->get_value());
|
|
|
|
} else {
|
|
|
|
$this->assertEquals('', $field->get_value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that there is no custom fields returned if not required.
|
|
|
|
$result = cohort_get_user_cohorts($user->id);
|
|
|
|
$this->assertCount(2, $result);
|
|
|
|
foreach ($result as $cohort) {
|
2024-03-19 11:11:59 +01:00
|
|
|
$this->assertObjectNotHasProperty('customfields', $cohort);
|
2023-03-07 13:28:02 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 14:54:18 +01:00
|
|
|
/**
|
|
|
|
* Create a cohort with allowcohortthemes enabled/disabled.
|
|
|
|
*/
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_cohort_add_theme_cohort(): void {
|
2018-01-23 14:54:18 +01:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->resetAfterTest();
|
|
|
|
|
|
|
|
// Theme is added when allowcohortthemes is enabled.
|
|
|
|
set_config('allowcohortthemes', 1);
|
|
|
|
set_config('theme', 'boost');
|
|
|
|
|
2022-02-05 10:30:50 +01:00
|
|
|
$systemctx = \context_system::instance();
|
2018-01-23 14:54:18 +01:00
|
|
|
$cohort1 = $this->getDataGenerator()->create_cohort(array('contextid' => $systemctx->id, 'name' => 'test cohort 1',
|
2019-02-28 13:42:23 +08:00
|
|
|
'idnumber' => 'testid1', 'description' => 'test cohort desc', 'descriptionformat' => FORMAT_HTML, 'theme' => 'classic'));
|
2018-01-23 14:54:18 +01:00
|
|
|
|
|
|
|
$id = cohort_add_cohort($cohort1);
|
|
|
|
$this->assertNotEmpty($id);
|
|
|
|
$newcohort = $DB->get_record('cohort', array('id' => $id));
|
|
|
|
$this->assertEquals($cohort1->contextid, $newcohort->contextid);
|
|
|
|
$this->assertSame($cohort1->name, $newcohort->name);
|
|
|
|
$this->assertSame($cohort1->description, $newcohort->description);
|
|
|
|
$this->assertEquals($cohort1->descriptionformat, $newcohort->descriptionformat);
|
|
|
|
$this->assertNotEmpty($newcohort->theme);
|
|
|
|
$this->assertSame($cohort1->theme, $newcohort->theme);
|
|
|
|
$this->assertNotEmpty($newcohort->timecreated);
|
|
|
|
$this->assertSame($newcohort->component, '');
|
|
|
|
$this->assertSame($newcohort->timecreated, $newcohort->timemodified);
|
|
|
|
|
|
|
|
// Theme is not added when allowcohortthemes is disabled.
|
|
|
|
set_config('allowcohortthemes', 0);
|
|
|
|
|
|
|
|
$cohort2 = $this->getDataGenerator()->create_cohort(array('contextid' => $systemctx->id, 'name' => 'test cohort 2',
|
2019-02-28 13:42:23 +08:00
|
|
|
'idnumber' => 'testid2', 'description' => 'test cohort desc', 'descriptionformat' => FORMAT_HTML, 'theme' => 'classic'));
|
2018-01-23 14:54:18 +01:00
|
|
|
|
|
|
|
$id = cohort_add_cohort($cohort2);
|
|
|
|
$this->assertNotEmpty($id);
|
|
|
|
$newcohort = $DB->get_record('cohort', array('id' => $id));
|
|
|
|
$this->assertSame($cohort2->name, $newcohort->name);
|
|
|
|
$this->assertEmpty($newcohort->theme);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a cohort with allowcohortthemes enabled/disabled.
|
|
|
|
*/
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_cohort_update_theme_cohort(): void {
|
2018-01-23 14:54:18 +01:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$this->resetAfterTest();
|
|
|
|
|
|
|
|
// Enable cohort themes.
|
|
|
|
set_config('allowcohortthemes', 1);
|
|
|
|
set_config('theme', 'boost');
|
|
|
|
|
2022-02-05 10:30:50 +01:00
|
|
|
$systemctx = \context_system::instance();
|
2018-01-23 14:54:18 +01:00
|
|
|
$cohort1 = $this->getDataGenerator()->create_cohort(array('contextid' => $systemctx->id, 'name' => 'test cohort 1',
|
2019-02-28 13:42:23 +08:00
|
|
|
'idnumber' => 'testid1', 'description' => 'test cohort desc', 'descriptionformat' => FORMAT_HTML, 'theme' => 'classic'));
|
2018-01-23 14:54:18 +01:00
|
|
|
$id = cohort_add_cohort($cohort1);
|
|
|
|
$this->assertNotEmpty($id);
|
|
|
|
|
|
|
|
// Theme is updated when allowcohortthemes is enabled.
|
|
|
|
$cohort1 = $DB->get_record('cohort', array('id' => $id));
|
|
|
|
$cohort1->name = 'test cohort 1 updated';
|
2019-02-28 13:42:23 +08:00
|
|
|
$cohort1->theme = 'classic';
|
2018-01-23 14:54:18 +01:00
|
|
|
cohort_update_cohort($cohort1);
|
|
|
|
$updatedcohort = $DB->get_record('cohort', array('id' => $id));
|
|
|
|
$this->assertEquals($cohort1->contextid, $updatedcohort->contextid);
|
|
|
|
$this->assertSame($cohort1->name, $updatedcohort->name);
|
|
|
|
$this->assertSame($cohort1->description, $updatedcohort->description);
|
|
|
|
$this->assertNotEmpty($updatedcohort->theme);
|
|
|
|
$this->assertSame($cohort1->theme, $updatedcohort->theme);
|
|
|
|
|
|
|
|
// Theme is not updated neither overwritten when allowcohortthemes is disabled.
|
|
|
|
set_config('allowcohortthemes', 0);
|
|
|
|
$cohort2 = $DB->get_record('cohort', array('id' => $id));
|
2019-02-28 13:42:23 +08:00
|
|
|
$cohort2->theme = 'classic';
|
2018-01-23 14:54:18 +01:00
|
|
|
cohort_update_cohort($cohort2);
|
|
|
|
$updatedcohort = $DB->get_record('cohort', array('id' => $id));
|
|
|
|
$this->assertEquals($cohort2->contextid, $updatedcohort->contextid);
|
|
|
|
$this->assertNotEmpty($updatedcohort->theme);
|
|
|
|
$this->assertSame($cohort1->theme, $updatedcohort->theme);
|
|
|
|
}
|
2023-03-07 13:28:02 +11:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that lib function returns custom field data for a cohorts.
|
|
|
|
*
|
|
|
|
* @covers \cohort_get_custom_fields_data
|
|
|
|
*/
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_cohort_get_custom_fields_data(): void {
|
2023-03-07 13:28:02 +11:00
|
|
|
$this->resetAfterTest();
|
|
|
|
$this->setAdminUser();
|
|
|
|
|
|
|
|
$this->create_cohort_custom_field();
|
|
|
|
|
|
|
|
$cohort1 = $this->getDataGenerator()->create_cohort(['customfield_testfield1' => 'Test value 1']);
|
|
|
|
$cohort2 = $this->getDataGenerator()->create_cohort();
|
|
|
|
|
|
|
|
$result = cohort_get_custom_fields_data([$cohort1->id, $cohort2->id, 777]);
|
|
|
|
$this->assertArrayHasKey($cohort1->id, $result);
|
|
|
|
$this->assertArrayHasKey($cohort2->id, $result);
|
|
|
|
$this->assertArrayHasKey(777, $result);
|
|
|
|
|
|
|
|
foreach ($result as $cohortid => $fieldcontrollers) {
|
|
|
|
foreach ($fieldcontrollers as $fieldcontroller) {
|
|
|
|
$this->assertInstanceOf(data_controller::class, $fieldcontroller);
|
|
|
|
if ($cohortid == $cohort1->id) {
|
|
|
|
$this->assertSame('Test value 1', $fieldcontroller->export_value());
|
|
|
|
} else {
|
|
|
|
$this->assertNull($fieldcontroller->export_value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-20 12:28:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the behaviour of cohort_get_cohort().
|
|
|
|
*
|
|
|
|
* @covers ::cohort_get_cohort
|
|
|
|
*/
|
2024-05-30 09:37:38 +02:00
|
|
|
public function test_cohort_get_cohort(): void {
|
2023-06-20 12:28:39 +08:00
|
|
|
$this->resetAfterTest();
|
|
|
|
|
|
|
|
$cat = $this->getDataGenerator()->create_category();
|
|
|
|
$cat1 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);
|
|
|
|
$cat2 = $this->getDataGenerator()->create_category(['parent' => $cat->id]);
|
|
|
|
|
|
|
|
$course1 = $this->getDataGenerator()->create_course(['category' => $cat1->id, 'shortname' => 'ANON1']);
|
|
|
|
$course2 = $this->getDataGenerator()->create_course(['category' => $cat2->id, 'shortname' => 'ANON2']);
|
|
|
|
|
|
|
|
$cohort1 = $this->getDataGenerator()->create_cohort(['contextid' => \context_coursecat::instance($cat1->id)->id]);
|
|
|
|
|
|
|
|
$result = cohort_get_cohort($cohort1->id, \context_course::instance($course2->id));
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = cohort_get_cohort($cohort1->id, \context_course::instance($course2->id), true);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$result = cohort_get_cohort($cohort1->id, \context_course::instance($course1->id));
|
|
|
|
$this->assertEquals($cohort1->id, $result->id);
|
|
|
|
|
|
|
|
$result = cohort_get_cohort($cohort1->id, \context_course::instance($course1->id), true);
|
|
|
|
$this->assertEquals($cohort1->id, $result->id);
|
|
|
|
}
|
|
|
|
|
2012-09-16 20:17:16 +02:00
|
|
|
}
|