MediaWiki  REL1_31
backup_LogTest.php
Go to the documentation of this file.
1 <?php
2 
4 
7 use Title;
8 use User;
10 
24 
25  // We'll add several log entries and users for this test. The following
26  // variables hold the corresponding ids.
27  private $userId1, $userId2;
28  private $logId1, $logId2, $logId3;
29 
43  private function addLogEntry( $type, $subtype, User $user, $ns, $title,
44  $comment = null, $parameters = null
45  ) {
46  $logEntry = new ManualLogEntry( $type, $subtype );
47  $logEntry->setPerformer( $user );
48  $logEntry->setTarget( Title::newFromText( $title, $ns ) );
49  if ( $comment !== null ) {
50  $logEntry->setComment( $comment );
51  }
52  if ( $parameters !== null ) {
53  $logEntry->setParameters( $parameters );
54  }
55 
56  return $logEntry->insert();
57  }
58 
59  function addDBData() {
60  $this->tablesUsed[] = 'logging';
61  $this->tablesUsed[] = 'user';
62 
63  try {
64  $user1 = User::newFromName( 'BackupDumperLogUserA' );
65  $this->userId1 = $user1->getId();
66  if ( $this->userId1 === 0 ) {
67  $user1->addToDatabase();
68  $this->userId1 = $user1->getId();
69  }
70  $this->assertGreaterThan( 0, $this->userId1 );
71 
72  $user2 = User::newFromName( 'BackupDumperLogUserB' );
73  $this->userId2 = $user2->getId();
74  if ( $this->userId2 === 0 ) {
75  $user2->addToDatabase();
76  $this->userId2 = $user2->getId();
77  }
78  $this->assertGreaterThan( 0, $this->userId2 );
79 
80  $this->logId1 = $this->addLogEntry( 'type', 'subtype',
81  $user1, NS_MAIN, "PageA" );
82  $this->assertGreaterThan( 0, $this->logId1 );
83 
84  $this->logId2 = $this->addLogEntry( 'supress', 'delete',
85  $user2, NS_TALK, "PageB", "SomeComment" );
86  $this->assertGreaterThan( 0, $this->logId2 );
87 
88  $this->logId3 = $this->addLogEntry( 'move', 'delete',
89  $user2, NS_MAIN, "PageA", "SomeOtherComment",
90  [ 'key1' => 1, 3 => 'value3' ] );
91  $this->assertGreaterThan( 0, $this->logId3 );
92  } catch ( Exception $e ) {
93  // We'd love to pass $e directly. However, ... see
94  // documentation of exceptionFromAddDBData in
95  // DumpTestCase
96  $this->exceptionFromAddDBData = $e;
97  }
98  }
99 
113  private function assertLogItem( $id, $user_name, $user_id, $comment, $type,
114  $subtype, $title, $parameters = []
115  ) {
116  $this->assertNodeStart( "logitem" );
117  $this->skipWhitespace();
118 
119  $this->assertTextNode( "id", $id );
120  $this->assertTextNode( "timestamp", false );
121 
122  $this->assertNodeStart( "contributor" );
123  $this->skipWhitespace();
124  $this->assertTextNode( "username", $user_name );
125  $this->assertTextNode( "id", $user_id );
126  $this->assertNodeEnd( "contributor" );
127  $this->skipWhitespace();
128 
129  if ( $comment !== null ) {
130  $this->assertTextNode( "comment", $comment );
131  }
132  $this->assertTextNode( "type", $type );
133  $this->assertTextNode( "action", $subtype );
134  $this->assertTextNode( "logtitle", $title );
135 
136  $this->assertNodeStart( "params" );
137  $parameters_xml = unserialize( $this->xml->value );
138  $this->assertEquals( $parameters, $parameters_xml );
139  $this->assertTrue( $this->xml->read(), "Skipping past processed text of params" );
140  $this->assertNodeEnd( "params" );
141  $this->skipWhitespace();
142 
143  $this->assertNodeEnd( "logitem" );
144  $this->skipWhitespace();
145  }
146 
147  function testPlain() {
149 
150  // Preparing the dump
151  $fname = $this->getNewTempFile();
152 
153  $dumper = new DumpBackup( [ '--output=file:' . $fname ] );
154  $dumper->startId = $this->logId1;
155  $dumper->endId = $this->logId3 + 1;
156  $dumper->reporting = false;
157  $dumper->setDB( $this->db );
158 
159  // Performing the dump
160  $dumper->dump( WikiExporter::LOGS, WikiExporter::TEXT );
161 
162  // Analyzing the dumped data
163  $this->assertDumpStart( $fname );
164 
165  $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
166  $this->userId1, null, "type", "subtype", "PageA" );
167 
168  $this->assertNotNull( $wgContLang, "Content language object validation" );
169  $namespace = $wgContLang->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 
185 
186  $this->checkHasGzip();
187 
188  // Preparing the dump
189  $fname = $this->getNewTempFile();
190 
191  $dumper = new DumpBackup();
192  $dumper->loadWithArgv( [ '--logs', '--output=gzip:' . $fname,
193  '--reporting=2' ] );
194  $dumper->startId = $this->logId1;
195  $dumper->endId = $this->logId3 + 1;
196  $dumper->setDB( $this->db );
197 
198  // xmldumps-backup demands reporting, although this is currently not
199  // implemented in BackupDumper, when dumping logging data. We
200  // nevertheless capture the output of the dump process already now,
201  // to be able to alert (once dumping produces reports) that this test
202  // needs updates.
203  $dumper->stderr = fopen( 'php://output', 'a' );
204  if ( $dumper->stderr === false ) {
205  $this->fail( "Could not open stream for stderr" );
206  }
207 
208  // Performing the dump
209  $dumper->execute();
210 
211  $this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" );
212 
213  // Analyzing the dumped data
214  $this->gunzip( $fname );
215 
216  $this->assertDumpStart( $fname );
217 
218  $this->assertLogItem( $this->logId1, "BackupDumperLogUserA",
219  $this->userId1, null, "type", "subtype", "PageA" );
220 
221  $this->assertNotNull( $wgContLang, "Content language object validation" );
222  $namespace = $wgContLang->getNsText( NS_TALK );
223  $this->assertInternalType( 'string', $namespace );
224  $this->assertGreaterThan( 0, strlen( $namespace ) );
225  $this->assertLogItem( $this->logId2, "BackupDumperLogUserB",
226  $this->userId2, "SomeComment", "supress", "delete",
227  $namespace . ":PageB" );
228 
229  $this->assertLogItem( $this->logId3, "BackupDumperLogUserB",
230  $this->userId2, "SomeOtherComment", "move", "delete",
231  "PageA", [ 'key1' => 1, 3 => 'value3' ] );
232 
233  $this->assertDumpEnd();
234 
235  // Currently, no reporting is implemented. Alert via failure, once
236  // this changes.
237  // If reporting for log dumps has been implemented, please update
238  // the following statement to catch good output
239  $this->expectOutputString( '' );
240  }
241 }
$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:247
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest
Tests for log dumps of BackupDumper.
Definition: backup_LogTest.php:23
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:273
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
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\$logId1
$logId1
Definition: backup_LogTest.php:28
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:113
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:30
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
unserialize
unserialize( $serialized)
Definition: ApiMessage.php:192
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:591
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:37
MediaWiki\Tests\Maintenance
Definition: backup_LogTest.php:3
NS_MAIN
const NS_MAIN
Definition: Defines.php:74
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:58
MediaWikiTestCase\getNewTempFile
getNewTempFile()
Obtains a new temporary file name.
Definition: MediaWikiTestCase.php:462
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
WikiExporter
Definition: WikiExporter.php:36
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\$userId2
$userId2
Definition: backup_LogTest.php:27
$fname
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition: Setup.php:112
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\$logId2
$logId2
Definition: backup_LogTest.php:28
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:43
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\testPlain
testPlain()
Definition: backup_LogTest.php:147
Title
Represents a title within MediaWiki.
Definition: Title.php:39
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\addDBData
addDBData()
Stub.
Definition: backup_LogTest.php:59
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:27
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\testXmlDumpsBackupUseCaseLogging
testXmlDumpsBackupUseCaseLogging()
Definition: backup_LogTest.php:183
ManualLogEntry
Class for creating log entries manually, to inject them into the database.
Definition: LogEntry.php:432
NS_TALK
const NS_TALK
Definition: Defines.php:75
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
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:53
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\$logId3
$logId3
Definition: backup_LogTest.php:28
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2171
$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:57
$type
$type
Definition: testCompression.php:48