Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / pager / TablePagerTest.php
blobf7d63511cd7bc05206958e5aef470593d31515a4
1 <?php
3 use HtmlFormatter\HtmlFormatter;
4 use MediaWiki\Pager\TablePager;
5 use Wikimedia\Rdbms\FakeResultWrapper;
6 use Wikimedia\TestingAccessWrapper;
7 use Wikimedia\Zest\Zest;
9 /**
10 * @covers \MediaWiki\Pager\TablePager
11 * @group Database
13 class TablePagerTest extends MediaWikiIntegrationTestCase {
14 /**
15 * @var TestingAccessWrapper
17 private $tablePager;
19 protected function setUp(): void {
20 parent::setUp();
22 // Create a concrete implementation of the abstract TablePager
23 $pager = new class extends TablePager {
24 public function getFieldNames() {
25 return [
26 'id' => 'ID',
27 'name' => 'Name'
31 public function getQueryInfo() {
32 return [
33 'tables' => 'dummy_table',
34 'fields' => [ 'id', 'name' ],
35 'conds' => [],
39 public function getIndexField() {
40 return 'id';
43 public function formatValue( $name, $value ) {
44 return htmlspecialchars( $value );
47 protected function isFieldSortable( $field ) {
48 return false;
51 public function getDefaultSort() {
52 return '';
55 public function reallyDoQuery( $offset, $limit, $order ) {
56 $rows = [
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 );
70 /**
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
75 * @return DOMDocument
77 private static function getOutputHtml( string $markup ) {
78 $html = HtmlFormatter::wrapHTML( $markup );
79 return ( new HtmlFormatter( $html ) )->getDoc();
82 protected function tearDown(): void {
83 parent::tearDown();
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 ];
92 // Check the colspan
93 $this->assertEquals(
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 );