|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00004 // // 00005 // NOTICE OF COPYRIGHT // 00006 // // 00007 // Moodle - Modular Object-Oriented Dynamic Learning Environment // 00008 // http://moodle.org // 00009 // // 00010 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com // 00011 // // 00012 // This program is free software; you can redistribute it and/or modify // 00013 // it under the terms of the GNU General Public License as published by // 00014 // the Free Software Foundation; either version 2 of the License, or // 00015 // (at your option) any later version. // 00016 // // 00017 // This program is distributed in the hope that it will be useful, // 00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of // 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // 00020 // GNU General Public License for more details: // 00021 // // 00022 // http://www.gnu.org/copyleft/gpl.html // 00023 // // 00025 00026 if (!defined('MOODLE_INTERNAL')) { 00027 die('Direct access to this script is forbidden.'); 00028 } 00029 00030 global $CFG; 00031 require_once($CFG->libdir . '/form/duration.php'); 00032 00039 class duration_form_element_test extends UnitTestCase { 00040 private $element; 00041 public static $includecoverage = array('lib/form/duration.php'); 00042 00043 function setUp() { 00044 $this->element = new MoodleQuickForm_duration(); 00045 } 00046 00047 function tearDown() { 00048 $this->element = null; 00049 } 00050 00051 function test_constructor() { 00052 // Test trying to create with an invalid unit. 00053 $this->expectException(); 00054 $this->element = new MoodleQuickForm_duration('testel', null, array('defaultunit' => 123)); 00055 } 00056 00057 function test_get_units() { 00058 $units = $this->element->get_units(); 00059 ksort($units); 00060 $this->assertEqual($units, array(1 => get_string('seconds'), 60 => get_string('minutes'), 00061 3600 => get_string('hours'), 86400 => get_string('days'))); 00062 } 00063 00064 function test_seconds_to_unit() { 00065 $this->assertEqual($this->element->seconds_to_unit(0), array(0, 60)); // Zero minutes, for a nice default unit. 00066 $this->assertEqual($this->element->seconds_to_unit(1), array(1, 1)); 00067 $this->assertEqual($this->element->seconds_to_unit(3601), array(3601, 1)); 00068 $this->assertEqual($this->element->seconds_to_unit(60), array(1, 60)); 00069 $this->assertEqual($this->element->seconds_to_unit(180), array(3, 60)); 00070 $this->assertEqual($this->element->seconds_to_unit(3600), array(1, 3600)); 00071 $this->assertEqual($this->element->seconds_to_unit(7200), array(2, 3600)); 00072 $this->assertEqual($this->element->seconds_to_unit(86400), array(1, 86400)); 00073 $this->assertEqual($this->element->seconds_to_unit(90000), array(25, 3600)); 00074 00075 $this->element = new MoodleQuickForm_duration('testel', null, array('defaultunit' => 86400)); 00076 $this->assertEqual($this->element->seconds_to_unit(0), array(0, 86400)); // Zero minutes, for a nice default unit. 00077 } 00078 00079 function test_exportValue() { 00080 $el = new MoodleQuickForm_duration('testel'); 00081 $el->_createElements(); 00082 $this->assertEqual($el->exportValue(array('testel' => array('number' => 10, 'timeunit' => 1))), array('testel' => 10)); 00083 $this->assertEqual($el->exportValue(array('testel' => array('number' => 3, 'timeunit' => 60))), array('testel' => 180)); 00084 $this->assertEqual($el->exportValue(array('testel' => array('number' => 1.5, 'timeunit' => 60))), array('testel' => 90)); 00085 $this->assertEqual($el->exportValue(array('testel' => array('number' => 2, 'timeunit' => 3600))), array('testel' => 7200)); 00086 $this->assertEqual($el->exportValue(array('testel' => array('number' => 1, 'timeunit' => 86400))), array('testel' => 86400)); 00087 $this->assertEqual($el->exportValue(array('testel' => array('number' => 0, 'timeunit' => 3600))), array('testel' => 0)); 00088 00089 $el = new MoodleQuickForm_duration('testel', null, array('optional' => true)); 00090 $el->_createElements(); 00091 $this->assertEqual($el->exportValue(array('testel' => array('number' => 10, 'timeunit' => 1))), array('testel' => 0)); 00092 $this->assertEqual($el->exportValue(array('testel' => array('number' => 20, 'timeunit' => 1, 'enabled' => 1))), array('testel' => 20)); 00093 } 00094 }