diff options
Diffstat (limited to 'www/wiki/tests/phpunit/includes/pager')
-rw-r--r-- | www/wiki/tests/phpunit/includes/pager/RangeChronologicalPagerTest.php | 99 | ||||
-rw-r--r-- | www/wiki/tests/phpunit/includes/pager/ReverseChronologicalPagerTest.php | 68 |
2 files changed, 167 insertions, 0 deletions
diff --git a/www/wiki/tests/phpunit/includes/pager/RangeChronologicalPagerTest.php b/www/wiki/tests/phpunit/includes/pager/RangeChronologicalPagerTest.php new file mode 100644 index 00000000..72390ac8 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/pager/RangeChronologicalPagerTest.php @@ -0,0 +1,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', ], + ]; + } + +} diff --git a/www/wiki/tests/phpunit/includes/pager/ReverseChronologicalPagerTest.php b/www/wiki/tests/phpunit/includes/pager/ReverseChronologicalPagerTest.php new file mode 100644 index 00000000..3910ab64 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/pager/ReverseChronologicalPagerTest.php @@ -0,0 +1,68 @@ +<?php + +/** + * Test class for ReverseChronologicalPagerTest methods. + * + * @group Pager + * + * @author Geoffrey Mon <geofbot@gmail.com> + */ +class ReverseChronologicalPagerTest extends MediaWikiLangTestCase { + + /** + * @covers ReverseChronologicalPager::getDateCond + */ + public function testGetDateCond() { + $pager = $this->getMockForAbstractClass( ReverseChronologicalPager::class ); + $timestamp = MWTimestamp::getInstance(); + $db = wfGetDB( DB_MASTER ); + + $currYear = $timestamp->format( 'Y' ); + $currMonth = $timestamp->format( 'n' ); + + // Test that getDateCond sets and returns mOffset + $this->assertEquals( $pager->getDateCond( 2006, 6 ), $pager->mOffset ); + + // Test year and month + $pager->getDateCond( 2006, 6 ); + $this->assertEquals( $pager->mOffset, $db->timestamp( '20060701000000' ) ); + + // Test year, month, and day + $pager->getDateCond( 2006, 6, 5 ); + $this->assertEquals( $pager->mOffset, $db->timestamp( '20060606000000' ) ); + + // Test month overflow into the next year + $pager->getDateCond( 2006, 12 ); + $this->assertEquals( $pager->mOffset, $db->timestamp( '20070101000000' ) ); + + // Test day overflow to the next month + $pager->getDateCond( 2006, 6, 30 ); + $this->assertEquals( $pager->mOffset, $db->timestamp( '20060701000000' ) ); + + // Test invalid month (should use end of year) + $pager->getDateCond( 2006, -1 ); + $this->assertEquals( $pager->mOffset, $db->timestamp( '20070101000000' ) ); + + // Test invalid day (should use end of month) + $pager->getDateCond( 2006, 6, 1337 ); + $this->assertEquals( $pager->mOffset, $db->timestamp( '20060701000000' ) ); + + // Test last day of year + $pager->getDateCond( 2006, 12, 31 ); + $this->assertEquals( $pager->mOffset, $db->timestamp( '20070101000000' ) ); + + // Test invalid day that overflows to next year + $pager->getDateCond( 2006, 12, 32 ); + $this->assertEquals( $pager->mOffset, $db->timestamp( '20070101000000' ) ); + + // Test month past current month (should use previous year) + if ( $currMonth < 5 ) { + $pager->getDateCond( -1, 5 ); + $this->assertEquals( $pager->mOffset, $db->timestamp( $currYear - 1 . '0601000000' ) ); + } + if ( $currMonth < 12 ) { + $pager->getDateCond( -1, 12 ); + $this->assertEquals( $pager->mOffset, $db->timestamp( $currYear . '0101000000' ) ); + } + } +} |