Skip to content

Commit

Permalink
MDL-69507 duration form field: should return an int number of seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt authored and andrewnicols committed Sep 1, 2020
1 parent 9e9a1db commit 33f880f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/form/duration.php
Expand Up @@ -261,6 +261,6 @@ function exportValue(&$submitValues, $assoc = false) {
if ($this->_options['optional'] && empty($valuearray['enabled'])) {
return $this->_prepareValue(0, $assoc);
}
return $this->_prepareValue($valuearray['number'] * $valuearray['timeunit'], $assoc);
return $this->_prepareValue((int) round($valuearray['number'] * $valuearray['timeunit']), $assoc);
}
}
13 changes: 13 additions & 0 deletions lib/form/tests/duration_test.php
Expand Up @@ -113,18 +113,31 @@ public function test_exportValue() {
$values = array('testel' => array('number' => 10, 'timeunit' => 1));
$this->assertEquals(array('testel' => 10), $el->exportValue($values, true));
$this->assertEquals(10, $el->exportValue($values));

$values = array('testel' => array('number' => 9.3, 'timeunit' => 1));
$this->assertEquals(array('testel' => 9), $el->exportValue($values, true));
$this->assertEquals(9, $el->exportValue($values));

$values = array('testel' => array('number' => 9.5, 'timeunit' => 1));
$this->assertEquals(array('testel' => 10), $el->exportValue($values, true));
$this->assertEquals(10, $el->exportValue($values));

$values = array('testel' => array('number' => 3, 'timeunit' => 60));
$this->assertEquals(array('testel' => 180), $el->exportValue($values, true));
$this->assertEquals(180, $el->exportValue($values));

$values = array('testel' => array('number' => 1.5, 'timeunit' => 60));
$this->assertEquals(array('testel' => 90), $el->exportValue($values, true));
$this->assertEquals(90, $el->exportValue($values));

$values = array('testel' => array('number' => 2, 'timeunit' => 3600));
$this->assertEquals(array('testel' => 7200), $el->exportValue($values, true));
$this->assertEquals(7200, $el->exportValue($values));

$values = array('testel' => array('number' => 1, 'timeunit' => 86400));
$this->assertEquals(array('testel' => 86400), $el->exportValue($values, true));
$this->assertEquals(86400, $el->exportValue($values));

$values = array('testel' => array('number' => 0, 'timeunit' => 3600));
$this->assertEquals(array('testel' => 0), $el->exportValue($values, true));
$this->assertEquals(0, $el->exportValue($values));
Expand Down

0 comments on commit 33f880f

Please sign in to comment.