| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Unit\CommentFormatter; |
| 4 |
|
| 5 |
use ArrayIterator; |
| 6 |
use MediaWiki\CommentFormatter\CommentFormatter; |
| 7 |
use MediaWiki\CommentFormatter\RevisionCommentBatch; |
| 8 |
use MediaWiki\Permissions\Authority; |
| 9 |
use MediaWiki\Permissions\SimpleAuthority; |
| 10 |
use MediaWiki\User\UserIdentityValue; |
| 11 |
use MediaWikiUnitTestCase; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class RevisionCommentBatchTest extends MediaWikiUnitTestCase { |
| 19 |
private function getFormatter( $callback ) { |
| 20 |
return new class( $callback ) extends CommentFormatter { |
| 21 |
private $callback; |
| 22 |
|
| 23 |
public function __construct( $callback ) { |
| 24 |
$this->callback = $callback; |
| 25 |
} |
| 26 |
|
| 27 |
public function formatRevisions( |
| 28 |
$revisions, Authority $authority, $samePage = false, $isPublic = false, |
| 29 |
$useParentheses = true, $indexById = false |
| 30 |
) { |
| 31 |
( $this->callback )( get_defined_vars() ); |
| 32 |
} |
| 33 |
}; |
| 34 |
} |
| 35 |
|
| 36 |
private function newBatch( $callback ) { |
| 37 |
return new RevisionCommentBatch( |
| 38 |
$this->getFormatter( $callback ) |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
private function getAuthority() { |
| 43 |
return new SimpleAuthority( |
| 44 |
new UserIdentityValue( 1, 'Sysop' ), |
| 45 |
[] |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
public function testAuthority() { |
| 50 |
$authority = $this->getAuthority(); |
| 51 |
$batch = $this->newBatch( |
| 52 |
function ( $params ) use ( $authority ) { |
| 53 |
$this->assertSame( $authority, $params['authority'] ); |
| 54 |
} |
| 55 |
); |
| 56 |
$batch->authority( $authority )->execute(); |
| 57 |
} |
| 58 |
|
| 59 |
public function testNoAuthority() { |
| 60 |
$this->expectException( \TypeError::class ); |
| 61 |
$batch = $this->newBatch( |
| 62 |
static function ( $params ) { |
| 63 |
} |
| 64 |
); |
| 65 |
$batch |
| 66 |
->revisions( [] ) |
| 67 |
->execute(); |
| 68 |
} |
| 69 |
|
| 70 |
public function testRevisions() { |
| 71 |
$revisions = new ArrayIterator( [] ); |
| 72 |
$batch = $this->newBatch( |
| 73 |
function ( $params ) use ( $revisions ) { |
| 74 |
$this->assertSame( $revisions, $params['revisions'] ); |
| 75 |
|
| 76 |
$this->assertFalse( $params['samePage'] ); |
| 77 |
$this->assertFalse( $params['isPublic'] ); |
| 78 |
$this->assertFalse( $params['useParentheses'] ); |
| 79 |
$this->assertFalse( $params['indexById'] ); |
| 80 |
} |
| 81 |
); |
| 82 |
$batch |
| 83 |
->authority( $this->getAuthority() ) |
| 84 |
->revisions( $revisions ) |
| 85 |
->execute(); |
| 86 |
} |
| 87 |
|
| 88 |
public function testSamePage() { |
| 89 |
$batch = $this->newBatch( |
| 90 |
function ( $params ) { |
| 91 |
$this->assertTrue( $params['samePage'] ); |
| 92 |
} |
| 93 |
); |
| 94 |
$batch |
| 95 |
->authority( $this->getAuthority() ) |
| 96 |
->samePage() |
| 97 |
->execute(); |
| 98 |
} |
| 99 |
|
| 100 |
public function testUseParentheses() { |
| 101 |
$batch = $this->newBatch( |
| 102 |
function ( $params ) { |
| 103 |
$this->assertTrue( $params['useParentheses'] ); |
| 104 |
} |
| 105 |
); |
| 106 |
$batch |
| 107 |
->authority( $this->getAuthority() ) |
| 108 |
->useParentheses() |
| 109 |
->execute(); |
| 110 |
} |
| 111 |
|
| 112 |
public function hideIfPrivate() { |
| 113 |
$batch = $this->newBatch( |
| 114 |
function ( $params ) { |
| 115 |
$this->assertTrue( $params['isPublic'] ); |
| 116 |
} |
| 117 |
); |
| 118 |
$batch |
| 119 |
->authority( $this->getAuthority() ) |
| 120 |
->hideIfDeleted() |
| 121 |
->execute(); |
| 122 |
} |
| 123 |
|
| 124 |
public function indexById() { |
| 125 |
$batch = $this->newBatch( |
| 126 |
function ( $params ) { |
| 127 |
$this->assertTrue( $params['indexById'] ); |
| 128 |
} |
| 129 |
); |
| 130 |
$batch |
| 131 |
->authority( $this->getAuthority() ) |
| 132 |
->indexById() |
| 133 |
->execute(); |
| 134 |
} |
| 135 |
} |