diff --git a/admin/tool/uploadcourse/tests/course_test.php b/admin/tool/uploadcourse/tests/course_test.php index a15de21e3fb..98a37f35491 100644 --- a/admin/tool/uploadcourse/tests/course_test.php +++ b/admin/tool/uploadcourse/tests/course_test.php @@ -244,8 +244,6 @@ class tool_uploadcourse_course_testcase extends advanced_testcase { 'fullname' => 'Fullname', 'category' => '1', 'visible' => '0', - 'startdate' => '8 June 1990', - 'enddate' => '18 June 1990', 'idnumber' => '123abc', 'summary' => 'Summary', 'format' => 'weeks', @@ -271,6 +269,20 @@ class tool_uploadcourse_course_testcase extends advanced_testcase { 'enrolment_3_disable' => '1', ); + // There should be a start date if there is a end date. + $data['enddate'] = '7 June 1990'; + $co = new tool_uploadcourse_course($mode, $updatemode, $data); + $this->assertFalse($co->prepare()); + $this->assertArrayHasKey('nostartdatenoenddate', $co->get_errors()); + + $data['startdate'] = '8 June 1990'; + $co = new tool_uploadcourse_course($mode, $updatemode, $data); + $this->assertFalse($co->prepare()); + $this->assertArrayHasKey('enddatebeforestartdate', $co->get_errors()); + + // They are correct now. + $data['enddate'] = '18 June 1990'; + $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1'))); $co = new tool_uploadcourse_course($mode, $updatemode, $data); $this->assertTrue($co->prepare()); @@ -335,8 +347,6 @@ class tool_uploadcourse_course_testcase extends advanced_testcase { 'fullname' => 'Fullname 2', 'category' => $cat->id, 'visible' => '1', - 'startdate' => '11 June 1984', - 'enddate' => '31 June 1984', 'idnumber' => 'changeidn', 'summary' => 'Summary 2', 'format' => 'topics', @@ -363,6 +373,21 @@ class tool_uploadcourse_course_testcase extends advanced_testcase { ); $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1'))); + + $data['enddate'] = '31 June 1984'; + // Previous start and end dates are 8 and 18 June 1990. + $co = new tool_uploadcourse_course($mode, $updatemode, $data); + $this->assertFalse($co->prepare()); + $this->assertArrayHasKey('enddatebeforestartdate', $co->get_errors()); + + $data['startdate'] = '19 June 1990'; + $co = new tool_uploadcourse_course($mode, $updatemode, $data); + $this->assertFalse($co->prepare()); + $this->assertArrayHasKey('enddatebeforestartdate', $co->get_errors()); + + // They are correct now. + $data['startdate'] = '11 June 1984'; + $co = new tool_uploadcourse_course($mode, $updatemode, $data); $this->assertTrue($co->prepare()); $co->proceed(); diff --git a/backup/moodle2/tests/moodle2_test.php b/backup/moodle2/tests/moodle2_test.php index 52310825a21..a3b055a9b1a 100644 --- a/backup/moodle2/tests/moodle2_test.php +++ b/backup/moodle2/tests/moodle2_test.php @@ -340,7 +340,9 @@ class core_backup_moodle2_testcase extends advanced_testcase { // Create a course with specific start date. $generator = $this->getDataGenerator(); $course = $generator->create_course(array( - 'startdate' => strtotime('1 Jan 2014 00:00 GMT'))); + 'startdate' => strtotime('1 Jan 2014 00:00 GMT'), + 'enddate' => strtotime('3 Aug 2014 00:00 GMT') + )); // Add a forum with conditional availability date restriction, including // one of them nested inside a tree. @@ -361,6 +363,9 @@ class core_backup_moodle2_testcase extends advanced_testcase { // Do backup and restore. $newcourseid = $this->backup_and_restore($course, strtotime('3 Jan 2015 00:00 GMT')); + $newcourse = $DB->get_record('course', array('id' => $newcourseid)); + $this->assertEquals(strtotime('5 Aug 2015 00:00 GMT'), $newcourse->enddate); + $modinfo = get_fast_modinfo($newcourseid); // Check forum dates are modified by the same amount as the course start. diff --git a/course/format/topics/tests/format_topics_test.php b/course/format/topics/tests/format_topics_test.php index 45e8c008645..5e477df3e08 100644 --- a/course/format/topics/tests/format_topics_test.php +++ b/course/format/topics/tests/format_topics_test.php @@ -211,4 +211,43 @@ class format_topics_testcase extends advanced_testcase { $this->assertEquals(1, preg_match('/^Can not find data record in database/', $e->getMessage())); } } + + /** + * Test get_default_course_enddate. + * + * @return void + */ + public function test_default_course_enddate() { + global $CFG, $DB; + + $this->resetAfterTest(true); + + require_once($CFG->dirroot . '/course/tests/fixtures/testable_course_edit_form.php'); + + $this->setTimezone('UTC'); + + $params = array('format' => 'topics', 'numsections' => 5, 'startdate' => 1445644800); + $course = $this->getDataGenerator()->create_course($params); + $category = $DB->get_record('course_categories', array('id' => $course->category)); + + $args = [ + 'course' => $course, + 'category' => $category, + 'editoroptions' => [ + 'context' => context_course::instance($course->id), + 'subdirs' => 0 + ], + 'returnto' => new moodle_url('/'), + 'returnurl' => new moodle_url('/'), + ]; + + $courseform = new testable_course_edit_form(null, $args); + $courseform->definition_after_data(); + + $enddate = $params['startdate'] + get_config('moodlecourse', 'courseduration'); + + $weeksformat = course_get_format($course->id); + $this->assertEquals($enddate, $weeksformat->get_default_course_enddate($courseform->get_quick_form())); + + } } diff --git a/course/format/weeks/tests/format_weeks_test.php b/course/format/weeks/tests/format_weeks_test.php index a22ce026d79..481caa771db 100644 --- a/course/format/weeks/tests/format_weeks_test.php +++ b/course/format/weeks/tests/format_weeks_test.php @@ -217,4 +217,44 @@ class format_weeks_testcase extends advanced_testcase { $this->assertEquals(1, preg_match('/^Can not find data record in database/', $e->getMessage())); } } + + /** + * Test get_default_course_enddate. + * + * @return void + */ + public function test_default_course_enddate() { + global $CFG, $DB; + + $this->resetAfterTest(true); + + require_once($CFG->dirroot . '/course/tests/fixtures/testable_course_edit_form.php'); + + $this->setTimezone('UTC'); + + $params = array('format' => 'weeks', 'numsections' => 5, 'startdate' => 1445644800); + $course = $this->getDataGenerator()->create_course($params); + $category = $DB->get_record('course_categories', array('id' => $course->category)); + + $args = [ + 'course' => $course, + 'category' => $category, + 'editoroptions' => [ + 'context' => context_course::instance($course->id), + 'subdirs' => 0 + ], + 'returnto' => new moodle_url('/'), + 'returnurl' => new moodle_url('/'), + ]; + + $courseform = new testable_course_edit_form(null, $args); + $courseform->definition_after_data(); + + // format_weeks::get_section_dates is adding 2h to avoid DST problems, we need to replicate it here. + $enddate = $params['startdate'] + (WEEKSECS * $params['numsections']) + 7200; + + $weeksformat = course_get_format($course->id); + $this->assertEquals($enddate, $weeksformat->get_default_course_enddate($courseform->get_quick_form())); + } + } diff --git a/course/tests/behat/course_creation.feature b/course/tests/behat/course_creation.feature index 42ea5014ae0..3b2b0ff0171 100644 --- a/course/tests/behat/course_creation.feature +++ b/course/tests/behat/course_creation.feature @@ -32,8 +32,8 @@ Feature: Managers can create courses Scenario: Create a course from the management interface and return to it Given the following "courses" exist: - | fullname | shortname | idnumber | - | Course 1 | Course 1 | C1 | + | fullname | shortname | idnumber | startdate | enddate | + | Course 1 | Course 1 | C1 | 957139200 | 960163200 | And I log in as "admin" And I go to the courses management page And I should see the "Categories" management page @@ -44,6 +44,24 @@ Feature: Managers can create courses | Course full name | Course 2 | | Course short name | Course 2 | | Course summary | Course 2 summary | + | id_startdate_day | 24 | + | id_startdate_month | October | + | id_startdate_year | 2015 | + | id_enddate_day | 24 | + | id_enddate_month | October | + | id_enddate_year | 2016 | And I press "Save and return" Then I should see the "Course categories and courses" management page And I should see course listing "Course 1" before "Course 2" + And I follow "Course 2" + And I click on "Edit" "link" in the ".course-detail" "css_element" + And the following fields match these values: + | Course full name | Course 2 | + | Course short name | Course 2 | + | Course summary | Course 2 summary | + | id_startdate_day | 24 | + | id_startdate_month | October | + | id_startdate_year | 2015 | + | id_enddate_day | 24 | + | id_enddate_month | October | + | id_enddate_year | 2016 | diff --git a/course/tests/courselib_test.php b/course/tests/courselib_test.php index ce3105ff702..6b3001279a8 100644 --- a/course/tests/courselib_test.php +++ b/course/tests/courselib_test.php @@ -3137,4 +3137,164 @@ class core_course_courselib_testcase extends advanced_testcase { $this->expectExceptionMessage(get_string('invalidcourse', 'error')); update_course($course); } + + /** + * test_course_enddate + * + * @dataProvider course_enddate_provider + * @param int $startdate + * @param int $enddate + * @param string $errorcode + */ + public function test_course_enddate($startdate, $enddate, $errorcode) { + + $this->resetAfterTest(true); + + $record = array('startdate' => $startdate, 'enddate' => $enddate); + try { + $course1 = $this->getDataGenerator()->create_course($record); + if ($errorcode !== false) { + $this->fail('Expected exception with "' . $errorcode . '" error code in create_create'); + } + } catch (moodle_exception $e) { + if ($errorcode === false) { + $this->fail('Got "' . $errorcode . '" exception error code and no exception was expected'); + } + if ($e->errorcode != $errorcode) { + $this->fail('Got "' . $e->errorcode. '" exception error code and "' . $errorcode . '" was expected'); + } + return; + } + + $this->assertEquals($startdate, $course1->startdate); + $this->assertEquals($enddate, $course1->enddate); + } + + /** + * Provider for test_course_enddate. + * + * @return array + */ + public function course_enddate_provider() { + // Each provided example contains startdate, enddate and the expected exception error code if there is any. + return [ + [ + 111, + 222, + false + ], [ + 222, + 111, + 'enddatebeforestartdate' + ], [ + 111, + 0, + false + ], [ + 0, + 222, + 'nostartdatenoenddate' + ] + ]; + } + + + /** + * test_course_reset + * + * @dataProvider course_dates_reset_provider + * @param int $startdate + * @param int $enddate + * @param int $resetstartdate + * @param int $resetenddate + * @param int $resultingstartdate + * @param int $resultingenddate + */ + public function test_course_dates_reset($startdate, $enddate, $resetstartdate, $resetenddate, $resultingstartdate, $resultingenddate) { + global $DB; + + $this->resetAfterTest(true); + + $this->setTimezone('UTC'); + + $record = array('startdate' => $startdate, 'enddate' => $enddate); + $originalcourse = $this->getDataGenerator()->create_course($record); + + $resetdata = new stdClass(); + $resetdata->id = $originalcourse->id; + $resetdata->reset_start_date_old = $originalcourse->startdate; + $resetdata->reset_start_date = $resetstartdate; + $resetdata->reset_end_date = $resetenddate; + $resetdata->reset_end_date_old = $record['enddate']; + reset_course_userdata($resetdata); + + $course = $DB->get_record('course', array('id' => $originalcourse->id)); + + $this->assertEquals($resultingstartdate, $course->startdate); + $this->assertEquals($resultingenddate, $course->enddate); + } + + /** + * Provider for test_course_dates_reset. + * + * @return array + */ + public function course_dates_reset_provider() { + + // Each example contains the following: + // - course startdate + // - course enddate + // - startdate to reset to (false if not reset) + // - enddate to reset to (false if not reset) + // - resulting startdate + // - resulting enddate + $time = 1445644800; + return [ + // No date changes. + [ + $time, + $time + DAYSECS, + false, + false, + $time, + $time + DAYSECS + ], + // End date changes to a valid value. + [ + $time, + $time + DAYSECS, + false, + $time + DAYSECS + 111, + $time, + $time + DAYSECS + 111 + ], + // Start date changes to a valid value. End date does not get updated because it does not have value. + [ + $time, + 0, + $time + DAYSECS, + false, + $time + DAYSECS, + 0 + ], + // Start date changes to a valid value. End date gets updated accordingly. + [ + $time, + $time + DAYSECS, + $time + WEEKSECS, + false, + $time + WEEKSECS, + $time + WEEKSECS + DAYSECS + ], + // Start date and end date change to a valid value. + [ + $time, + $time + DAYSECS, + $time + WEEKSECS, + $time + YEARSECS, + $time + WEEKSECS, + $time + YEARSECS + ] + ]; + } } diff --git a/course/tests/fixtures/testable_course_edit_form.php b/course/tests/fixtures/testable_course_edit_form.php new file mode 100644 index 00000000000..9d3f677ef7a --- /dev/null +++ b/course/tests/fixtures/testable_course_edit_form.php @@ -0,0 +1,16 @@ +dirroot . '/course/edit_form.php'); + +class testable_course_edit_form extends course_edit_form { + + /** + * Expose the internal moodleform's MoodleQuickForm + * + * @return MoodleQuickForm + */ + public function get_quick_form() { + return $this->_form; + } +}