| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\DiscussionTools\Tests; |
| 4 |
|
| 5 |
use ImportStringSource; |
| 6 |
use MediaWiki\Extension\DiscussionTools\ThreadItemStore; |
| 7 |
use MediaWiki\Registration\ExtensionRegistry; |
| 8 |
use MediaWiki\Title\TitleValue; |
| 9 |
use TestUser; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class ThreadItemStoreTest extends IntegrationTestCase { |
| 17 |
|
| 18 |
protected function setUp(): void { |
| 19 |
parent::setUp(); |
| 20 |
|
| 21 |
if ( |
| 22 |
$this->getDb()->getType() === 'mysql' && |
| 23 |
strpos( $this->getDb()->getSoftwareLink(), 'MySQL' ) && |
| 24 |
!$this->getCliArg( 'use-normal-tables' ) |
| 25 |
) { |
| 26 |
$this->markTestSkipped( 'Set PHPUNIT_USE_NORMAL_TABLES=1 env variable to run these tests, ' . |
| 27 |
'otherwise they would fail due to a MySQL bug with temporary tables (T256006)' ); |
| 28 |
} |
| 29 |
if ( ExtensionRegistry::getInstance()->isLoaded( 'Liquid Threads' ) ) { |
| 30 |
$this->overrideConfigValue( 'LqtTalkPages', false ); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
private function importTestCase( string $dir ) { |
| 35 |
|
| 36 |
new TestUser( 'X' ); |
| 37 |
new TestUser( 'Y' ); |
| 38 |
new TestUser( 'Z' ); |
| 39 |
|
| 40 |
|
| 41 |
$source = new ImportStringSource( static::getText( "$dir/dump.xml" ) ); |
| 42 |
$importer = $this->getServiceContainer() |
| 43 |
->getWikiImporterFactory() |
| 44 |
->getWikiImporter( $source, $this->getTestSysop()->getAuthority() ); |
| 45 |
|
| 46 |
$importer->setUsernamePrefix( 'import', true ); |
| 47 |
$importer->doImport(); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function testInsertThreadItems( string $dir ): void { |
| 54 |
$this->importTestCase( $dir ); |
| 55 |
|
| 56 |
|
| 57 |
$expected = []; |
| 58 |
$actual = []; |
| 59 |
$tables = [ |
| 60 |
'discussiontools_items' => [ 'it_id' ], |
| 61 |
'discussiontools_item_pages' => [ 'itp_id' ], |
| 62 |
|
| 63 |
|
| 64 |
'discussiontools_item_revisions' => [ 'itr_revision_id', 'itr_items_id', 'itr_itemid_id' ], |
| 65 |
'discussiontools_item_ids' => [ 'itid_id' ], |
| 66 |
]; |
| 67 |
foreach ( $tables as $table => $order ) { |
| 68 |
$expected[$table] = static::getJson( "../$dir/$table.json", true ); |
| 69 |
|
| 70 |
$res = $this->getDb()->newSelectQueryBuilder() |
| 71 |
->from( $table ) |
| 72 |
->field( '*' ) |
| 73 |
->caller( __METHOD__ ) |
| 74 |
->orderBy( $order ) |
| 75 |
->fetchResultSet(); |
| 76 |
foreach ( $res as $i => $row ) { |
| 77 |
foreach ( $row as $key => $val ) { |
| 78 |
if ( $key === 'it_timestamp' ) { |
| 79 |
|
| 80 |
$val = wfTimestampOrNull( TS_MW, $val ); |
| 81 |
} |
| 82 |
$actual[$table][$i][$key] = $val; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
if ( getenv( 'DISCUSSIONTOOLS_OVERWRITE_TESTS' ) ) { |
| 89 |
foreach ( $tables as $table => $order ) { |
| 90 |
static::overwriteJsonFile( "../$dir/$table.json", $actual[$table] ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
static::assertEquals( $expected, $actual ); |
| 95 |
} |
| 96 |
|
| 97 |
public static function provideInsertCases(): array { |
| 98 |
return [ |
| 99 |
[ 'cases/ThreadItemStore/1simple-example' ], |
| 100 |
[ 'cases/ThreadItemStore/2archived-section' ], |
| 101 |
[ 'cases/ThreadItemStore/3indistinguishable-comments' ], |
| 102 |
[ 'cases/ThreadItemStore/4transcluded-section' ], |
| 103 |
[ 'cases/ThreadItemStore/5changed-comment-indentation' ], |
| 104 |
[ 'cases/ThreadItemStore/6changed-heading-level' ], |
| 105 |
[ 'cases/ThreadItemStore/7identical-rev-timestamp' ], |
| 106 |
[ 'cases/ThreadItemStore/8indistinguishable-comments-same-page' ], |
| 107 |
]; |
| 108 |
} |
| 109 |
|
| 110 |
public function testFindSimple(): void { |
| 111 |
$this->importTestCase( 'cases/ThreadItemStore/1simple-example' ); |
| 112 |
|
| 113 |
$itemStore = $this->getServiceContainer()->getService( 'DiscussionTools.ThreadItemStore' ); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
$set = $itemStore->findNewestRevisionsByName( 'c-Y-20220720010200' ); |
| 118 |
$this->assertCount( 1, $set ); |
| 119 |
$this->assertEquals( 'ThreadItemStore1', $set[0]->getPage()->getDBkey() ); |
| 120 |
$this->assertEquals( 'c-Y-20220720010200', $set[0]->getName() ); |
| 121 |
|
| 122 |
|
| 123 |
$set = $itemStore->findNewestRevisionsByName( 'c-blah' ); |
| 124 |
$this->assertCount( 0, $set ); |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
$set = $itemStore->findNewestRevisionsById( 'c-Y-20220720010200-X-20220720010100' ); |
| 129 |
$this->assertCount( 1, $set ); |
| 130 |
$this->assertEquals( 'ThreadItemStore1', $set[0]->getPage()->getDBkey() ); |
| 131 |
$this->assertEquals( 'c-Y-20220720010200-X-20220720010100', $set[0]->getId() ); |
| 132 |
|
| 133 |
|
| 134 |
$set = $itemStore->findNewestRevisionsById( 'c-blah' ); |
| 135 |
$this->assertCount( 0, $set ); |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
$page = $this->getServiceContainer()->getPageStore()->getPageByName( NS_TALK, 'ThreadItemStore1' ); |
| 140 |
$set = $itemStore->findNewestRevisionsByHeading( 'A', $page->getId(), TitleValue::newFromPage( $page ) ); |
| 141 |
$this->assertCount( 1, $set ); |
| 142 |
$this->assertEquals( 'ThreadItemStore1', $set[0]->getPage()->getDBkey() ); |
| 143 |
|
| 144 |
|
| 145 |
$set = $itemStore->findNewestRevisionsByHeading( 'blah', $page->getId(), TitleValue::newFromPage( $page ) ); |
| 146 |
$this->assertCount( 0, $set ); |
| 147 |
|
| 148 |
|
| 149 |
$set = $itemStore->findNewestRevisionsByHeading( 'A', 12345, new TitleValue( NS_TALK, 'Blah' ) ); |
| 150 |
$this->assertCount( 1, $set ); |
| 151 |
} |
| 152 |
|
| 153 |
public function testFindArchived(): void { |
| 154 |
$this->importTestCase( 'cases/ThreadItemStore/2archived-section' ); |
| 155 |
|
| 156 |
$itemStore = $this->getServiceContainer()->getService( 'DiscussionTools.ThreadItemStore' ); |
| 157 |
|
| 158 |
|
| 159 |
$set = $itemStore->findNewestRevisionsByName( 'c-X-20220720020100' ); |
| 160 |
$this->assertCount( 2, $set ); |
| 161 |
|
| 162 |
|
| 163 |
$set = $itemStore->findNewestRevisionsById( 'c-X-20220720020100-A' ); |
| 164 |
$this->assertCount( 2, $set ); |
| 165 |
|
| 166 |
|
| 167 |
$page = $this->getServiceContainer()->getPageStore()->getPageByName( NS_TALK, 'ThreadItemStore2' ); |
| 168 |
$set = $itemStore->findNewestRevisionsByHeading( 'A', $page->getId(), TitleValue::newFromPage( $page ) ); |
| 169 |
$this->assertCount( 2, $set ); |
| 170 |
} |
| 171 |
} |