From 64c45a292857e280dfb5f5ee6bed2d45b8eccd32 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Mon, 12 Sep 2016 11:56:04 +0800 Subject: [PATCH] MDL-55123 forms: correctly create elements in unittests --- calendar/tests/calendartype_test.php | 40 ++++++++++++++++++++++++++++---- lib/form/tests/dateselector_test.php | 8 +++---- lib/form/tests/datetimeselector_test.php | 8 +++---- lib/form/tests/duration_test.php | 40 ++++++++++++++++++++++++++------ 4 files changed, 76 insertions(+), 20 deletions(-) diff --git a/calendar/tests/calendartype_test.php b/calendar/tests/calendartype_test.php index c124477dc83..a8ccc858ddb 100644 --- a/calendar/tests/calendartype_test.php +++ b/calendar/tests/calendartype_test.php @@ -50,6 +50,8 @@ require_once($CFG->dirroot . '/user/profile/index_field_form.php'); * @since Moodle 2.6 */ class core_calendar_type_testcase extends advanced_testcase { + /** @var MoodleQuickForm Keeps reference of dummy form object */ + private $mform; /** * The test user. @@ -63,6 +65,10 @@ class core_calendar_type_testcase extends advanced_testcase { // The user we are going to test this on. $this->user = self::getDataGenerator()->create_user(); self::setUser($this->user); + + // Get form data. + $form = new temp_form_calendartype(); + $this->mform = $form->getform(); } /** @@ -216,15 +222,17 @@ class core_calendar_type_testcase extends advanced_testcase { private function convert_dateselector_to_unixtime_test($element, $type, $date) { $this->set_calendar_type($type); + static $counter = 0; + $counter++; + if ($element == 'dateselector') { - $el = new MoodleQuickForm_date_selector('dateselector', null, array('timezone' => 0.0, 'step' => 1)); + $el = $this->mform->addElement('date_selector', 'dateselector' . $counter, null, array('timezone' => 0.0, 'step' => 1)); } else { - $el = new MoodleQuickForm_date_time_selector('dateselector', null, array('timezone' => 0.0, 'step' => 1)); + $el = $this->mform->addElement('date_time_selector', 'dateselector' . $counter, null, array('timezone' => 0.0, 'step' => 1, 'optional' => false)); } - $el->_createElements(); - $submitvalues = array('dateselector' => $date); + $submitvalues = array('dateselector' . $counter => $date); - $this->assertSame($el->exportValue($submitvalues), array('dateselector' => $date['timestamp'])); + $this->assertSame($el->exportValue($submitvalues), array('dateselector' . $counter => $date['timestamp'])); } /** @@ -300,3 +308,25 @@ class core_calendar_type_testcase extends advanced_testcase { \core\session\manager::set_user($this->user); } } + +/** + * Form object to be used in test case. + */ +class temp_form_calendartype extends moodleform { + /** + * Form definition. + */ + public function definition() { + // No definition required. + } + /** + * Returns form reference + * @return MoodleQuickForm + */ + public function getform() { + $mform = $this->_form; + // Set submitted flag, to simulate submission. + $mform->_flagSubmitted = true; + return $mform; + } +} diff --git a/lib/form/tests/dateselector_test.php b/lib/form/tests/dateselector_test.php index f6e4e24198d..84727ed8a2f 100644 --- a/lib/form/tests/dateselector_test.php +++ b/lib/form/tests/dateselector_test.php @@ -129,8 +129,8 @@ class core_form_dateselector_testcase extends advanced_testcase { // Create dateselector element with different timezones. $elparams = array('optional'=>false, 'timezone' => $vals['timezone']); - $el = new MoodleQuickForm_date_selector('dateselector', null, $elparams); - $el->_createElements(); + $el = $this->mform->addElement('date_selector', 'dateselector', null, $elparams); + $this->assertTrue($el instanceof MoodleQuickForm_date_selector); $submitvalues = array('dateselector' => $vals); $this->assertSame(array('dateselector' => $vals['timestamp']), $el->exportValue($submitvalues), @@ -153,8 +153,8 @@ class core_form_dateselector_testcase extends advanced_testcase { // Create dateselector element with different timezones. $elparams = array('optional'=>false, 'timezone' => $vals['timezone']); - $el = new MoodleQuickForm_date_selector('dateselector', null, $elparams); - $el->_createElements(); + $el = $this->mform->addElement('date_selector', 'dateselector', null, $elparams); + $this->assertTrue($el instanceof MoodleQuickForm_date_selector); $expectedvalues = array( 'day' => array($vals['day']), 'month' => array($vals['month']), diff --git a/lib/form/tests/datetimeselector_test.php b/lib/form/tests/datetimeselector_test.php index ba188ee35b9..40b69c7b794 100644 --- a/lib/form/tests/datetimeselector_test.php +++ b/lib/form/tests/datetimeselector_test.php @@ -141,8 +141,8 @@ class core_form_datetimeselector_testcase extends advanced_testcase { // Create dateselector element with different timezones. $elparams = array('optional'=>false, 'timezone' => $vals['timezone']); - $el = new MoodleQuickForm_date_time_selector('dateselector', null, $elparams); - $el->_createElements(); + $el = $this->mform->addElement('date_time_selector', 'dateselector', null, $elparams); + $this->assertTrue($el instanceof MoodleQuickForm_date_time_selector); $submitvalues = array('dateselector' => $vals); $this->assertSame(array('dateselector' => $vals['timestamp']), $el->exportValue($submitvalues), @@ -165,8 +165,8 @@ class core_form_datetimeselector_testcase extends advanced_testcase { // Create dateselector element with different timezones. $elparams = array('optional'=>false, 'timezone' => $vals['timezone']); - $el = new MoodleQuickForm_date_time_selector('dateselector', null, $elparams); - $el->_createElements(); + $el = $this->mform->addElement('date_time_selector', 'dateselector', null, $elparams); + $this->assertTrue($el instanceof MoodleQuickForm_date_time_selector); $expectedvalues = array( 'day' => array($vals['day']), 'month' => array($vals['month']), diff --git a/lib/form/tests/duration_test.php b/lib/form/tests/duration_test.php index 3b7f20f9518..35900d9372a 100644 --- a/lib/form/tests/duration_test.php +++ b/lib/form/tests/duration_test.php @@ -41,6 +41,8 @@ require_once($CFG->libdir . '/form/duration.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class core_form_duration_testcase extends basic_testcase { + /** @var MoodleQuickForm Keeps reference of dummy form object */ + private $mform; /** @var MoodleQuickForm_duration Keeps reference of MoodleQuickForm_duration object */ private $element; @@ -49,7 +51,11 @@ class core_form_duration_testcase extends basic_testcase { */ protected function setUp() { parent::setUp(); - $this->element = new MoodleQuickForm_duration(); + + // Get form data. + $form = new temp_form_duration(); + $this->mform = $form->getform(); + $this->element = $this->mform->addElement('duration', 'duration'); } /** @@ -67,7 +73,7 @@ class core_form_duration_testcase extends basic_testcase { */ public function test_constructor() { // Test trying to create with an invalid unit. - $this->element = new MoodleQuickForm_duration('testel', null, array('defaultunit' => 123)); + $this->element = $this->mform->addElement('duration', 'testel', null, array('defaultunit' => 123, 'optional' => false)); } /** @@ -94,7 +100,7 @@ class core_form_duration_testcase extends basic_testcase { $this->assertEquals(array(1, 86400), $this->element->seconds_to_unit(86400)); $this->assertEquals(array(25, 3600), $this->element->seconds_to_unit(90000)); - $this->element = new MoodleQuickForm_duration('testel', null, array('defaultunit' => 86400)); + $this->element = $this->mform->addElement('duration', 'testel', null, array('defaultunit' => 86400, 'optional' => false)); $this->assertEquals(array(0, 86400), $this->element->seconds_to_unit(0)); // Zero minutes, for a nice default unit. } @@ -102,8 +108,7 @@ class core_form_duration_testcase extends basic_testcase { * Testcase to check generated timestamp */ public function test_exportValue() { - $el = new MoodleQuickForm_duration('testel'); - $el->_createElements(); + $el = $this->mform->addElement('duration', 'testel'); $values = array('testel' => array('number' => 10, 'timeunit' => 1)); $this->assertEquals(array('testel' => 10), $el->exportValue($values)); $values = array('testel' => array('number' => 3, 'timeunit' => 60)); @@ -117,11 +122,32 @@ class core_form_duration_testcase extends basic_testcase { $values = array('testel' => array('number' => 0, 'timeunit' => 3600)); $this->assertEquals(array('testel' => 0), $el->exportValue($values)); - $el = new MoodleQuickForm_duration('testel', null, array('optional' => true)); - $el->_createElements(); + $el = $this->mform->addElement('duration', 'testel', null, array('optional' => true)); $values = array('testel' => array('number' => 10, 'timeunit' => 1)); $this->assertEquals(array('testel' => 0), $el->exportValue($values)); $values = array('testel' => array('number' => 20, 'timeunit' => 1, 'enabled' => 1)); $this->assertEquals(array('testel' => 20), $el->exportValue($values)); } } + +/** + * Form object to be used in test case. + */ +class temp_form_duration extends moodleform { + /** + * Form definition. + */ + public function definition() { + // No definition required. + } + /** + * Returns form reference + * @return MoodleQuickForm + */ + public function getform() { + $mform = $this->_form; + // Set submitted flag, to simulate submission. + $mform->_flagSubmitted = true; + return $mform; + } +} -- 2.11.4.GIT