From 74e8fb830dfd1547ff17f32e8b7cf3b5bc9e7f68 Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Sun, 11 Nov 2018 20:20:20 +0100 Subject: [PATCH] fixed PHP notice --- lam/lib/account.inc | 18 +++++------ lam/tests/lib/accountTest.php | 61 +++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 9 deletions(-) create mode 100644 lam/tests/lib/accountTest.php diff --git a/lam/lib/account.inc b/lam/lib/account.inc index acee4707..99a0e822 100644 --- a/lam/lib/account.inc +++ b/lam/lib/account.inc @@ -1501,19 +1501,19 @@ function unformatShortFormatToSeconds($text) { return $text; } $matches = array(); - if (preg_match('/^([0-9]+d)?([0-9]+h)?([0-9]+m)?([0-9]+s)?$/', $text, $matches)) { + if (preg_match('/^(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?$/', $text, $matches)) { $newValue = 0; - if (!empty($matches[1])) { - $newValue += $matches[1] * 86400; - } if (!empty($matches[2])) { - $newValue += $matches[2] * 3600; - } - if (!empty($matches[3])) { - $newValue += $matches[3] * 60; + $newValue += $matches[2] * 86400; } if (!empty($matches[4])) { - $newValue += $matches[4]; + $newValue += $matches[4] * 3600; + } + if (!empty($matches[6])) { + $newValue += $matches[6] * 60; + } + if (!empty($matches[8])) { + $newValue += $matches[8]; } return $newValue; } diff --git a/lam/tests/lib/accountTest.php b/lam/tests/lib/accountTest.php new file mode 100644 index 00000000..3990fa7f --- /dev/null +++ b/lam/tests/lib/accountTest.php @@ -0,0 +1,61 @@ +assertEquals(15, unformatShortFormatToSeconds('15')); + } + + /** + * Tests unformatShortFormatToSeconds() with characters. + */ + function testUnformatShortFormatToSeconds_conversion() { + $this->assertEquals(15, unformatShortFormatToSeconds('15')); + $this->assertEquals(12, unformatShortFormatToSeconds('12s')); + $this->assertEquals(180, unformatShortFormatToSeconds('3m')); + $this->assertEquals(7200, unformatShortFormatToSeconds('2h')); + $this->assertEquals(86400, unformatShortFormatToSeconds('1d')); + $this->assertEquals(135, unformatShortFormatToSeconds('2m15s')); + $this->assertEquals(7215, unformatShortFormatToSeconds('2h15s')); + $this->assertEquals(172815, unformatShortFormatToSeconds('2d15s')); + $this->assertEquals(173700, unformatShortFormatToSeconds('2d15m')); + } + + /** + * Tests unformatShortFormatToSeconds() with invalid values. + */ + function testUnformatShortFormatToSeconds_invalidNumber() { + $this->assertEquals('abc', unformatShortFormatToSeconds('abc')); + $this->assertEquals('', unformatShortFormatToSeconds('')); + } + +}