MediaWiki  1.33.0
backup_LogTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Exception;
9 use Title;
10 use User;
12 
26 
27  // We'll add several log entries and users for this test. The following
28  // variables hold the corresponding ids.
29  private $userId1, $userId2;
30  private $logId1, $logId2, $logId3;
31 
45  private function addLogEntry( $type, $subtype, User $user, $ns, $title,
46  $comment = null, $parameters = null
47  ) {
48  $logEntry = new ManualLogEntry( $type, $subtype );
49  $logEntry->setPerformer( $user );
50  $logEntry->setTarget( Title::newFromText( $title, $ns ) );
51  if ( $comment !== null ) {
52  $logEntry->setComment( $comment );
53  }
54  if ( $parameters !== null ) {
55  $logEntry->setParameters( $parameters );
56  }
57 
58  return $logEntry->insert();
59  }
60 
61  function addDBData() {
62  $this->tablesUsed[] = 'logging';
63  $this->tablesUsed[] = 'user';
64 
65  try {
66  $user1 = User::newFromName( 'BackupDumperLogUserA' );
67  $this->userId1 = $user1->getId();
68  if ( $this->userId1 === 0 ) {
69  $user1->addToDatabase();
70  $this->userId1 = $user1->getId();
71  }
72  $this->assertGreaterThan( 0, $this->userId1 );
73 
74  $user2 = User::newFromName( 'BackupDumperLogUserB' );
75  $this->userId2 = $user2->getId();
76  if ( $this->userId2 === 0 ) {
77  $user2->addToDatabase();
78  $this->userId2 = $user2->getId();
79  }
80  $this->assertGreaterThan( 0, $this->userId2 );
81 
82  $this->logId1 = $this->addLogEntry( 'type', 'subtype',
83  $user1, NS_MAIN, "PageA" );
84  $this->assertGreaterThan( 0, $this->logId1 );
85 
86  $this->logId2 = $this->addLogEntry( 'supress', 'delete',
87  $user2, NS_TALK, "PageB", "SomeComment" );
88  $this->assertGreaterThan( 0, $this->logId2 );
89 
90  $this->logId3 = $this->addLogEntry( 'move', 'delete',
91  $user2, NS_MAIN, "PageA", "SomeOtherComment",
92  [ 'key1' => 1, 3 => 'value3' ] );
93  $this->assertGreaterThan( 0, $this->logId3 );
94  } catch ( Exception $e ) {
95  // We'd love to pass $e directly. However, ... see
96  // documentation of exceptionFromAddDBData in
97  // DumpTestCase
98  $this->exceptionFromAddDBData = $e;
99  }
100  }
101 
102  function testPlain() {
103  // Preparing the dump
104  $fname = $this->getNewTempFile();
105 
106  $dumper = new DumpBackup( [ '--output=file:' . $fname ] );
107  $dumper->startId = $this->logId1;
108  $dumper->endId = $this->logId3 + 1;
109  $dumper->reporting = false;
110  $dumper->setDB( $this->db );
111 
112  // Performing the dump
113  $dumper->dump( WikiExporter::LOGS, WikiExporter::TEXT );
114 
115  // Analyzing the dumped data
116  $this->assertDumpSchema( $fname, $this->getXmlSchemaPath() );
117 
118  $asserter = $this->getDumpAsserter();
119  $asserter->assertDumpStart( $fname );
120 
121  $asserter->assertLogItem( $this->logId1, "BackupDumperLogUserA",
122  $this->userId1, null, "type", "subtype", "PageA" );
123 
124  $contLang = MediaWikiServices::getInstance()->getContentLanguage();
125  $this->assertNotNull( $contLang, "Content language object validation" );
126  $namespace = $contLang->getNsText( NS_TALK );
127  $this->assertInternalType( 'string', $namespace );
128  $this->assertGreaterThan( 0, strlen( $namespace ) );
129  $asserter->assertLogItem( $this->logId2, "BackupDumperLogUserB",
130  $this->userId2, "SomeComment", "supress", "delete",
131  $namespace . ":PageB" );
132 
133  $asserter->assertLogItem( $this->logId3, "BackupDumperLogUserB",
134  $this->userId2, "SomeOtherComment", "move", "delete",
135  "PageA", [ 'key1' => 1, 3 => 'value3' ] );
136 
137  $asserter->assertDumpEnd();
138  }
139 
141  $this->checkHasGzip();
142 
143  // Preparing the dump
144  $fname = $this->getNewTempFile();
145 
146  $dumper = new DumpBackup();
147  $dumper->loadWithArgv( [ '--logs', '--output=gzip:' . $fname,
148  '--reporting=2' ] );
149  $dumper->startId = $this->logId1;
150  $dumper->endId = $this->logId3 + 1;
151  $dumper->setDB( $this->db );
152 
153  // xmldumps-backup demands reporting, although this is currently not
154  // implemented in BackupDumper, when dumping logging data. We
155  // nevertheless capture the output of the dump process already now,
156  // to be able to alert (once dumping produces reports) that this test
157  // needs updates.
158  $dumper->stderr = fopen( 'php://output', 'a' );
159  if ( $dumper->stderr === false ) {
160  $this->fail( "Could not open stream for stderr" );
161  }
162 
163  // Performing the dump
164  $dumper->execute();
165 
166  $this->assertTrue( fclose( $dumper->stderr ), "Closing stderr handle" );
167 
168  // Analyzing the dumped data
169  $this->gunzip( $fname );
170 
171  $this->assertDumpSchema( $fname, $this->getXmlSchemaPath() );
172 
173  $asserter = $this->getDumpAsserter();
174  $asserter->assertDumpStart( $fname );
175 
176  $asserter->assertLogItem( $this->logId1, "BackupDumperLogUserA",
177  $this->userId1, null, "type", "subtype", "PageA" );
178 
179  $contLang = MediaWikiServices::getInstance()->getContentLanguage();
180  $this->assertNotNull( $contLang, "Content language object validation" );
181  $namespace = $contLang->getNsText( NS_TALK );
182  $this->assertInternalType( 'string', $namespace );
183  $this->assertGreaterThan( 0, strlen( $namespace ) );
184  $asserter->assertLogItem( $this->logId2, "BackupDumperLogUserB",
185  $this->userId2, "SomeComment", "supress", "delete",
186  $namespace . ":PageB" );
187 
188  $asserter->assertLogItem( $this->logId3, "BackupDumperLogUserB",
189  $this->userId2, "SomeOtherComment", "move", "delete",
190  "PageA", [ 'key1' => 1, 3 => 'value3' ] );
191 
192  $asserter->assertDumpEnd();
193 
194  // Currently, no reporting is implemented. Alert via failure, once
195  // this changes.
196  // If reporting for log dumps has been implemented, please update
197  // the following statement to catch good output
198  $this->expectOutputString( '' );
199  }
200 }
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest
Tests for log dumps of BackupDumper.
Definition: backup_LogTest.php:25
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:306
MediaWiki\Tests\Maintenance\DumpTestCase\checkHasGzip
checkHasGzip()
Skip the test if 'gzip' is not in $PATH.
Definition: DumpTestCase.php:40
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\$logId1
$logId1
Definition: backup_LogTest.php:30
DumpBackup
Definition: dumpBackup.php:31
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:585
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:925
WikiExporter\TEXT
const TEXT
Definition: WikiExporter.php:55
MediaWikiTestCase\getNewTempFile
getNewTempFile()
Obtains a new temporary file name.
Definition: MediaWikiTestCase.php:474
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:29
$fname
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition: Setup.php:123
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:124
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2162
MediaWiki\Tests\Maintenance\DumpTestCase\getDumpAsserter
getDumpAsserter( $schemaVersion=null)
Definition: DumpTestCase.php:203
MediaWiki\Tests\Maintenance\DumpTestCase\getXmlSchemaPath
getXmlSchemaPath( $schemaVersion=null)
Returns the path to the XML schema file for the given schema version.
Definition: DumpTestCase.php:164
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\$logId2
$logId2
Definition: backup_LogTest.php:30
MediaWiki\Tests\Maintenance\DumpTestCase\gunzip
gunzip( $fname)
gunzips the given file and stores the result in the original file name
Definition: DumpTestCase.php:95
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:45
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\testPlain
testPlain()
Definition: backup_LogTest.php:102
MediaWiki\Tests\Maintenance\DumpTestCase\assertDumpSchema
assertDumpSchema( $fname, $schemaFile)
Checks an XML file against an XSD schema.
Definition: DumpTestCase.php:211
Title
Represents a title within MediaWiki.
Definition: Title.php:40
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\addDBData
addDBData()
Stub.
Definition: backup_LogTest.php:61
WikiExporter\LOGS
const LOGS
Definition: WikiExporter.php:52
MediaWiki\Tests\Maintenance\DumpTestCase
Base TestCase for dumps.
Definition: DumpTestCase.php:17
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\$userId1
$userId1
Definition: backup_LogTest.php:29
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\testXmlDumpsBackupUseCaseLogging
testXmlDumpsBackupUseCaseLogging()
Definition: backup_LogTest.php:140
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: LogEntry.php:441
NS_TALK
const NS_TALK
Definition: Defines.php:65
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:48
MediaWiki\Tests\Maintenance\BackupDumperLoggerTest\$logId3
$logId3
Definition: backup_LogTest.php:30
$type
$type
Definition: testCompression.php:48