MDL-78282 core_courseformat: add non-ajax cm actions translation

This commit is contained in:
Ferran Recio 2023-07-10 11:25:20 +02:00
parent b4cd63746a
commit b2b1915e25
2 changed files with 100 additions and 0 deletions

View File

@ -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
*

View File

@ -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,
],
];
}
}
/**