MDL-41022 cleanup form tests

This commit is contained in:
Petr Škoda 2013-08-11 12:41:13 +02:00
parent 5f15f7c9aa
commit 1eb135568a
4 changed files with 104 additions and 100 deletions

View File

@ -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
*
@ -32,28 +31,6 @@ global $CFG;
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
*
@ -64,7 +41,7 @@ class temp_form_date extends moodleform {
* @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 */
@ -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,
@ -145,8 +123,9 @@ class dateselector_form_element_testcase extends basic_testcase {
* 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();
}
/**
@ -169,11 +148,11 @@ class dateselector_form_element_testcase extends basic_testcase {
$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();
}
@ -183,7 +162,7 @@ class dateselector_form_element_testcase extends basic_testcase {
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();
@ -202,20 +181,20 @@ class dateselector_form_element_testcase extends basic_testcase {
);
$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;
@ -239,7 +218,7 @@ class dateselector_form_element_testcase extends basic_testcase {
}
/**
* Restore user timezone to orignal state
* Restore user timezone to original state
*/
private function restoretimezone() {
global $USER, $CFG;
@ -257,4 +236,26 @@ class dateselector_form_element_testcase extends basic_testcase {
$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;
}
}

View File

@ -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
*
@ -32,28 +31,6 @@ global $CFG;
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
*
@ -64,7 +41,7 @@ class temp_form_datetime extends moodleform {
* @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 */
@ -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,
@ -157,8 +135,9 @@ class datetimeselector_form_element_testcase extends basic_testcase {
* 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();
}
/**
@ -181,11 +160,11 @@ class datetimeselector_form_element_testcase extends basic_testcase {
$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();
}
@ -195,7 +174,7 @@ class datetimeselector_form_element_testcase extends basic_testcase {
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();
@ -216,10 +195,10 @@ class datetimeselector_form_element_testcase extends basic_testcase {
);
$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();
}
@ -253,7 +232,7 @@ class datetimeselector_form_element_testcase extends basic_testcase {
}
/**
* Restore user timezone to orignal state
* Restore user timezone to original state
*/
private function restoretimezone() {
global $USER, $CFG;
@ -271,4 +250,26 @@ class datetimeselector_form_element_testcase extends basic_testcase {
$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;
}
}

View File

@ -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 forms lib.
*
@ -37,18 +36,19 @@ require_once($CFG->libdir . '/form/duration.php');
* Contains test cases for testing MoodleQuickForm_duration
*
* @package core_form
* @category unittest
* @category phpunit
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class duration_form_element_testcase extends basic_testcase {
class core_form_duration_testcase extends basic_testcase {
/** @var MoodleQuickForm_duration Keeps reference of MoodleQuickForm_duration object */
private $element;
/**
* Initalize test wide variable, it is called in start of the testcase
*/
function setUp() {
protected function setUp() {
parent::setUp();
$this->element = new MoodleQuickForm_duration();
}
@ -56,7 +56,7 @@ class duration_form_element_testcase extends basic_testcase {
* Clears the data set in the setUp() method call.
* @see duration_form_element_test::setUp()
*/
function tearDown() {
protected function tearDown() {
$this->element = null;
}
@ -65,7 +65,7 @@ class duration_form_element_testcase extends basic_testcase {
* @expectedException coding_exception
* @retrun void
*/
function test_constructor() {
public function test_constructor() {
// Test trying to create with an invalid unit.
$this->element = new MoodleQuickForm_duration('testel', null, array('defaultunit' => 123));
}
@ -73,7 +73,7 @@ class duration_form_element_testcase extends basic_testcase {
/**
* Testcase for testing units (seconds, minutes, hours and days)
*/
function test_get_units() {
public function test_get_units() {
$units = $this->element->get_units();
ksort($units);
$this->assertEquals($units, array(1 => get_string('seconds'), 60 => get_string('minutes'),
@ -83,45 +83,45 @@ class duration_form_element_testcase extends basic_testcase {
/**
* Testcase for testing conversion of seconds to the best possible unit
*/
function test_seconds_to_unit() {
$this->assertEquals($this->element->seconds_to_unit(0), array(0, 60)); // Zero minutes, for a nice default unit.
$this->assertEquals($this->element->seconds_to_unit(1), array(1, 1));
$this->assertEquals($this->element->seconds_to_unit(3601), array(3601, 1));
$this->assertEquals($this->element->seconds_to_unit(60), array(1, 60));
$this->assertEquals($this->element->seconds_to_unit(180), array(3, 60));
$this->assertEquals($this->element->seconds_to_unit(3600), array(1, 3600));
$this->assertEquals($this->element->seconds_to_unit(7200), array(2, 3600));
$this->assertEquals($this->element->seconds_to_unit(86400), array(1, 86400));
$this->assertEquals($this->element->seconds_to_unit(90000), array(25, 3600));
public function test_seconds_to_unit() {
$this->assertEquals(array(0, 60), $this->element->seconds_to_unit(0)); // Zero minutes, for a nice default unit.
$this->assertEquals(array(1, 1), $this->element->seconds_to_unit(1));
$this->assertEquals(array(3601, 1), $this->element->seconds_to_unit(3601));
$this->assertEquals(array(1, 60), $this->element->seconds_to_unit(60));
$this->assertEquals(array(3, 60), $this->element->seconds_to_unit(180));
$this->assertEquals(array(1, 3600), $this->element->seconds_to_unit(3600));
$this->assertEquals(array(2, 3600), $this->element->seconds_to_unit(7200));
$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->assertEquals($this->element->seconds_to_unit(0), array(0, 86400)); // Zero minutes, for a nice default unit.
$this->assertEquals(array(0, 86400), $this->element->seconds_to_unit(0)); // Zero minutes, for a nice default unit.
}
/**
* Testcase to check generated timestamp
*/
function test_exportValue() {
public function test_exportValue() {
$el = new MoodleQuickForm_duration('testel');
$el->_createElements();
$values = array('testel' => array('number' => 10, 'timeunit' => 1));
$this->assertEquals($el->exportValue($values), array('testel' => 10));
$this->assertEquals(array('testel' => 10), $el->exportValue($values));
$values = array('testel' => array('number' => 3, 'timeunit' => 60));
$this->assertEquals($el->exportValue($values), array('testel' => 180));
$this->assertEquals(array('testel' => 180), $el->exportValue($values));
$values = array('testel' => array('number' => 1.5, 'timeunit' => 60));
$this->assertEquals($el->exportValue($values), array('testel' => 90));
$this->assertEquals(array('testel' => 90), $el->exportValue($values));
$values = array('testel' => array('number' => 2, 'timeunit' => 3600));
$this->assertEquals($el->exportValue($values), array('testel' => 7200));
$this->assertEquals(array('testel' => 7200), $el->exportValue($values));
$values = array('testel' => array('number' => 1, 'timeunit' => 86400));
$this->assertEquals($el->exportValue($values), array('testel' => 86400));
$this->assertEquals(array('testel' => 86400), $el->exportValue($values));
$values = array('testel' => array('number' => 0, 'timeunit' => 3600));
$this->assertEquals($el->exportValue($values), array('testel' => 0));
$this->assertEquals(array('testel' => 0), $el->exportValue($values));
$el = new MoodleQuickForm_duration('testel', null, array('optional' => true));
$el->_createElements();
$values = array('testel' => array('number' => 10, 'timeunit' => 1));
$this->assertEquals($el->exportValue($values), array('testel' => 0));
$this->assertEquals(array('testel' => 0), $el->exportValue($values));
$values = array('testel' => array('number' => 20, 'timeunit' => 1, 'enabled' => 1));
$this->assertEquals($el->exportValue($values), array('testel' => 20));
$this->assertEquals(array('testel' => 20), $el->exportValue($values));
}
}

View File

@ -40,9 +40,11 @@
<testsuite name="core">
<directory suffix="_test.php">lib/tests</directory>
<directory suffix="_test.php">lib/ajax/tests</directory>
<directory suffix="_test.php">lib/form/tests</directory>
<directory>lib/password_compat/tests</directory>
</testsuite>
<testsuite name="core_form">
<directory suffix="_test.php">lib/form/tests</directory>
</testsuite>
<testsuite name="core_files">
<directory suffix="_test.php">lib/filestorage/tests</directory>
<directory suffix="_test.php">files/tests</directory>