MediaWiki  1.32.5
backup_LogTest.php
Go to the documentation of this file.
1 <?php
2 
4 
8 use Title;
9 use User;
11 
25 
26  // We'll add several log entries and users for this test. The following
27  // variables hold the corresponding ids.
28  private $userId1, $userId2;
29  private $logId1, $logId2, $logId3;
30 
44  private function addLogEntry( $type, $subtype, User $user, $ns, $title,
45  $comment = null, $parameters = null
46  ) {
47  $logEntry = new ManualLogEntry( $type, $subtype );
48  $logEntry->setPerformer( $user );
49  $logEntry->setTarget( Title::newFromText( $title, $ns ) );
50  if ( $comment !== null ) {
51  $logEntry->setComment( $comment );
52  }
53  if ( $parameters !== null ) {
54  $logEntry->setParameters( $parameters );
55  }
56 
57  return $logEntry->insert();
58  }
59 
60  function addDBData() {
61  $this->tablesUsed[] = 'logging';
62  $this->tablesUsed[] = 'user';
63 
64  try {
65  $user1 = User::newFromName( 'BackupDumperLogUserA' );
66  $this->userId1 = $user1->getId();
67  if ( $this->userId1 === 0 ) {
68  $user1->addToDatabase();
69  $this->userId1 = $user1->getId();
70  }
71  $this->assertGreaterThan( 0, $this->userId1 );
72 
73  $user2 = User::newFromName( 'BackupDumperLogUserB' );
74  $this->userId2 = $user2->getId();
75  if ( $this->userId2 === 0 ) {
76  $user2->addToDatabase();
77  $this->userId2 = $user2->getId();
78  }
79  $this->assertGreaterThan( 0, $this->userId2 );
80 
81  $this->logId1 = $this->addLogEntry( 'type', 'subtype',
82  $user1, NS_MAIN, "PageA" );
83  $this->assertGreaterThan( 0, $this->logId1 );
84 
85  $this->logId2 = $this->addLogEntry( 'supress', 'delete',
86  $user2, NS_TALK, "PageB", "SomeComment" );
87  $this->assertGreaterThan( 0, $this->logId2 );
88 
89  $this->logId3 = $this->addLogEntry( 'move', 'delete',
90  $user2, NS_MAIN, "PageA", "SomeOtherComment",
91  [ 'key1' => 1, 3 => 'value3' ] );
92  $this->assertGreaterThan( 0, $this->logId3 );
93  } catch ( Exception $e ) {
94  // We'd love to pass $e directly. However, ... see
95  // documentation of exceptionFromAddDBData in
96  // DumpTestCase
97  $this->exceptionFromAddDBData = $e;
98  }
99  }
100 
114  private function assertLogItem( $id, $user_name, $user_id, $comment, $type,
115  $subtype, $title, $parameters = []
116  ) {
117  $this->assertNodeStart( "logitem" );
118  $this->skipWhitespace();
119 
120  $this->assertTextNode( "id", $id );
121  $this->assertTextNode( "timestamp", false );
122 
123  $this->assertNodeStart( "contributor" );
124  $this->skipWhitespace();
125  $this->assertTextNode( "username", $user_name );
126  $this->assertTextNode( "id", $user_id );
127  $this->assertNodeEnd( "contributor" );
128  $this->skipWhitespace();
129 
130  if ( $comment !== null ) {
131  $this->assertTextNode( "comment", $comment );
132  }
133  $this->assertTextNode( "type", $type );
134  $this->assertTextNode( "action", $subtype );
135  $this->assertTextNode( "logtitle", $title );
136 
137  $this->assertNodeStart( "params" );
138  $parameters_xml = unserialize( $this->xml->value );
139  $this->assertEquals( $parameters, $parameters_xml );
140  $this->assertTrue( $this->xml->read(), "Skipping past processed text of params" );
141  $this->assertNodeEnd( "params" );
142  $this->skipWhitespace();
143 
144  $this->assertNodeEnd( "logitem" );
145  $this->skipWhitespace();
146  }
147 
148  function testPlain() {
149  // Preparing the dump
150  $fname = $this->getNewTempFile();
151 
152  $dumper = new DumpBackup( [ '--output=file:' . $fname ] );
153  $dumper->startId = $this->logId1;
154  $dumper->endId = $this->logId3 + 1;
155  $dumper->reporting = false;
156  $dumper->setDB( $this->db );
157 
158  // Performing the dump
159  $dumper->dump( WikiExporter::LOGS, WikiExporter::TEXT );
160 
161  // Analyzing the dumped data
162  $this->assertDumpStart( $fname );
163 
164  $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
165  $this->userId1, null, "type", "subtype", "PageA" );
166 
167  $contLang = MediaWikiServices::getInstance()->getContentLanguage();
168  $this->assertNotNull( $contLang, "Content language object validation" );
169  $namespace = $contLang->getNsText( NS_TALK );
170  $this->assertInternalType( 'string', $namespace );
171  $this->assertGreaterThan( 0, strlen( $namespace ) );
172  $this->assertLogItem( $this->logId2, "BackupDumperLogUserB",
173  $this->userId2, "SomeComment", "supress", "delete",
174  $namespace . ":PageB" );
175 
176  $this->assertLogItem( $this->logId3, "BackupDumperLogUserB",
177  $this->userId2, "SomeOtherComment", "move", "delete",
178  "PageA", [ 'key1' => 1, 3 => 'value3' ] );
179 
180  $this->assertDumpEnd();
181  }
182 
184  $this->checkHasGzip();
185 
186  // Preparing the dump
187  $fname = $this->getNewTempFile();
188 
189  $dumper = new DumpBackup();
190  $dumper->loadWithArgv( [ '--logs', '--output=gzip:' . $fname,
191  '--reporting=2' ] );
192  $dumper->startId = $this->logId1;
193  $dumper->endId = $this->logId3 + 1;
194  $dumper->setDB( $this->db );
195 
196  // xmldumps-backup demands reporting, although this is currently not
197  // implemented in BackupDumper, when dumping logging data. We
198  // nevertheless capture the output of the dump process already now,
199  // to be able to alert (once dumping produces reports) that this test
200  // needs updates.
201  $dumper->stderr = fopen( 'php://output', 'a' );
202  if ( $dumper->stderr === false ) {
203  $this->fail( "Could not open stream for stderr" );
204  }
205 
206  // Performing the dump
207  $dumper->execute();
208 
209  $this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" );
210 
211  // Analyzing the dumped data
212  $this->gunzip( $fname );
213 
214  $this->assertDumpStart( $fname );
215 
216  $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
217  $this->userId1, null, "type", "subtype", "PageA" );
218 
219  $contLang = MediaWikiServices::getInstance()->getContentLanguage();
220  $this->assertNotNull( $contLang, "Content language object validation" );
221  $namespace = $contLang->getNsText( NS_TALK );
222  $this->assertInternalType( 'string', $namespace );
223  $this->assertGreaterThan( 0, strlen( $namespace ) );
224  $this->assertLogItem( $this->logId2, "BackupDumperLogUserB",
225  $this->userId2, "SomeComment", "supress", "delete",
226  $namespace . ":PageB" );
227 
228  $this->assertLogItem( $this->logId3, "BackupDumperLogUserB",
229  $this->userId2, "SomeOtherComment", "move", "delete",
230  "PageA", [ 'key1' => 1, 3 => 'value3' ] );
231 
232  $this->assertDumpEnd();
233 
234  // Currently, no reporting is implemented. Alert via failure, once
235  // this changes.
236  // If reporting for log dumps has been implemented, please update
237  // the following statement to catch good output
238  $this->expectOutputString( '' );
239  }
240 }
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest
Tests for log dumps of BackupDumper.
Definition: backup_LogTest.php:24
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:280
MediaWiki\Tests\Maintenance\DumpTestCase\assertDumpStart
assertDumpStart( $fname, $skip_siteinfo=true)
Opens an XML file to analyze and optionally skips past siteinfo.
Definition: DumpTestCase.php:203
MediaWiki\Tests\Maintenance\DumpTestCase\checkHasGzip
checkHasGzip()
Skip the test if 'gzip' is not in $PATH.
Definition: DumpTestCase.php:46
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\$logId1
$logId1
Definition: backup_LogTest.php:29
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\assertLogItem
assertLogItem( $id, $user_name, $user_id, $comment, $type, $subtype, $title, $parameters=[])
asserts that the xml reader is at the beginning of a log entry and skips over it while analyzing it.
Definition: backup_LogTest.php:114
MediaWiki\Tests\Maintenance\DumpTestCase\skipWhitespace
skipWhitespace()
Steps the xml reader over white space.
Definition: DumpTestCase.php:233
MediaWiki\Tests\Maintenance\DumpTestCase\assertNodeEnd
assertNodeEnd( $name, $skip=true)
Asserts that the xml reader is at an closing element of given name, and optionally skips past it.
Definition: DumpTestCase.php:265
DumpBackup
Definition: dumpBackup.php:31
MediaWiki\Tests\Maintenance\DumpTestCase\assertDumpEnd
assertDumpEnd( $name="mediawiki")
Asserts that the xml reader is at the final closing tag of an xml file and closes the reader.
Definition: DumpTestCase.php:220
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:592
User
User
Definition: All_system_messages.txt:425
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWiki\Tests\Maintenance
Definition: backup_LogTest.php:3
NS_MAIN
const NS_MAIN
Definition: Defines.php:64
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
MediaWiki\Tests\Maintenance\DumpTestCase\assertNodeStart
assertNodeStart( $name, $skip=true)
Asserts that the xml reader is at an element of given name, and optionally skips past it.
Definition: DumpTestCase.php:249
WikiExporter\TEXT
const TEXT
Definition: WikiExporter.php:55
MediaWikiTestCase\getNewTempFile
getNewTempFile()
Obtains a new temporary file name.
Definition: MediaWikiTestCase.php:471
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
WikiExporter
Definition: WikiExporter.php:36
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\$userId2
$userId2
Definition: backup_LogTest.php:28
$fname
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition: Setup.php:121
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:120
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2221
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\$logId2
$logId2
Definition: backup_LogTest.php:29
MediaWiki\Tests\Maintenance\DumpTestCase\gunzip
gunzip( $fname)
gunzips the given file and stores the result in the original file name
Definition: DumpTestCase.php:96
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\addLogEntry
addLogEntry( $type, $subtype, User $user, $ns, $title, $comment=null, $parameters=null)
adds a log entry to the database.
Definition: backup_LogTest.php:44
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\testPlain
testPlain()
Definition: backup_LogTest.php:148
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:139
Title
Represents a title within MediaWiki.
Definition: Title.php:39
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\addDBData
addDBData()
Stub.
Definition: backup_LogTest.php:60
WikiExporter\LOGS
const LOGS
Definition: WikiExporter.php:52
MediaWiki\Tests\Maintenance\DumpTestCase
Base TestCase for dumps.
Definition: DumpTestCase.php:16
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\$userId1
$userId1
Definition: backup_LogTest.php:28
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\testXmlDumpsBackupUseCaseLogging
testXmlDumpsBackupUseCaseLogging()
Definition: backup_LogTest.php:183
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: LogEntry.php:437
NS_TALK
const NS_TALK
Definition: Defines.php:65
MediaWiki\Tests\Maintenance\DumpTestCase\assertTextNode
assertTextNode( $name, $text, $skip_ws=true)
Asserts that the xml reader is at an element of given tag that contains a given text,...
Definition: DumpTestCase.php:284
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:47
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\$logId3
$logId3
Definition: backup_LogTest.php:29
$type
$type
Definition: testCompression.php:48