[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/form/tests/ -> duration_test.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Unit tests for forms lib.
  19   *
  20   * This file contains all unit test related to forms library.
  21   *
  22   * @package    core_form
  23   * @category   phpunit
  24   * @copyright  2009 Tim Hunt
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  global $CFG;
  31  require_once($CFG->libdir . '/form/duration.php');
  32  
  33  /**
  34   * Unit tests for MoodleQuickForm_duration
  35   *
  36   * Contains test cases for testing MoodleQuickForm_duration
  37   *
  38   * @package    core_form
  39   * @category   phpunit
  40   * @copyright  2009 Tim Hunt
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class core_form_duration_testcase extends basic_testcase {
  44      /** @var MoodleQuickForm_duration Keeps reference of MoodleQuickForm_duration object */
  45      private $element;
  46  
  47      /**
  48       * Initalize test wide variable, it is called in start of the testcase
  49       */
  50      protected function setUp() {
  51          parent::setUp();
  52          $this->element = new MoodleQuickForm_duration();
  53      }
  54  
  55      /**
  56       * Clears the data set in the setUp() method call.
  57       * @see duration_form_element_test::setUp()
  58       */
  59      protected function tearDown() {
  60          $this->element = null;
  61      }
  62  
  63      /**
  64       * Testcase for testing contructor.
  65       * @expectedException coding_exception
  66       * @retrun void
  67       */
  68      public function test_constructor() {
  69          // Test trying to create with an invalid unit.
  70          $this->element = new MoodleQuickForm_duration('testel', null, array('defaultunit' => 123));
  71      }
  72  
  73      /**
  74       * Testcase for testing units (seconds, minutes, hours and days)
  75       */
  76      public function test_get_units() {
  77          $units = $this->element->get_units();
  78          ksort($units);
  79          $this->assertEquals($units, array(1 => get_string('seconds'), 60 => get_string('minutes'),
  80              3600 => get_string('hours'), 86400 => get_string('days'), 604800 => get_string('weeks')));
  81      }
  82  
  83      /**
  84       * Testcase for testing conversion of seconds to the best possible unit
  85       */
  86      public function test_seconds_to_unit() {
  87          $this->assertEquals(array(0, 60), $this->element->seconds_to_unit(0)); // Zero minutes, for a nice default unit.
  88          $this->assertEquals(array(1, 1), $this->element->seconds_to_unit(1));
  89          $this->assertEquals(array(3601, 1), $this->element->seconds_to_unit(3601));
  90          $this->assertEquals(array(1, 60), $this->element->seconds_to_unit(60));
  91          $this->assertEquals(array(3, 60), $this->element->seconds_to_unit(180));
  92          $this->assertEquals(array(1, 3600), $this->element->seconds_to_unit(3600));
  93          $this->assertEquals(array(2, 3600), $this->element->seconds_to_unit(7200));
  94          $this->assertEquals(array(1, 86400), $this->element->seconds_to_unit(86400));
  95          $this->assertEquals(array(25, 3600), $this->element->seconds_to_unit(90000));
  96  
  97          $this->element = new MoodleQuickForm_duration('testel', null, array('defaultunit' => 86400));
  98          $this->assertEquals(array(0, 86400), $this->element->seconds_to_unit(0)); // Zero minutes, for a nice default unit.
  99      }
 100  
 101      /**
 102       * Testcase to check generated timestamp
 103       */
 104      public function test_exportValue() {
 105          $el = new MoodleQuickForm_duration('testel');
 106          $el->_createElements();
 107          $values = array('testel' => array('number' => 10, 'timeunit' => 1));
 108          $this->assertEquals(array('testel' => 10), $el->exportValue($values));
 109          $values = array('testel' => array('number' => 3, 'timeunit' => 60));
 110          $this->assertEquals(array('testel' => 180), $el->exportValue($values));
 111          $values = array('testel' => array('number' => 1.5, 'timeunit' => 60));
 112          $this->assertEquals(array('testel' => 90), $el->exportValue($values));
 113          $values = array('testel' => array('number' => 2, 'timeunit' => 3600));
 114          $this->assertEquals(array('testel' => 7200), $el->exportValue($values));
 115          $values = array('testel' => array('number' => 1, 'timeunit' => 86400));
 116          $this->assertEquals(array('testel' => 86400), $el->exportValue($values));
 117          $values = array('testel' => array('number' => 0, 'timeunit' => 3600));
 118          $this->assertEquals(array('testel' => 0), $el->exportValue($values));
 119  
 120          $el = new MoodleQuickForm_duration('testel', null, array('optional' => true));
 121          $el->_createElements();
 122          $values = array('testel' => array('number' => 10, 'timeunit' => 1));
 123          $this->assertEquals(array('testel' => 0), $el->exportValue($values));
 124          $values = array('testel' => array('number' => 20, 'timeunit' => 1, 'enabled' => 1));
 125          $this->assertEquals(array('testel' => 20), $el->exportValue($values));
 126      }
 127  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1