1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
<?php
namespace MediaWiki\Tests\Api;
use MediaWiki\Page\Event\PageRevisionUpdatedEvent;
use MediaWiki\Tests\ExpectCallbackTrait;
use MediaWiki\Tests\recentchanges\ChangeTrackingUpdateSpyTrait;
use MediaWiki\Tests\Search\SearchUpdateSpyTrait;
use PHPUnit\Framework\Assert;
/**
* Tests for MediaWiki api.php?action=import.
*
* @group API
* @group Database
* @group medium
*
* @covers \MediaWiki\Api\ApiImport
*/
class ApiImportTest extends ApiUploadTestCase {
use ChangeTrackingUpdateSpyTrait;
use SearchUpdateSpyTrait;
use ExpectCallbackTrait;
public function tearDown(): void {
// phpcs:ignore MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
unset( $_FILES['xml'] );
parent::tearDown();
}
public function testImport() {
$title = $this->getNonexistingTestPage()->getTitle();
// We expect two PageRevisionUpdated events, one triggered by
// ImportableOldRevisionImporter when importing the latest revision;
// And one triggered by ApiImportReporter when creating a dummy revision.
// NOTE: it's not clear whether this is intentional or desirable!
$calls = 0;
// Declare expectations
$this->expectDomainEvent(
PageRevisionUpdatedEvent::TYPE, 2,
static function ( PageRevisionUpdatedEvent $event ) use ( &$calls, $title ) {
$calls++;
Assert::assertTrue( $event->getPage()->isSamePageAs( $title ) );
Assert::assertTrue(
$event->hasCause( PageRevisionUpdatedEvent::CAUSE_IMPORT ),
PageRevisionUpdatedEvent::CAUSE_IMPORT
);
Assert::assertTrue( $event->isSilent(), 'isSilent' );
Assert::assertTrue( $event->isImplicit(), 'isImplicit' );
if ( $calls === 1 ) {
// First call, from ImportableOldRevisionImporter
Assert::assertTrue( $event->isCreation(), 'isCreation' );
Assert::assertTrue( $event->changedCurrentRevisionId(), 'changedCurrentRevisionId' );
Assert::assertTrue( $event->isEffectiveContentChange(), 'isEffectiveContentChange' );
Assert::assertTrue( $event->isNominalContentChange(), 'isNominalContentChange' );
Assert::assertTrue( $event->isSilent(), 'isSilent' );
Assert::assertFalse(
$event->getNewRevision()->isMinor(),
'isMinor'
);
} else {
// Second call, from ApiImportReporter
Assert::assertFalse( $event->isCreation(), 'isCreation' );
Assert::assertTrue( $event->changedCurrentRevisionId(), 'changedCurrentRevisionId' );
Assert::assertFalse( $event->isEffectiveContentChange(), 'isEffectiveContentChange' );
Assert::assertFalse( $event->isNominalContentChange(), 'isNominalContentChange' );
Assert::assertTrue( $event->isSilent(), 'isSilent' );
Assert::assertTrue(
$event->getNewRevision()->isMinor(),
'isMinor'
);
}
}
);
// Hooks fired by PageUpdater
$this->expectHook( 'RevisionFromEditComplete', 1 );
$this->expectHook( 'PageSaveComplete', 1 );
// Expect only non-edit recent changes entry, but no edit count
// or user talk.
$this->expectChangeTrackingUpdates( 0, 1, 0, 0 );
// Expect search updates to be triggered
$this->expectSearchUpdates( 1 );
// Prepare the fake upload XML
$dumpData = file_get_contents( __DIR__ . '/../../data/import/Basic.import-1.xml' );
$dumpData = str_replace(
'{{page1_title}}',
$title->getPrefixedDBkey(),
$dumpData
);
// NOTE: We can't use $this->fakeUploadChunk(), because ImportStreamSource
// doesn't use WebRequest but hits $_FILES directly.
$tmpName = $this->getNewTempFile();
file_put_contents( $tmpName, $dumpData );
// phpcs:ignore MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
$_FILES['xml'] = [
'name' => 'xml',
'type' => 'application/xml',
'tmp_name' => $tmpName,
'size' => strlen( $dumpData ),
'error' => UPLOAD_ERR_OK,
];
// Do the import
$admin = $this->getTestSysop()->getAuthority();
$apiResult = $this->doApiRequestWithToken( [
'action' => 'import',
'interwikiprefix' => 'test'
], null, $admin )[0];
// check response
$this->assertArrayHasKey( 'import', $apiResult );
$this->assertArrayHasKey( 0, $apiResult['import'] );
$this->assertArrayHasKey( 'title', $apiResult['import'][0] );
$this->assertSame( $title->getText(), $apiResult['import'][0]['title'] );
$this->assertArrayHasKey( 'revisions', $apiResult['import'][0] );
$this->assertSame( 1, $apiResult['import'][0]['revisions'] );
// check that the page exists now
$page = $this->getServiceContainer()->getPageStore()
->getPageByReference( $title );
$this->assertTrue( $page->exists() );
}
}
|