| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\MediaWikiServices; |
| 4 |
use MediaWiki\Pager\RangeChronologicalPager; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class RangeChronologicalPagerTest extends MediaWikiIntegrationTestCase { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public function testGetDateCond( $inputYear, $inputMonth, $inputDay, $expected ) { |
| 21 |
$pager = $this->getMockForAbstractClass( RangeChronologicalPager::class ); |
| 22 |
$this->assertEquals( |
| 23 |
$expected, |
| 24 |
wfTimestamp( TS_MW, $pager->getDateCond( $inputYear, $inputMonth, $inputDay ) ) |
| 25 |
); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public static function getDateCondProvider() { |
| 32 |
return [ |
| 33 |
[ 2016, 12, 5, '20161206000000' ], |
| 34 |
[ 2016, 12, 31, '20170101000000' ], |
| 35 |
[ 2016, 12, 1337, '20170101000000' ], |
| 36 |
[ 2016, 1337, 1337, '20170101000000' ], |
| 37 |
[ 2016, 1337, -1, '20170101000000' ], |
| 38 |
[ 2016, 12, 32, '20170101000000' ], |
| 39 |
[ 2016, 12, -1, '20170101000000' ], |
| 40 |
[ 2016, -1, -1, '20170101000000' ], |
| 41 |
]; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function testGetDateRangeCond( $start, $end, $expected ) { |
| 49 |
$pager = $this->getMockBuilder( RangeChronologicalPager::class ) |
| 50 |
->onlyMethods( [ 'getTimestampField' ] ) |
| 51 |
->getMockForAbstractClass(); |
| 52 |
$pager->method( 'getTimestampField' )->willReturn( 'timestampfield' ); |
| 53 |
$this->assertArrayEquals( $expected, $pager->getDateRangeCond( $start, $end ) ); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public static function getDateRangeCondProvider() { |
| 60 |
$dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase(); |
| 61 |
|
| 62 |
return [ |
| 63 |
[ |
| 64 |
'20161201000000', |
| 65 |
'20161202235959', |
| 66 |
[ |
| 67 |
$dbw->buildComparison( '>=', [ 'timestampfield' => $dbw->timestamp( '20161201000000' ) ] ), |
| 68 |
$dbw->buildComparison( '<', [ 'timestampfield' => $dbw->timestamp( '20161203000000' ) ] ), |
| 69 |
], |
| 70 |
], |
| 71 |
[ |
| 72 |
'', |
| 73 |
'20161202235959', |
| 74 |
[ |
| 75 |
$dbw->buildComparison( '<', [ 'timestampfield' => $dbw->timestamp( '20161203000000' ) ] ), |
| 76 |
], |
| 77 |
], |
| 78 |
[ |
| 79 |
'20161201000000', |
| 80 |
'', |
| 81 |
[ |
| 82 |
$dbw->buildComparison( '>=', [ 'timestampfield' => $dbw->timestamp( '20161201000000' ) ] ), |
| 83 |
], |
| 84 |
], |
| 85 |
[ '', '', [] ], |
| 86 |
]; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public function testGetDateRangeCondInvalid( $start, $end ) { |
| 94 |
$pager = $this->getMockForAbstractClass( RangeChronologicalPager::class ); |
| 95 |
$this->assertNull( $pager->getDateRangeCond( $start, $end ) ); |
| 96 |
} |
| 97 |
|
| 98 |
public static function getDateRangeCondInvalidProvider() { |
| 99 |
return [ |
| 100 |
[ '-2016-12-01', '2017-12-01', ], |
| 101 |
[ '2016-12-01', '-2017-12-01', ], |
| 102 |
[ 'abcdefghij', 'klmnopqrstu', ], |
| 103 |
]; |
| 104 |
} |
| 105 |
|
| 106 |
} |