From 070bd1db6f3d67925c5f49feb94ae810c5a1f885 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Thu, 26 Sep 2013 17:47:50 +0800 Subject: [PATCH] MDL-41792 core_calendar: added missing abstract functions to test calendar --- calendar/tests/calendartype_test.php | 28 +++-- calendar/tests/calendartype_test_example.php | 113 +++++++++++++++++-- 2 files changed, 122 insertions(+), 19 deletions(-) diff --git a/calendar/tests/calendartype_test.php b/calendar/tests/calendartype_test.php index 9f117992c76..596c8382063 100644 --- a/calendar/tests/calendartype_test.php +++ b/calendar/tests/calendartype_test.php @@ -33,6 +33,9 @@ require_once($CFG->dirroot . '/calendar/tests/calendartype_test_example.php'); require_once($CFG->libdir . '/form/dateselector.php'); require_once($CFG->libdir . '/form/datetimeselector.php'); +// Used to test the calendar/lib.php functions. +require_once($CFG->dirroot . '/calendar/lib.php'); + // Used to test the user datetime profile field. require_once($CFG->dirroot . '/user/profile/lib.php'); require_once($CFG->dirroot . '/user/profile/definelib.php'); @@ -70,8 +73,8 @@ class core_calendar_type_testcase extends advanced_testcase { $this->resetAfterTest(); // Test setting it as the 'Test' calendar type. - $this->set_calendar_type('test'); - $this->assertEquals('test', \core_calendar\type_factory::get_calendar_type()); + $this->set_calendar_type('test_example'); + $this->assertEquals('test_example', \core_calendar\type_factory::get_calendar_type()); // Test setting it as the 'Gregorian' calendar type. $this->set_calendar_type('gregorian'); @@ -90,7 +93,7 @@ class core_calendar_type_testcase extends advanced_testcase { $this->core_functions_test('gregorian'); // Test that the core functions reproduce the same results as the test calendar. - $this->core_functions_test('test'); + $this->core_functions_test('test_example'); } /** @@ -120,7 +123,7 @@ class core_calendar_type_testcase extends advanced_testcase { $date2['hour'] = 0; // The dateselector element does not have hours. $date2['minute'] = 0; // The dateselector element does not have minutes. $date2['timestamp'] = 1372896000; - $this->convert_dateselector_to_unixtime_test('dateselector', 'test', $date2); + $this->convert_dateselector_to_unixtime_test('dateselector', 'test_example', $date2); $date3 = array(); $date3['day'] = 4; @@ -138,12 +141,12 @@ class core_calendar_type_testcase extends advanced_testcase { $date4['hour'] = 1; $date4['minute'] = 17; $date4['timestamp'] = 1372979700; - $this->convert_dateselector_to_unixtime_test('datetimeselector', 'test', $date4); + $this->convert_dateselector_to_unixtime_test('datetimeselector', 'test_example', $date4); // The date selector element values are set by using the function usergetdate, here we want to check that // the unixtime passed is being successfully converted to the correct values for the calendar type. $this->convert_unixtime_to_dateselector_test('gregorian', $date3); - $this->convert_unixtime_to_dateselector_test('test', $date4); + $this->convert_unixtime_to_dateselector_test('test_example', $date4); } /** @@ -166,7 +169,7 @@ class core_calendar_type_testcase extends advanced_testcase { // the year 1967 should be saved in the DB, as 1/1/1970 converts to 30/10/1967 in Gregorian. $date['expectedminyear'] = '1967'; $date['expectedmaxyear'] = '2010'; - $this->datetime_field_submission_test('test', $date); + $this->datetime_field_submission_test('test_example', $date); } /** @@ -183,6 +186,17 @@ class core_calendar_type_testcase extends advanced_testcase { // Test the userdate function. $this->assertEquals($calendar->timestamp_to_date_string($this->user->timecreated, '', 99, true, true), userdate($this->user->timecreated)); + + // Test the calendar/lib.php functions. + $this->assertEquals($calendar->get_weekdays(), calendar_get_days()); + $this->assertEquals($calendar->get_starting_weekday(), calendar_get_starting_weekday()); + $this->assertEquals($calendar->get_num_days_in_month('1986', '9'), calendar_days_in_month('1986', '9')); + $this->assertEquals($calendar->get_next_month('1986', '9'), calendar_add_month('1986', '9')); + $this->assertEquals($calendar->get_prev_month('1986', '9'), calendar_sub_month('1986', '9')); + + // Test the lib/moodle.php functions. + $this->assertEquals($calendar->get_num_days_in_month('1986', '9'), days_in_month('1986', '9')); + $this->assertEquals($calendar->get_weekday('1986', '9', '16'), dayofweek('16', '9', '1986')); } /** diff --git a/calendar/tests/calendartype_test_example.php b/calendar/tests/calendartype_test_example.php index 679422e9256..862cdc7b549 100644 --- a/calendar/tests/calendartype_test_example.php +++ b/calendar/tests/calendartype_test_example.php @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -namespace calendartype_test; +namespace calendartype_test_example; use \core_calendar\type_base; /** @@ -29,6 +29,15 @@ use \core_calendar\type_base; */ class structure extends type_base { + /** + * Returns the name of the calendar. + * + * @return string the calendar name + */ + public function get_name() { + return 'test_example'; + } + /** * Returns a list of all the possible days for all months. * @@ -84,10 +93,90 @@ class structure extends type_base { return 2050; } + /** + * Returns the number of days in a week. + * + * @return int the number of days + */ + public function get_num_weekdays() { + return 7; + } + + /** + * Returns an indexed list of all the names of the weekdays. + * + * The list starts with the index 0. Each index, representing a + * day, must be an array that contains the indexes 'shortname' + * and 'fullname'. + * + * @return array array of days + */ + public function get_weekdays() { + return ''; + } + + /** + * Returns the index of the starting week day. + * + * @return int + */ + public function get_starting_weekday() { + return ''; + } + + /** + * Returns the index of the weekday for a specific calendar date. + * + * @param int $year + * @param int $month + * @param int $day + * @return int + */ + public function get_weekday($year, $month, $day) { + return ''; + } + + /** + * Returns the number of days in a given month. + * + * @param int $year + * @param int $month + * @return int the number of days + */ + public function get_num_days_in_month($year, $month) { + return ''; + } + + /** + * Get the previous month. + * + * If the current month is January, it will get the last month of the previous year. + * + * @param int $year + * @param int $month + * @return array previous month and year + */ + public function get_prev_month($year, $month) { + return ''; + } + + /** + * Get the next month. + * + * If the current month is December, it will get the first month of the following year. + * + * @param int $year + * @param int $month + * @return array the following month and year + */ + public function get_next_month($year, $month) { + return ''; + } + /** * Returns a formatted string that represents a date in user time. * - * @param int $date the timestamp in UTC, as obtained from the database + * @param int $time the timestamp in UTC, as obtained from the database * @param string $format strftime format * @param int|float|string $timezone the timezone to use * {@link http://docs.moodle.org/dev/Time_API#Timezone} @@ -97,7 +186,7 @@ class structure extends type_base { * if false then the leading zero is maintained * @return string the formatted date/time */ - public function timestamp_to_date_string($date, $format, $timezone, $fixday, $fixhour) { + public function timestamp_to_date_string($time, $format, $timezone, $fixday, $fixhour) { return ''; } @@ -110,7 +199,7 @@ class structure extends type_base { * {@link http://docs.moodle.org/dev/Time_API#Timezone} * @return array an array that represents the date in user time */ - public function timestamp_to_date_array($time, $timezone) { + public function timestamp_to_date_array($time, $timezone = 99) { $gregoriancalendar = \core_calendar\type_factory::get_calendar_instance('gregorian'); $date = $gregoriancalendar->timestamp_to_date_array($time, $timezone); $newdate = $this->convert_from_gregorian($date['year'], $date['mon'], $date['mday'], @@ -143,10 +232,10 @@ class structure extends type_base { list($year, $month, $day, $hour, $minute) = explode('/', $date); return array('year' => (int) $year, - 'month' => (int) $month, - 'day' => (int) $day, - 'hour' => (int) $hour, - 'minute' => (int) $minute); + 'month' => (int) $month, + 'day' => (int) $day, + 'hour' => (int) $hour, + 'minute' => (int) $minute); } @@ -168,9 +257,9 @@ class structure extends type_base { list($year, $month, $day, $hour, $minute) = explode('/', $date); return array('year' => (int) $year, - 'month' => (int) $month, - 'day' => (int) $day, - 'hour' => (int) $hour, - 'minute' => (int) $minute); + 'month' => (int) $month, + 'day' => (int) $day, + 'hour' => (int) $hour, + 'minute' => (int) $minute); } }