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,156 @@
@availability @availability_group
Feature: availability_group
In order to control student access to activities
As a teacher
I need to set group conditions which prevent student access
Background:
Given the following "courses" exist:
| fullname | shortname | format | enablecompletion | numsections |
| Course 1 | C1 | topics | 1 | 3 |
And the following "users" exist:
| username |
| teacher1 |
| student1 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following "activities" exist:
| activity | course | name |
| page | C1 | P1 |
| page | C1 | P2 |
| page | C1 | P3 |
@javascript
Scenario: Test condition
# Basic setup.
Given I am on the "P1" "page activity editing" page logged in as "teacher1"
And I expand all fieldsets
And I click on "Add restriction..." "button"
Then "Group" "button" should not exist in the "Add restriction..." "dialogue"
And I click on "Cancel" "button" in the "Add restriction..." "dialogue"
# Back to course page but add groups.
Given the following "groups" exist:
| name | course | idnumber |
| G1 | C1 | GI1 |
| G2 | C1 | GI2 |
# This step used to be 'And I follow "C1"', but Chrome thinks the breadcrumb
# is not clickable, so we'll go via the home page instead.
And I am on the "P1" "page activity editing" page
And I expand all fieldsets
And I click on "Add restriction..." "button"
Then "Group" "button" should exist in the "Add restriction..." "dialogue"
# Page P1 any group.
Given I click on "Group" "button" in the "Add restriction..." "dialogue"
And I set the field "Group" to "(Any group)"
And I click on ".availability-item .availability-eye img" "css_element"
And I click on "Save and return to course" "button"
# Page P2 with group G1.
And I am on the "P2" "page activity editing" page
And I expand all fieldsets
And I click on "Add restriction..." "button"
And I click on "Group" "button" in the "Add restriction..." "dialogue"
And I set the field "Group" to "G1"
And I click on ".availability-item .availability-eye img" "css_element"
And I click on "Save and return to course" "button"
# Page P3 with group G2
And I am on the "P3" "page activity editing" page
And I expand all fieldsets
And I click on "Add restriction..." "button"
And I click on "Group" "button" in the "Add restriction..." "dialogue"
And I set the field "Group" to "G2"
And I click on ".availability-item .availability-eye img" "css_element"
And I click on "Save and return to course" "button"
# Log back in as student.
When I am on the "Course 1" "course" page logged in as "student1"
# No pages should appear yet.
Then I should not see "P1" in the "region-main" "region"
And I should not see "P2" in the "region-main" "region"
And I should not see "P3" in the "region-main" "region"
# Add to groups and log out/in again.
Given the following "group members" exist:
| user | group |
| student1 | GI1 |
And I am on "Course 1" course homepage
# P1 (any groups) and P2 should show but not P3.
Then I should see "P1" in the "region-main" "region"
And I should see "P2" in the "region-main" "region"
And I should not see "P3" in the "region-main" "region"
@javascript
Scenario: Condition display with filters
# Teacher sets up a restriction on group G1, using multilang filter.
Given the following "groups" exist:
| name | course | idnumber |
| <span lang="en" class="multilang">G-One</span><span lang="fr" class="multilang">G-Un</span> | C1 | GI1 |
And the "multilang" filter is "on"
And the "multilang" filter applies to "content and headings"
# The activity names filter is enabled because it triggered a bug in older versions.
And the "activitynames" filter is "on"
And the "activitynames" filter applies to "content and headings"
And I am on the "P1" "page activity editing" page logged in as "teacher1"
And I expand all fieldsets
And I click on "Add restriction..." "button"
And I click on "Group" "button" in the "Add restriction..." "dialogue"
And I set the field "Group" to "G-One"
And I click on "Save and return to course" "button"
# Student sees information about no access to group, with group name in correct language.
When I am on the "C1" "Course" page logged in as "student1"
Then I should see "Not available unless: You belong to G-One"
And I should not see "G-Un"
@javascript
Scenario: Condition using a hidden group
Given the following "groups" exist:
| name | course | idnumber | visibility |
| Hidden Group | C1 | GA | 3 |
And I log in as "teacher1"
And I add a page activity to course "Course 1" section "1"
And I expand all fieldsets
# Page P1 any group.
And I am on the "P1" "page activity editing" page
And I expand all fieldsets
And I click on "Add restriction..." "button"
And "Group" "button" should exist in the "Add restriction..." "dialogue"
And I click on "Group" "button" in the "Add restriction..." "dialogue"
And I set the field "Group" to "(Any group)"
And I click on ".availability-item .availability-eye img" "css_element"
And I click on "Save and return to course" "button"
# Page P2 with hidden group.
And I am on the "P2" "page activity editing" page
And I expand all fieldsets
And I click on "Add restriction..." "button"
And I click on "Group" "button" in the "Add restriction..." "dialogue"
And I set the field "Group" to "Hidden Group"
And I click on "Save and return to course" "button"
# Log back in as student.
When I am on the "Course 1" "course" page logged in as "student1"
# No pages should appear yet.
Then I should not see "P1" in the "region-main" "region"
And I should not see "P2" in the "region-main" "region"
And I should not see "Hidden Group"
# Add to groups and log out/in again.
And the following "group members" exist:
| user | group |
| student1 | GA |
And I am on "Course 1" course homepage
# P1 (any groups) and P2 should show. The user should not see the hidden group mentioned anywhere.
And I should see "P1" in the "region-main" "region"
And I should see "P2" in the "region-main" "region"
And I should not see "Hidden Group"
@@ -0,0 +1,253 @@
<?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 availability_group;
/**
* Unit tests for the condition.
*
* @package availability_group
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class condition_test extends \advanced_testcase {
/**
* Load required classes.
*/
public function setUp(): void {
// Load the mock info class so that it can be used.
global $CFG;
require_once($CFG->dirroot . '/availability/tests/fixtures/mock_info.php');
}
/**
* Tests constructing and using condition.
*/
public function test_usage(): void {
global $CFG, $USER;
$this->resetAfterTest();
$CFG->enableavailability = true;
// Erase static cache before test.
condition::wipe_static_cache();
// Make a test course and user.
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$user = $generator->create_and_enrol($course);
$usertwo = $generator->create_and_enrol($course);
$info = new \core_availability\mock_info($course, $user->id);
// Make 2 test groups, one in a grouping and one not.
$grouping = $generator->create_grouping(array('courseid' => $course->id));
$group1 = $generator->create_group(array('courseid' => $course->id, 'name' => 'G1!'));
groups_assign_grouping($grouping->id, $group1->id);
$group2 = $generator->create_group(array('courseid' => $course->id, 'name' => 'G2!'));
// Do test (not in group).
$cond = new condition((object)array('id' => (int)$group1->id));
// Check if available (when not available).
$this->assertFalse($cond->is_available(false, $info, true, $user->id));
$information = $cond->get_description(false, false, $info);
$information = \core_availability\info::format_info($information, $course);
$this->assertMatchesRegularExpression('~You belong to.*G1!~', $information);
$this->assertTrue($cond->is_available(true, $info, true, $user->id));
// Add user to groups and refresh cache.
groups_add_member($group1, $user);
groups_add_member($group2, $user);
$info = new \core_availability\mock_info($course, $user->id);
// Recheck.
$this->assertTrue($cond->is_available(false, $info, true, $user->id));
$this->assertFalse($cond->is_available(true, $info, true, $user->id));
$this->assertFalse($cond->is_available(false, $info, true, $usertwo->id));
$this->assertTrue($cond->is_available(true, $info, true, $usertwo->id));
$information = $cond->get_description(false, true, $info);
$information = \core_availability\info::format_info($information, $course);
$this->assertMatchesRegularExpression('~do not belong to.*G1!~', $information);
// Check group 2 works also.
$cond = new condition((object)array('id' => (int)$group2->id));
$this->assertTrue($cond->is_available(false, $info, true, $user->id));
$this->assertFalse($cond->is_available(false, $info, true, $usertwo->id));
// What about an 'any group' condition?
$cond = new condition((object)array());
$this->assertTrue($cond->is_available(false, $info, true, $user->id));
$this->assertFalse($cond->is_available(true, $info, true, $user->id));
$this->assertFalse($cond->is_available(false, $info, true, $usertwo->id));
$this->assertTrue($cond->is_available(true, $info, true, $usertwo->id));
$information = $cond->get_description(false, true, $info);
$information = \core_availability\info::format_info($information, $course);
$this->assertMatchesRegularExpression('~do not belong to any~', $information);
// Admin user doesn't belong to a group, but they can access it
// either way (positive or NOT).
$this->setAdminUser();
$this->assertTrue($cond->is_available(false, $info, true, $USER->id));
$this->assertTrue($cond->is_available(true, $info, true, $USER->id));
// Group that doesn't exist uses 'missing' text.
$cond = new condition((object)array('id' => $group2->id + 1000));
$this->assertFalse($cond->is_available(false, $info, true, $user->id));
$information = $cond->get_description(false, false, $info);
$information = \core_availability\info::format_info($information, $course);
$this->assertMatchesRegularExpression('~You belong to.*\(Missing group\)~', $information);
}
/**
* Tests the constructor including error conditions. Also tests the
* string conversion feature (intended for debugging only).
*/
public function test_constructor(): void {
// Invalid id (not int).
$structure = (object)array('id' => 'bourne');
try {
$cond = new condition($structure);
$this->fail();
} catch (\coding_exception $e) {
$this->assertStringContainsString('Invalid ->id', $e->getMessage());
}
// Valid (with id).
$structure->id = 123;
$cond = new condition($structure);
$this->assertEquals('{group:#123}', (string)$cond);
// Valid (no id).
unset($structure->id);
$cond = new condition($structure);
$this->assertEquals('{group:any}', (string)$cond);
}
/**
* Tests the save() function.
*/
public function test_save(): void {
$structure = (object)array('id' => 123);
$cond = new condition($structure);
$structure->type = 'group';
$this->assertEquals($structure, $cond->save());
$structure = (object)array();
$cond = new condition($structure);
$structure->type = 'group';
$this->assertEquals($structure, $cond->save());
}
/**
* Tests the update_dependency_id() function.
*/
public function test_update_dependency_id(): void {
$cond = new condition((object)array('id' => 123));
$this->assertFalse($cond->update_dependency_id('frogs', 123, 456));
$this->assertFalse($cond->update_dependency_id('groups', 12, 34));
$this->assertTrue($cond->update_dependency_id('groups', 123, 456));
$after = $cond->save();
$this->assertEquals(456, $after->id);
}
/**
* Tests the filter_users (bulk checking) function. Also tests the SQL
* variant get_user_list_sql.
*/
public function test_filter_users(): void {
global $DB;
$this->resetAfterTest();
// Erase static cache before test.
condition::wipe_static_cache();
// Make a test course and some users.
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
$teacher = $generator->create_user();
$generator->enrol_user($teacher->id, $course->id, $roleids['editingteacher']);
$allusers = array($teacher->id => $teacher);
$students = array();
for ($i = 0; $i < 3; $i++) {
$student = $generator->create_user();
$students[$i] = $student;
$generator->enrol_user($student->id, $course->id, $roleids['student']);
$allusers[$student->id] = $student;
}
$info = new \core_availability\mock_info($course);
// Make test groups.
$group1 = $generator->create_group(array('courseid' => $course->id));
$group2 = $generator->create_group(array('courseid' => $course->id));
// Assign students to groups as follows (teacher is not in a group):
// 0: no groups.
// 1: in group 1.
// 2: in group 2.
groups_add_member($group1, $students[1]);
groups_add_member($group2, $students[2]);
// Test 'any group' condition.
$checker = new \core_availability\capability_checker($info->get_context());
$cond = new condition((object)array());
$result = array_keys($cond->filter_user_list($allusers, false, $info, $checker));
ksort($result);
$expected = array($teacher->id, $students[1]->id, $students[2]->id);
$this->assertEquals($expected, $result);
// Test it with get_user_list_sql.
list ($sql, $params) = $cond->get_user_list_sql(false, $info, true);
$result = $DB->get_fieldset_sql($sql, $params);
sort($result);
$this->assertEquals($expected, $result);
// Test NOT version (note that teacher can still access because AAG works
// both ways).
$result = array_keys($cond->filter_user_list($allusers, true, $info, $checker));
ksort($result);
$expected = array($teacher->id, $students[0]->id);
$this->assertEquals($expected, $result);
// Test with get_user_list_sql.
list ($sql, $params) = $cond->get_user_list_sql(true, $info, true);
$result = $DB->get_fieldset_sql($sql, $params);
sort($result);
$this->assertEquals($expected, $result);
// Test specific group.
$cond = new condition((object)array('id' => (int)$group1->id));
$result = array_keys($cond->filter_user_list($allusers, false, $info, $checker));
ksort($result);
$expected = array($teacher->id, $students[1]->id);
$this->assertEquals($expected, $result);
list ($sql, $params) = $cond->get_user_list_sql(false, $info, true);
$result = $DB->get_fieldset_sql($sql, $params);
sort($result);
$this->assertEquals($expected, $result);
$result = array_keys($cond->filter_user_list($allusers, true, $info, $checker));
ksort($result);
$expected = array($teacher->id, $students[0]->id, $students[2]->id);
$this->assertEquals($expected, $result);
list ($sql, $params) = $cond->get_user_list_sql(true, $info, true);
$result = $DB->get_fieldset_sql($sql, $params);
sort($result);
$this->assertEquals($expected, $result);
}
}