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