From 7095fe0caa6eaa3afd87ef3a07178b5069ef5dde Mon Sep 17 00:00:00 2001 From: Mark Johnson Date: Wed, 15 Feb 2023 13:35:28 +0000 Subject: [PATCH] MDL-68093 wiki: Restrict group options to participation groups --- mod/wiki/create.php | 4 +- mod/wiki/tests/behat/behat_mod_wiki.php | 86 +++++++++++++++++ mod/wiki/tests/behat/wiki_groups.feature | 112 +++++++++++++++++++++++ 3 files changed, 200 insertions(+), 2 deletions(-) create mode 100644 mod/wiki/tests/behat/behat_mod_wiki.php create mode 100644 mod/wiki/tests/behat/wiki_groups.feature diff --git a/mod/wiki/create.php b/mod/wiki/create.php index 7042e5503e6..a1479a36434 100644 --- a/mod/wiki/create.php +++ b/mod/wiki/create.php @@ -65,13 +65,13 @@ if (groups_get_activity_groupmode($cm)) { $modulecontext = context_module::instance($cm->id); $canaccessgroups = has_capability('moodle/site:accessallgroups', $modulecontext); if ($canaccessgroups) { - $groups->availablegroups = groups_get_all_groups($cm->course); + $groups->availablegroups = groups_get_all_groups($cm->course, 0, 0, 'g.*', false, true); $allpart = new stdClass(); $allpart->id = '0'; $allpart->name = get_string('allparticipants'); array_unshift($groups->availablegroups, $allpart); } else { - $groups->availablegroups = groups_get_all_groups($cm->course, $USER->id); + $groups->availablegroups = groups_get_all_groups($cm->course, $USER->id, 0, 'g.*', false, true); } if (!empty($group)) { $groups->currentgroup = $group; diff --git a/mod/wiki/tests/behat/behat_mod_wiki.php b/mod/wiki/tests/behat/behat_mod_wiki.php new file mode 100644 index 00000000000..e69dcc676c2 --- /dev/null +++ b/mod/wiki/tests/behat/behat_mod_wiki.php @@ -0,0 +1,86 @@ +. + +/** + * Steps definitions related to mod_quiz. + * + * @package mod_wiki + * @category test + * @copyright 2023 Catalyst IT Europe Ltd. + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. + +use Behat\Gherkin\Node\TableNode as TableNode; +use Behat\Mink\Exception\ExpectationException as ExpectationException; + +/** + * Steps definitions related to mod_wiki. + * + * @copyright 2023 Catalyst IT Europe Ltd. + * @author Mark Johnson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_mod_wiki extends behat_base { + + /** + * Add the specified pages to the specified wiki + * + * The first row should be column names: + * | wiki | user | group | title | content | + * + * + * wiki idnumber of the wiki course module + * user username of the user who is creating the page + * group (optional) idnumber of the group the page belongs to + * title (optional) the title text of the page + * content (optional) the content of the page + * + * @param TableNode $data The pages to add + * + * @Given /^the following wiki pages exist:$/ + */ + public function the_following_wiki_pages_exist(TableNode $data): void { + global $DB; + + $generator = behat_util::get_data_generator()->get_plugin_generator('mod_wiki'); + // Add the pages. + foreach ($data->getHash() as $pagedata) { + if (!array_key_exists('wiki', $pagedata)) { + throw new ExpectationException('When adding pages to a wiki, ' . + 'the wiki column is required containing the wiki idnumber.', $this->getSession()); + } + + $wikicm = $this->get_course_module_for_identifier($pagedata['wiki']); + $wiki = $DB->get_record('wiki', ['id' => $wikicm->instance]); + $wiki->cmid = $wikicm->cmid; + $pagedata['wikiid'] = $wiki->id; + unset($pagedata['wiki']); + + if (array_key_exists('group', $pagedata)) { + $pagedata['group'] = $DB->get_field('groups', 'id', ['idnumber' => $pagedata['group']], MUST_EXIST); + } + + if (array_key_exists('user', $pagedata)) { + $pagedata['userid'] = $DB->get_field('user', 'id', ['username' => $pagedata['user']], MUST_EXIST); + unset($pagedata['user']); + } + + $generator->create_page($wiki, $pagedata); + } + } +} diff --git a/mod/wiki/tests/behat/wiki_groups.feature b/mod/wiki/tests/behat/wiki_groups.feature new file mode 100644 index 00000000000..7cb6850000a --- /dev/null +++ b/mod/wiki/tests/behat/wiki_groups.feature @@ -0,0 +1,112 @@ +@mod @mod_wiki +Feature: Groups can have separate content on a wiki + In order to create a wiki with my group + As a user + I need to view and add wiki pages by group + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | teacher1@example.com | + | student1 | Student | 1 | student1@example.com | + | student2 | Student | 2 | student2@example.com | + | student3 | Student | 3 | student1@example.com | + | student4 | Student | 4 | student2@example.com | + And the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + | student2 | C1 | student | + | student3 | C1 | student | + | student4 | C1 | student | + Given the following "groups" exist: + | name | course | idnumber | participation | + | Group 1 | C1 | G1 | 1 | + | Group 2 | C1 | G2 | 1 | + | Group 3 | C1 | G3 | 0 | + And the following "group members" exist: + | user | group | + | student1 | G1 | + | student2 | G2 | + | student3 | G3 | + And the following "activities" exist: + | activity | course | name | idnumber | wikimode | firstpagetitle | groupmode | + | wiki | C1 | Separate wiki | wiki1 | collaborative | Separate page 1 | 1 | + | wiki | C1 | Visible wiki | wiki2 | collaborative | Visible page 1 | 2 | + And the following wiki pages exist: + | wiki | title | content | group | + | wiki1 | Separate page 1 | Group 1 page | G1 | + | wiki1 | Separate page 1 | Group 2 page | G2 | + | wiki2 | Visible page 1 | Group 1 page | G1 | + | wiki2 | Visible page 1 | Group 2 page | G2 | + And the following wiki pages exist: + | wiki | title | content | + | wiki1 | Separate page 1 | No group page | + | wiki2 | Visible page 1 | No group page | + + Scenario Outline: Teacher can see all participation group wikis + Given I am on the "" "wiki activity" page logged in as teacher1 + And I should see "All participants" in the " groups" "select" + And I should see "Group 1" in the " groups" "select" + And I should see "Group 2" in the " groups" "select" + And I should not see "Group 3" in the " groups" "select" + And I should see "No group page" + And I should not see "Group 1 page" + And I should not see "Group 2 page" + When I select "Group 1" from the " groups" singleselect + Then I should not see "No group page" + And I should see "Group 1 page" + And I should not see "Group 2 page" + + Examples: + | wiki | mode | + | wiki1 | Separate | + | wiki2 | Visible | + + Scenario Outline: Teacher can add a page to any participation group's wiki + Given I am on the "" "wiki activity" page logged in as teacher1 + And I select "Edit" from the "jump" singleselect + And I set the field "HTML format" to "[[Internal link]]" + And I press "Save" + When I follow "Internal link" + And I should see "New page" + Then I should see "All participants" in the "Group" "select" + And I should see "Group 1" in the "Group" "select" + And I should see "Group 2" in the "Group" "select" + And I should not see "Group 3" in the "Group" "select" + + Examples: + | wiki | + | wiki1 | + | wiki2 | + + Scenario Outline: Students should only see their participation groups' own wiki in separate groups mode + Given I am on the "wiki1" "wiki activity" page logged in as + Then I should see "Separate groups: " + And "Separate groups" "select" should not exist + And I should see "" + + Examples: + | user | group | page | + | student1 | Group 1 | Group 1 page | + | student2 | Group 2 | Group 2 page | + # The view page throws an exception if the user is not in a group, so we cannot test + # student3 and student4. + + Scenario Outline: Students can see all participation groups' own wikis in visible groups mode + Given I am on the "wiki2" "wiki activity" page logged in as + And I should see "All participants" in the "Visible groups" "select" + And I should see "Group 1" in the "Visible groups" "select" + And I should see "Group 2" in the "Visible groups" "select" + And I should not see "Group 3" in the "Visible groups" "select" + And I should see "" + + Examples: + | user | page | + | student1 | Group 1 page | + | student2 | Group 2 page | + | student3 | Group 1 page | + | student4 | Group 1 page |