[BUGFIX] BackendUtility::calcAge returns negative value for 0 70/19670/4
authorNicole Cordes <typo3@cordes.co>
Sat, 6 Apr 2013 13:05:21 +0000 (15:05 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Sat, 6 Apr 2013 14:35:06 +0000 (16:35 +0200)
Currently the function calcAge returns the value -0 min for zero seconds.
This should be changed as zero is unsigned. Besides some UnitTests are
added to verify the functionality of that function.

Change-Id: Ie0c7572e63bf8d3a6f074f2ccf0db63ac6f5c801
Fixes: #46984
Releated: 6.1, 6.0
Reviewed-on: https://review.typo3.org/19670
Reviewed-by: Alexander Opitz
Tested-by: Alexander Opitz
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
typo3/sysext/backend/Classes/Utility/BackendUtility.php
typo3/sysext/backend/Tests/Unit/Utility/BackendUtilityTest.php

index 382e7d7..928cc72 100644 (file)
@@ -1403,14 +1403,14 @@ class BackendUtility {
        static public function calcAge($seconds, $labels = ' min| hrs| days| yrs| min| hour| day| year') {
                $labelArr = explode('|', $labels);
                $absSeconds = abs($seconds);
-               $sign = $seconds > 0 ? 1 : -1;
-               if ($seconds < 3600) {
+               $sign = $seconds < 0 ? -1 : 1;
+               if ($absSeconds < 3600) {
                        $val = round($absSeconds / 60);
                        $seconds = $sign * $val . ($val == 1 ? $labelArr[4] : $labelArr[0]);
-               } elseif ($seconds < 24 * 3600) {
+               } elseif ($absSeconds < 24 * 3600) {
                        $val = round($absSeconds / 3600);
                        $seconds = $sign * $val . ($val == 1 ? $labelArr[5] : $labelArr[1]);
-               } elseif ($seconds < 365 * 24 * 3600) {
+               } elseif ($absSeconds < 365 * 24 * 3600) {
                        $val = round($absSeconds / (24 * 3600));
                        $seconds = $sign * $val . ($val == 1 ? $labelArr[6] : $labelArr[2]);
                } else {
index dd6f9d5..c9915bc 100644 (file)
@@ -46,6 +46,95 @@ class BackendUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        }
 
        ///////////////////////////////////////
+       // Tests concerning calcAge
+       ///////////////////////////////////////
+       /**
+        * Data provider for calcAge function
+        *
+        * @return array
+        */
+       public function calcAgeDataProvider() {
+               return array(
+                       'Single year' => array(
+                               'seconds' => 60 * 60 * 24 * 365,
+                               'expectedLabel' => '1 year'
+                       ),
+                       'Plural years' => array(
+                               'seconds' => 60 * 60 * 24 * 365 * 2,
+                               'expectedLabel' => '2 yrs'
+                       ),
+                       'Single negative year' => array(
+                               'seconds' => 60 * 60 * 24 * 365 * -1,
+                               'expectedLabel' => '-1 year'
+                       ),
+                       'Plural negative years' => array(
+                               'seconds' => 60 * 60 * 24 * 365 * 2 * -1,
+                               'expectedLabel' => '-2 yrs'
+                       ),
+                       'Single day' => array(
+                               'seconds' => 60 * 60 * 24,
+                               'expectedLabel' => '1 day'
+                       ),
+                       'Plural days' => array(
+                               'seconds' => 60 * 60 * 24 * 2,
+                               'expectedLabel' => '2 days'
+                       ),
+                       'Single negative day' => array(
+                               'seconds' => 60 * 60 * 24 * -1,
+                               'expectedLabel' => '-1 day'
+                       ),
+                       'Plural negative days' => array(
+                               'seconds' => 60 * 60 * 24 * 2 * -1,
+                               'expectedLabel' => '-2 days'
+                       ),
+                       'Single hour' => array(
+                               'seconds' => 60 * 60,
+                               'expectedLabel' => '1 hour'
+                       ),
+                       'Plural hours' => array(
+                               'seconds' => 60 * 60 * 2,
+                               'expectedLabel' => '2 hrs'
+                       ),
+                       'Single negative hour' => array(
+                               'seconds' => 60 * 60 * -1,
+                               'expectedLabel' => '-1 hour'
+                       ),
+                       'Plural negative hours' => array(
+                               'seconds' => 60 * 60 * 2 * -1,
+                               'expectedLabel' => '-2 hrs'
+                       ),
+                       'Single minute' => array(
+                               'seconds' => 60,
+                               'expectedLabel' => '1 min'
+                       ),
+                       'Plural minutes' => array(
+                               'seconds' => 60 * 2,
+                               'expectedLabel' => '2 min'
+                       ),
+                       'Single negative minute' => array(
+                               'seconds' => 60 * -1,
+                               'expectedLabel' => '-1 min'
+                       ),
+                       'Plural negative minutes' => array(
+                               'seconds' => 60 * 2 * -1,
+                               'expectedLabel' => '-2 min'
+                       ),
+                       'Zero seconds' => array(
+                               'seconds' => 0,
+                               'expectedLabel' => '0 min'
+                       )
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider calcAgeDataProvider
+        */
+       public function calcAgeReturnsExpectedValues($seconds, $expectedLabel) {
+               $this->assertSame($expectedLabel, $this->fixture->calcAge($seconds));
+       }
+
+       ///////////////////////////////////////
        // Tests concerning getProcessedValue
        ///////////////////////////////////////
        /**