Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / pager / CodexTablePagerTest.php
blob60f4585dec7ef2aaf512941c367142cf7d45db91
1 <?php
3 use HtmlFormatter\HtmlFormatter;
4 use MediaWiki\Pager\CodexTablePager;
5 use Wikimedia\Rdbms\FakeResultWrapper;
6 use Wikimedia\TestingAccessWrapper;
7 use Wikimedia\Zest\Zest;
9 /**
10 * @covers \MediaWiki\Pager\CodexTablePager
11 * @group Database
13 class CodexTablePagerTest extends MediaWikiIntegrationTestCase {
14 /**
15 * @var TestingAccessWrapper
17 private $pager;
19 protected function setUp(): void {
20 parent::setUp();
22 // Create a concrete implementation of the abstract CodexTablePager.
23 // The parent class requires a constructor argument, hence the parentheses.
24 $pager = new class( "Dummy Caption" ) extends CodexTablePager {
25 public function getFieldNames() {
26 return [
27 'id' => 'ID',
28 'name' => 'Name'
32 public function getQueryInfo() {
33 return [
34 'tables' => 'dummy_table',
35 'fields' => [ 'id', 'name' ],
36 'conds' => [],
40 public function getIndexField() {
41 return 'id';
44 public function formatValue( $name, $value ) {
45 return htmlspecialchars( $value );
48 protected function isFieldSortable( $field ) {
49 return false;
52 public function getDefaultSort() {
53 return '';
56 public function reallyDoQuery( $offset, $limit, $order ) {
57 $rows = [
58 [ 'id' => 1, 'name' => 'foo' ],
59 [ 'id' => 2, 'name' => 'bar' ],
62 return new FakeResultWrapper( $rows );
66 // Wrap our instance with TestingAccessWrapper so that private/protected
67 // methods are accessible in our tests
68 $this->pager = TestingAccessWrapper::newFromObject( $pager );
71 /**
72 * Convenience function to convert strings of markup into a parse-able document
73 * that we can query with tools like Zest.
75 * @param string $markup
76 * @return DOMDocument
78 private static function getOutputHtml( string $markup ) {
79 $html = HtmlFormatter::wrapHTML( $markup );
80 return ( new HtmlFormatter( $html ) )->getDoc();
83 protected function tearDown(): void {
84 parent::tearDown();
87 public function testGetEmptyBody() {
88 $doc = self::getOutputHtml( $this->pager->getEmptyBody() );
90 // Find the <td> element inside that document with Zest
91 $td = Zest::find( 'td', $doc )[ 0 ];
93 // Check the colspan
94 $this->assertEquals(
95 count( $this->pager->getFieldNames() ),
96 $td->getAttribute( 'colspan' )
100 public function testGetCellAttrs() {
101 $field1 = $this->pager->getFieldNames()[ 'id' ];
102 $expected1 = [
103 'class' => [
104 'cdx-table-pager__col--' . $field1,
105 'cdx-table__table__cell--align-' . $this->pager->getCellAlignment( $field1 )
109 $field2 = $this->pager->getFieldNames()[ 'name' ];
110 $expected2 = [
111 'class' => [
112 'cdx-table-pager__col--' . $field2,
113 'cdx-table__table__cell--align-' . $this->pager->getCellAlignment( $field2 )
117 // Test that cells have expected class names
118 $this->assertEquals( $expected1, $this->pager->getCellAttrs( $field1, '' ) );
119 $this->assertEquals( $expected2, $this->pager->getCellAttrs( $field2, '' ) );
122 public function testGetBodyOutput() {
123 $doc = self::getOutputHtml( $this->pager->getBodyOutput()->getRawText() );
124 $table = Zest::find( 'table', $doc )[ 0 ];
125 $rows = Zest::find( 'tr', $doc );
126 $caption = Zest::find( 'caption', $doc );
127 $captionText = $caption[ 0 ]->textContent;
128 // Test that the <table> has the correct class name
129 $this->assertEquals( 'cdx-table__table', $table->getAttribute( 'class' ) );
130 // Test that there are 3 rows: 2 for content plus the header
131 $this->assertCount( 3, $rows );
132 // Test that the first row contains table header elements
133 $this->assertCount( 2, Zest::find( 'th', $rows[ 0 ] ) );
134 // Test that the row has the correct number of columns
135 $this->assertCount( 2, Zest::find( 'td', $rows[ 1 ] ) );
136 // Test that rows don't have any CSS class by default
137 $this->assertCount( 0, $rows[ 1 ]->attributes );
138 // Test that the table includes a <caption> element
139 $this->assertCount( 1, $caption );
140 $this->assertEquals( "Dummy Caption", $captionText );