MDL-58596 core_stats: unit test for stats_get_base_weekly
[moodle.git] / lib / tests / statslib_test.php
index f8ced49..7af80e0 100644 (file)
@@ -219,6 +219,34 @@ class core_statslib_testcase extends advanced_testcase {
         return $logfiles;
     }
 
+    /**
+     * Set of data for test_statlibs_get_base_weekly
+     *
+     * @return array Dates and timezones for which the first day of the week will be calculated
+     */
+    public function get_base_weekly_provider() {
+        return [
+            [
+                "startwday" => 0,
+                "timezone" => 'America/Chicago',
+                "timestart" => '18-03-2017 22:00',
+                "expected" => '12-03-2017 00:00:00'
+            ],
+            [
+                "startwday" => 0,
+                "timezone" => 'America/Chicago',
+                "date" => '25-03-2017 22:00',
+                "expected" => '19-03-2017 00:00:00'
+            ],
+            [
+                "startwday" => 1,
+                "timezone" => 'Atlantic/Canary',
+                "date" => '06-08-2018 22:00',
+                "expected" => '06-08-2018 00:00:00'
+            ],
+        ];
+    }
+
     /**
      * Compare the expected stats to those in the database.
      *
@@ -420,6 +448,27 @@ class core_statslib_testcase extends advanced_testcase {
         $this->assertEquals(24, ((1446526800 - 1446440400) / 60 ) / 60);
     }
 
+    /**
+     * Test the function that calculates the start of the week.
+     *
+     * @dataProvider get_base_weekly_provider
+     * @param int $startwday Day in which the week starts (Sunday = 0)
+     * @param string $timezone Default timezone
+     * @param string $timestart Date and time for which the first day of the week will be obtained
+     * @param string $expected Expected date of the first day of the week
+     */
+    public function test_statslib_get_base_weekly($startwday, $timezone, $timestart, $expected) {
+        $this->setTimezone($timezone);
+        $time = strtotime($timestart);
+        $expected = strtotime($expected);
+        set_config('calendar_startwday', $startwday);
+        set_config('statslastweekly', $time);
+
+        $weekstarttime = stats_get_base_weekly($time);
+
+        $this->assertEquals($expected, $weekstarttime);
+    }
+
     /**
      * Test the function that gets the action names.
      *