Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 25... Línea 25...
25
 * @package    enrol_database
25
 * @package    enrol_database
26
 * @category   test
26
 * @category   test
27
 * @copyright  2011 Petr Skoda {@link http://skodak.org}
27
 * @copyright  2011 Petr Skoda {@link http://skodak.org}
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
29
 */
30
class sync_test extends \advanced_testcase {
30
final class sync_test extends \advanced_testcase {
31
    protected static $courses = array();
31
    protected static $courses = array();
32
    protected static $users = array();
32
    protected static $users = array();
33
    protected static $roles = array();
33
    protected static $roles = array();
Línea 34... Línea 34...
34
 
34
 
Línea 42... Línea 42...
42
        if ($DB->get_dbfamily() === 'mssql') {
42
        if ($DB->get_dbfamily() === 'mssql') {
43
            sqlsrv_configure("WarningsReturnAsErrors", false);
43
            sqlsrv_configure("WarningsReturnAsErrors", false);
44
            sqlsrv_configure("LogSubsystems", SQLSRV_LOG_SYSTEM_OFF);
44
            sqlsrv_configure("LogSubsystems", SQLSRV_LOG_SYSTEM_OFF);
45
            sqlsrv_configure("LogSeverity", SQLSRV_LOG_SEVERITY_ERROR);
45
            sqlsrv_configure("LogSeverity", SQLSRV_LOG_SEVERITY_ERROR);
46
        }
46
        }
-
 
47
        parent::tearDownAfterClass();
47
    }
48
    }
Línea 48... Línea 49...
48
 
49
 
49
    protected function init_enrol_database() {
50
    protected function init_enrol_database() {
Línea 79... Línea 80...
79
                    }
80
                    }
80
                    set_config('dbtype', 'mysqli://'.rawurlencode($CFG->dbuser).':'.rawurlencode($CFG->dbpass).'@'.rawurlencode($CFG->dbhost).'/'.rawurlencode($CFG->dbname).'?socket='.rawurlencode($dbsocket), 'enrol_database');
81
                    set_config('dbtype', 'mysqli://'.rawurlencode($CFG->dbuser).':'.rawurlencode($CFG->dbpass).'@'.rawurlencode($CFG->dbhost).'/'.rawurlencode($CFG->dbname).'?socket='.rawurlencode($dbsocket), 'enrol_database');
81
                }
82
                }
82
                break;
83
                break;
Línea 83... Línea -...
83
 
-
 
84
            case 'oracle':
-
 
85
                set_config('dbtype', 'oci8po', 'enrol_database');
-
 
86
                set_config('dbsybasequoting', '1', 'enrol_database');
-
 
87
                break;
-
 
88
 
84
 
89
            case 'postgres':
85
            case 'postgres':
90
                set_config('dbtype', 'postgres7', 'enrol_database');
86
                set_config('dbtype', 'postgres7', 'enrol_database');
91
                $setupsql = "SET NAMES 'UTF-8'";
87
                $setupsql = "SET NAMES 'UTF-8'";
92
                if (!empty($CFG->dboptions['dbschema'])) {
88
                if (!empty($CFG->dboptions['dbschema'])) {
Línea 148... Línea 144...
148
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
144
        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
149
        $table->add_field('fullname', XMLDB_TYPE_CHAR, '255', null, null, null);
145
        $table->add_field('fullname', XMLDB_TYPE_CHAR, '255', null, null, null);
150
        $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null);
146
        $table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null);
151
        $table->add_field('idnumber', XMLDB_TYPE_CHAR, '255', null, null, null);
147
        $table->add_field('idnumber', XMLDB_TYPE_CHAR, '255', null, null, null);
152
        $table->add_field('category', XMLDB_TYPE_CHAR, '255', null, null, null);
148
        $table->add_field('category', XMLDB_TYPE_CHAR, '255', null, null, null);
-
 
149
        $table->add_field('startdate', XMLDB_TYPE_CHAR, '255', null, null, null);
-
 
150
        $table->add_field('enddate', XMLDB_TYPE_CHAR, '255', null, null, null);
153
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
151
        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
