3 use HtmlFormatter\HtmlFormatter
;
4 use MediaWiki\Pager\TablePager
;
5 use Wikimedia\Rdbms\FakeResultWrapper
;
6 use Wikimedia\TestingAccessWrapper
;
7 use Wikimedia\Zest\Zest
;
10 * @covers \MediaWiki\Pager\TablePager
13 class TablePagerTest
extends MediaWikiIntegrationTestCase
{
15 * @var TestingAccessWrapper
19 protected function setUp(): void
{
22 // Create a concrete implementation of the abstract TablePager
23 $pager = new class extends TablePager
{
24 public function getFieldNames() {
31 public function getQueryInfo() {
33 'tables' => 'dummy_table',
34 'fields' => [ 'id', 'name' ],
39 public function getIndexField() {
43 public function formatValue( $name, $value ) {
44 return htmlspecialchars( $value );
47 protected function isFieldSortable( $field ) {
51 public function getDefaultSort() {
55 public function reallyDoQuery( $offset, $limit, $order ) {
57 [ 'id' => 1, 'name' => 'foo' ],
58 [ 'id' => 2, 'name' => 'bar' ],
61 return new FakeResultWrapper( $rows );
65 // Wrap our instance with TestingAccessWrapper so that private/protected
66 // methods are accessible in our tests
67 $this->tablePager
= TestingAccessWrapper
::newFromObject( $pager );
71 * Convenience function to convert strings of markup into a parse-able document
72 * that we can query with tools like Zest.
74 * @param string $markup
77 private static function getOutputHtml( string $markup ) {
78 $html = HtmlFormatter
::wrapHTML( $markup );
79 return ( new HtmlFormatter( $html ) )->getDoc();
82 protected function tearDown(): void
{
86 public function testGetEmptyBody() {
87 $doc = self
::getOutputHtml( $this->tablePager
->getEmptyBody() );
89 // Find the <td> element inside that document with Zest
90 $td = Zest
::find( 'td', $doc )[ 0 ];
94 count( $this->tablePager
->getFieldNames() ),
95 $td->getAttribute( 'colspan' )
99 public function testGetCellAttrs() {
100 $field1 = $this->tablePager
->getFieldNames()[ 'id' ];
101 $expected1 = 'TablePager_col_' . $field1;
103 $field2 = $this->tablePager
->getFieldNames()[ 'name' ];
104 $expected2 = 'TablePager_col_' . $field2;
106 // Test that cells have expected class names
107 $this->assertEquals( $expected1, $this->tablePager
->getCellAttrs( $field1, '' )[ 'class' ] );
108 $this->assertEquals( $expected2, $this->tablePager
->getCellAttrs( $field2, '' )[ 'class' ] );
111 public function testGetBodyOutput() {
112 $doc = self
::getOutputHtml( $this->tablePager
->getBodyOutput()->getRawText() );
113 $table = Zest
::find( 'table', $doc )[ 0 ];
114 $rows = Zest
::find( 'tr', $doc );
116 // Test that the <table> has the correct class name
117 $this->assertEquals( 'mw-datatable', $table->getAttribute( 'class' ) );
118 // Test that there are 3 rows: 2 for content plus the header
119 $this->assertCount( 3, $rows );
120 // Test that the first row contains table header elements
121 $this->assertCount( 2, Zest
::find( 'th', $rows[ 0 ] ) );
122 // Test that the row has the correct number of columns
123 $this->assertCount( 2, Zest
::find( 'td', $rows[ 1 ] ) );
124 // Test that rows don't have any CSS class by default
125 $this->assertCount( 0, $rows[ 1 ]->attributes
);