summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Unit/MediaWiki/LocalTimeTest.php
blob: 53745c88cf1df75f68833405e1348c3cc599e3d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php

namespace SMW\Tests\MediaWiki;

use DateTime;
use SMW\MediaWiki\LocalTime;

/**
 * @covers \SMW\MediaWiki\LocalTime
 * @group semantic-mediawiki
 *
 * @license GNU GPL v2+
 * @since   3.0
 *
 * @author mwjames
 */
class LocalTimeTest extends \PHPUnit_Framework_TestCase {

	public function testNoModifiedLocalTime() {

		$dateTime = LocalTime::getLocalizedTime(
			new DateTime( '2017-08-01 10:00:00+00:00' )
		);

		$this->assertFalse(
			$dateTime->hasLocalTimeCorrection
		);
	}

	public function testModifiedTimeWithPositiveLocalTimeOffset() {

		$dti = new DateTime( '2017-08-01 10:00:00+00:00' );

		LocalTime::setLocalTimeOffset( 60 );
		$dateTime = LocalTime::getLocalizedTime( $dti );

		$this->assertTrue(
			$dateTime->hasLocalTimeCorrection
		);

		$this->assertEquals(
			'2017-08-01 11:00:00',
			$dateTime->format( 'Y-m-d H:i:s' )
		);
	}

	public function testModifiedTimeWithNegativeLocalTimeOffset() {

		$dti = new DateTime( '2017-08-01 10:00:00+00:00' );

		LocalTime::setLocalTimeOffset( -60 );
		$dateTime = LocalTime::getLocalizedTime( $dti );

		$this->assertTrue(
			$dateTime->hasLocalTimeCorrection
		);

		$this->assertEquals(
			'2017-08-01 09:00:00',
			$dateTime->format( 'Y-m-d H:i:s' )
		);
	}

	public function testModifiedTimeWithUserTimeCorrection() {

		$user = $this->getMockBuilder( '\User' )
			->disableOriginalConstructor()
			->getMock();

		$user->expects( $this->once() )
			->method( 'getOption' )
			->with( $this->equalTo( 'timecorrection' ) )
			->will( $this->returnValue( 'ZoneInfo|+120|Europe/Berlin' ) );

		$dti = new DateTime( '2017-08-01 10:00:00+00:00' );

		LocalTime::setLocalTimeOffset( 0 );
		$dateTime = LocalTime::getLocalizedTime( $dti, $user );

		$this->assertTrue(
			$dateTime->hasLocalTimeCorrection
		);

		$this->assertEquals(
			'2017-08-01 12:00:00',
			$dateTime->format( 'Y-m-d H:i:s' )
		);
	}

	public function testModifiedTimeWithUserTimeCorrectionOnInvalidZone() {

		$user = $this->getMockBuilder( '\User' )
			->disableOriginalConstructor()
			->getMock();

		$user->expects( $this->once() )
			->method( 'getOption' )
			->with( $this->equalTo( 'timecorrection' ) )
			->will( $this->returnValue( 'ZoneInfo|+125|Foo' ) );

		$dti = new DateTime( '2017-08-01 10:00:00+00:00' );

		$dateTime = LocalTime::getLocalizedTime( $dti, $user );

		$this->assertTrue(
			$dateTime->hasLocalTimeCorrection
		);

		$this->assertEquals(
			'2017-08-01 12:05:00',
			$dateTime->format( 'Y-m-d H:i:s' )
		);
	}

}