diff --git a/tests/mod_learningmap_activitymanager_test.php b/tests/mod_learningmap_activitymanager_test.php index cf6fec5..fe03af5 100644 --- a/tests/mod_learningmap_activitymanager_test.php +++ b/tests/mod_learningmap_activitymanager_test.php @@ -27,69 +27,13 @@ * @group mebis * @covers \mod_learningmap\activitymanager */ -class mod_learningmap_activitymanager_test extends \advanced_testcase { +class mod_learningmap_activitymanager_test extends mod_learningmap_testcase { /** - * The course used for testing - * - * @var \stdClass + * Activitymanager instance for testing. + * + * @var activitymanager $activitymanager */ - protected $course; - /** - * The activities linked in the learningmap - * - * @var array - */ - protected $activities; - /** - * The user used for testing - * - * @var \stdClass - */ - protected $user1; - /** - * The modinfo of the course - * - * @var \course_modinfo|null - */ - protected $modinfo; - /** - * The completion info of the course - * - * @var \completion_info - */ - protected $completion; - /** - * Prepare testing environment - */ - public function setUp(): void { - global $DB; - $this->course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]); - - $this->activities = []; - for ($i = 0; $i < 9; $i++) { - $this->activities[] = $this->getDataGenerator()->create_module( - 'page', - [ - 'name' => 'A', - 'content' => 'B', - 'course' => $this->course, - 'completion' => COMPLETION_TRACKING_AUTOMATIC, - 'completionview' => COMPLETION_VIEW_REQUIRED - ] - ); - } - - $this->user1 = $this->getDataGenerator()->create_user( - [ - 'email' => 'user1@example.com', - 'username' => 'user1' - ] - ); - - $this->modinfo = get_fast_modinfo($this->course, $this->user1->id); - $this->completion = new \completion_info($this->modinfo->get_course()); - } - + protected $activitymanager; /** * Test completion checking * @return void @@ -97,7 +41,37 @@ public function setUp(): void { public function test_is_completed(): void { $this->resetAfterTest(); $this->setAdminUser(); - $this->setUser($this->user1); + $this->prepare(LEARNINGMAP_NOCOMPLETION, false, false); + $this->activitymanager = new activitymanager($this->course, $this->users[0]); + $this->completion->set_module_viewed($this->activities[0], $this->users[0]->id); + $this->assertEmpty($this->activitymanager->is_completed($this->activities[0]), true); + + $assign = $this->activities[7]; + $grades = []; + $grades[$this->users[0]] = (object)[ + 'rawgrade' => 1, 'userid' => $this->users[0] + ]; + $assign['cmidnumber'] = null; + assign_grade_item_update((object)$assign, $grades); + $this->assertEmpty($this->activitymanager->is_completed($this->activities[7]), false); + $this->completion->set_module_viewed($this->activities[7], $this->users[0]->id); + $this->assertEmpty($this->activitymanager->is_completed($this->activities[7]), true); + + $assign = $this->activities[8]; + $grades = []; + $grades[$this->users[0]] = (object)[ + 'rawgrade' => 1, 'userid' => $this->users[0] + ]; + $assign['cmidnumber'] = null; + assign_grade_item_update((object)$assign, $grades); + $this->assertEmpty($this->activitymanager->is_completed($this->activities[8]), false); + $this->completion->set_module_viewed($this->activities[7], $this->users[0]->id); + $this->assertEmpty($this->activitymanager->is_completed($this->activities[8]), false); + $grades[$this->users[0]] = (object)[ + 'rawgrade' => 10, 'userid' => $this->users[0] + ]; + assign_grade_item_update((object)$assign, $grades); + $this->assertEmpty($this->activitymanager->is_completed($this->activities[8]), true); } } diff --git a/tests/mod_learningmap_completion_test.php b/tests/mod_learningmap_completion_test.php index 7a8cb7b..a7d6be3 100644 --- a/tests/mod_learningmap_completion_test.php +++ b/tests/mod_learningmap_completion_test.php @@ -30,127 +30,7 @@ * @group mebis * @covers \mod_learningmap\completion\custom_completion */ -class mod_learningmap_completion_test extends \advanced_testcase { - /** - * The course used for testing - * - * @var \stdClass - */ - protected $course; - /** - * The learning map used for testing - * - * @var \stdClass - */ - protected $learningmap; - /** - * The activities linked in the learningmap - * - * @var array - */ - protected $activities; - /** - * The users used for testing - * - * @var array - */ - protected $users; - /** - * The group used for testing - * - * @var \stdClass - */ - protected $group; - /** - * Whether group mode is active - * - * @var boolean - */ - protected $groupmode; - /** - * The modinfo of the course - * - * @var \course_modinfo|null - */ - protected $modinfo; - /** - * The completion info of the course - * - * @var \completion_info - */ - protected $completion; - /** - * The cm_info object belonging to the learning map (differs from the learningmap record) - * - * @var \cm_info - */ - protected $cm; - /** - * Prepare testing environment - */ - /** - * Prepare testing environment - * @param int $completiontype Type for automatic completion - * @param bool $groupmode Whether to use group mode (defaults to false) - */ - public function prepare($completiontype, $groupmode = false): void { - global $DB; - $this->groupmode = $groupmode; - $this->course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]); - $this->learningmap = $this->getDataGenerator()->create_module('learningmap', - ['course' => $this->course, 'completion' => 2, 'completiontype' => $completiontype, - 'groupmode' => ($groupmode ? SEPARATEGROUPS : NOGROUPS), ] - ); - - $this->activities = []; - for ($i = 0; $i < 9; $i++) { - $this->activities[] = $this->getDataGenerator()->create_module( - 'page', - ['name' => 'A', 'content' => 'B', 'course' => $this->course, 'completion' => 2, 'completionview' => 1] - ); - $this->learningmap->placestore = str_replace(99990 + $i, $this->activities[$i]->cmid, $this->learningmap->placestore); - } - $DB->set_field('learningmap', 'placestore', $this->learningmap->placestore, ['id' => $this->learningmap->id]); - - $this->users[0] = $this->getDataGenerator()->create_user( - [ - 'email' => 'user1@example.com', - 'username' => 'user1', - ] - ); - $studentrole = $DB->get_record('role', ['shortname' => 'student']); - $this->getDataGenerator()->enrol_user($this->users[0]->id, $this->course->id, $studentrole->id); - if ($this->groupmode) { - $this->group = $this->getDataGenerator()->create_group(['courseid' => $this->course->id]); - $this->users[1] = $this->getDataGenerator()->create_user( - [ - 'email' => 'user2@example.com', - 'username' => 'user2', - ] - ); - $this->users[2] = $this->getDataGenerator()->create_user( - [ - 'email' => 'user3@example.com', - 'username' => 'user3', - ] - ); - $this->getDataGenerator()->enrol_user($this->users[1]->id, $this->course->id, $studentrole->id); - $this->getDataGenerator()->enrol_user($this->users[2]->id, $this->course->id, $studentrole->id); - $this->getDataGenerator()->create_group_member([ - 'userid' => $this->users[0]->id, - 'groupid' => $this->group->id, - ]); - $this->getDataGenerator()->create_group_member([ - 'userid' => $this->users[1]->id, - 'groupid' => $this->group->id, - ]); - } - - $this->modinfo = get_fast_modinfo($this->course, $this->users[0]->id); - $this->completion = new \completion_info($this->modinfo->get_course()); - $this->cm = $this->modinfo->get_cm($this->learningmap->cmid); - } - +class mod_learningmap_completion_test extends mod_learningmap_testcase { /** * Tests completiontype 1 in individual mode * diff --git a/tests/mod_learningmap_testcase.php b/tests/mod_learningmap_testcase.php new file mode 100644 index 0000000..de9027a --- /dev/null +++ b/tests/mod_learningmap_testcase.php @@ -0,0 +1,178 @@ +. + +namespace mod_learningmap; + +defined('MOODLE_INTERNAL') || die(); +require_once(__DIR__ . '/../lib.php'); + +/** + * Test class with common test methods for mod_learningmap + * + * @package mod_learningmap + * @copyright 2021-2022, ISB Bayern + * @author Stefan Hanauska + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @group mod_learningmap + * @group mebis + */ +class mod_learningmap_testcase extends \advanced_testcase { + /** + * The course used for testing + * + * @var \stdClass + */ + protected $course; + /** + * The learning map used for testing + * + * @var \stdClass + */ + protected $learningmap; + /** + * The activities linked in the learningmap + * + * @var array + */ + protected $activities; + /** + * The users used for testing + * + * @var array + */ + protected $users; + /** + * The group used for testing + * + * @var \stdClass + */ + protected $group; + /** + * Whether group mode is active + * + * @var boolean + */ + protected $groupmode; + /** + * The modinfo of the course + * + * @var \course_modinfo|null + */ + protected $modinfo; + /** + * The completion info of the course + * + * @var \completion_info + */ + protected $completion; + /** + * The cm_info object belonging to the learning map (differs from the learningmap record) + * + * @var \cm_info + */ + protected $cm; + /** + * Prepare testing environment + */ + /** + * Prepare testing environment + * @param int $completiontype Type for automatic completion + * @param bool $groupmode Whether to use group mode (defaults to false) + * @param bool $passinggrade Whether to use activities with passing grade + */ + public function prepare($completiontype, $groupmode = false, $passinggrade = false): void { + global $DB; + $this->groupmode = $groupmode; + $this->course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]); + $this->learningmap = $this->getDataGenerator()->create_module('learningmap', + ['course' => $this->course, 'completion' => 2, 'completiontype' => $completiontype, + 'groupmode' => ($groupmode ? SEPARATEGROUPS : NOGROUPS), ] + ); + + $this->activities = []; + for ($i = 0; $i < ($passinggrade ? 7 : 9); $i++) { + $this->activities[] = $this->getDataGenerator()->create_module( + 'page', + ['name' => 'A', 'content' => 'B', 'course' => $this->course, 'completion' => 2, 'completionview' => 1] + ); + $this->learningmap->placestore = str_replace(99990 + $i, $this->activities[$i]->cmid, $this->learningmap->placestore); + } + if ($passinggrade) { + $this->activities[] = $this->getDataGenerator()->create_module( + 'assign', + [ + 'name' => 'A', + 'course' => $this->course, + 'completion' => COMPLETION_TRACKING_AUTOMATIC, + 'completionusegrade' => false, + 'completionview' => COMPLETION_VIEW_REQUIRED, + 'gradepass' => 10, + ] + ); + $this->learningmap->placestore = str_replace(99997, $this->activities[7]->cmid, $this->learningmap->placestore); + $this->activities[] = $this->getDataGenerator()->create_module( + 'assign', + [ + 'name' => 'A', + 'course' => $this->course, + 'completion' => COMPLETION_TRACKING_AUTOMATIC, + 'completionusegrade' => true, + 'gradepass' => 10, + ] + ); + $this->learningmap->placestore = str_replace(99998, $this->activities[8]->cmid, $this->learningmap->placestore); + } + $DB->set_field('learningmap', 'placestore', $this->learningmap->placestore, ['id' => $this->learningmap->id]); + + $this->users[0] = $this->getDataGenerator()->create_user( + [ + 'email' => 'user1@example.com', + 'username' => 'user1', + ] + ); + $studentrole = $DB->get_record('role', ['shortname' => 'student']); + $this->getDataGenerator()->enrol_user($this->users[0]->id, $this->course->id, $studentrole->id); + if ($this->groupmode) { + $this->group = $this->getDataGenerator()->create_group(['courseid' => $this->course->id]); + $this->users[1] = $this->getDataGenerator()->create_user( + [ + 'email' => 'user2@example.com', + 'username' => 'user2', + ] + ); + $this->users[2] = $this->getDataGenerator()->create_user( + [ + 'email' => 'user3@example.com', + 'username' => 'user3', + ] + ); + $this->getDataGenerator()->enrol_user($this->users[1]->id, $this->course->id, $studentrole->id); + $this->getDataGenerator()->enrol_user($this->users[2]->id, $this->course->id, $studentrole->id); + $this->getDataGenerator()->create_group_member([ + 'userid' => $this->users[0]->id, + 'groupid' => $this->group->id, + ]); + $this->getDataGenerator()->create_group_member([ + 'userid' => $this->users[1]->id, + 'groupid' => $this->group->id, + ]); + } + + $this->modinfo = get_fast_modinfo($this->course, $this->users[0]->id); + $this->completion = new \completion_info($this->modinfo->get_course()); + $this->cm = $this->modinfo->get_cm($this->learningmap->cmid); + } +}