Merge "Introduce DatabaseLogEntry::newFromId"
[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 string $migration
33 */
34 public function testNewFromId( $id,
35 array $selectFields,
36 array $row = null,
37 array $expectedFields = null,
38 $migration
39 ) {
40 $this->setMwGlobals( [
41 'wgCommentTableSchemaMigrationStage' => $migration,
42 'wgActorTableSchemaMigrationStage' => $migration,
43 ] );
44
45 $row = $row ? (object)$row : null;
46 $db = $this->getMock( IDatabase::class );
47 $db->expects( self::once() )
48 ->method( 'selectRow' )
49 ->with( $selectFields['tables'],
50 $selectFields['fields'],
51 $selectFields['conds'],
52 'DatabaseLogEntry::newFromId',
53 $selectFields['options'],
54 $selectFields['join_conds']
55 )
56 ->will( self::returnValue( $row ) );
57
58 /** @var IDatabase $db */
59 $logEntry = DatabaseLogEntry::newFromId( $id, $db );
60
61 if ( !$expectedFields ) {
62 self::assertNull( $logEntry, "Expected no log entry returned for id=$id" );
63 } else {
64 self::assertEquals( $id, $logEntry->getId() );
65 self::assertEquals( $expectedFields['type'], $logEntry->getType() );
66 self::assertEquals( $expectedFields['comment'], $logEntry->getComment() );
67 }
68 }
69
70 public function provideNewFromId() {
71 $oldTables = [
72 'tables' => [ 'logging', 'user' ],
73 'fields' => [
74 'log_id',
75 'log_type',
76 'log_action',
77 'log_timestamp',
78 'log_namespace',
79 'log_title',
80 'log_params',
81 'log_deleted',
82 'user_id',
83 'user_name',
84 'user_editcount',
85 'log_comment_text' => 'log_comment',
86 'log_comment_data' => 'NULL',
87 'log_comment_cid' => 'NULL',
88 'log_user' => 'log_user',
89 'log_user_text' => 'log_user_text',
90 'log_actor' => 'NULL',
91 ],
92 'options' => [],
93 'join_conds' => [ 'user' => [ 'LEFT JOIN', 'user_id=log_user' ] ],
94 ];
95 $newTables = [
96 'tables' => [
97 'logging',
98 'user',
99 'comment_log_comment' => 'comment',
100 'actor_log_user' => 'actor'
101 ],
102 'fields' => [
103 'log_id',
104 'log_type',
105 'log_action',
106 'log_timestamp',
107 'log_namespace',
108 'log_title',
109 'log_params',
110 'log_deleted',
111 'user_id',
112 'user_name',
113 'user_editcount',
114 'log_comment_text' => 'comment_log_comment.comment_text',
115 'log_comment_data' => 'comment_log_comment.comment_data',
116 'log_comment_cid' => 'comment_log_comment.comment_id',
117 'log_user' => 'actor_log_user.actor_user',
118 'log_user_text' => 'actor_log_user.actor_name',
119 'log_actor' => 'log_actor',
120 ],
121 'options' => [],
122 'join_conds' => [
123 'user' => [ 'LEFT JOIN', 'user_id=actor_log_user.actor_user' ],
124 'comment_log_comment' => [ 'JOIN', 'comment_log_comment.comment_id = log_comment_id' ],
125 'actor_log_user' => [ 'JOIN', 'actor_log_user.actor_id = log_actor' ],
126 ],
127 ];
128 return [
129 [
130 0,
131 $oldTables + [ 'conds' => [ 'log_id' => 0 ] ],
132 null,
133 null,
134 MIGRATION_OLD,
135 ],
136 [
137 123,
138 $oldTables + [ 'conds' => [ 'log_id' => 123 ] ],
139 [
140 'log_id' => 123,
141 'log_type' => 'foobarize',
142 'log_comment_text' => 'test!',
143 'log_comment_data' => null,
144 ],
145 [ 'type' => 'foobarize', 'comment' => 'test!' ],
146 MIGRATION_OLD,
147 ],
148 [
149 567,
150 $newTables + [ 'conds' => [ 'log_id' => 567 ] ],
151 [
152 'log_id' => 567,
153 'log_type' => 'foobarize',
154 'log_comment_text' => 'test!',
155 'log_comment_data' => null,
156 ],
157 [ 'type' => 'foobarize', 'comment' => 'test!' ],
158 MIGRATION_NEW,
159 ],
160 ];
161 }
162 }