MDL-57975 core: test for user date external function
authorRyan Wyllie <ryan@moodle.com>
Wed, 22 Feb 2017 05:53:46 +0000 (05:53 +0000)
committerRyan Wyllie <ryan@moodle.com>
Wed, 8 Mar 2017 03:33:23 +0000 (03:33 +0000)
Part of MDL-55611

lib/external/tests/external_test.php

index 2ceec65..6277bc7 100644 (file)
@@ -201,4 +201,38 @@ class core_external_testcase extends externallib_advanced_testcase {
         $res = external_api::clean_returnvalue(core_external::update_inplace_editable_returns(), $res);
         $this->assertEquals('new tag name', $res['value']);
     }
+
+    public function test_get_user_dates() {
+        global $USER, $CFG, $DB;
+        $this->resetAfterTest();
+
+        $this->setAdminUser();
+
+        // Set default timezone to Australia/Perth, else time calculated
+        // will not match expected values.
+        $this->setTimezone(99, 'Australia/Perth');
+
+        $context = context_system::instance();
+        $request = [
+            [
+                'timestamp' => 1293876000,
+                'format' => '%A, %d %B %Y, %I:%M'
+            ],
+            [
+                'timestamp' => 1293876000,
+                'format' => '%d %m %Y'
+            ],
+            [
+                'timestamp' => 1293876000,
+                'format' => 'some invalid format'
+            ],
+        ];
+
+        $result = core_external::get_user_dates($context->id, null, null, $request);
+        $result = external_api::clean_returnvalue(core_external::get_user_dates_returns(), $result);
+
+        $this->assertEquals('Saturday, 1 January 2011, 6:00', $result['dates'][0]);
+        $this->assertEquals('1 01 2011', $result['dates'][1]);
+        $this->assertEquals('some invalid format', $result['dates'][2]);
+    }
 }