From c1872e7df3337d41605385d4d93ff6086c0c07b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luca=20B=C3=B6sch?= Date: Mon, 11 Nov 2024 22:20:59 +0100 Subject: [PATCH] Compatibility with Moodle 4.5. --- .github/workflows/moodle-ci.yml | 31 ++++++++++--------- .../configuration/abstract_configuration.php | 4 +-- classes/local/data_grid/filter/filter.php | 4 +-- .../data_grid/filter/filter_interface.php | 4 +-- classes/table/groups.php | 13 ++++++++ classes/table/members.php | 13 ++++++++ download.php | 2 +- lang/en/block_dash.php | 10 +++--- tests/behat/mycontacts.feature | 14 ++++----- tests/behat/mygroups.feature | 2 +- tests/behat/mylearning.feature | 29 +++++++++++++++-- tests/filter_test.php | 15 ++++----- tests/framework_query_builder_test.php | 12 +++---- tests/widgets_test.php | 9 +++--- version.php | 5 +-- 15 files changed, 111 insertions(+), 56 deletions(-) diff --git a/.github/workflows/moodle-ci.yml b/.github/workflows/moodle-ci.yml index 421b641..5ee6b32 100644 --- a/.github/workflows/moodle-ci.yml +++ b/.github/workflows/moodle-ci.yml @@ -1,10 +1,10 @@ name: Moodle Plugin CI -on: [pull_request] +on: [pull_request, push] jobs: test: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest services: postgres: @@ -28,12 +28,24 @@ jobs: fail-fast: false matrix: include: + - php: '8.3' + moodle-branch: 'MOODLE_405_STABLE' + database: 'pgsql' - php: '8.2' - moodle-branch: 'MOODLE_404_STABLE' + moodle-branch: 'MOODLE_405_STABLE' database: 'mariadb' - php: '8.1' + moodle-branch: 'MOODLE_405_STABLE' + database: 'pgsql' + - php: '8.3' + moodle-branch: 'MOODLE_404_STABLE' + database: 'mariadb' + - php: '8.2' moodle-branch: 'MOODLE_404_STABLE' database: 'pgsql' + - php: '8.1' + moodle-branch: 'MOODLE_404_STABLE' + database: 'mariadb' - php: '8.2' moodle-branch: 'MOODLE_403_STABLE' database: 'pgsql' @@ -46,7 +58,7 @@ jobs: steps: - name: Check out repository code - uses: actions/checkout@v2 + uses: actions/checkout@v4 with: path: plugin @@ -59,7 +71,7 @@ jobs: - name: Initialise moodle-plugin-ci run: | - composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^3 + composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^4 echo $(cd ci/bin; pwd) >> $GITHUB_PATH echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH sudo locale-gen en_AU.UTF-8 @@ -77,15 +89,6 @@ jobs: if: ${{ always() }} run: moodle-plugin-ci phplint - - name: PHP Copy/Paste Detector - continue-on-error: true # This step will show errors but will not fail - if: ${{ always() }} - run: moodle-plugin-ci phpcpd - - - name: PHP Mess Detector - if: ${{ always() }} - run: moodle-plugin-ci phpmd - - name: Moodle Code Checker if: ${{ always() }} run: moodle-plugin-ci codechecker --max-warnings 0 diff --git a/classes/local/configuration/abstract_configuration.php b/classes/local/configuration/abstract_configuration.php index 8f544ea..84ada6a 100644 --- a/classes/local/configuration/abstract_configuration.php +++ b/classes/local/configuration/abstract_configuration.php @@ -47,9 +47,9 @@ abstract class abstract_configuration implements configuration_interface { * abstract_configuration constructor. * * @param \context $context - * @param data_source_interface|null $datasource + * @param ?data_source_interface $datasource */ - protected function __construct(\context $context, data_source_interface $datasource = null) { + protected function __construct(\context $context, ?data_source_interface $datasource = null) { $this->context = $context; $this->datasource = $datasource; } diff --git a/classes/local/data_grid/filter/filter.php b/classes/local/data_grid/filter/filter.php index 9659161..df26fe7 100644 --- a/classes/local/data_grid/filter/filter.php +++ b/classes/local/data_grid/filter/filter.php @@ -457,9 +457,9 @@ public function get_custom_operation(): string { /** * Set preferences on this filter. * - * @param array $preferences + * @param ?array $preferences */ - public function set_preferences(array $preferences = null): void { + public function set_preferences(?array $preferences = null): void { $this->preferences = $preferences; } diff --git a/classes/local/data_grid/filter/filter_interface.php b/classes/local/data_grid/filter/filter_interface.php index f0c10c3..e05d6cf 100644 --- a/classes/local/data_grid/filter/filter_interface.php +++ b/classes/local/data_grid/filter/filter_interface.php @@ -306,9 +306,9 @@ public function get_custom_operation(): string; /** * Set preferences on this filter. * - * @param array $preferences + * @param ?array $preferences */ - public function set_preferences(array $preferences = null): void; + public function set_preferences(?array $preferences = null): void; /** * Get preferences related to this filter. diff --git a/classes/table/groups.php b/classes/table/groups.php index 92aafa0..eaa1821 100644 --- a/classes/table/groups.php +++ b/classes/table/groups.php @@ -128,4 +128,17 @@ public function col_groupname($row) { public function col_course($row) { return format_string(get_course($row->courseid)->fullname); } + + /** + * Check capability for users accessing the groups table. + * + * @return bool + */ + public function has_capability(): bool { + global $CFG; + require_once($CFG->dirroot . '/course/lib.php'); + + $context = $this->course->id == SITEID ? \context_system::instance() : $this->get_context(); + return has_capability('block/dash:mygroups_view', $context); + } } diff --git a/classes/table/members.php b/classes/table/members.php index 4b82bf1..fa60657 100644 --- a/classes/table/members.php +++ b/classes/table/members.php @@ -133,4 +133,17 @@ public function col_fullname($row) { public function col_roles($row) { return get_user_roles_in_course($row->userid, $row->courseid); } + + /** + * Check capability for users accessing the members table. + * + * @return bool + */ + public function has_capability(): bool { + global $CFG; + require_once($CFG->dirroot . '/course/lib.php'); + + $context = $this->course->id == SITEID ? \context_system::instance() : $this->get_context(); + return has_capability('block/dash:mygroups_viewmembers', $context); + } } diff --git a/download.php b/download.php index 319131b..b11049a 100644 --- a/download.php +++ b/download.php @@ -40,7 +40,7 @@ $sortdir = optional_param('sort_direction', '', PARAM_TEXT); $PAGE->set_context(context_system::instance()); -$PAGE->set_url('/blocks/dash/download.php', array('block_instance_id' => $instanceid)); +$PAGE->set_url('/blocks/dash/download.php', ['block_instance_id' => $instanceid]); $renderer = $PAGE->get_renderer('block_dash'); $binstance = $DB->get_record('block_instances', ['id' => $instanceid]); diff --git a/lang/en/block_dash.php b/lang/en/block_dash.php index 35f8eee..7c0a4dd 100644 --- a/lang/en/block_dash.php +++ b/lang/en/block_dash.php @@ -22,6 +22,9 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +// phpcs:disable moodle.Files.LangFilesOrdering.UnexpectedComment +// phpcs:disable moodle.Files.LangFilesOrdering.IncorrectOrder + $string['accordionfield1'] = 'Top left field'; $string['accordionfield1icon'] = 'Top left field icon'; $string['accordionfield2'] = 'Top right field'; @@ -64,12 +67,11 @@ $string['courseoverviewfilesurl'] = 'Course image URL'; $string['coursesummary'] = 'Course summary'; $string['courseurl'] = 'Course URL'; -$string['courseurl'] = 'Course URL'; $string['createcustomdatasource'] = 'Create custom data source'; $string['createcustomlayout'] = 'Create custom layout'; $string['createdashboard'] = 'Create dashboard'; $string['createlayout'] = 'Create custom layout'; -$string['cssclass'] = 'CSS Class'; +$string['cssclass'] = 'CSS classes'; $string['currentcourse'] = 'Current course'; $string['currentcoursecontext'] = 'Current course context'; $string['currentcoursegroups'] = 'Current course groups'; @@ -409,7 +411,6 @@ $string['coursedate:present'] = 'Present'; $string['coursedate:future'] = 'Future'; -$string['viewcourse'] = 'View course'; $string['enrolnow'] = 'Enrol now'; $string['buynow'] = 'Buy now'; $string['notavailable'] = 'Not available'; @@ -570,7 +571,6 @@ // ...Form section headers. $string['fieldssection'] = 'Fields setup'; $string['conditionsection'] = 'Condition setup'; -$string['fields'] = 'Fields'; $string['field'] = 'Field'; $string['fieldtransformdata'] = 'Transform the format'; $string['fieldtransformdata_help'] = 'Transform the format of the value when displayed in the table'; @@ -779,7 +779,6 @@ // ... enrol program string. $string['programimagelink'] = 'Program image linked'; $string['smart_coursebutton'] = 'Smart course button'; -$string['fullnamelinked'] = 'Fullname linked'; $string['contextlinked'] = 'Context linked'; $string['description'] = 'Description'; $string['programs:view'] = 'View programs'; @@ -814,7 +813,6 @@ $string['categoryimgfallbackdesc'] = 'Upload the image for the default fallback of categories. If new categories are created, the fallback image will be displayed until a new image is added for the category'; $string['categoryimageurl'] = 'Category image url'; -$string['cssclass'] = "CSS classes"; $string['cssclass_help'] = "Use a custom CSS class to apply multiple classes."; $string['backgroundposition'] = 'Background Position'; $string['backgroundposition_help'] = 'Background image will focused on the given position'; diff --git a/tests/behat/mycontacts.feature b/tests/behat/mycontacts.feature index 4e42c0b..e7a889f 100644 --- a/tests/behat/mycontacts.feature +++ b/tests/behat/mycontacts.feature @@ -76,15 +76,15 @@ Feature: Add user contacts widget in dash block Given I log in as "student1" And I should see "Student Two" in the "New Dash" "block" And I should see "Student Three" in the "New Dash" "block" - And I should see "2" in the ".block_dash-community-block .contact-element .row div.col-xl-3:nth-child(1) .badge-block" "css_element" - And ".badge-block" "css_element" should not exist in the ".block_dash-community-block .contact-element .row div:nth-child(2)" "css_element" - And I hover ".block_dash-community-block .contact-element .row div.col-xl-3:nth-child(1)" "css_element" - And I click on ".contact-widget-viewgroup" "css_element" in the ".block_dash-community-block .contact-element .row div.col-xl-3:nth-child(1)" "css_element" + And I should see "2" in the ".block_dash-community-block .contact-block .contact-element .row .col-xl-3:nth-child(1) .contact-img-block .img-block .badge-block" "css_element" + And ".badge-block" "css_element" should not exist in the ".block_dash-community-block .contact-block .contact-element .row .col-xl-3:nth-child(2)" "css_element" + And I hover ".block_dash-community-block .contact-element .row .col-xl-3:nth-child(1)" "css_element" + And I click on ".contact-widget-viewgroup" "css_element" in the ".block_dash-community-block .contact-element .row .col-xl-3:nth-child(1)" "css_element" And I should see "Groups" in the ".modal-title" "css_element" And "Group 1" "table_row" should exist - And I click on ".close" "css_element" in the ".modal-header" "css_element" - And I hover ".block_dash-community-block .contact-element .row div.col-xl-3:nth-child(2)" "css_element" - And I click on ".contact-widget-viewgroup" "css_element" in the ".block_dash-community-block .contact-element .row div.col-xl-3:nth-child(2)" "css_element" + And I click on "Close" "button" in the "Groups" "dialogue" + And I hover ".block_dash-community-block .contact-element .row .col-xl-3:nth-child(2)" "css_element" + And I click on ".contact-widget-viewgroup" "css_element" in the ".block_dash-community-block .contact-element .row .col-xl-3:nth-child(2)" "css_element" And I should see "Groups" in the ".modal-title" "css_element" Then I should see "Nothing to display" in the ".modal-body" "css_element" diff --git a/tests/behat/mygroups.feature b/tests/behat/mygroups.feature index 933d6dc..e75ce32 100644 --- a/tests/behat/mygroups.feature +++ b/tests/behat/mygroups.feature @@ -78,7 +78,7 @@ Feature: Add My Groups widget in dash block And I click on ".dropdown-toggle" "css_element" in the ".block_dash-community-block .list-block:nth-child(1)" "css_element" And I click on ".group-widget-viewmembers" "css_element" in the ".block_dash-community-block .list-block:nth-child(1)" "css_element" And "Student Two" "table_row" should exist - And I click on ".close" "css_element" in the ".modal-header" "css_element" + And I click on "Close" "button" in the "Groups" "dialogue" And I click on ".dropdown-toggle" "css_element" in the ".block_dash-community-block .list-block:nth-child(3)" "css_element" And I click on ".group-widget-viewmembers" "css_element" in the ".block_dash-community-block .list-block:nth-child(3)" "css_element" Then I should see "Nothing to display" in the ".modal-body" "css_element" diff --git a/tests/behat/mylearning.feature b/tests/behat/mylearning.feature index 7405624..b51918c 100644 --- a/tests/behat/mylearning.feature +++ b/tests/behat/mylearning.feature @@ -77,8 +77,9 @@ Feature: Enable the widget in dash block on the dashboard page and view it's con And ".fa.fa-check" "css_element" should exist in the ".block_dash-info-element .card:nth-child(1)" "css_element" @javascript @_file_upload - Scenario: Course badges list in Mylearning widget - Given I log in as "admin" + Scenario: Course badges list in Mylearning widget for Moodle ≤ 4.4 + Given the site is running Moodle version 4.4 or lower + And I log in as "admin" And I am on "Course 1" course homepage And I navigate to "Badges > Add a new badge" in current page administration And I set the following fields to these values: @@ -99,6 +100,30 @@ Feature: Enable the widget in dash block on the dashboard page and view it's con When I log in as "student1" Then ".collected .activatebadge[alt=\"Badge 1\"]" "css_element" should exist in the "My Learning" "block" + @javascript @_file_upload + Scenario: Course badges list in Mylearning widget for Moodle ≥ 4.5 + Given the site is running Moodle version 4.5 or higher + And I log in as "admin" + And I am on "Course 1" course homepage + And I navigate to "Badges > Add a new badge" in current page administration + And I set the following fields to these values: + | id_name | Badge 1 | + | id_description | Badge 1 | + And I upload "blocks/badges/tests/fixtures/badge.png" file to "Image" filemanager + And I press "Create badge" + And I select "Manual issue by role" from the "Add badge criteria" singleselect + And I set the field "Teacher" to "1" + And I press "Save" + And I press "Enable access" + And I click on "Enable" "button" in the "Confirm" "dialogue" + And I follow badge recipients + And I press "Award badge" + And I set the field "potentialrecipients[]" to "Student First (student1@example.com)" + And I press "Award badge" + And I log out + When I log in as "student1" + Then ".collected .activatebadge[alt=\"Badge 1\"]" "css_element" should exist in the "My Learning" "block" + @javascript Scenario: Check the empty state option. Given I log in as "student2" diff --git a/tests/filter_test.php b/tests/filter_test.php index 0a667db..9defae5 100644 --- a/tests/filter_test.php +++ b/tests/filter_test.php @@ -37,7 +37,7 @@ * @group bdecent * @group filter_test */ -class filter_test extends \advanced_testcase { +final class filter_test extends \advanced_testcase { /** * @var filter_collection_interface @@ -53,6 +53,7 @@ class filter_test extends \advanced_testcase { * This method is called before each test. */ protected function setUp(): void { + parent::setUp(); $this->resetAfterTest(); $this->setAdminUser(); @@ -71,7 +72,7 @@ protected function setUp(): void { * @covers ::general_stuff * @return void */ - public function test_general_stuff() { + public function test_general_stuff(): void { $this->assertEquals('testing', $this->filtercollection->get_unique_identifier()); $this->assertCount(1, $this->filtercollection->get_filters()); $this->assertTrue($this->filtercollection->has_filter('filter1')); @@ -84,7 +85,7 @@ public function test_general_stuff() { * @covers ::remove_filter * @return void */ - public function test_remove_filter() { + public function test_remove_filter(): void { $filter = $this->filtercollection->get_filter('filter1'); $this->filtercollection->remove_filter($filter); @@ -99,7 +100,7 @@ public function test_remove_filter() { * @covers ::applying_filter * @return void */ - public function test_applying_filter() { + public function test_applying_filter(): void { $this->assertCount(0, $this->filtercollection->get_applied_filters()); $this->assertCount(0, $this->filtercollection->get_filters_with_values()); @@ -118,7 +119,7 @@ public function test_applying_filter() { * @covers ::filter_sql_and_params_collection * @return void */ - public function test_filter_sql_and_params_collection() { + public function test_filter_sql_and_params_collection(): void { $this->assertTrue($this->filtercollection->apply_filter('filter1', 123)); list($sql, $params) = $this->filtercollection->get_sql_and_params(); @@ -132,7 +133,7 @@ public function test_filter_sql_and_params_collection() { * @covers ::required_filters * @return void */ - public function test_required_filters() { + public function test_required_filters(): void { $this->assertFalse($this->filtercollection->has_required_filters()); $this->assertCount(0, $this->filtercollection->get_required_filters()); @@ -150,7 +151,7 @@ public function test_required_filters() { * @covers ::caching * @return void */ - public function test_caching() { + public function test_caching(): void { $this->filtercollection->apply_filter('filter1', 234); $this->filtercollection->cache($this->user); diff --git a/tests/framework_query_builder_test.php b/tests/framework_query_builder_test.php index 0c0175e..219641a 100644 --- a/tests/framework_query_builder_test.php +++ b/tests/framework_query_builder_test.php @@ -36,7 +36,7 @@ * @group bdecent * @group query_builder_test */ -class framework_query_builder_test extends \advanced_testcase { +final class framework_query_builder_test extends \advanced_testcase { /** * Test for where() to ensure that the where conditions are correctly applied. @@ -44,7 +44,7 @@ class framework_query_builder_test extends \advanced_testcase { * @covers ::where * @return void */ - public function test_where() { + public function test_where(): void { $this->resetAfterTest(); $category = $this->getDataGenerator()->create_category(); @@ -97,7 +97,7 @@ public function test_where() { * @covers ::where_in_query * @return void */ - public function test_where_in_query() { + public function test_where_in_query(): void { $this->resetAfterTest(); $course1 = $this->getDataGenerator()->create_course([ @@ -140,7 +140,7 @@ public function test_where_in_query() { * @covers ::limits * @return void */ - public function test_limits() { + public function test_limits(): void { $this->resetAfterTest(); $users = []; @@ -168,7 +168,7 @@ public function test_limits() { * @covers ::orderby * @return void */ - public function test_orderby() { + public function test_orderby(): void { $this->resetAfterTest(); $courses = []; @@ -201,7 +201,7 @@ public function test_orderby() { * @covers ::joins * @return void */ - public function test_joins() { + public function test_joins(): void { $this->resetAfterTest(); $category = $this->getDataGenerator()->create_category(); diff --git a/tests/widgets_test.php b/tests/widgets_test.php index 0b0728c..18ec4a3 100644 --- a/tests/widgets_test.php +++ b/tests/widgets_test.php @@ -35,7 +35,7 @@ * @runInSeparateProcess * @runTestsInSeparateProcesses */ -class widgets_test extends \advanced_testcase { +final class widgets_test extends \advanced_testcase { /** * Demo of test user. @@ -76,6 +76,7 @@ class widgets_test extends \advanced_testcase { * This method is called before each test. */ protected function setUp(): void { + parent::setUp(); $this->resetAfterTest(); $this->setAdminUser(); global $USER; @@ -158,7 +159,7 @@ protected function create_block($page) { * @runInSeparateProcess * @runTestsInSeparateProcesses */ - public function test_mylearning() { + public function test_mylearning(): void { $user = self::getDataGenerator()->create_and_enrol($this->course1, 'student'); $teacher = self::getDataGenerator()->create_and_enrol($this->course1, 'editingteacher'); self::getDataGenerator()->enrol_user($user->id, $this->course2->id); @@ -206,7 +207,7 @@ public function test_mylearning() { * @covers ::contacts_widget * @return void */ - public function test_mycontacts() { + public function test_mycontacts(): void { global $DB; $block = $this->create_user_block('My contacts', 'block_dash\local\widget\contacts\contacts_widget'); @@ -249,7 +250,7 @@ public function test_mycontacts() { * @covers ::groups_widget * @return void */ - public function test_mygroups() { + public function test_mygroups(): void { global $CFG; require_once($CFG->dirroot.'/group/lib.php'); diff --git a/version.php b/version.php index d24c86b..8d16334 100644 --- a/version.php +++ b/version.php @@ -24,9 +24,10 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2024050804; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2024111100; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2021051700; // Requires this Moodle version. $plugin->component = 'block_dash'; // Full name of the plugin (used for diagnostics). $plugin->maturity = MATURITY_STABLE; $plugin->release = '2.2'; -$plugin->supported = [401, 404]; \ No newline at end of file +$plugin->supported = [401, 405]; +