MediaWiki  REL1_31
PageArchiveTest.php
Go to the documentation of this file.
1 <?php
2 
14 
18  private $archivedPage;
19 
24  private $ipEditor;
25 
30  private $ipRevId;
31 
32  function __construct( $name = null, array $data = [], $dataName = '' ) {
33  parent::__construct( $name, $data, $dataName );
34 
35  $this->tablesUsed = array_merge(
36  $this->tablesUsed,
37  [
38  'page',
39  'revision',
40  'ip_changes',
41  'text',
42  'archive',
43  'recentchanges',
44  'logging',
45  'page_props',
46  ]
47  );
48  }
49 
50  protected function setUp() {
51  parent::setUp();
52 
53  $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
54  $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD );
55  $this->overrideMwServices();
56 
57  // First create our dummy page
58  $page = Title::newFromText( 'PageArchiveTest_thePage' );
59  $page = new WikiPage( $page );
60  $content = ContentHandler::makeContent(
61  'testing',
62  $page->getTitle(),
64  );
65  $page->doEditContent( $content, 'testing', EDIT_NEW );
66 
67  // Insert IP revision
68  $this->ipEditor = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
69  $rev = new Revision( [
70  'text' => 'Lorem Ipsum',
71  'comment' => 'just a test',
72  'page' => $page->getId(),
73  'user_text' => $this->ipEditor,
74  ] );
75  $dbw = wfGetDB( DB_MASTER );
76  $this->ipRevId = $rev->insertOn( $dbw );
77 
78  // Delete the page
79  $page->doDeleteArticleReal( 'Just a test deletion' );
80 
81  $this->archivedPage = new PageArchive( $page->getTitle() );
82  }
83 
88  public function testUndeleteRevisions() {
89  // First make sure old revisions are archived
90  $dbr = wfGetDB( DB_REPLICA );
91  $arQuery = Revision::getArchiveQueryInfo();
92  $res = $dbr->select(
93  $arQuery['tables'],
94  $arQuery['fields'],
95  [ 'ar_rev_id' => $this->ipRevId ],
96  __METHOD__,
97  [],
98  $arQuery['joins']
99  );
100  $row = $res->fetchObject();
101  $this->assertEquals( $this->ipEditor, $row->ar_user_text );
102 
103  // Should not be in revision
104  $res = $dbr->select( 'revision', '1', [ 'rev_id' => $this->ipRevId ] );
105  $this->assertFalse( $res->fetchObject() );
106 
107  // Should not be in ip_changes
108  $res = $dbr->select( 'ip_changes', '1', [ 'ipc_rev_id' => $this->ipRevId ] );
109  $this->assertFalse( $res->fetchObject() );
110 
111  // Restore the page
112  $this->archivedPage->undelete( [] );
113 
114  // Should be back in revision
116  $res = $dbr->select(
117  $revQuery['tables'],
118  $revQuery['fields'],
119  [ 'rev_id' => $this->ipRevId ],
120  __METHOD__,
121  [],
122  $revQuery['joins']
123  );
124  $row = $res->fetchObject();
125  $this->assertEquals( $this->ipEditor, $row->rev_user_text );
126 
127  // Should be back in ip_changes
128  $res = $dbr->select( 'ip_changes', [ 'ipc_hex' ], [ 'ipc_rev_id' => $this->ipRevId ] );
129  $row = $res->fetchObject();
130  $this->assertEquals( IP::toHex( $this->ipEditor ), $row->ipc_hex );
131  }
132 
136  public function testListRevisions() {
137  $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
138  $this->overrideMwServices();
139 
140  $revisions = $this->archivedPage->listRevisions();
141  $this->assertEquals( 2, $revisions->numRows() );
142 
143  // Get the rows as arrays
144  $row1 = (array)$revisions->current();
145  $row2 = (array)$revisions->next();
146  // Unset the timestamps (we assume they will be right...
147  $this->assertInternalType( 'string', $row1['ar_timestamp'] );
148  $this->assertInternalType( 'string', $row2['ar_timestamp'] );
149  unset( $row1['ar_timestamp'] );
150  unset( $row2['ar_timestamp'] );
151 
152  $this->assertEquals(
153  [
154  'ar_minor_edit' => '0',
155  'ar_user' => '0',
156  'ar_user_text' => '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7',
157  'ar_actor' => null,
158  'ar_len' => '11',
159  'ar_deleted' => '0',
160  'ar_rev_id' => '3',
161  'ar_sha1' => '0qdrpxl537ivfnx4gcpnzz0285yxryy',
162  'ar_page_id' => '2',
163  'ar_comment_text' => 'just a test',
164  'ar_comment_data' => null,
165  'ar_comment_cid' => null,
166  'ar_content_format' => null,
167  'ar_content_model' => null,
168  'ts_tags' => null,
169  'ar_id' => '2',
170  'ar_namespace' => '0',
171  'ar_title' => 'PageArchiveTest_thePage',
172  'ar_text_id' => '3',
173  'ar_parent_id' => '2',
174  ],
175  $row1
176  );
177  $this->assertEquals(
178  [
179  'ar_minor_edit' => '0',
180  'ar_user' => '0',
181  'ar_user_text' => '127.0.0.1',
182  'ar_actor' => null,
183  'ar_len' => '7',
184  'ar_deleted' => '0',
185  'ar_rev_id' => '2',
186  'ar_sha1' => 'pr0s8e18148pxhgjfa0gjrvpy8fiyxc',
187  'ar_page_id' => '2',
188  'ar_comment_text' => 'testing',
189  'ar_comment_data' => null,
190  'ar_comment_cid' => null,
191  'ar_content_format' => null,
192  'ar_content_model' => null,
193  'ts_tags' => null,
194  'ar_id' => '1',
195  'ar_namespace' => '0',
196  'ar_title' => 'PageArchiveTest_thePage',
197  'ar_text_id' => '2',
198  'ar_parent_id' => '0',
199  ],
200  $row2
201  );
202  }
203 
207  public function testListPagesBySearch() {
208  $pages = PageArchive::listPagesBySearch( 'PageArchiveTest_thePage' );
209  $this->assertSame( 1, $pages->numRows() );
210 
211  $page = (array)$pages->current();
212 
213  $this->assertSame(
214  [
215  'ar_namespace' => '0',
216  'ar_title' => 'PageArchiveTest_thePage',
217  'count' => '2',
218  ],
219  $page
220  );
221  }
222 
226  public function testListPagesByPrefix() {
227  $pages = PageArchive::listPagesByPrefix( 'PageArchiveTest' );
228  $this->assertSame( 1, $pages->numRows() );
229 
230  $page = (array)$pages->current();
231 
232  $this->assertSame(
233  [
234  'ar_namespace' => '0',
235  'ar_title' => 'PageArchiveTest_thePage',
236  'count' => '2',
237  ],
238  $page
239  );
240  }
241 
245  public function testGetTextFromRow() {
246  $row = (object)[ 'ar_text_id' => 2 ];
247  $text = $this->archivedPage->getTextFromRow( $row );
248  $this->assertSame( 'testing', $text );
249  }
250 
254  public function testGetLastRevisionText() {
255  $text = $this->archivedPage->getLastRevisionText();
256  $this->assertSame( 'Lorem Ipsum', $text );
257  }
258 
262  public function testIsDeleted() {
263  $this->assertTrue( $this->archivedPage->isDeleted() );
264  }
265 }
IP\toHex
static toHex( $ip)
Return a zero-padded upper case hexadecimal representation of an IP address.
Definition: IP.php:417
PageArchiveTest\$ipEditor
$ipEditor
A logged out user who edited the page before it was archived.
Definition: PageArchiveTest.php:24
object
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php For a description of the see design txt $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest object
Definition: globals.txt:64
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
PageArchiveTest\testGetTextFromRow
testGetTextFromRow()
PageArchive::getTextFromRow.
Definition: PageArchiveTest.php:245
PageArchive
Used to show archived pages and eventually restore them.
Definition: PageArchive.php:28
array
the array() calling protocol came about after MediaWiki 1.4rc1.
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:37
PageArchive\listPagesByPrefix
static listPagesByPrefix( $prefix)
List deleted pages recorded in the archive table matching the given title prefix.
Definition: PageArchive.php:128
Revision\getArchiveQueryInfo
static getArchiveQueryInfo()
Return the tables, fields, and join conditions to be selected to create a new archived revision objec...
Definition: Revision.php:506
$res
$res
Definition: database.txt:21
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:245
PageArchiveTest\testGetLastRevisionText
testGetLastRevisionText()
PageArchive::getLastRevisionText.
Definition: PageArchiveTest.php:254
PageArchiveTest\testListRevisions
testListRevisions()
PageArchive::listRevisions.
Definition: PageArchiveTest.php:136
PageArchive\listPagesBySearch
static listPagesBySearch( $term)
List deleted pages recorded in the archive matching the given term, using search engine archive.
Definition: PageArchive.php:78
$revQuery
$revQuery
Definition: testCompression.php:51
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
$dbr
$dbr
Definition: testCompression.php:50
MediaWikiTestCase\overrideMwServices
overrideMwServices(Config $configOverrides=null, array $services=[])
Stashes the global instance of MediaWikiServices, and installs a new one, allowing test cases to over...
Definition: MediaWikiTestCase.php:845
Revision
Definition: Revision.php:41
Revision\getQueryInfo
static getQueryInfo( $options=[])
Return the tables, fields, and join conditions to be selected to create a new revision object.
Definition: Revision.php:492
PageArchiveTest\testListPagesBySearch
testListPagesBySearch()
PageArchive::listPagesBySearch.
Definition: PageArchiveTest.php:207
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2812
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:678
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
PageArchiveTest\setUp
setUp()
Definition: PageArchiveTest.php:50
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_MASTER
const DB_MASTER
Definition: defines.php:26
ContentHandler\makeContent
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
Definition: ContentHandler.php:129
MIGRATION_OLD
const MIGRATION_OLD
Definition: Defines.php:302
PageArchiveTest\$ipRevId
$ipRevId
Revision ID of the IP edit.
Definition: PageArchiveTest.php:30
PageArchiveTest
Test class for page archiving.
Definition: PageArchiveTest.php:13
PageArchiveTest\testIsDeleted
testIsDeleted()
PageArchive::isDeleted.
Definition: PageArchiveTest.php:262
EDIT_NEW
const EDIT_NEW
Definition: Defines.php:162
$rev
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition: hooks.txt:1777
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
PageArchiveTest\testUndeleteRevisions
testUndeleteRevisions()
PageArchive::undelete PageArchive::undeleteRevisions.
Definition: PageArchiveTest.php:88
PageArchiveTest\$archivedPage
$archivedPage
Definition: PageArchiveTest.php:18
PageArchiveTest\__construct
__construct( $name=null, array $data=[], $dataName='')
Definition: PageArchiveTest.php:32
PageArchiveTest\testListPagesByPrefix
testListPagesByPrefix()
PageArchive::listPagesBySearch.
Definition: PageArchiveTest.php:226