154
        if ($dbman->table_exists($table)) {
152
        if ($dbman->table_exists($table)) {
155
            $dbman->drop_table($table);
153
            $dbman->drop_table($table);
156
        }
154
        }
157
        $dbman->create_table($table);
155
        $dbman->create_table($table);
Línea 438... Línea 436...
438
 
436
 
Línea 439... Línea 437...
439
        $trace = new \null_progress_trace();
437
        $trace = new \null_progress_trace();
Línea 440... Línea -...
440
 
-
 
441
        // Test basic enrol sync for one user after login.
438
 
442
 
439
        // Test basic enrol sync for one user after login.
443
        $this->reset_enrol_database();
440
 
Línea 444... Línea 441...
444
        $plugin->set_config('localcoursefield', 'idnumber');
441
        $plugin->set_config('localcoursefield', 'idnumber');
Línea 796... Línea 793...
796
        $plugin->set_config('newcoursecategory', '');
793
        $plugin->set_config('newcoursecategory', '');
797
        $plugin->sync_courses($trace);
794
        $plugin->sync_courses($trace);
798
        $this->assertEquals(1+2+1+4+1+count(self::$courses), $DB->count_records('course'));
795
        $this->assertEquals(1+2+1+4+1+count(self::$courses), $DB->count_records('course'));
799
        $this->assertTrue($DB->record_exists('course', array('idnumber' => 'ncid9')));
796
        $this->assertTrue($DB->record_exists('course', array('idnumber' => 'ncid9')));
Línea 800... Línea -...
800
 
-
 
801
 
797
 
802
        // Final cleanup - remove extra tables, fixtures and caches.
798
        // Final cleanup - remove extra tables, fixtures and caches.
803
        $this->cleanup_enrol_database();
799
        $this->cleanup_enrol_database();
-
 
800
    }
-
 
801
 
-
 
802
    /**
-
 
803
     * Test syncing courses with start and end dates.
-
 
804
     *
-
 
805
     * @covers \enrol_database_plugin::sync_courses
-
 
806
     */
-
 
