Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / import / ImportableOldRevisionImporterTest.php
blobab127c7604f1704967fc4e1083177a7263f220a5
1 <?php
3 use MediaWiki\Content\ContentHandler;
4 use MediaWiki\Revision\SlotRecord;
5 use MediaWiki\Storage\PageUpdatedEvent;
6 use MediaWiki\Tests\Language\LanguageEventIngressSpyTrait;
7 use MediaWiki\Tests\recentchanges\ChangeTrackingEventIngressSpyTrait;
8 use MediaWiki\Tests\Search\SearchEventIngressSpyTrait;
9 use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
10 use MediaWiki\Title\Title;
11 use PHPUnit\Framework\Assert;
12 use Psr\Log\NullLogger;
14 /**
15 * @group Database
16 * @covers \ImportableOldRevisionImporter
18 class ImportableOldRevisionImporterTest extends MediaWikiIntegrationTestCase {
19 use TempUserTestTrait;
20 use ChangeTrackingEventIngressSpyTrait;
21 use SearchEventIngressSpyTrait;
22 use LanguageEventIngressSpyTrait;
24 protected function setUp(): void {
25 parent::setUp();
27 $this->getServiceContainer()->getChangeTagsStore()->defineTag( 'tag1' );
30 public static function provideTestCases() {
31 yield [ [] ];
32 yield [ [ "tag1" ] ];
35 /**
36 * @param Title $title
38 * @return WikiRevision
39 * @throws MWContentSerializationException
40 * @throws MWUnknownContentModelException
42 private function getWikiRevision( Title $title ): WikiRevision {
43 $revision = new WikiRevision();
44 $revision->setTitle( $title );
45 $content = ContentHandler::makeContent( 'dummy edit', $title );
46 $revision->setContent( SlotRecord::MAIN, $content );
48 return $revision;
51 private function getImporter(): ImportableOldRevisionImporter {
52 $services = $this->getServiceContainer();
54 return new ImportableOldRevisionImporter(
55 true,
56 new NullLogger(),
57 $services->getConnectionProvider(),
58 $services->getRevisionStoreFactory()->getRevisionStoreForImport(),
59 $services->getSlotRoleRegistry(),
60 $services->getWikiPageFactory(),
61 $services->getPageUpdaterFactory(),
62 $services->getUserFactory(),
63 $services->getDomainEventDispatcher()
67 /**
68 * @dataProvider provideTestCases
70 public function testImport( $expectedTags ) {
71 $title = Title::newFromText( __CLASS__ . rand() );
72 $revision = $this->getWikiRevision( $title );
73 $revision->setTags( $expectedTags );
75 $importer = $this->getImporter();
76 $result = $importer->import( $revision );
77 $this->assertTrue( $result );
79 $tags = $this->getServiceContainer()->getChangeTagsStore()->getTags(
80 $this->getDb(),
81 null,
82 $title->getLatestRevID()
84 $this->assertSame( $expectedTags, $tags );
87 /**
88 * @covers \MediaWiki\Revision\RevisionStoreFactory::getRevisionStoreForImport
89 * @covers \MediaWiki\User\ActorMigration::newMigrationForImport
90 * @covers \MediaWiki\User\ActorStoreFactory::getActorStoreForImport
91 * @covers \MediaWiki\User\ActorStoreFactory::getActorNormalizationForImport
92 * @dataProvider provideTestCases
94 public function testImportWithTempAccountsEnabled( $expectedTags ) {
95 $this->enableAutoCreateTempUser();
96 $this->testImport( $expectedTags );
99 private function makeDomainEventSourceListener(
100 int &$counter,
101 $new
103 $flags = [
104 PageUpdatedEvent::FLAG_IMPORTED => true,
105 PageUpdatedEvent::FLAG_AUTOMATED => true,
106 PageUpdatedEvent::FLAG_SILENT => true,
109 return static function ( PageUpdatedEvent $event ) use (
110 &$counter, $flags, $new
112 Assert::assertTrue( $event->isRevisionChange(), 'isPurge' );
113 Assert::assertSame( $new, $event->isNew(), 'isNew' );
115 foreach ( $flags as $name => $value ) {
116 Assert::assertSame( $value, $event->hasFlag( $name ), $name );
119 $counter++;
124 * Check that importing revisions for a non-existing page emits a
125 * PageUpdatedEvent indicating page creation.
127 public function testEventEmission_new() {
128 $calls = 0;
130 $title = Title::newFromText( __CLASS__ . rand() );
132 $this->getServiceContainer()->getDomainEventSource()->registerListener(
133 'PageUpdated',
134 $this->makeDomainEventSourceListener( $calls, true )
137 // Perform an import
138 $importer = $this->getImporter();
140 $revision = $this->getWikiRevision( $title );
141 $importer->import( $revision );
143 $this->runDeferredUpdates();
144 $this->assertSame( 1, $calls );
148 * Check that importing an old revision for an existing page does not emit
149 * a PageUpdatedEvent.
151 public function testEventEmission_old() {
152 $calls = 0;
154 $page = $this->getExistingTestPage();
155 $title = $page->getTitle();
157 $this->getServiceContainer()->getDomainEventSource()->registerListener(
158 'PageUpdated',
159 $this->makeDomainEventSourceListener( $calls, false )
162 // Import an old revision
163 $importer = $this->getImporter();
165 $revision = $this->getWikiRevision( $title );
166 $revision->setTimestamp( '20110101223344' );
167 $importer->import( $revision );
169 $this->runDeferredUpdates();
170 $this->assertSame( 0, $calls );
174 * Check that importing a new revision for an existing page emits
175 * a PageUpdatedEvent.
177 public function testEventEmission_current() {
178 $calls = 0;
180 MWTimestamp::setFakeTime( '20110101223344' );
181 $page = $this->getExistingTestPage();
182 $title = $page->getTitle();
184 $this->getServiceContainer()->getDomainEventSource()->registerListener(
185 'PageUpdated',
186 $this->makeDomainEventSourceListener( $calls, false )
189 // Import latest revision
190 $importer = $this->getImporter();
192 $revision = $this->getWikiRevision( $title );
193 $revision->setTimestamp( '20240101223344' );
194 $importer->import( $revision );
196 $this->runDeferredUpdates();
197 $this->assertSame( 1, $calls );
201 * Regression test for T381225
203 public function testEventPropagation() {
204 // Make sure SearchEventIngress is triggered and tries to re-index the page
205 $this->installChangeTrackingEventIngressSpyForImport();
206 $this->installSearchEventIngressSpyForImport();
207 $this->installLanguageEventIngressSpyForImport();
209 // Now perform the import
210 $title = Title::makeTitle( NS_MEDIAWIKI, __CLASS__ . rand() );
211 $revision = $this->getWikiRevision( $title );
212 $importer = $this->getImporter();
213 $importer->import( $revision );