3 use HtmlFormatter\HtmlFormatter
;
4 use MediaWiki\Pager\CodexTablePager
;
5 use Wikimedia\Rdbms\FakeResultWrapper
;
6 use Wikimedia\TestingAccessWrapper
;
7 use Wikimedia\Zest\Zest
;
10 * @covers \MediaWiki\Pager\CodexTablePager
13 class CodexTablePagerTest
extends MediaWikiIntegrationTestCase
{
15 * @var TestingAccessWrapper
19 protected function setUp(): void
{
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() {
32 public function getQueryInfo() {
34 'tables' => 'dummy_table',
35 'fields' => [ 'id', 'name' ],
40 public function getIndexField() {
44 public function formatValue( $name, $value ) {
45 return htmlspecialchars( $value );
48 protected function isFieldSortable( $field ) {
52 public function getDefaultSort() {
56 public function reallyDoQuery( $offset, $limit, $order ) {
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 );
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
78 private static function getOutputHtml( string $markup ) {
79 $html = HtmlFormatter
::wrapHTML( $markup );
80 return ( new HtmlFormatter( $html ) )->getDoc();
83 protected function tearDown(): void
{
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 ];
95 count( $this->pager
->getFieldNames() ),
96 $td->getAttribute( 'colspan' )
100 public function testGetCellAttrs() {
101 $field1 = $this->pager
->getFieldNames()[ 'id' ];
104 'cdx-table-pager__col--' . $field1,
105 'cdx-table__table__cell--align-' . $this->pager
->getCellAlignment( $field1 )
109 $field2 = $this->pager
->getFieldNames()[ 'name' ];
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 );