From 3c47fbe3b6c265875c1846182dd1a6724cf5bbe0 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Fri, 30 Aug 2024 11:07:04 +0200 Subject: [PATCH] MDL-82953 badges: Fix redirect when form is cancelled From the new badge page, when the form is cancelled, a redirect should be done to the badges/index.php. This is a regression introduced when newbadge.php and edit.php pages were merged. --- badges/edit.php | 6 +++- badges/tests/behat/add_badge.feature | 41 ++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/badges/edit.php b/badges/edit.php index a65fdd9b128..7bd2676796c 100644 --- a/badges/edit.php +++ b/badges/edit.php @@ -60,6 +60,8 @@ if (!empty($badgeid)) { } else { require_capability('moodle/badges:configuredetails', $context); } + + $cancelurl = new moodle_url('/badges/overview.php', ['id' => $badgeid]); } else { // New badge. if ($courseid) { @@ -79,6 +81,8 @@ if (!empty($badgeid)) { // Check capabilities. require_capability('moodle/badges:createbadge', $context); + + $cancelurl = new moodle_url('/badges/index.php', ['type' => $badge->type, 'id' => $courseid]); } // Check if course badges are enabled. @@ -141,7 +145,7 @@ if ($badge->id) { $form = new $formclass($currenturl, $params); if ($form->is_cancelled()) { - redirect(new moodle_url('/badges/overview.php', ['id' => $badgeid])); + redirect($cancelurl); } else if ($form->is_submitted() && $form->is_validated() && ($data = $form->get_data())) { switch ($action) { case 'new': diff --git a/badges/tests/behat/add_badge.feature b/badges/tests/behat/add_badge.feature index d5d8f0ec025..f0ec65341d9 100644 --- a/badges/tests/behat/add_badge.feature +++ b/badges/tests/behat/add_badge.feature @@ -67,7 +67,6 @@ Feature: Add badges to the system | Image caption | Test caption image | And I upload "badges/tests/behat/badge.png" file to "Image" filemanager And I press "Create badge" - And I wait until the page is ready And I navigate to "Badges > Manage badges" in site administration And I press "Add a new badge" And I set the following fields to these values: @@ -83,7 +82,6 @@ Feature: Add badges to the system And I should see "This badge does not have any related badges." And I press "Add related badge" And I follow "Related badges" - And I wait until the page is ready And I follow "Related badges" And I set the field "relatedbadgeids[]" to "Test Badge 1 (version: v1, language: French, Site badges)" When I press "Save changes" @@ -220,3 +218,42 @@ Feature: Add badges to the system | badges_defaultissuername | Test Badge Site | And I navigate to "Badges > Add a new badge" in site administration And the field "Issuer name" matches value "Test Badge Site" + + Scenario: Cancel button behaviour when creating badges + Given the following "course" exists: + | fullname | Course 1 | + | shortname | C1 | + And the following "core_badges > Badge" exists: + | name | Site Badge 1 | + | description | Site badge 1 description | + | image | badges/tests/behat/badge.png | + | status | 0 | + | type | 1 | + And the following "core_badges > Badge" exists: + | name | Course Badge 1 | + | course | C1 | + | description | Course badge 1 description | + | image | badges/tests/behat/badge.png | + | status | 0 | + | type | 2 | + # Site badge: cancel when creating. + When I navigate to "Badges > Add a new badge" in site administration + And I click on "Cancel" "button" + Then I should see "Manage badges" + And I should see "Add a new badge" + # Site badge: cancel when editing. + And I press "Edit" action in the "Site Badge 1" report row + And I click on "Cancel" "button" + And I should see "Site badge 1" + And I should not see "Save changes" + # Course badge: cancel when creating. + And I am on the "Course 1" "course" page + And I navigate to "Badges > Manage badges > Add a new badge" in current page administration + And I click on "Cancel" "button" + Then I should see "Manage badges" + And I should see "Add a new badge" + # Course badge: cancel when editing. + And I press "Edit" action in the "Course Badge 1" report row + And I click on "Cancel" "button" + And I should see "Course badge 1" + And I should not see "Save changes"