MediaWiki  1.29.1
backup_LogTest.php
Go to the documentation of this file.
1 <?php
15 
16  // We'll add several log entries and users for this test. The following
17  // variables hold the corresponding ids.
18  private $userId1, $userId2;
19  private $logId1, $logId2, $logId3;
20 
34  private function addLogEntry( $type, $subtype, User $user, $ns, $title,
35  $comment = null, $parameters = null
36  ) {
37  $logEntry = new ManualLogEntry( $type, $subtype );
38  $logEntry->setPerformer( $user );
39  $logEntry->setTarget( Title::newFromText( $title, $ns ) );
40  if ( $comment !== null ) {
41  $logEntry->setComment( $comment );
42  }
43  if ( $parameters !== null ) {
44  $logEntry->setParameters( $parameters );
45  }
46 
47  return $logEntry->insert();
48  }
49 
50  function addDBData() {
51  $this->tablesUsed[] = 'logging';
52  $this->tablesUsed[] = 'user';
53 
54  try {
55  $user1 = User::newFromName( 'BackupDumperLogUserA' );
56  $this->userId1 = $user1->getId();
57  if ( $this->userId1 === 0 ) {
58  $user1->addToDatabase();
59  $this->userId1 = $user1->getId();
60  }
61  $this->assertGreaterThan( 0, $this->userId1 );
62 
63  $user2 = User::newFromName( 'BackupDumperLogUserB' );
64  $this->userId2 = $user2->getId();
65  if ( $this->userId2 === 0 ) {
66  $user2->addToDatabase();
67  $this->userId2 = $user2->getId();
68  }
69  $this->assertGreaterThan( 0, $this->userId2 );
70 
71  $this->logId1 = $this->addLogEntry( 'type', 'subtype',
72  $user1, NS_MAIN, "PageA" );
73  $this->assertGreaterThan( 0, $this->logId1 );
74 
75  $this->logId2 = $this->addLogEntry( 'supress', 'delete',
76  $user2, NS_TALK, "PageB", "SomeComment" );
77  $this->assertGreaterThan( 0, $this->logId2 );
78 
79  $this->logId3 = $this->addLogEntry( 'move', 'delete',
80  $user2, NS_MAIN, "PageA", "SomeOtherComment",
81  [ 'key1' => 1, 3 => 'value3' ] );
82  $this->assertGreaterThan( 0, $this->logId3 );
83  } catch ( Exception $e ) {
84  // We'd love to pass $e directly. However, ... see
85  // documentation of exceptionFromAddDBData in
86  // DumpTestCase
87  $this->exceptionFromAddDBData = $e;
88  }
89  }
90 
104  private function assertLogItem( $id, $user_name, $user_id, $comment, $type,
105  $subtype, $title, $parameters = []
106  ) {
107 
108  $this->assertNodeStart( "logitem" );
109  $this->skipWhitespace();
110 
111  $this->assertTextNode( "id", $id );
112  $this->assertTextNode( "timestamp", false );
113 
114  $this->assertNodeStart( "contributor" );
115  $this->skipWhitespace();
116  $this->assertTextNode( "username", $user_name );
117  $this->assertTextNode( "id", $user_id );
118  $this->assertNodeEnd( "contributor" );
119  $this->skipWhitespace();
120 
121  if ( $comment !== null ) {
122  $this->assertTextNode( "comment", $comment );
123  }
124  $this->assertTextNode( "type", $type );
125  $this->assertTextNode( "action", $subtype );
126  $this->assertTextNode( "logtitle", $title );
127 
128  $this->assertNodeStart( "params" );
129  $parameters_xml = unserialize( $this->xml->value );
130  $this->assertEquals( $parameters, $parameters_xml );
131  $this->assertTrue( $this->xml->read(), "Skipping past processed text of params" );
132  $this->assertNodeEnd( "params" );
133  $this->skipWhitespace();
134 
135  $this->assertNodeEnd( "logitem" );
136  $this->skipWhitespace();
137  }
138 
139  function testPlain() {
141 
142  // Preparing the dump
143  $fname = $this->getNewTempFile();
144 
145  $dumper = new DumpBackup( [ '--output=file:' . $fname ] );
146  $dumper->startId = $this->logId1;
147  $dumper->endId = $this->logId3 + 1;
148  $dumper->reporting = false;
149  $dumper->setDB( $this->db );
150 
151  // Performing the dump
152  $dumper->dump( WikiExporter::LOGS, WikiExporter::TEXT );
153 
154  // Analyzing the dumped data
155  $this->assertDumpStart( $fname );
156 
157  $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
158  $this->userId1, null, "type", "subtype", "PageA" );
159 
160  $this->assertNotNull( $wgContLang, "Content language object validation" );
161  $namespace = $wgContLang->getNsText( NS_TALK );
162  $this->assertInternalType( 'string', $namespace );
163  $this->assertGreaterThan( 0, strlen( $namespace ) );
164  $this->assertLogItem( $this->logId2, "BackupDumperLogUserB",
165  $this->userId2, "SomeComment", "supress", "delete",
166  $namespace . ":PageB" );
167 
168  $this->assertLogItem( $this->logId3, "BackupDumperLogUserB",
169  $this->userId2, "SomeOtherComment", "move", "delete",
170  "PageA", [ 'key1' => 1, 3 => 'value3' ] );
171 
172  $this->assertDumpEnd();
173  }
174 
177 
178  $this->checkHasGzip();
179 
180  // Preparing the dump
181  $fname = $this->getNewTempFile();
182 
183  $dumper = new DumpBackup();
184  $dumper->loadWithArgv( [ '--logs', '--output=gzip:' . $fname,
185  '--reporting=2' ] );
186  $dumper->startId = $this->logId1;
187  $dumper->endId = $this->logId3 + 1;
188  $dumper->setDB( $this->db );
189 
190  // xmldumps-backup demands reporting, although this is currently not
191  // implemented in BackupDumper, when dumping logging data. We
192  // nevertheless capture the output of the dump process already now,
193  // to be able to alert (once dumping produces reports) that this test
194  // needs updates.
195  $dumper->stderr = fopen( 'php://output', 'a' );
196  if ( $dumper->stderr === false ) {
197  $this->fail( "Could not open stream for stderr" );
198  }
199 
200  // Performing the dump
201  $dumper->execute();
202 
203  $this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" );
204 
205  // Analyzing the dumped data
206  $this->gunzip( $fname );
207 
208  $this->assertDumpStart( $fname );
209 
210  $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
211  $this->userId1, null, "type", "subtype", "PageA" );
212 
213  $this->assertNotNull( $wgContLang, "Content language object validation" );
214  $namespace = $wgContLang->getNsText( NS_TALK );
215  $this->assertInternalType( 'string', $namespace );
216  $this->assertGreaterThan( 0, strlen( $namespace ) );
217  $this->assertLogItem( $this->logId2, "BackupDumperLogUserB",
218  $this->userId2, "SomeComment", "supress", "delete",
219  $namespace . ":PageB" );
220 
221  $this->assertLogItem( $this->logId3, "BackupDumperLogUserB",
222  $this->userId2, "SomeOtherComment", "move", "delete",
223  "PageA", [ 'key1' => 1, 3 => 'value3' ] );
224 
225  $this->assertDumpEnd();
226 
227  // Currently, no reporting is implemented. Alert via failure, once
228  // this changes.
229  // If reporting for log dumps has been implemented, please update
230  // the following statement to catch good output
231  $this->expectOutputString( '' );
232  }
233 }
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:265
BackupDumperLoggerTest\addLogEntry
addLogEntry( $type, $subtype, User $user, $ns, $title, $comment=null, $parameters=null)
adds a log entry to the database.
Definition: backup_LogTest.php:34
BackupDumperLoggerTest\$userId1
$userId1
Definition: backup_LogTest.php:18
BackupDumperLoggerTest
Tests for log dumps of BackupDumper.
Definition: backup_LogTest.php:14
DumpTestCase
Base TestCase for dumps.
Definition: DumpTestCase.php:6
DumpBackup
Definition: dumpBackup.php:30
$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:246
$fname
if(!defined( 'MEDIAWIKI')) $fname
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined.
Definition: Setup.php:36
unserialize
unserialize( $serialized)
Definition: ApiMessage.php:185
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:274
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:556
BackupDumperLoggerTest\addDBData
addDBData()
Stub.
Definition: backup_LogTest.php:50
$type
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2536
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:210
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
NS_MAIN
const NS_MAIN
Definition: Defines.php:62
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
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:255
WikiExporter\TEXT
const TEXT
Definition: WikiExporter.php:58
BackupDumperLoggerTest\testPlain
testPlain()
Definition: backup_LogTest.php:139
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:104
MediaWikiTestCase\getNewTempFile
getNewTempFile()
Obtains a new temporary file name.
Definition: MediaWikiTestCase.php:443
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:239
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
BackupDumperLoggerTest\testXmlDumpsBackupUseCaseLogging
testXmlDumpsBackupUseCaseLogging()
Definition: backup_LogTest.php:175
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2122
DumpTestCase\checkHasGzip
checkHasGzip()
Skip the test if 'gzip' is not in $PATH.
Definition: DumpTestCase.php:36
DumpTestCase\assertDumpStart
assertDumpStart( $fname, $skip_siteinfo=true)
Opens an XML file to analyze and optionally skips past siteinfo.
Definition: DumpTestCase.php:193
WikiExporter\LOGS
const LOGS
Definition: WikiExporter.php:52
BackupDumperLoggerTest\$userId2
$userId2
Definition: backup_LogTest.php:18
BackupDumperLoggerTest\$logId3
$logId3
Definition: backup_LogTest.php:19
DumpTestCase\gunzip
gunzip( $fname)
gunzips the given file and stores the result in the original file name
Definition: DumpTestCase.php:86
ManualLogEntry
Class for creating log entries manually, to inject them into the database.
Definition: LogEntry.php:396
NS_TALK
const NS_TALK
Definition: Defines.php:63
BackupDumperLoggerTest\$logId1
$logId1
Definition: backup_LogTest.php:19
DumpTestCase\skipWhitespace
skipWhitespace()
Steps the xml reader over white space.
Definition: DumpTestCase.php:223
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:50
BackupDumperLoggerTest\$logId2
$logId2
Definition: backup_LogTest.php:19
$wgContLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the content language as $wgContLang
Definition: design.txt:56