Merge "Provide command to adjust phpunit.xml for code coverage"
[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 */
33 public function testNewFromId( $id,
34 array $selectFields,
35 array $row = null,
36 array $expectedFields = null
37 ) {
38 $row = $row ? (object)$row : null;
39 $db = $this->getMock( IDatabase::class );
40 $db->expects( self::once() )
41 ->method( 'selectRow' )
42 ->with( $selectFields['tables'],
43 $selectFields['fields'],
44 $selectFields['conds'],
45 'DatabaseLogEntry::newFromId',
46 $selectFields['options'],
47 $selectFields['join_conds']
48 )
49 ->will( self::returnValue( $row ) );
50
51 /** @var IDatabase $db */
52 $logEntry = DatabaseLogEntry::newFromId( $id, $db );
53
54 if ( !$expectedFields ) {
55 self::assertNull( $logEntry, "Expected no log entry returned for id=$id" );
56 } else {
57 self::assertEquals( $id, $logEntry->getId() );
58 self::assertEquals( $expectedFields['type'], $logEntry->getType() );
59 self::assertEquals( $expectedFields['comment'], $logEntry->getComment() );
60 }
61 }
62
63 public function provideNewFromId() {
64 $newTables = [
65 'tables' => [
66 'logging',
67 'user',
68 'comment_log_comment' => 'comment',
69 'actor_log_user' => 'actor'
70 ],
71 'fields' => [
72 'log_id',
73 'log_type',
74 'log_action',
75 'log_timestamp',
76 'log_namespace',
77 'log_title',
78 'log_params',
79 'log_deleted',
80 'user_id',
81 'user_name',
82 'user_editcount',
83 'log_comment_text' => 'comment_log_comment.comment_text',
84 'log_comment_data' => 'comment_log_comment.comment_data',
85 'log_comment_cid' => 'comment_log_comment.comment_id',
86 'log_user' => 'actor_log_user.actor_user',
87 'log_user_text' => 'actor_log_user.actor_name',
88 'log_actor' => 'log_actor',
89 ],
90 'options' => [],
91 'join_conds' => [
92 'user' => [ 'LEFT JOIN', 'user_id=actor_log_user.actor_user' ],
93 'comment_log_comment' => [ 'JOIN', 'comment_log_comment.comment_id = log_comment_id' ],
94 'actor_log_user' => [ 'JOIN', 'actor_log_user.actor_id = log_actor' ],
95 ],
96 ];
97 return [
98 [
99 0,
100 $newTables + [ 'conds' => [ 'log_id' => 0 ] ],
101 null,
102 null
103 ],
104 [
105 123,
106 $newTables + [ 'conds' => [ 'log_id' => 123 ] ],
107 [
108 'log_id' => 123,
109 'log_type' => 'foobarize',
110 'log_comment_text' => 'test!',
111 'log_comment_data' => null,
112 ],
113 [ 'type' => 'foobarize', 'comment' => 'test!' ]
114 ],
115 [
116 567,
117 $newTables + [ 'conds' => [ 'log_id' => 567 ] ],
118 [
119 'log_id' => 567,
120 'log_type' => 'foobarize',
121 'log_comment_text' => 'test!',
122 'log_comment_data' => null,
123 ],
124 [ 'type' => 'foobarize', 'comment' => 'test!' ]
125 ],
126 ];
127 }
128 }