807
    public function test_sync_courses_start_end_dates(): void {
-
 
808
        global $DB;
-
 
809
 
-
 
810
        $this->resetAfterTest();
-
 
811
        $this->preventResetByRollback();
-
 
812
        $this->init_enrol_database();
-
 
813
 
-
 
814
        $courseconfig = get_config('moodlecourse');
-
 
815
        $nextyear = (int) date('Y') + 1;
-
 
816
        $prev = (int) date('Y') - 1;
-
 
817
 
-
 
818
        $midnightstartdate = usergetmidnight(time());
-
 
819
        $midnightenddate = usergetmidnight(time()) + $courseconfig->courseduration;
-
 
820
 
-
 
821
        $plugin = enrol_get_plugin('database');
-
 
822
 
-
 
823
        $trace = new \null_progress_trace();
-
 
824
 
-
 
825
        $course1 = ['fullname' => 'C1', 'shortname' => 'c1', 'idnumber' => 'c1', 'startdate' => 0,
-
 
826
            'enddate' => 0];
-
 
827
        $course2 = ['fullname' => 'C2', 'shortname' => 'c2', 'idnumber' => 'c2', 'startdate' => null,
-
 
828
            'enddate' => null];
-
 
829
        // This course won't be created. Broken start date.
-
 
830
        $course3 = ['fullname' => 'C3', 'shortname' => 'c3', 'idnumber' => 'c3', 'startdate' => 'not date',
-
 
831
            'enddate' => 0];
-
 
832
        // This course won't be created. Broken end date.
-
 
833
        $course4 = ['fullname' => 'C4', 'shortname' => 'c4', 'idnumber' => 'c4', 'startdate' => 0,
-
 
834
            'enddate' => 'not date'];
-
 
835
        // This course won't be created. Start date after end date.
-
 
836
        $course5 = ['fullname' => 'C5', 'shortname' => 'c5', 'idnumber' => 'c5', 'startdate' => '12.05.2024',
-
 
837
            'enddate' => '12.05.2021'];
-
 
838
        $course6 = ['fullname' => 'C6', 'shortname' => 'c6', 'idnumber' => 'c6', 'startdate' => '2024-05-22',
-
 
839
            'enddate' => '2027-05-12'];
-
 
840
        $course7 = ['fullname' => 'C7', 'shortname' => 'c7', 'idnumber' => 'c7', 'startdate' => null,
-
 
841
            'enddate' => '12.05.' . $nextyear];
-
 
842
        $course8 = ['fullname' => 'C8', 'shortname' => 'c8', 'idnumber' => 'c8', 'startdate' => '12.05.2024',
-
 
843
            'enddate' => null];
-
 
844
        // This course won't be created. Start date is not set, but it should be set to date after end date.
-
 
845
        $course9 = ['fullname' => 'C9', 'shortname' => 'c9', 'idnumber' => 'c9', 'startdate' => null,
-
 
846
            'enddate' => '12.05.' . $prev];
-
 
847
 
-
 
848
        $DB->insert_record('enrol_database_test_courses', $course1);
-
 
849
        $DB->insert_record('enrol_database_test_courses', $course2);
-
 
850
        $DB->insert_record('enrol_database_test_courses', $course3);
-
 
851
        $DB->insert_record('enrol_database_test_courses', $course4);
-
 
852
        $DB->insert_record('enrol_database_test_courses', $course5);
-
 
853
        $DB->insert_record('enrol_database_test_courses', $course6);
-
 
854
        $DB->insert_record('enrol_database_test_courses', $course7);
-
 
855
        $DB->insert_record('enrol_database_test_courses', $course8);
-
 
856
        $DB->insert_record('enrol_database_test_courses', $course9);
-
 
857
 
-
 
858
        $plugin->set_config('newcoursestartdate', 'startdate');
-
 
859
        $plugin->set_config('newcourseenddate', 'enddate');
-
 
860
 
-
 
861
        $plugin->sync_courses($trace);
-
 
862
 
-
 
863
        // Course 3, course 4, course 5 and course 9 should not be created.
-
 
864
        $this->assertTrue($DB->record_exists('course', ['shortname' => $course1['shortname']]));
-
 
865
        $this->assertTrue($DB->record_exists('course', ['shortname' => $course2['shortname']]));
-
 
866
        $this->assertFalse($DB->record_exists('course', ['shortname' => $course3['shortname']]));
-
 
867
        $this->assertFalse($DB->record_exists('course', ['shortname' => $course4['shortname']]));
-
 
868
        $this->assertFalse($DB->record_exists('course', ['shortname' => $course5['shortname']]));
-
 
869
        $this->assertTrue($DB->record_exists('course', ['shortname' => $course6['shortname']]));
-
 
870
        $this->assertTrue($DB->record_exists('course', ['shortname' => $course7['shortname']]));
-
 
871
        $this->assertTrue($DB->record_exists('course', ['shortname' => $course8['shortname']]));
-
 
872
        $this->assertFalse($DB->record_exists('course', ['shortname' => $course9['shortname']]));
-
 
873
 
-
 
874
        // Check dates for created courses.
-
 
875
        $this->assertEquals($midnightstartdate, $DB->get_field('course', 'startdate', ['shortname' => $course1['shortname']]));
-
 
876
        $this->assertEquals($midnightenddate, $DB->get_field('course', 'enddate', ['shortname' => $course1['shortname']]));
-
 
877
 
-
 
878
        $this->assertEquals($midnightstartdate, $DB->get_field('course', 'startdate', ['shortname' => $course2['shortname']]));
-
 
879
        $this->assertEquals($midnightenddate, $DB->get_field('course', 'enddate', ['shortname' => $course2['shortname']]));
-
 
880
 
-
 
881
        $this->assertEquals(strtotime('22.05.2024'), $DB->get_field('course', 'startdate', ['shortname' => $course6['shortname']]));
-
 
882
        $this->assertEquals(strtotime('12.05.2027'), $DB->get_field('course', 'enddate', ['shortname' => $course6['shortname']]));
-
 
883
 
-
 
884
        $this->assertEquals($midnightstartdate, $DB->get_field('course', 'startdate', ['shortname' => $course7['shortname']]));
-
 
885
        $expected = strtotime('12.05.' . $nextyear);
-
 
886
        $this->assertEquals($expected, $DB->get_field('course', 'enddate', ['shortname' => $course7['shortname']]));
-
 
887
 
-
 
888
        $this->assertEquals(strtotime('12.05.2024'), $DB->get_field('course', 'startdate', ['shortname' => $course8['shortname']]));
-
 
889
        $expected = strtotime('12.05.2024') + $courseconfig->courseduration;
-
 
890
        $this->assertEquals($expected, $DB->get_field('course', 'enddate', ['shortname' => $course8['shortname']]));
-
 
891
 
-
 
892
        // Push course with dates as timestamp.
-
 
893
        $course10 = ['fullname' => 'C10', 'shortname' => 'c10', 'idnumber' => 'c10', 'startdate' => 1810051200,
-
 
894
            'enddate' => 1810051211];
-
 
895
        $DB->insert_record('enrol_database_test_courses', $course10);
-
 
896
 
-
 
897
        $plugin->sync_courses($trace);
-
 
898
 
-
 
899
        $this->assertTrue($DB->record_exists('course', ['shortname' => $course10['shortname']]));
-
 
900
        $this->assertEquals(1810051200, $DB->get_field('course', 'startdate', ['shortname' => $course10['shortname']]));
-
 
901
        $this->assertEquals(1810051211, $DB->get_field('course', 'enddate', ['shortname' => $course10['shortname']]));
-
 
902
 
-
 
903
        // Push course with broken dates, but delete dates from plugin configuration before syncing.
-
 
904
        $course11 = ['fullname' => 'C11', 'shortname' => 'c11', 'idnumber' => 'c11', 'startdate' => 'not date',
-
 
905
            'enddate' => 'not date'];
-
 
906
        $DB->insert_record('enrol_database_test_courses', $course11);
-
 
907
 
-
 
908
        $plugin->set_config('newcoursestartdate', '');
-
 
909
        $plugin->set_config('newcourseenddate', '');
-
 
910
        $plugin->sync_courses($trace);
-
 
911
 
-
 
912
        $this->assertTrue($DB->record_exists('course', ['shortname' => $course11['shortname']]));
-
 
913
        $this->assertEquals($midnightstartdate, $DB->get_field('course', 'startdate', ['shortname' => $course11['shortname']]));
-
 
914
        $this->assertEquals($midnightenddate, $DB->get_field('course', 'enddate', ['shortname' => $course11['shortname']]));
-
 
915
 
-
 
916
        // Push courses with correct dates, but set date configuration to not existing date fields.
-
 
917
        $course12 = ['fullname' => 'C12', 'shortname' => 'c12', 'idnumber' => 'c12', 'startdate' => '2024-05-22',
-
 
918
            'enddate' => '2027-05-12'];
-
 
919
        $DB->insert_record('enrol_database_test_courses', $course11);
-
 
920
 
-
 
921
        $plugin->set_config('newcoursestartdate', 'startdate');
-
 
922
        $plugin->set_config('newcourseenddate', 'ed');
-
 
923
        $plugin->sync_courses($trace);
-
 
924
 
-
 
925
        // Course should not be synced to prevent setting up incorrect dates.
-
 
926
        $this->assertFalse($DB->record_exists('course', ['shortname' => $course12['shortname']]));
-
 
927
 
-
 
928
        $course13 = ['fullname' => 'C13', 'shortname' => 'c13', 'idnumber' => 'c13', 'startdate' => '2024-05-22',
-
 
929
            'enddate' => '2027-05-12'];
-
 
930
        $DB->insert_record('enrol_database_test_courses', $course11);
-
 
931
 
-
 
932
        $plugin->set_config('newcoursestartdate', 'sd');
-
 
933
        $plugin->set_config('newcourseenddate', 'enddate');
-
 
934
        $plugin->sync_courses($trace);
-
 
935
 
-
 
936
        // Course should not be synced to prevent setting up incorrect dates.
-
 
937
        $this->assertFalse($DB->record_exists('course', ['shortname' => $course13['shortname']]));
-
 
938
 
-
 
939
        $this->cleanup_enrol_database();
804
    }
940
    }