From b31d663c25be26d1740df32f2211d5c116f35073 Mon Sep 17 00:00:00 2001
From: Jaime Perez Crespo <jaime.perez@uninett.no>
Date: Fri, 4 Mar 2016 14:59:29 +0100
Subject: [PATCH] Complete unit testing for SimpleSAML\Utils\Time by adding
 test for initTimezone().

---
 tests/lib/SimpleSAML/Utils/TimeTest.php | 43 +++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/tests/lib/SimpleSAML/Utils/TimeTest.php b/tests/lib/SimpleSAML/Utils/TimeTest.php
index 8ac6d0059..17cbc6e5a 100644
--- a/tests/lib/SimpleSAML/Utils/TimeTest.php
+++ b/tests/lib/SimpleSAML/Utils/TimeTest.php
@@ -22,6 +22,49 @@ class TimeTest extends \PHPUnit_Framework_TestCase
     }
 
 
+    /**
+     * Test the SimpleSAML\Utils\Time::initTimezone() method.
+     *
+     * @covers SimpleSAML\Utils\Time::initTimezone
+     */
+    public function testInitTimezon()
+    {
+        $tz = 'UTC';
+        if (@date_default_timezone_get() === 'UTC') { // avoid collisions
+            $tz = 'Europe/Oslo';
+        }
+
+        // test guessing timezone from the OS
+        \SimpleSAML_Configuration::loadFromArray(array('timezone' => 'Europe/Madrid'), '[ARRAY]', 'simplesaml');
+        @Time::initTimezone();
+        $this->assertEquals('Europe/Madrid', @date_default_timezone_get());
+
+        // clear initialization
+        $c = new \ReflectionProperty('\SimpleSAML\Utils\Time', 'tz_initialized');
+        $c->setAccessible(true);
+        $c->setValue(false);
+
+        // test unknown timezone
+        \SimpleSAML_Configuration::loadFromArray(array('timezone' => 'INVALID'), '[ARRAY]', 'simplesaml');
+        try {
+            @Time::initTimezone();
+            $this->fail('Failed to recognize an invalid timezone.');
+        } catch (\SimpleSAML_Error_Exception $e) {
+            $this->assertEquals('Invalid timezone set in the "timezone" option in config.php.', $e->getMessage());
+        }
+
+        // test a valid timezone
+        \SimpleSAML_Configuration::loadFromArray(array('timezone' => $tz), '[ARRAY]', 'simplesaml');
+        @Time::initTimezone();
+        $this->assertEquals($tz, @date_default_timezone_get());
+
+        // make sure initialization happens only once
+        \SimpleSAML_Configuration::loadFromArray(array('timezone' => 'Europe/Madrid'), '[ARRAY]', 'simplesaml');
+        @Time::initTimezone();
+        $this->assertEquals($tz, @date_default_timezone_get());
+    }
+
+
     /**
      * Test the SimpleSAML\Utils\Time::parseDuration() method.
      *
-- 
GitLab