Merge "Prevent fatal PHP errors when PageRestriction::getTitle() returns null."
[lhc/web/wiklou.git] / tests / phpunit / includes / logging / DatabaseLogEntryTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use Wikimedia\Rdbms\IDatabase;
5
6 class DatabaseLogEntryTest extends MediaWikiTestCase {
7 public function setUp() {
8 parent::setUp();
9
10 // These services cache their joins
11 MediaWikiServices::getInstance()->resetServiceForTesting( 'CommentStore' );
12 MediaWikiServices::getInstance()->resetServiceForTesting( 'ActorMigration' );
13 }
14
15 public function tearDown() {
16 parent::tearDown();
17
18 MediaWikiServices::getInstance()->resetServiceForTesting( 'CommentStore' );
19 MediaWikiServices::getInstance()->resetServiceForTesting( 'ActorMigration' );
20 }
21
22 /**
23 * @covers DatabaseLogEntry::newFromId
24 * @covers DatabaseLogEntry::getSelectQueryData
25 *
26 * @dataProvider provideNewFromId
27 *
28 * @param int $id
29 * @param array $selectFields
30 * @param string[]|null $row
31 * @param string[]|null $expectedFields
32 * @param int $actorMigration
33 */
34 public function testNewFromId( $id,
35 array $selectFields,
36 array $row = null,
37 array $expectedFields = null,
38 $actorMigration
39 ) {
40 $this->setMwGlobals( [
41 'wgActorTableSchemaMigrationStage' => $actorMigration,
42 ] );
43
44 $row = $row ? (object)$row : null;
45 $db = $this->getMock( IDatabase::class );
46 $db->expects( self::once() )
47 ->method( 'selectRow' )
48 ->with( $selectFields['tables'],
49 $selectFields['fields'],
50 $selectFields['conds'],
51 'DatabaseLogEntry::newFromId',
52 $selectFields['options'],
53 $selectFields['join_conds']
54 )
55 ->will( self::returnValue( $row ) );
56
57 /** @var IDatabase $db */
58 $logEntry = DatabaseLogEntry::newFromId( $id, $db );
59
60 if ( !$expectedFields ) {
61 self::assertNull( $logEntry, "Expected no log entry returned for id=$id" );
62 } else {
63 self::assertEquals( $id, $logEntry->getId() );
64 self::assertEquals( $expectedFields['type'], $logEntry->getType() );
65 self::assertEquals( $expectedFields['comment'], $logEntry->getComment() );
66 }
67 }
68
69 public function provideNewFromId() {
70 $oldTables = [
71 'tables' => [
72 'logging', 'user',
73 'comment_log_comment' => 'comment',
74 ],
75 'fields' => [
76 'log_id',
77 'log_type',
78 'log_action',
79 'log_timestamp',
80 'log_namespace',
81 'log_title',
82 'log_params',
83 'log_deleted',
84 'user_id',
85 'user_name',
86 'user_editcount',
87 'log_comment_text' => 'comment_log_comment.comment_text',
88 'log_comment_data' => 'comment_log_comment.comment_data',
89 'log_comment_cid' => 'comment_log_comment.comment_id',
90 'log_user' => 'log_user',
91 'log_user_text' => 'log_user_text',
92 'log_actor' => 'NULL',
93 ],
94 'options' => [],
95 'join_conds' => [
96 'user' => [ 'LEFT JOIN', 'user_id=log_user' ],
97 'comment_log_comment' => [ 'JOIN', 'comment_log_comment.comment_id = log_comment_id' ],
98 ],
99 ];
100 $newTables = [
101 'tables' => [
102 'logging',
103 'user',
104 'comment_log_comment' => 'comment',
105 'actor_log_user' => 'actor'
106 ],
107 'fields' => [
108 'log_id',
109 'log_type',
110 'log_action',
111 'log_timestamp',
112 'log_namespace',
113 'log_title',
114 'log_params',
115 'log_deleted',
116 'user_id',
117 'user_name',
118 'user_editcount',
119 'log_comment_text' => 'comment_log_comment.comment_text',
120 'log_comment_data' => 'comment_log_comment.comment_data',
121 'log_comment_cid' => 'comment_log_comment.comment_id',
122 'log_user' => 'actor_log_user.actor_user',
123 'log_user_text' => 'actor_log_user.actor_name',
124 'log_actor' => 'log_actor',
125 ],
126 'options' => [],
127 'join_conds' => [
128 'user' => [ 'LEFT JOIN', 'user_id=actor_log_user.actor_user' ],
129 'comment_log_comment' => [ 'JOIN', 'comment_log_comment.comment_id = log_comment_id' ],
130 'actor_log_user' => [ 'JOIN', 'actor_log_user.actor_id = log_actor' ],
131 ],
132 ];
133 return [
134 [
135 0,
136 $oldTables + [ 'conds' => [ 'log_id' => 0 ] ],
137 null,
138 null,
139 SCHEMA_COMPAT_OLD,
140 ],
141 [
142 123,
143 $oldTables + [ 'conds' => [ 'log_id' => 123 ] ],
144 [
145 'log_id' => 123,
146 'log_type' => 'foobarize',
147 'log_comment_text' => 'test!',
148 'log_comment_data' => null,
149 ],
150 [ 'type' => 'foobarize', 'comment' => 'test!' ],
151 SCHEMA_COMPAT_OLD,
152 ],
153 [
154 567,
155 $newTables + [ 'conds' => [ 'log_id' => 567 ] ],
156 [
157 'log_id' => 567,
158 'log_type' => 'foobarize',
159 'log_comment_text' => 'test!',
160 'log_comment_data' => null,
161 ],
162 [ 'type' => 'foobarize', 'comment' => 'test!' ],
163 SCHEMA_COMPAT_NEW,
164 ],
165 ];
166 }
167 }