summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/pager/RangeChronologicalPagerTest.php
blob: 72390ac856eff707645eafb0c17333045aed7903 (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
<?php

/**
 * Test class for RangeChronologicalPagerTest logic.
 *
 * @group Pager
 *
 * @author Geoffrey Mon <geofbot@gmail.com>
 */
class RangeChronologicalPagerTest extends MediaWikiLangTestCase {

	/**
	 * @covers       RangeChronologicalPager::getDateCond
	 * @dataProvider getDateCondProvider
	 */
	public function testGetDateCond( $inputYear, $inputMonth, $inputDay, $expected ) {
		$pager = $this->getMockForAbstractClass( RangeChronologicalPager::class );
		$this->assertEquals(
			$expected,
			wfTimestamp( TS_MW, $pager->getDateCond( $inputYear, $inputMonth, $inputDay ) )
		);
	}

	/**
	 * Data provider in [ input year, input month, input day, expected timestamp output ] format
	 */
	public function getDateCondProvider() {
		return [
			[ 2016, 12, 5, '20161205235959' ],
			[ 2016, 12, 31, '20161231235959' ],
			[ 2016, 12, 1337, '20161231235959' ],
			[ 2016, 1337, 1337, '20161231235959' ],
			[ 2016, 1337, -1, '20161231235959' ],
			[ 2016, 12, 32, '20161231235959' ],
			[ 2016, 12, -1, '20161231235959' ],
			[ 2016, -1, -1, '20161231235959' ],
		];
	}

	/**
	 * @covers       RangeChronologicalPager::getDateRangeCond
	 * @dataProvider getDateRangeCondProvider
	 */
	public function testGetDateRangeCond( $start, $end, $expected ) {
		$pager = $this->getMockForAbstractClass( RangeChronologicalPager::class );
		$this->assertArrayEquals( $expected, $pager->getDateRangeCond( $start, $end ) );
	}

	/**
	 * Data provider in [ start, end, [ expected output has start condition, has end cond ] ] format
	 */
	public function getDateRangeCondProvider() {
		$db = wfGetDB( DB_MASTER );

		return [
			[
				'20161201000000',
				'20161203000000',
				[
					'>=' . $db->addQuotes( $db->timestamp( '20161201000000' ) ),
					'<=' . $db->addQuotes( $db->timestamp( '20161203000000' ) ),
				],
			],
			[
				'',
				'20161203000000',
				[
					'<=' . $db->addQuotes( $db->timestamp( '20161203000000' ) ),
				],
			],
			[
				'20161201000000',
				'',
				[
					'>=' . $db->addQuotes( $db->timestamp( '20161201000000' ) ),
				],
			],
			[ '', '', [] ],
		];
	}

	/**
	 * @covers       RangeChronologicalPager::getDateRangeCond
	 * @dataProvider getDateRangeCondInvalidProvider
	 */
	public function testGetDateRangeCondInvalid( $start, $end ) {
		$pager = $this->getMockForAbstractClass( RangeChronologicalPager::class );
		$this->assertEquals( null, $pager->getDateRangeCond( $start, $end ) );
	}

	public function getDateRangeCondInvalidProvider() {
		return [
			[ '-2016-12-01', '2017-12-01', ],
			[ '2016-12-01', '-2017-12-01', ],
			[ 'abcdefghij', 'klmnopqrstu', ],
		];
	}

}