| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\DiscussionTools\Tests; |
| 4 |
|
| 5 |
use DateTimeImmutable; |
| 6 |
use DateTimeZone; |
| 7 |
use MediaWiki\Extension\DiscussionTools\CommentUtils; |
| 8 |
use MediaWiki\Extension\DiscussionTools\ImmutableRange; |
| 9 |
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentCommentItem; |
| 10 |
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentHeadingItem; |
| 11 |
use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem; |
| 12 |
use MediaWiki\Extension\DiscussionTools\ThreadItem\ThreadItem; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class ContentThreadItemTest extends IntegrationTestCase { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public function testGetAuthorsOrThreadItemsBelow( |
| 26 |
array $thread, array $expectedAuthors, array $expectedThreadItemIds |
| 27 |
): void { |
| 28 |
$doc = $this->createDocument( '' ); |
| 29 |
$node = $doc->createElement( 'div' ); |
| 30 |
$range = new ImmutableRange( $node, 0, $node, 0 ); |
| 31 |
|
| 32 |
$makeThreadItem = static function ( array $arr ) use ( &$makeThreadItem, $range ): ContentThreadItem { |
| 33 |
if ( $arr['type'] === 'comment' ) { |
| 34 |
$item = new ContentCommentItem( |
| 35 |
1, $range, false, [], [], new DateTimeImmutable( 'now', new DateTimeZone( 'UTC' ) ), |
| 36 |
$arr['author'], |
| 37 |
$arr['displayName'] ?? null |
| 38 |
); |
| 39 |
} else { |
| 40 |
$item = new ContentHeadingItem( $range, false, 2 ); |
| 41 |
} |
| 42 |
$item->setId( $arr['id'] ); |
| 43 |
foreach ( $arr['replies'] as $reply ) { |
| 44 |
$item->addReply( $makeThreadItem( $reply ) ); |
| 45 |
} |
| 46 |
return $item; |
| 47 |
}; |
| 48 |
|
| 49 |
$threadItem = $makeThreadItem( $thread ); |
| 50 |
|
| 51 |
static::assertEquals( $expectedAuthors, $threadItem->getAuthorsBelow() ); |
| 52 |
static::assertEquals( $expectedThreadItemIds, array_map( static function ( ThreadItem $threadItem ): string { |
| 53 |
return $threadItem->getId(); |
| 54 |
}, $threadItem->getThreadItemsBelow() ) ); |
| 55 |
} |
| 56 |
|
| 57 |
public static function provideAuthors(): array { |
| 58 |
return static::getJson( '../cases/authors.json' ); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function testGetTranscludedFrom( |
| 65 |
string $name, string $title, string $dom, string $expected, string $config, string $data |
| 66 |
): void { |
| 67 |
$dom = static::getHtml( $dom ); |
| 68 |
$expectedPath = $expected; |
| 69 |
$expected = static::getJson( $expected ); |
| 70 |
$config = static::getJson( $config ); |
| 71 |
$data = static::getJson( $data ); |
| 72 |
|
| 73 |
$title = $this->createTitleParser( $config )->parseTitle( $title ); |
| 74 |
|
| 75 |
$doc = static::createDocument( $dom ); |
| 76 |
$container = static::getThreadContainer( $doc ); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
CommentUtils::unwrapParsoidSections( $container ); |
| 81 |
|
| 82 |
$threadItemSet = $this->createParser( $config, $data )->parse( $container, $title ); |
| 83 |
$comments = $threadItemSet->getCommentItems(); |
| 84 |
|
| 85 |
$transcludedFrom = []; |
| 86 |
foreach ( $comments as $comment ) { |
| 87 |
$transcludedFrom[ $comment->getId() ] = $comment->getTranscludedFrom(); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) { |
| 92 |
static::overwriteJsonFile( $expectedPath, $transcludedFrom ); |
| 93 |
} |
| 94 |
|
| 95 |
static::assertEquals( |
| 96 |
$expected, |
| 97 |
$transcludedFrom, |
| 98 |
$name |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
public static function provideTranscludedFrom(): array { |
| 103 |
return static::getJson( '../cases/transcluded.json' ); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function testGetText( |
| 110 |
string $name, string $title, string $dom, string $expected, string $config, string $data |
| 111 |
): void { |
| 112 |
$dom = static::getHtml( $dom ); |
| 113 |
$expectedPath = $expected; |
| 114 |
$expected = static::getJson( $expected ); |
| 115 |
$config = static::getJson( $config ); |
| 116 |
$data = static::getJson( $data ); |
| 117 |
|
| 118 |
$doc = static::createDocument( $dom ); |
| 119 |
$container = static::getThreadContainer( $doc ); |
| 120 |
|
| 121 |
$title = $this->createTitleParser( $config )->parseTitle( $title ); |
| 122 |
$threadItemSet = $this->createParser( $config, $data )->parse( $container, $title ); |
| 123 |
$items = $threadItemSet->getThreadItems(); |
| 124 |
|
| 125 |
$output = []; |
| 126 |
foreach ( $items as $item ) { |
| 127 |
$output[ $item->getId() ] = CommentUtils::htmlTrim( |
| 128 |
$item instanceof ContentCommentItem ? $item->getBodyText( true ) : $item->getText() |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) { |
| 134 |
static::overwriteJsonFile( $expectedPath, $output ); |
| 135 |
} |
| 136 |
|
| 137 |
static::assertEquals( |
| 138 |
$expected, |
| 139 |
$output, |
| 140 |
$name |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
public static function provideGetText(): array { |
| 145 |
return static::getJson( '../cases/getText.json' ); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public function testGetHTML( |
| 152 |
string $name, string $title, string $dom, string $expected, string $config, string $data |
| 153 |
): void { |
| 154 |
$dom = static::getHtml( $dom ); |
| 155 |
$expectedPath = $expected; |
| 156 |
$expected = static::getJson( $expected ); |
| 157 |
$config = static::getJson( $config ); |
| 158 |
$data = static::getJson( $data ); |
| 159 |
|
| 160 |
$doc = static::createDocument( $dom ); |
| 161 |
$container = static::getThreadContainer( $doc ); |
| 162 |
|
| 163 |
$title = $this->createTitleParser( $config )->parseTitle( $title ); |
| 164 |
$threadItemSet = $this->createParser( $config, $data )->parse( $container, $title ); |
| 165 |
$items = $threadItemSet->getThreadItems(); |
| 166 |
|
| 167 |
$output = []; |
| 168 |
foreach ( $items as $item ) { |
| 169 |
$output[ $item->getId() ] = CommentUtils::htmlTrim( |
| 170 |
$item instanceof ContentCommentItem ? $item->getBodyHTML( true ) : $item->getHTML() |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) { |
| 176 |
static::overwriteJsonFile( $expectedPath, $output ); |
| 177 |
} |
| 178 |
|
| 179 |
static::assertEquals( |
| 180 |
$expected, |
| 181 |
$output, |
| 182 |
$name |
| 183 |
); |
| 184 |
} |
| 185 |
|
| 186 |
public static function provideGetHTML(): array { |
| 187 |
return static::getJson( '../cases/getHTML.json' ); |
| 188 |
} |
| 189 |
|
| 190 |
} |