first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,208 @@
<?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/>.
namespace core_courseformat\output;
use stdClass;
/**
* Tests for activitybadge class.
*
* @package core_courseformat
* @copyright 2023 Sara Arjona <sara@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core_courseformat\output\activitybadge
*/
class activitybadge_test extends \advanced_testcase {
/**
* Test the behaviour of create_instance() and export_for_template() attributes.
* @runInSeparateProcess
*
* @covers ::export_for_template
* @covers ::create_instance
*/
public function test_activitybadge_export_for_template(): void {
$this->resetAfterTest();
$this->setAdminUser();
$data = $this->setup_scenario();
$user = $this->getDataGenerator()->create_user(['trackforums' => 1]);
$this->getDataGenerator()->enrol_user(
$user->id,
$data->course->id,
'student'
);
$this->setUser($user);
$renderer = $data->renderer;
// The activitybadge for a file with all options enabled shouldn't be empty.
$class = activitybadge::create_instance($data->fileshowtype);
$result = $class->export_for_template($renderer);
$this->check_activitybadge($result, 'TXT', 'badge-none');
// The activitybadge for a file with Show type option disabled should be empty.
$class = activitybadge::create_instance($data->filehidetype);
$result = $class->export_for_template($renderer);
$this->check_activitybadge($result);
// The activitybadge for a forum with unread messages shouldn't be empty.
$class = activitybadge::create_instance($data->forumunread);
$result = $class->export_for_template($renderer);
$this->check_activitybadge($result, '1 unread post', 'bg-dark text-white');
// The activitybadge for a forum without unread messages should be empty.
$class = activitybadge::create_instance($data->forumread);
$result = $class->export_for_template($renderer);
$this->check_activitybadge($result);
// The activitybadge for an assignment should be empty.
$class = activitybadge::create_instance($data->assign);
$this->assertNull($class);
// The activitybadge for a label should be empty.
$class = activitybadge::create_instance($data->label);
$this->assertNull($class);
}
/**
* Setup the default scenario, creating some activities:
* - A forum with one unread message from the teacher.
* - Another forum without unread messages.
* - A file with all the appearance options enabled.
* - A file with the "Show type" option disabled.
* - An assignment.
* - A label.
*
* @return stdClass the scenario instances.
*/
private function setup_scenario(): stdClass {
global $PAGE;
$course = $this->getDataGenerator()->create_course(['numsections' => 1]);
// Enrol editing teacher to the course.
$teacher = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user(
$teacher->id,
$course->id,
'editingteacher'
);
$this->setUser($teacher);
// Create a forum with tracking forced and add a discussion.
$record = new stdClass();
$record->introformat = FORMAT_HTML;
$record->course = $course->id;
$record->trackingtype = FORUM_TRACKING_FORCED;
$forumread = $this->getDataGenerator()->create_module('forum', $record);
$forumunread = $this->getDataGenerator()->create_module('forum', $record);
$record = new stdClass();
$record->course = $course->id;
$record->userid = $teacher->id;
$record->forum = $forumunread->id;
$discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
// Create a file with all the options enabled.
$record = (object)[
'course' => $course->id,
'showsize' => 1,
'showtype' => 1,
'showdate' => 1,
];
$fileshowtype = self::getDataGenerator()->create_module('resource', $record);
// Create a file with Show type disabled.
$record = (object)[
'course' => $course->id,
'showsize' => 1,
'showtype' => 0,
'showdate' => 1,
];
$filehidetype = self::getDataGenerator()->create_module('resource', $record);
// Create an assignment and a label.
$assign = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]);
$label = $this->getDataGenerator()->create_module('label', ['course' => $course->id]);
rebuild_course_cache($course->id, true);
$renderer = course_get_format($course->id)->get_renderer($PAGE);
$modinfo = get_fast_modinfo($course->id);
return (object)[
'course' => $course,
'forumunread' => $modinfo->get_cm($forumunread->cmid),
'discussion' => $discussion,
'forumread' => $modinfo->get_cm($forumread->cmid),
'fileshowtype' => $modinfo->get_cm($fileshowtype->cmid),
'filehidetype' => $modinfo->get_cm($filehidetype->cmid),
'assign' => $modinfo->get_cm($assign->cmid),
'label' => $modinfo->get_cm($label->cmid),
'renderer' => $renderer,
];
}
/**
* Method to check if the result of the export_from_template is the expected.
*
* @param stdClass $result The result of the export_from_template() call.
* @param string|null $content The expected activitybadge content.
* @param string|null $style The expected activitybadge style.
* @param string|null $url The expected activitybadge url.
* @param string|null $elementid The expected activitybadge element id.
* @param array|null $extra The expected activitybadge extra attributes.
*/
private function check_activitybadge(
stdClass $result,
?string $content = null,
?string $style = null,
?string $url = null,
?string $elementid = null,
?array $extra = null
): void {
if (is_null($content)) {
$this->assertObjectNotHasProperty('badgecontent', $result);
} else {
$this->assertEquals($content, $result->badgecontent);
}
if (is_null($style)) {
$this->assertObjectNotHasProperty('badgestyle', $result);
} else {
$this->assertEquals($style, $result->badgestyle);
}
if (is_null($url)) {
$this->assertObjectNotHasProperty('badgeurl', $result);
} else {
$this->assertEquals($url, $result->badgeurl);
}
if (is_null($elementid)) {
$this->assertObjectNotHasProperty('badgeelementid', $result);
} else {
$this->assertEquals($elementid, $result->badgeelementid);
}
if (is_null($extra)) {
$this->assertObjectNotHasProperty('badgeextraattributes', $result);
} else {
$this->assertEquals($extra, $result->badgeextraattributes);
}
}
}
@@ -0,0 +1,356 @@
<?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/>.
namespace core_courseformat\output\local\state;
use availability_date\condition;
use core_availability\tree;
use stdClass;
/**
* Tests for cm state class.
*
* @package core_courseformat
* @copyright 2022 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core_courseformat\output\local\state\cm
*/
class cm_test extends \advanced_testcase {
/**
* Setup to ensure that fixtures are loaded.
*/
public static function setupBeforeClass(): void {
global $CFG;
require_once($CFG->dirroot . '/course/lib.php');
require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest.php');
require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest_output_course_format_state.php');
}
/**
* Test the behaviour of state\cm hasavailability attribute.
*
* @dataProvider hasrestrictions_state_provider
* @covers ::export_for_template
*
* @param string $format the course format
* @param string $rolename the user role name (editingteacher or student)
* @param bool $hasavailability if the activity|section has availability
* @param bool $available if the activity availability condition is available or not to the user
* @param bool $expected the expected result
*/
public function test_cm_hasrestrictions_state(
string $format = 'topics',
string $rolename = 'editingteacher',
bool $hasavailability = false,
bool $available = false,
bool $expected = false
): void {
$data = $this->setup_hasrestrictions_scenario($format, $rolename, $hasavailability, $available);
// Get the cm state.
$courseformat = $data->courseformat;
$renderer = $data->renderer;
$cmclass = $courseformat->get_output_classname('state\\cm');
$cmstate = new $cmclass(
$courseformat,
$data->section,
$data->cm
);
$state = $cmstate->export_for_template($renderer);
$this->assertEquals($expected, $state->hascmrestrictions);
}
/**
* Setup section or cm has restrictions scenario.
*
* @param string $format the course format
* @param string $rolename the user role name (editingteacher or student)
* @param bool $hasavailability if the activity|section has availability
* @param bool $available if the activity availability condition is available or not to the user
* @return stdClass the scenario instances.
*/
private function setup_hasrestrictions_scenario(
string $format = 'topics',
string $rolename = 'editingteacher',
bool $hasavailability = false,
bool $available = false
): stdClass {
global $PAGE, $DB;
$this->resetAfterTest();
set_config('enableavailability', 1);
$course = $this->getDataGenerator()->create_course(['numsections' => 1, 'format' => $format]);
// Create and enrol user.
$user = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user(
$user->id,
$course->id,
$rolename
);
$this->setUser($user);
// Create an activity.
$activity = $this->getDataGenerator()->create_module('page', ['course' => $course->id], [
'section' => 1,
'visible' => 1
]);
// Set up the availability settings.
if ($hasavailability) {
$operation = ($available) ? condition::DIRECTION_UNTIL : condition::DIRECTION_FROM;
$availabilityjson = json_encode(tree::get_root_json(
[
condition::get_json($operation, time() + 3600),
],
'&',
true
));
$selector = ['id' => $activity->cmid];
$DB->set_field('course_modules', 'availability', trim($availabilityjson), $selector);
}
// Get the cm state.
$courseformat = course_get_format($course->id);
$modinfo = $courseformat->get_modinfo();
$renderer = $courseformat->get_renderer($PAGE);
if ($format == 'theunittest') {
// These course format's hasn't the renderer file, so a debugging message will be displayed.
$this->assertDebuggingCalled();
}
return (object)[
'courseformat' => $courseformat,
'section' => $modinfo->get_section_info(1),
'cm' => $modinfo->get_cm($activity->cmid),
'renderer' => $renderer,
];
}
/**
* Data provider for test_state().
*
* @return array
*/
public function hasrestrictions_state_provider(): array {
return [
// Teacher scenarios (topics).
'Teacher, Topics, can edit, has availability and is available' => [
'format' => 'topics',
'rolename' => 'editingteacher',
'hasavailability' => true,
'available' => true,
'expected' => true,
],
'Teacher, Topics, can edit, has availability and is not available' => [
'format' => 'topics',
'rolename' => 'editingteacher',
'hasavailability' => true,
'available' => false,
'expected' => true,
],
'Teacher, Topics, can edit and has not availability' => [
'format' => 'topics',
'rolename' => 'editingteacher',
'hasavailability' => false,
'available' => true,
'expected' => false,
],
// Teacher scenarios (weeks).
'Teacher, Weeks, can edit, has availability and is available' => [
'format' => 'weeks',
'rolename' => 'editingteacher',
'hasavailability' => true,
'available' => true,
'expected' => true,
],
'Teacher, Weeks, can edit, has availability and is not available' => [
'format' => 'weeks',
'rolename' => 'editingteacher',
'hasavailability' => true,
'available' => false,
'expected' => true,
],
'Teacher, Weeks, can edit and has not availability' => [
'format' => 'weeks',
'rolename' => 'editingteacher',
'hasavailability' => false,
'available' => true,
'expected' => false,
],
// Teacher scenarios (mock format).
'Teacher, Mock format, can edit, has availability and is available' => [
'format' => 'theunittest',
'rolename' => 'editingteacher',
'hasavailability' => true,
'available' => true,
'expected' => true,
],
'Teacher, Mock format, can edit, has availability and is not available' => [
'format' => 'theunittest',
'rolename' => 'editingteacher',
'hasavailability' => true,
'available' => false,
'expected' => true,
],
'Teacher, Mock format, can edit and has not availability' => [
'format' => 'theunittest',
'rolename' => 'editingteacher',
'hasavailability' => false,
'available' => true,
'expected' => false,
],
// Non editing teacher scenarios (topics).
'Non editing teacher, Topics, can edit, has availability and is available' => [
'format' => 'topics',
'rolename' => 'teacher',
'hasavailability' => true,
'available' => true,
'expected' => true,
],
'Non editing teacher, Topics, can edit, has availability and is not available' => [
'format' => 'topics',
'rolename' => 'teacher',
'hasavailability' => true,
'available' => false,
'expected' => true,
],
'Non editing teacher, Topics, can edit and has not availability' => [
'format' => 'topics',
'rolename' => 'teacher',
'hasavailability' => false,
'available' => true,
'expected' => false,
],
// Non editing teacher scenarios (weeks).
'Non editing teacher, Weeks, can edit, has availability and is available' => [
'format' => 'weeks',
'rolename' => 'teacher',
'hasavailability' => true,
'available' => true,
'expected' => true,
],
'Non editing teacher, Weeks, can edit, has availability and is not available' => [
'format' => 'weeks',
'rolename' => 'teacher',
'hasavailability' => true,
'available' => false,
'expected' => true,
],
'Non editing teacher, Weeks, can edit and has not availability' => [
'format' => 'weeks',
'rolename' => 'teacher',
'hasavailability' => false,
'available' => true,
'expected' => false,
],
// Non editing teacher scenarios (mock format).
'Non editing teacher, Mock format, can edit, has availability and is available' => [
'format' => 'theunittest',
'rolename' => 'teacher',
'hasavailability' => true,
'available' => true,
'expected' => true,
],
'Non editing teacher, Mock format, can edit, has availability and is not available' => [
'format' => 'theunittest',
'rolename' => 'teacher',
'hasavailability' => true,
'available' => false,
'expected' => true,
],
'Non editing teacher, Mock format, can edit and has not availability' => [
'format' => 'theunittest',
'rolename' => 'teacher',
'hasavailability' => false,
'available' => true,
'expected' => false,
],
// Student scenarios (topics).
'Student, Topics, cannot edit, has availability and is available' => [
'format' => 'topics',
'rolename' => 'student',
'hasavailability' => true,
'available' => true,
'expected' => false,
],
'Student, Topics, cannot edit, has availability and is not available' => [
'format' => 'topics',
'rolename' => 'student',
'hasavailability' => true,
'available' => false,
'expected' => true,
],
'Student, Topics, cannot edit and has not availability' => [
'format' => 'topics',
'rolename' => 'student',
'hasavailability' => false,
'available' => true,
'expected' => false,
],
// Student scenarios (weeks).
'Student, Weeks, cannot edit, has availability and is available' => [
'format' => 'weeks',
'rolename' => 'student',
'hasavailability' => true,
'available' => true,
'expected' => false,
],
'Student, Weeks, cannot edit, has availability and is not available' => [
'format' => 'weeks',
'rolename' => 'student',
'hasavailability' => true,
'available' => false,
'expected' => true,
],
'Student, Weeks, cannot edit and has not availability' => [
'format' => 'weeks',
'rolename' => 'student',
'hasavailability' => false,
'available' => true,
'expected' => false,
],
// Student scenarios (mock format).
'Student, Mock format, cannot edit, has availability and is available' => [
'format' => 'theunittest',
'rolename' => 'student',
'hasavailability' => true,
'available' => true,
'expected' => false,
],
'Student, Mock format, cannot edit, has availability and is not available' => [
'format' => 'theunittest',
'rolename' => 'student',
'hasavailability' => true,
'available' => false,
'expected' => true,
],
'Student, Mock format, cannot edit and has not availability' => [
'format' => 'theunittest',
'rolename' => 'student',
'hasavailability' => false,
'available' => true,
'expected' => false,
],
];
}
}
@@ -0,0 +1,348 @@
<?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/>.
namespace core_courseformat\output\local\state;
use availability_date\condition;
use core_availability\tree;
use stdClass;
/**
* Tests for section state class.
*
* @package core_courseformat
* @copyright 2022 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core_courseformat\output\local\state\section
*/
class section_test extends \advanced_testcase {
/**
* Setup to ensure that fixtures are loaded.
*/
public static function setupBeforeClass(): void {
global $CFG;
require_once($CFG->dirroot . '/course/lib.php');
require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest.php');
require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest_output_course_format_state.php');
}
/**
* Test the behaviour of state\section hasavailability attribute.
*
* @dataProvider hasrestrictions_state_provider
* @covers ::export_for_template
*
* @param string $format the course format
* @param string $rolename the user role name (editingteacher or student)
* @param bool $hasavailability if the activity|section has availability
* @param bool $available if the activity availability condition is available or not to the user
* @param bool $expected the expected result
*/
public function test_section_hasrestrictions_state(
string $format = 'topics',
string $rolename = 'editingteacher',
bool $hasavailability = false,
bool $available = false,
bool $expected = false
): void {
$data = $this->setup_hasrestrictions_scenario($format, $rolename, $hasavailability, $available);
// Get the cm state.
$courseformat = $data->courseformat;
$renderer = $data->renderer;
$sectionclass = $courseformat->get_output_classname('state\\section');
$sectionstate = new $sectionclass(
$courseformat,
$data->section
);
$state = $sectionstate->export_for_template($renderer);
$this->assertEquals($expected, $state->hasrestrictions);
}
/**
* Setup section or cm has restrictions scenario.
*
* @param string $format the course format
* @param string $rolename the user role name (editingteacher or student)
* @param bool $hasavailability if the section has availability
* @param bool $available if the section availability condition is available or not to the user
* @return stdClass the scenario instances.
*/
private function setup_hasrestrictions_scenario(
string $format = 'topics',
string $rolename = 'editingteacher',
bool $hasavailability = false,
bool $available = false
): stdClass {
global $PAGE, $DB;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course(['numsections' => 1, 'format' => $format]);
// Create and enrol user.
$user = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user(
$user->id,
$course->id,
$rolename
);
$this->setUser($user);
// Set up the availability settings.
if ($hasavailability) {
$operation = ($available) ? condition::DIRECTION_UNTIL : condition::DIRECTION_FROM;
$availabilityjson = json_encode(tree::get_root_json(
[
condition::get_json($operation, time() + 3600),
],
'&',
true
));
$modinfo = get_fast_modinfo($course);
$sectioninfo = $modinfo->get_section_info(1);
$selector = ['id' => $sectioninfo->id];
$DB->set_field('course_sections', 'availability', trim($availabilityjson), $selector);
}
rebuild_course_cache($course->id, true);
$courseformat = course_get_format($course->id);
$modinfo = $courseformat->get_modinfo();
$renderer = $courseformat->get_renderer($PAGE);
if ($format == 'theunittest') {
// These course format's hasn't the renderer file, so a debugging message will be displayed.
$this->assertDebuggingCalled();
}
return (object)[
'courseformat' => $courseformat,
'section' => $modinfo->get_section_info(1),
'renderer' => $renderer,
];
}
/**
* Data provider for test_state().
*
* @return array
*/
public function hasrestrictions_state_provider(): array {
return [
// Teacher scenarios (topics).
'Teacher, Topics, can edit, has availability and is available' => [
'format' => 'topics',
'rolename' => 'editingteacher',
'hasavailability' => true,
'available' => true,
'expected' => true,
],
'Teacher, Topics, can edit, has availability and is not available' => [
'format' => 'topics',
'rolename' => 'editingteacher',
'hasavailability' => true,
'available' => false,
'expected' => true,
],
'Teacher, Topics, can edit and has not availability' => [
'format' => 'topics',
'rolename' => 'editingteacher',
'hasavailability' => false,
'available' => true,
'expected' => false,
],
// Teacher scenarios (weeks).
'Teacher, Weeks, can edit, has availability and is available' => [
'format' => 'weeks',
'rolename' => 'editingteacher',
'hasavailability' => true,
'available' => true,
'expected' => true,
],
'Teacher, Weeks, can edit, has availability and is not available' => [
'format' => 'weeks',
'rolename' => 'editingteacher',
'hasavailability' => true,
'available' => false,
'expected' => true,
],
'Teacher, Weeks, can edit and has not availability' => [
'format' => 'weeks',
'rolename' => 'editingteacher',
'hasavailability' => false,
'available' => true,
'expected' => false,
],
// Teacher scenarios (mock format).
'Teacher, Mock format, can edit, has availability and is available' => [
'format' => 'theunittest',
'rolename' => 'editingteacher',
'hasavailability' => true,
'available' => true,
'expected' => true,
],
'Teacher, Mock format, can edit, has availability and is not available' => [
'format' => 'theunittest',
'rolename' => 'editingteacher',
'hasavailability' => true,
'available' => false,
'expected' => true,
],
'Teacher, Mock format, can edit and has not availability' => [
'format' => 'theunittest',
'rolename' => 'editingteacher',
'hasavailability' => false,
'available' => true,
'expected' => false,
],
// Non editing teacher scenarios (topics).
'Non editing teacher, Topics, can edit, has availability and is available' => [
'format' => 'topics',
'rolename' => 'teacher',
'hasavailability' => true,
'available' => true,
'expected' => false,
],
'Non editing teacher, Topics, can edit, has availability and is not available' => [
'format' => 'topics',
'rolename' => 'teacher',
'hasavailability' => true,
'available' => false,
'expected' => false,
],
'Non editing teacher, Topics, can edit and has not availability' => [
'format' => 'topics',
'rolename' => 'teacher',
'hasavailability' => false,
'available' => true,
'expected' => false,
],
// Non editing teacher scenarios (weeks).
'Non editing teacher, Weeks, can edit, has availability and is available' => [
'format' => 'weeks',
'rolename' => 'teacher',
'hasavailability' => true,
'available' => true,
'expected' => false,
],
'Non editing teacher, Weeks, can edit, has availability and is not available' => [
'format' => 'weeks',
'rolename' => 'teacher',
'hasavailability' => true,
'available' => false,
'expected' => false,
],
'Non editing teacher, Weeks, can edit and has not availability' => [
'format' => 'weeks',
'rolename' => 'teacher',
'hasavailability' => false,
'available' => true,
'expected' => false,
],
// Non editing teacher scenarios (mock format).
'Non editing teacher, Mock format, can edit, has availability and is available' => [
'format' => 'theunittest',
'rolename' => 'teacher',
'hasavailability' => true,
'available' => true,
'expected' => false,
],
'Non editing teacher, Mock format, can edit, has availability and is not available' => [
'format' => 'theunittest',
'rolename' => 'teacher',
'hasavailability' => true,
'available' => false,
'expected' => false,
],
'Non editing teacher, Mock format, can edit and has not availability' => [
'format' => 'theunittest',
'rolename' => 'teacher',
'hasavailability' => false,
'available' => true,
'expected' => false,
],
// Student scenarios (topics).
'Topics, cannot edit, has availability and is available' => [
'format' => 'topics',
'rolename' => 'student',
'hasavailability' => true,
'available' => true,
'expected' => false,
],
'Topics, cannot edit, has availability and is not available' => [
'format' => 'topics',
'rolename' => 'student',
'hasavailability' => true,
'available' => false,
'expected' => true,
],
'Topics, cannot edit and has not availability' => [
'format' => 'topics',
'rolename' => 'student',
'hasavailability' => false,
'available' => true,
'expected' => false,
],
// Student scenarios (weeks).
'Weeks, cannot edit, has availability and is available' => [
'format' => 'weeks',
'rolename' => 'student',
'hasavailability' => true,
'available' => true,
'expected' => false,
],
'Weeks, cannot edit, has availability and is not available' => [
'format' => 'weeks',
'rolename' => 'student',
'hasavailability' => true,
'available' => false,
'expected' => true,
],
'Weeks, cannot edit and has not availability' => [
'format' => 'weeks',
'rolename' => 'student',
'hasavailability' => false,
'available' => true,
'expected' => false,
],
// Student scenarios (mock format).
'Mock format, cannot edit, has availability and is available' => [
'format' => 'theunittest',
'rolename' => 'student',
'hasavailability' => true,
'available' => true,
'expected' => false,
],
'Mock format, cannot edit, has availability and is not available' => [
'format' => 'theunittest',
'rolename' => 'student',
'hasavailability' => true,
'available' => false,
'expected' => true,
],
'Mock format, cannot edit and has not availability' => [
'format' => 'theunittest',
'rolename' => 'student',
'hasavailability' => false,
'available' => true,
'expected' => false,
],
];
}
}
@@ -0,0 +1,169 @@
<?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/>.
namespace core_courseformat\output\local\state;
/**
* Tests for state classes (course, section, cm).
*
* @package core
* @subpackage course
* @copyright 2021 Ilya Tregubov <ilya@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class state_test extends \advanced_testcase {
/**
* Setup to ensure that fixtures are loaded.
*/
public static function setupBeforeClass(): void {
global $CFG;
require_once($CFG->dirroot . '/course/lib.php');
require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest.php');
require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest_output_course_format_state.php');
}
/**
* Test the behaviour of state::export_for_template().
*
* @dataProvider state_provider
* @covers \core_courseformat\output\local\state
*
* @param string $format The course format of the course where the method will be executed.
*/
public function test_state(string $format = 'topics'): void {
global $PAGE;
$this->resetAfterTest();
// Create a course.
$numsections = 6;
$course = $this->getDataGenerator()->create_course(['numsections' => $numsections, 'format' => $format]);
$hiddensections = [4, 6];
foreach ($hiddensections as $section) {
set_section_visible($course->id, $section, 0);
}
// Create and enrol user.
$this->setAdminUser();
$courseformat = course_get_format($course->id);
$modinfo = $courseformat->get_modinfo();
$issocialformat = $courseformat->get_format() === 'social';
// Only create activities if the course format is not social.
// There's no course home page (and sections) for social course format.
if (!$issocialformat || $format == 'theunittest') {
// Add some activities to the course.
$this->getDataGenerator()->create_module('page', ['course' => $course->id], ['section' => 1,
'visible' => 1]);
$this->getDataGenerator()->create_module('forum', ['course' => $course->id], ['section' => 1,
'visible' => 1]);
$this->getDataGenerator()->create_module('assign', ['course' => $course->id], ['section' => 2,
'visible' => 0]);
$this->getDataGenerator()->create_module('glossary', ['course' => $course->id], ['section' => 4,
'visible' => 1]);
$this->getDataGenerator()->create_module('label', ['course' => $course->id], ['section' => 5,
'visible' => 0]);
$this->getDataGenerator()->create_module('feedback', ['course' => $course->id], ['section' => 5,
'visible' => 1]);
}
$courseclass = $courseformat->get_output_classname('state\\course');
$sectionclass = $courseformat->get_output_classname('state\\section');
$cmclass = $courseformat->get_output_classname('state\\cm');
// Get the proper renderer.
$renderer = $courseformat->get_renderer($PAGE);
$result = (object)[
'course' => (object)[],
'section' => [],
'cm' => [],
];
// General state.
$coursestate = new $courseclass($courseformat);
$result->course = $coursestate->export_for_template($renderer);
if ($format == 'theunittest') {
// These course format's hasn't the renderer file, so a debugging message will be displayed.
$this->assertDebuggingCalled();
}
$this->assertEquals($course->id, $result->course->id);
$this->assertEquals($numsections, $result->course->numsections);
$this->assertFalse($result->course->editmode);
$sections = $modinfo->get_section_info_all();
foreach ($sections as $key => $section) {
$this->assertEquals($section->id, $result->course->sectionlist[$key]);
if (!$issocialformat || $format == 'theunittest') {
if (!empty($section->uservisible)) {
$sectionstate = new $sectionclass($courseformat, $section);
$result->section[$key] = $sectionstate->export_for_template($renderer);
$this->assertEquals($section->id, $result->section[$key]->id);
$this->assertEquals($section->section, $result->section[$key]->section);
$this->assertTrue($section->visible == $result->section[$key]->visible);
if ($key === 0 || $key === 3 || $key === 6) {
$this->assertEmpty($result->section[$key]->cmlist);
} else if ($key === 1) {
$this->assertEquals(2, count($result->section[$key]->cmlist));
} else if ($key === 2 || $key === 4) {
$this->assertEquals(1, count($result->section[$key]->cmlist));
} else if ($key === 5) {
$this->assertEquals(2, count($result->section[$key]->cmlist));
}
}
} else {
// Social course format doesn't have sections.
$this->assertEmpty($result->section);
}
}
foreach ($modinfo->cms as $key => $cm) {
$section = $sections[$cm->sectionnum];
$cmstate = new $cmclass($courseformat, $section, $cm);
$result->cm[$key] = $cmstate->export_for_template($renderer);
$this->assertEquals($cm->id, $result->cm[$key]->id);
$this->assertEquals($cm->name, $result->cm[$key]->name);
$this->assertTrue($cm->visible == $result->cm[$key]->visible);
}
}
/**
* Data provider for test_state().
*
* @return array
*/
public function state_provider(): array {
return [
// COURSEFORMAT. Test behaviour depending on course formats.
'Single activity format' => [
'format' => 'singleactivity',
],
'Social format' => [
'format' => 'social',
],
'Weeks format' => [
'format' => 'weeks',
],
'The unit tests format' => [
'format' => 'theunittest',
],
];
}
}