From b2b1915e25c40a68cfa9c48ce8627f299010afdf Mon Sep 17 00:00:00 2001 From: Ferran Recio Date: Mon, 10 Jul 2023 11:25:20 +0200 Subject: [PATCH] MDL-78282 core_courseformat: add non-ajax cm actions translation --- course/format/classes/base.php | 31 ++++++++++++++ course/format/tests/base_test.php | 69 +++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/course/format/classes/base.php b/course/format/classes/base.php index faa2757dd95..2d4cbe59f7f 100644 --- a/course/format/classes/base.php +++ b/course/format/classes/base.php @@ -740,6 +740,37 @@ abstract class base { return $url; } + /** + * Return the old non-ajax activity action url. + * + * Goutte behats tests cannot trigger javascript events, + * so we must translate to an old non-ajax url while non-ajax + * course editing is still supported. + * + * @param string $action action name the reactive action + * @param cm_info $cm course module + * @return moodle_url + */ + public function get_non_ajax_cm_action_url(string $action, cm_info $cm): moodle_url { + $nonajaxactions = [ + 'cmDelete' => 'delete', + 'cmDuplicate' => 'duplicate', + 'cmHide' => 'hide', + 'cmShow' => 'show', + 'cmStealth' => 'stealth', + ]; + if (!isset($nonajaxactions[$action])) { + throw new coding_exception('Unknown activity action: ' . $action); + } + $nonajaxaction = $nonajaxactions[$action]; + $nonajaxurl = new moodle_url( + '/course/mod.php', + ['sesskey' => sesskey(), $nonajaxaction => $cm->id] + ); + $nonajaxurl->param('sr', $this->get_section_number()); + return $nonajaxurl; + } + /** * Loads all of the course sections into the navigation * diff --git a/course/format/tests/base_test.php b/course/format/tests/base_test.php index 74314abc50f..4f17db1179a 100644 --- a/course/format/tests/base_test.php +++ b/course/format/tests/base_test.php @@ -662,6 +662,75 @@ class base_test extends advanced_testcase { ], ]; } + + /** + * Test for the get_non_ajax_cm_action_url method. + * + * @covers ::get_non_ajax_cm_action_url + * @dataProvider get_non_ajax_cm_action_url_provider + * @param string $action the ajax action name + * @param string $expectedparam the expected param to check + * @param string $exception if an exception is expected + */ + public function test_get_non_ajax_cm_action_url(string $action, string $expectedparam, bool $exception) { + global $DB; + + $this->resetAfterTest(); + + $generator = $this->getDataGenerator(); + $course = $generator->create_course(); + $assign0 = $generator->create_module('assign', array('course' => $course, 'section' => 0)); + + $format = course_get_format($course); + $modinfo = $format->get_modinfo(); + $cminfo = $modinfo->get_cm($assign0->cmid); + + if ($exception) { + $this->expectException(\coding_exception::class); + } + $result = $format->get_non_ajax_cm_action_url($action, $cminfo); + $this->assertEquals($assign0->cmid, $result->param($expectedparam)); + } + + /** + * Data provider for test_get_non_ajax_cm_action_url. + * + * @return array the testing scenarios + */ + public function get_non_ajax_cm_action_url_provider(): array { + return [ + 'duplicate' => [ + 'action' => 'cmDuplicate', + 'expectedparam' => 'duplicate', + 'exception' => false, + ], + 'hide' => [ + 'action' => 'cmHide', + 'expectedparam' => 'hide', + 'exception' => false, + ], + 'show' => [ + 'action' => 'cmShow', + 'expectedparam' => 'show', + 'exception' => false, + ], + 'stealth' => [ + 'action' => 'cmStealth', + 'expectedparam' => 'stealth', + 'exception' => false, + ], + 'delete' => [ + 'action' => 'cmDelete', + 'expectedparam' => 'delete', + 'exception' => false, + ], + 'non-existent' => [ + 'action' => 'nonExistent', + 'expectedparam' => '', + 'exception' => true, + ], + ]; + } } /**