Debian Codegraph @

extensions/DiscussionTools/tests/phpunit/ThreadItemStoreTest.php

raw symbols SCIP
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 * @group DiscussionTools
13 * @group Database
14 * @covers \MediaWiki\Extension\DiscussionTools\ThreadItemStore
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 // Create users for the imported revisions
36 new TestUser( 'X' );
37 new TestUser( 'Y' );
38 new TestUser( 'Z' );
39
40 // Import revisions
41 $source = new ImportStringSource( static::getText( "$dir/dump.xml" ) );
42 $importer = $this->getServiceContainer()
43 ->getWikiImporterFactory()
44 ->getWikiImporter( $source, $this->getTestSysop()->getAuthority() );
45 // `true` means to assign edits to the users we created above
46 $importer->setUsernamePrefix( 'import', true );
47 $importer->doImport();
48 }
49
50 /**
51 * @dataProvider provideInsertCases
52 */
53 public function testInsertThreadItems( string $dir ): void {
54 $this->importTestCase( $dir );
55
56 // Check that expected data has been stored in the database
57 $expected = [];
58 $actual = [];
59 $tables = [
60 'discussiontools_items' => [ 'it_id' ],
61 'discussiontools_item_pages' => [ 'itp_id' ],
62 // We reuse rows causing the primary key to be all out of order.
63 // Use a consistent ordering for the output here.
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 // Normalize timestamp values returned by different database engines (T370671)
80 $val = wfTimestampOrNull( TS_MW, $val );
81 }
82 $actual[$table][$i][$key] = $val;
83 }
84 }
85 }
86
87 // Optionally write updated content to the JSON files
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 /** @var ThreadItemStore $itemStore */
113 $itemStore = $this->getServiceContainer()->getService( 'DiscussionTools.ThreadItemStore' );
114
115 // BY NAME
116 // Valid name finds the item
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 // Wrong name - no results
123 $set = $itemStore->findNewestRevisionsByName( 'c-blah' );
124 $this->assertCount( 0, $set );
125
126 // BY ID
127 // Valid ID finds the item
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 // Wrong ID - no results
134 $set = $itemStore->findNewestRevisionsById( 'c-blah' );
135 $this->assertCount( 0, $set );
136
137 // BY HEADING
138 // Valid heading + page title finds the item
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 // Wrong heading - no results
145 $set = $itemStore->findNewestRevisionsByHeading( 'blah', $page->getId(), TitleValue::newFromPage( $page ) );
146 $this->assertCount( 0, $set );
147
148 // Wrong page - but we still get a result, since it's unique (case 3.)
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 /** @var ThreadItemStore $itemStore */
156 $itemStore = $this->getServiceContainer()->getService( 'DiscussionTools.ThreadItemStore' );
157
158 // Valid name finds the original item in old revision, and the item in the archive
159 $set = $itemStore->findNewestRevisionsByName( 'c-X-20220720020100' );
160 $this->assertCount( 2, $set );
161
162 // Valid ID finds the original item in old revision, and the item in the archive
163 $set = $itemStore->findNewestRevisionsById( 'c-X-20220720020100-A' );
164 $this->assertCount( 2, $set );
165
166 // Valid heading + page title finds the original item in old revision, and the item in the archive
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 }