From fafb0159d8e9f017118eba0ffade60023e3e8ef3 Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Thu, 16 Aug 2018 11:37:12 +1000 Subject: [PATCH] Add tests to support functionality (#185) --- .../daterange/tests/phpunit/element_test.php | 212 ++++++++++++++++++ .../fixtures/fake_datarange_element.php | 40 ++++ 2 files changed, 252 insertions(+) create mode 100644 element/daterange/tests/phpunit/element_test.php create mode 100644 element/daterange/tests/phpunit/fixtures/fake_datarange_element.php diff --git a/element/daterange/tests/phpunit/element_test.php b/element/daterange/tests/phpunit/element_test.php new file mode 100644 index 0000000..f709dbc --- /dev/null +++ b/element/daterange/tests/phpunit/element_test.php @@ -0,0 +1,212 @@ +. + +/** + * Test datarange element. + * + * @package customcertelement_daterange + * @copyright 2018 Dmitrii Metelkin + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +require_once($CFG->dirroot . '/mod/customcert/element/daterange/tests/phpunit/fixtures/fake_datarange_element.php'); + +class customcertelement_daterange_element_test extends advanced_testcase { + + /** + * Helper function to build element data. + * + * @param stdClass $data Element data. + * + * @return object + */ + protected function build_element_data(stdClass $data) { + return (object) [ + 'id' => 1, + 'pageid' => 1, + 'name' => 'Test', + 'data' => json_encode($data), + 'font' => 'Font', + 'fontsize' => 1, + 'colour' => '#EEE', + 'posx' => 0, + 'posy' => 0, + 'width' => 100, + 'refpoint' => 1 + ]; + } + + /** + * Helper function to build datarange data. + * + * @param array $dataranges A list of dataranges. + * @param string $fallbackstring Fall back string. + * + * @return object + */ + protected function build_datarange_data(array $dataranges, $fallbackstring = '') { + return (object) [ + 'dateitem' => 1, + 'fallbackstring' => $fallbackstring, + 'numranges' => count($dataranges), + 'dateranges' => $dataranges, + ]; + } + + /** + * A helper function to get datarange element for testing. + * + * @param array $dataranges A list of dataranges. + * @param string $fallbackstring Fall back strin + * + * @return \fake_datarange_element + */ + protected function get_datarange_element(array $dataranges, $fallbackstring = '') { + $datarangedata = $this->build_datarange_data($dataranges, $fallbackstring); + $elementdata = $this->build_element_data($datarangedata); + + return new fake_datarange_element($elementdata); + } + + /** + * Data provider for test_get_daterange_string_for_recurring_ranges. + * @return array + */ + public function get_test_get_daterange_string_for_recurring_ranges_data_provider() { + return [ + ['1.11.2016', 'WS 2016/2017'], + ['1.11.2017', 'WS 2017/2018'], + ['1.11.2018', 'WS 2018/2019'], + ['1.11.2019', 'WS 2019/2020'], + ['1.02.2017', 'WS 2016/2017'], + ['1.02.2018', 'WS 2017/2018'], + ['1.02.2019', 'WS 2018/2019'], + ['1.02.2020', 'WS 2019/2020'], + ['1.05.2016', 'SS 2016'], + ['1.05.2017', 'SS 2017'], + ['1.05.2018', 'SS 2018'], + ['1.05.2019', 'SS 2019'], + ]; + } + + /** + * Test get correct strings for recurring ranges. + * + * @dataProvider get_test_get_daterange_string_for_recurring_ranges_data_provider + * + * @param string $date Date to test. + * @param string $expected Expected result. + */ + public function test_get_daterange_string_for_recurring_ranges($date, $expected) { + $dateranges = [ + (object)[ + 'startdate' => strtotime('01.04.2017'), + 'enddate' => strtotime('30.09.2017'), + 'datestring' => 'SS {{date_year}}', + 'recurring' => true, + 'enabled' => true, + ], + (object)[ + 'startdate' => strtotime('01.10.2017'), + 'enddate' => strtotime('31.03.2018'), + 'datestring' => 'WS {{first_year}}/{{last_year}}', + 'recurring' => true, + 'enabled' => true, + ], + ]; + + $element = $this->get_datarange_element($dateranges); + $date = strtotime($date); + $this->assertEquals($expected, $element->get_daterange_string($date)); + } + + /** + * Test that first found element matched. + */ + public function test_that_first_matched_range_applied_first() { + $dateranges = [ + (object)[ + 'startdate' => strtotime('01.04.2017'), + 'enddate' => strtotime('30.09.2017'), + 'datestring' => 'First range', + 'recurring' => false, + 'enabled' => true, + ], + (object)[ + 'startdate' => strtotime('01.05.2017'), + 'enddate' => strtotime('01.07.2018'), + 'datestring' => 'Second range', + 'recurring' => false, + 'enabled' => true, + ], + ]; + + $element = $this->get_datarange_element($dateranges); + $date = strtotime('1.06.2017'); + $this->assertEquals('First range', $element->get_daterange_string($date)); + } + + /** + * Test that placeholders correctly applied to matched range and fall back string. + */ + public function test_placeholders_and_fall_back_string() { + $dateranges = [ + (object)[ + 'startdate' => strtotime('01.04.2017'), + 'enddate' => strtotime('30.09.2018'), + 'datestring' => '{{current_year}} - {{first_year}} - {{last_year}} - {{date_year}}', + 'recurring' => false, + 'enabled' => true, + ], + ]; + + $fallbackstring = '{{current_year}} - {{first_year}} - {{last_year}} - {{date_year}}'; + $element = $this->get_datarange_element($dateranges, $fallbackstring); + + $date = strtotime('1.01.2000'); + $expected = date('Y', time()) . ' - {{first_year}} - {{last_year}} - 2000'; + $this->assertEquals($expected, $element->get_daterange_string($date)); + + $date = strtotime('1.07.2017'); + $expected = date('Y', time()) . ' - 2017 - 2018 - 2017'; + $this->assertEquals($expected, $element->get_daterange_string($date)); + } + + /** + * Test that nothing will be displayed if not matched and empty fall back string. + */ + public function test_nothing_will_be_displayed_if_empty_fallback_string() { + $dateranges = [ + (object)[ + 'startdate' => strtotime('01.04.2017'), + 'enddate' => strtotime('30.09.2018'), + 'datestring' => '{{current_year}} - {{first_year}} - {{last_year}} - {{date_year}}', + 'recurring' => false, + 'enabled' => true, + ], + ]; + + $fallbackstring = ''; + $element = $this->get_datarange_element($dateranges, $fallbackstring); + + $date = strtotime('1.07.2011'); + $this->assertEquals($fallbackstring, $element->get_daterange_string($date)); + } + +} diff --git a/element/daterange/tests/phpunit/fixtures/fake_datarange_element.php b/element/daterange/tests/phpunit/fixtures/fake_datarange_element.php new file mode 100644 index 0000000..ff6a18c --- /dev/null +++ b/element/daterange/tests/phpunit/fixtures/fake_datarange_element.php @@ -0,0 +1,40 @@ +. + +/** + * Fake datarange element for testing. + * + * @package customcertelement_daterange + * @copyright 2018 Dmitrii Metelkin + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +class fake_datarange_element extends \customcertelement_daterange\element { + + /** + * Override protected method for testing. + * + * @param int $date + * + * @return string + */ + public function get_daterange_string($date) { + $result = parent::get_daterange_string($date); + return $result; + } +}