|
20 | 20 | use base_plan_exception; |
21 | 21 | use base_setting_exception; |
22 | 22 | use block_massaction; |
| 23 | +use block_massaction\hook\filter_sections_different_course; |
23 | 24 | use coding_exception; |
| 25 | +use core\di; |
24 | 26 | use core\event\course_module_updated; |
25 | 27 | use core\task\manager; |
26 | 28 | use dml_exception; |
@@ -860,4 +862,107 @@ private function shuffle_modules(): void { |
860 | 862 | moveto_module(get_fast_modinfo($this->course->id)->get_cm(get_fast_modinfo($this->course->id)->get_sections()[3][3]), |
861 | 863 | get_fast_modinfo($this->course->id)->get_section_info(3)); |
862 | 864 | } |
| 865 | + |
| 866 | + /** |
| 867 | + * Tests the duplication of modules to a course with filter_sections hook. |
| 868 | + * |
| 869 | + * @covers \block_massaction\actions::duplicate_to_course |
| 870 | + * @return void |
| 871 | + */ |
| 872 | + public function test_duplicate_to_course_with_filter_sections_different_course_hook(): void { |
| 873 | + $this->resetAfterTest(); |
| 874 | + |
| 875 | + // Callback for filter_sections_different_course hook. |
| 876 | + $testcallback = function(filter_sections_different_course $hook) { |
| 877 | + foreach ($hook->get_sectionnums() as $sectionnum) { |
| 878 | + // Restrict section 3 onward. |
| 879 | + if ($sectionnum >= 3) { |
| 880 | + $hook->remove_sectionnum($sectionnum); |
| 881 | + } |
| 882 | + } |
| 883 | + |
| 884 | + // Disable the options to keep the original section. |
| 885 | + $hook->disable_originsectionkept(); |
| 886 | + |
| 887 | + // Disable the option to create a new section. |
| 888 | + $hook->disable_makesection(); |
| 889 | + }; |
| 890 | + $this->redirectHook(filter_sections_different_course::class, $testcallback); |
| 891 | + |
| 892 | + // Source course. |
| 893 | + $sourcecourseid = $this->course->id; |
| 894 | + $sourcecoursemodinfo = get_fast_modinfo($sourcecourseid); |
| 895 | + |
| 896 | + // Move modules around so that they are not in id order. |
| 897 | + $this->shuffle_modules(); |
| 898 | + |
| 899 | + // Select some random course modules from different sections to be duplicated. |
| 900 | + $selectedmoduleids[] = $sourcecoursemodinfo->get_sections()[1][0]; |
| 901 | + $selectedmoduleids[] = $sourcecoursemodinfo->get_sections()[1][1]; |
| 902 | + $selectedmoduleids[] = $sourcecoursemodinfo->get_sections()[3][0]; |
| 903 | + $selectedmoduleids[] = $sourcecoursemodinfo->get_sections()[3][2]; |
| 904 | + |
| 905 | + $selectedmodules = array_filter($this->get_test_course_modules(), function($module) use ($selectedmoduleids) { |
| 906 | + return in_array($module->id, $selectedmoduleids); |
| 907 | + }); |
| 908 | + |
| 909 | + // Target course. |
| 910 | + $targetcourseid = $this->setup_target_course_for_duplicating(3); |
| 911 | + $targetcoursemodinfo = get_fast_modinfo($targetcourseid); |
| 912 | + // Four sections (0 1 2 3). |
| 913 | + $this->assertCount(4, $targetcoursemodinfo->get_section_info_all()); |
| 914 | + // There is no module. |
| 915 | + $this->assertEmpty($targetcoursemodinfo->get_cms()); |
| 916 | + |
| 917 | + // Test keep origin section. |
| 918 | + actions::duplicate_to_course($selectedmodules, $targetcourseid, -1); |
| 919 | + $targetcoursemodinfo = get_fast_modinfo($targetcourseid); |
| 920 | + // Four sections (0 1 2 3). |
| 921 | + $this->assertCount(4, $targetcoursemodinfo->get_section_info_all()); |
| 922 | + // There is no module as we cannot keep origin section. |
| 923 | + $this->assertEmpty($targetcoursemodinfo->get_cms()); |
| 924 | + |
| 925 | + // Test create new section. |
| 926 | + actions::duplicate_to_course($selectedmodules, $targetcourseid, 4); |
| 927 | + $targetcoursemodinfo = get_fast_modinfo($targetcourseid); |
| 928 | + // Still four sections (0 1 2 3). |
| 929 | + $this->assertCount(4, $targetcoursemodinfo->get_section_info_all()); |
| 930 | + // And still no module. |
| 931 | + $this->assertEmpty($targetcoursemodinfo->get_cms()); |
| 932 | + |
| 933 | + // Duplicate to section 3, which is restricted by the hook. |
| 934 | + actions::duplicate_to_course($selectedmodules, $targetcourseid, 3); |
| 935 | + $targetcoursemodinfo = get_fast_modinfo($targetcourseid); |
| 936 | + // Still four sections (0 1 2 3). |
| 937 | + $this->assertCount(4, $targetcoursemodinfo->get_section_info_all()); |
| 938 | + // And still no module. |
| 939 | + $this->assertEmpty($targetcoursemodinfo->get_cms()); |
| 940 | + |
| 941 | + // Duplicate to section 1, this should work as normal. |
| 942 | + actions::duplicate_to_course($selectedmodules, $targetcourseid, 1); |
| 943 | + $targetcoursemodinfo = get_fast_modinfo($targetcourseid); |
| 944 | + $this->assertCount(4, $targetcoursemodinfo->get_section_info_all()); |
| 945 | + // There should be 4 modules now. |
| 946 | + $this->assertCount(4, $targetcoursemodinfo->get_cms()); |
| 947 | + // These module ids should be in section 1. |
| 948 | + $duplicatedmoduleids = $targetcoursemodinfo->get_sections()[1]; |
| 949 | + $this->assertCount(4, $duplicatedmoduleids); |
| 950 | + |
| 951 | + // Sort name of the modules to be able to compare them. |
| 952 | + $sourcemodulenames = []; |
| 953 | + foreach ($selectedmoduleids as $selectedmoduleid) { |
| 954 | + $sourcemodulenames[] = $sourcecoursemodinfo->get_cm($selectedmoduleid)->name; |
| 955 | + } |
| 956 | + sort($sourcemodulenames); |
| 957 | + |
| 958 | + // Sort name of the duplicated modules to be able to compare them. |
| 959 | + $duplicatedmodulenames = []; |
| 960 | + foreach ($duplicatedmoduleids as $moduleid) { |
| 961 | + $duplicatedmodulenames[] = $targetcoursemodinfo->get_cm($moduleid)->name; |
| 962 | + } |
| 963 | + sort($duplicatedmodulenames); |
| 964 | + |
| 965 | + // The names of the duplicated modules should be the same as the source module names. |
| 966 | + $this->assertEquals($sourcemodulenames, $duplicatedmodulenames); |
| 967 | + } |
863 | 968 | } |
0 commit comments