Skip to content

Commit

Permalink
MDL-41022 cleanup form tests
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Aug 24, 2013
1 parent 5f15f7c commit 1eb1355
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 100 deletions.
75 changes: 38 additions & 37 deletions lib/form/tests/dateselector_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.


/**
* Unit tests for dateselector form element
*
Expand All @@ -32,28 +31,6 @@
require_once($CFG->libdir . '/form/dateselector.php');
require_once($CFG->libdir.'/formslib.php');

/**
* Form object to be used in test case.
*/
class temp_form_date extends moodleform {
/**
* Form defination.
*/
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;
}
}

/**
* Unit tests for MoodleQuickForm_date_selector
*
Expand All @@ -64,7 +41,7 @@ public function getform() {
* @copyright 2012 Rajesh Taneja
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class dateselector_form_element_testcase extends basic_testcase {
class core_form_dateselector_testcase extends basic_testcase {
/** @var MoodleQuickForm Keeps reference of dummy form object */
private $mform;
/** @var stdClass saves current user data */
Expand All @@ -83,12 +60,13 @@ class dateselector_form_element_testcase extends basic_testcase {
/**
* Initalize test wide variable, it is called in start of the testcase
*/
public function setUp() {
// Get form data
protected function setUp() {
parent::setUp();
// Get form data.
$form = new temp_form_date();
$this->mform = $form->getform();

// Set test values
// Set test values.
$this->testvals = array(
array (
'day' => 1,
Expand Down Expand Up @@ -145,8 +123,9 @@ public function setUp() {
* Clears the data set in the setUp() method call.
* @see dateselector_form_element_testcase::setUp()
*/
public function tearDown() {
protected function tearDown() {
unset($this->testvals);
parent::tearDown();
}

/**
Expand All @@ -169,11 +148,11 @@ public function test_exportvalue() {
$el->_createElements();
$submitvalues = array('dateselector' => $vals);

$this->assertSame($el->exportValue($submitvalues), array('dateselector' => $vals['timestamp']),
$this->assertSame(array('dateselector' => $vals['timestamp']), $el->exportValue($submitvalues),
"Please check if timezones are updated (Site adminstration -> location -> update timezone)");
}

// Restore user orignal timezone.
// Restore user original timezone.
$this->restoretimezone();
}

Expand All @@ -183,7 +162,7 @@ public function test_exportvalue() {
public function test_onquickformevent() {
global $USER;
$testvals = $this->testvals;
// Get dummy form for data
// Get dummy form for data.
$mform = $this->mform;
// Set timezone to Australia/Perth for testing.
$this->settimezone();
Expand All @@ -202,20 +181,20 @@ public function test_onquickformevent() {
);
$mform->_submitValues = array('dateselector' => $vals['timestamp']);
$el->onQuickFormEvent('updateValue', null, $mform);
$this->assertSame($el->getValue(), $expectedvalues);
$this->assertSame($expectedvalues, $el->getValue());
}

// Restore user orignal timezone.
// Restore user original timezone.
$this->restoretimezone();
}

/**
* Set user timezone to Australia/Perth for testing
* Set user timezone to Australia/Perth for testing.
*/
private function settimezone() {
global $USER, $CFG, $DB;
$this->olduser = $USER;
$USER = $DB->get_record('user', array('id'=>2)); //admin
$USER = $DB->get_record('user', array('id'=>2)); // Admin.

// Check if forcetimezone is set then save it and set it to use user timezone.
$this->cfgforcetimezone = null;
Expand All @@ -239,7 +218,7 @@ private function settimezone() {
}

/**
* Restore user timezone to orignal state
* Restore user timezone to original state
*/
private function restoretimezone() {
global $USER, $CFG;
Expand All @@ -257,4 +236,26 @@ private function restoretimezone() {

$USER = $this->olduser;
}
}
}

/**
* Form object to be used in test case.
*/
class temp_form_date 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;
}
}
71 changes: 36 additions & 35 deletions lib/form/tests/datetimeselector_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.


/**
* Unit tests for datetimeselector form element
*
Expand All @@ -32,28 +31,6 @@
require_once($CFG->libdir . '/form/datetimeselector.php');
require_once($CFG->libdir.'/formslib.php');

/**
* Form object to be used in test case
*/
class temp_form_datetime extends moodleform {
/**
* Form defination.
*/
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;
}
}

/**
* Unit tests for MoodleQuickForm_date_time_selector
*
Expand All @@ -64,7 +41,7 @@ public function getform() {
* @copyright 2012 Rajesh Taneja
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class datetimeselector_form_element_testcase extends basic_testcase {
class core_form_datetimeselector_testcase extends basic_testcase {
/** @var MoodleQuickForm Keeps reference of dummy form object */
private $mform;
/** @var stdClass saves current user data */
Expand All @@ -83,12 +60,13 @@ class datetimeselector_form_element_testcase extends basic_testcase {
/**
* Initalize test wide variable, it is called in start of the testcase
*/
public function setUp() {
// Get form data
protected function setUp() {
parent::setUp();
// Get form data.
$form = new temp_form_datetime();
$this->mform = $form->getform();

// Set test values
// Set test values.
$this->testvals = array(
array (
'minute' => 0,
Expand Down Expand Up @@ -157,8 +135,9 @@ public function setUp() {
* Clears the data set in the setUp() method call.
* @see datetimeselector_form_element_testcase::setUp()
*/
public function tearDown() {
protected function tearDown() {
unset($this->testvals);
parent::tearDown();
}

/**
Expand All @@ -181,11 +160,11 @@ public function test_exportvalue() {
$el->_createElements();
$submitvalues = array('dateselector' => $vals);

$this->assertSame($el->exportValue($submitvalues), array('dateselector' => $vals['timestamp']),
$this->assertSame(array('dateselector' => $vals['timestamp']), $el->exportValue($submitvalues),
"Please check if timezones are updated (Site adminstration -> location -> update timezone)");
}

// Restore user orignal timezone.
// Restore user original timezone.
$this->restoretimezone();
}

Expand All @@ -195,7 +174,7 @@ public function test_exportvalue() {
public function test_onquickformevent() {
global $USER;
$testvals = $this->testvals;
// Get dummy form for data
// Get dummy form for data.
$mform = $this->mform;
// Set timezone to Australia/Perth for testing.
$this->settimezone();
Expand All @@ -216,10 +195,10 @@ public function test_onquickformevent() {
);
$mform->_submitValues = array('dateselector' => $vals['timestamp']);
$el->onQuickFormEvent('updateValue', null, $mform);
$this->assertSame($el->getValue(), $expectedvalues);
$this->assertSame($expectedvalues, $el->getValue());
}

// Restore user orignal timezone.
// Restore user original timezone.
$this->restoretimezone();
}

Expand Down Expand Up @@ -253,7 +232,7 @@ private function settimezone() {
}

/**
* Restore user timezone to orignal state
* Restore user timezone to original state
*/
private function restoretimezone() {
global $USER, $CFG;
Expand All @@ -271,4 +250,26 @@ private function restoretimezone() {

$USER = $this->olduser;
}
}
}

/**
* Form object to be used in test case
*/
class temp_form_datetime 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;
}
}
Loading

0 comments on commit 1eb1355

Please sign in to comment.