@@ -151,6 +151,8 @@ protected function init_enrol_database() {
151
151
$ table ->add_field ('shortname ' , XMLDB_TYPE_CHAR , '255 ' , null , null , null );
152
152
$ table ->add_field ('idnumber ' , XMLDB_TYPE_CHAR , '255 ' , null , null , null );
153
153
$ table ->add_field ('category ' , XMLDB_TYPE_CHAR , '255 ' , null , null , null );
154
+ $ table ->add_field ('startdate ' , XMLDB_TYPE_CHAR , '255 ' , null , null , null );
155
+ $ table ->add_field ('enddate ' , XMLDB_TYPE_CHAR , '255 ' , null , null , null );
154
156
$ table ->add_key ('primary ' , XMLDB_KEY_PRIMARY , array ('id ' ));
155
157
if ($ dbman ->table_exists ($ table )) {
156
158
$ dbman ->drop_table ($ table );
@@ -441,7 +443,6 @@ public function test_sync_users(): void {
441
443
442
444
// Test basic enrol sync for one user after login.
443
445
444
- $ this ->reset_enrol_database ();
445
446
$ plugin ->set_config ('localcoursefield ' , 'idnumber ' );
446
447
$ plugin ->set_config ('localuserfield ' , 'idnumber ' );
447
448
$ plugin ->set_config ('localrolefield ' , 'shortname ' );
@@ -799,8 +800,148 @@ public function test_sync_courses(): void {
799
800
$ this ->assertEquals (1 +2 +1 +4 +1 +count (self ::$ courses ), $ DB ->count_records ('course ' ));
800
801
$ this ->assertTrue ($ DB ->record_exists ('course ' , array ('idnumber ' => 'ncid9 ' )));
801
802
802
-
803
803
// Final cleanup - remove extra tables, fixtures and caches.
804
804
$ this ->cleanup_enrol_database ();
805
805
}
806
+
807
+ /**
808
+ * Test syncing courses with start and end dates.
809
+ *
810
+ * @covers \enrol_database_plugin::sync_courses
811
+ */
812
+ public function test_sync_courses_start_end_dates (): void {
813
+ global $ DB ;
814
+
815
+ $ this ->resetAfterTest ();
816
+ $ this ->preventResetByRollback ();
817
+ $ this ->init_enrol_database ();
818
+
819
+ $ courseconfig = get_config ('moodlecourse ' );
820
+ $ nextyear = (int ) date ('Y ' ) + 1 ;
821
+ $ prev = (int ) date ('Y ' ) - 1 ;
822
+
823
+ $ midnightstartdate = usergetmidnight (time ());
824
+ $ midnightenddate = usergetmidnight (time ()) + $ courseconfig ->courseduration ;
825
+
826
+ $ plugin = enrol_get_plugin ('database ' );
827
+
828
+ $ trace = new \null_progress_trace ();
829
+
830
+ $ course1 = ['fullname ' => 'C1 ' , 'shortname ' => 'c1 ' , 'idnumber ' => '' , 'startdate ' => 0 ,
831
+ 'enddate ' => 0 ];
832
+ $ course2 = ['fullname ' => 'C2 ' , 'shortname ' => 'c2 ' , 'idnumber ' => '' , 'startdate ' => null ,
833
+ 'enddate ' => null ];
834
+ // This course won't be created. Broken start date.
835
+ $ course3 = ['fullname ' => 'C3 ' , 'shortname ' => 'c3 ' , 'idnumber ' => '' , 'startdate ' => 'not date ' ,
836
+ 'enddate ' => 0 ];
837
+ // This course won't be created. Broken end date.
838
+ $ course4 = ['fullname ' => 'C4 ' , 'shortname ' => 'c4 ' , 'idnumber ' => '' , 'startdate ' => 0 ,
839
+ 'enddate ' => 'not date ' ];
840
+ // This course won't be created. Start date after end date.
841
+ $ course5 = ['fullname ' => 'C5 ' , 'shortname ' => 'c5 ' , 'idnumber ' => '' , 'startdate ' => '12.05.2024 ' ,
842
+ 'enddate ' => '12.05.2021 ' ];
843
+ $ course6 = ['fullname ' => 'C6 ' , 'shortname ' => 'c6 ' , 'idnumber ' => '' , 'startdate ' => '2024-05-22 ' ,
844
+ 'enddate ' => '2027-05-12 ' ];
845
+ $ course7 = ['fullname ' => 'C7 ' , 'shortname ' => 'c7 ' , 'idnumber ' => '' , 'startdate ' => null ,
846
+ 'enddate ' => '12.05. ' . $ nextyear ];
847
+ $ course8 = ['fullname ' => 'C8 ' , 'shortname ' => 'c8 ' , 'idnumber ' => '' , 'startdate ' => '12.05.2024 ' ,
848
+ 'enddate ' => null ];
849
+ // This course won't be created. Start date is not set, but it should be set to date after end date.
850
+ $ course9 = ['fullname ' => 'C9 ' , 'shortname ' => 'c9 ' , 'idnumber ' => '' , 'startdate ' => null ,
851
+ 'enddate ' => '12.05. ' . $ prev ];
852
+
853
+ $ DB ->insert_record ('enrol_database_test_courses ' , $ course1 );
854
+ $ DB ->insert_record ('enrol_database_test_courses ' , $ course2 );
855
+ $ DB ->insert_record ('enrol_database_test_courses ' , $ course3 );
856
+ $ DB ->insert_record ('enrol_database_test_courses ' , $ course4 );
857
+ $ DB ->insert_record ('enrol_database_test_courses ' , $ course5 );
858
+ $ DB ->insert_record ('enrol_database_test_courses ' , $ course6 );
859
+ $ DB ->insert_record ('enrol_database_test_courses ' , $ course7 );
860
+ $ DB ->insert_record ('enrol_database_test_courses ' , $ course8 );
861
+ $ DB ->insert_record ('enrol_database_test_courses ' , $ course9 );
862
+
863
+ // Mess with case as we need to check that fields are lower cased.
864
+ $ plugin ->set_config ('newcoursestartdate ' , 'StartDaTE ' );
865
+ $ plugin ->set_config ('newcourseenddate ' , 'ENDdATE ' );
866
+
867
+ $ plugin ->sync_courses ($ trace );
868
+
869
+ // Course 3, course 4, course 5 and course 9 should not be created.
870
+ $ this ->assertTrue ($ DB ->record_exists ('course ' , ['shortname ' => $ course1 ['shortname ' ]]));
871
+ $ this ->assertTrue ($ DB ->record_exists ('course ' , ['shortname ' => $ course2 ['shortname ' ]]));
872
+ $ this ->assertFalse ($ DB ->record_exists ('course ' , ['shortname ' => $ course3 ['shortname ' ]]));
873
+ $ this ->assertFalse ($ DB ->record_exists ('course ' , ['shortname ' => $ course4 ['shortname ' ]]));
874
+ $ this ->assertFalse ($ DB ->record_exists ('course ' , ['shortname ' => $ course5 ['shortname ' ]]));
875
+ $ this ->assertTrue ($ DB ->record_exists ('course ' , ['shortname ' => $ course6 ['shortname ' ]]));
876
+ $ this ->assertTrue ($ DB ->record_exists ('course ' , ['shortname ' => $ course7 ['shortname ' ]]));
877
+ $ this ->assertTrue ($ DB ->record_exists ('course ' , ['shortname ' => $ course8 ['shortname ' ]]));
878
+ $ this ->assertFalse ($ DB ->record_exists ('course ' , ['shortname ' => $ course9 ['shortname ' ]]));
879
+
880
+ // Check dates for created courses.
881
+ $ this ->assertEquals ($ midnightstartdate , $ DB ->get_field ('course ' , 'startdate ' , ['shortname ' => $ course1 ['shortname ' ]]));
882
+ $ this ->assertEquals ($ midnightenddate , $ DB ->get_field ('course ' , 'enddate ' , ['shortname ' => $ course1 ['shortname ' ]]));
883
+
884
+ $ this ->assertEquals ($ midnightstartdate , $ DB ->get_field ('course ' , 'startdate ' , ['shortname ' => $ course2 ['shortname ' ]]));
885
+ $ this ->assertEquals ($ midnightenddate , $ DB ->get_field ('course ' , 'enddate ' , ['shortname ' => $ course2 ['shortname ' ]]));
886
+
887
+ $ this ->assertEquals (strtotime ('22.05.2024 ' ), $ DB ->get_field ('course ' , 'startdate ' , ['shortname ' => $ course6 ['shortname ' ]]));
888
+ $ this ->assertEquals (strtotime ('12.05.2027 ' ), $ DB ->get_field ('course ' , 'enddate ' , ['shortname ' => $ course6 ['shortname ' ]]));
889
+
890
+ $ this ->assertEquals ($ midnightstartdate , $ DB ->get_field ('course ' , 'startdate ' , ['shortname ' => $ course7 ['shortname ' ]]));
891
+ $ expected = strtotime ('12.05. ' . $ nextyear );
892
+ $ this ->assertEquals ($ expected , $ DB ->get_field ('course ' , 'enddate ' , ['shortname ' => $ course7 ['shortname ' ]]));
893
+
894
+ $ this ->assertEquals (strtotime ('12.05.2024 ' ), $ DB ->get_field ('course ' , 'startdate ' , ['shortname ' => $ course8 ['shortname ' ]]));
895
+ $ expected = strtotime ('12.05.2024 ' ) + $ courseconfig ->courseduration ;
896
+ $ this ->assertEquals ($ expected , $ DB ->get_field ('course ' , 'enddate ' , ['shortname ' => $ course8 ['shortname ' ]]));
897
+
898
+ // Push course with dates as timestamp.
899
+ $ course10 = ['fullname ' => 'C10 ' , 'shortname ' => 'c10 ' , 'idnumber ' => '' , 'startdate ' => 1810051200 ,
900
+ 'enddate ' => 1810051211 ];
901
+ $ DB ->insert_record ('enrol_database_test_courses ' , $ course10 );
902
+
903
+ $ plugin ->sync_courses ($ trace );
904
+
905
+ $ this ->assertTrue ($ DB ->record_exists ('course ' , ['shortname ' => $ course10 ['shortname ' ]]));
906
+ $ this ->assertEquals (1810051200 , $ DB ->get_field ('course ' , 'startdate ' , ['shortname ' => $ course10 ['shortname ' ]]));
907
+ $ this ->assertEquals (1810051211 , $ DB ->get_field ('course ' , 'enddate ' , ['shortname ' => $ course10 ['shortname ' ]]));
908
+
909
+ // Push course with broken dates, but delete dates from plugin configuration before syncing.
910
+ $ course11 = ['fullname ' => 'C11 ' , 'shortname ' => 'c11 ' , 'idnumber ' => '' , 'startdate ' => 'not date ' ,
911
+ 'enddate ' => 'not date ' ];
912
+ $ DB ->insert_record ('enrol_database_test_courses ' , $ course11 );
913
+
914
+ $ plugin ->set_config ('newcoursestartdate ' , '' );
915
+ $ plugin ->set_config ('newcourseenddate ' , '' );
916
+ $ plugin ->sync_courses ($ trace );
917
+
918
+ $ this ->assertTrue ($ DB ->record_exists ('course ' , ['shortname ' => $ course11 ['shortname ' ]]));
919
+ $ this ->assertEquals ($ midnightstartdate , $ DB ->get_field ('course ' , 'startdate ' , ['shortname ' => $ course11 ['shortname ' ]]));
920
+ $ this ->assertEquals ($ midnightenddate , $ DB ->get_field ('course ' , 'enddate ' , ['shortname ' => $ course11 ['shortname ' ]]));
921
+
922
+ // Push courses with correct dates, but set date configuration to not existing date fields.
923
+ $ course12 = ['fullname ' => 'C12 ' , 'shortname ' => 'c12 ' , 'idnumber ' => '' , 'startdate ' => '2024-05-22 ' ,
924
+ 'enddate ' => '2027-05-12 ' ];
925
+ $ DB ->insert_record ('enrol_database_test_courses ' , $ course11 );
926
+
927
+ $ plugin ->set_config ('newcoursestartdate ' , 'startdate ' );
928
+ $ plugin ->set_config ('newcourseenddate ' , 'ed ' );
929
+ $ plugin ->sync_courses ($ trace );
930
+
931
+ // Course should not be synced to prevent setting up incorrect dates.
932
+ $ this ->assertFalse ($ DB ->record_exists ('course ' , ['shortname ' => $ course12 ['shortname ' ]]));
933
+
934
+ $ course13 = ['fullname ' => 'C13 ' , 'shortname ' => 'c13 ' , 'idnumber ' => '' , 'startdate ' => '2024-05-22 ' ,
935
+ 'enddate ' => '2027-05-12 ' ];
936
+ $ DB ->insert_record ('enrol_database_test_courses ' , $ course11 );
937
+
938
+ $ plugin ->set_config ('newcoursestartdate ' , 'sd ' );
939
+ $ plugin ->set_config ('newcourseenddate ' , 'enddate ' );
940
+ $ plugin ->sync_courses ($ trace );
941
+
942
+ // Course should not be synced to prevent setting up incorrect dates.
943
+ $ this ->assertFalse ($ DB ->record_exists ('course ' , ['shortname ' => $ course13 ['shortname ' ]]));
944
+
945
+ $ this ->cleanup_enrol_database ();
946
+ }
806
947
}
0 commit comments