| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\User\ActorStore; |
| 4 |
use MediaWiki\User\UserIdentity; |
| 5 |
use MediaWiki\User\UserIdentityValue; |
| 6 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class DatabaseLogEntryTest extends MediaWikiIntegrationTestCase { |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public function testNewFromId( $id, |
| 25 |
array $selectFields, |
| 26 |
?array $row = null, |
| 27 |
?array $expectedFields = null |
| 28 |
) { |
| 29 |
$row = $row ? (object)$row : null; |
| 30 |
$db = $this->createMock( IReadableDatabase::class ); |
| 31 |
$db->expects( self::once() ) |
| 32 |
->method( 'selectRow' ) |
| 33 |
->with( $selectFields['tables'], |
| 34 |
$selectFields['fields'], |
| 35 |
$selectFields['conds'], |
| 36 |
'DatabaseLogEntry::newFromId', |
| 37 |
$selectFields['options'], |
| 38 |
$selectFields['join_conds'] |
| 39 |
) |
| 40 |
->will( self::returnValue( $row ) ); |
| 41 |
|
| 42 |
|
| 43 |
$logEntry = DatabaseLogEntry::newFromId( $id, $db ); |
| 44 |
|
| 45 |
if ( !$expectedFields ) { |
| 46 |
self::assertNull( $logEntry, "Expected no log entry returned for id=$id" ); |
| 47 |
} else { |
| 48 |
self::assertEquals( $id, $logEntry->getId() ); |
| 49 |
self::assertEquals( $expectedFields['type'], $logEntry->getType() ); |
| 50 |
self::assertEquals( $expectedFields['comment'], $logEntry->getComment() ); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
public static function provideNewFromId() { |
| 55 |
$newTables = [ |
| 56 |
'tables' => [ |
| 57 |
'logging', |
| 58 |
'comment_log_comment' => 'comment', |
| 59 |
'logging_actor' => 'actor', |
| 60 |
'user' => 'user', |
| 61 |
], |
| 62 |
'fields' => [ |
| 63 |
'log_id', |
| 64 |
'log_type', |
| 65 |
'log_action', |
| 66 |
'log_timestamp', |
| 67 |
'log_namespace', |
| 68 |
'log_title', |
| 69 |
'log_params', |
| 70 |
'log_deleted', |
| 71 |
'user_id', |
| 72 |
'user_name', |
| 73 |
'log_comment_text' => 'comment_log_comment.comment_text', |
| 74 |
'log_comment_data' => 'comment_log_comment.comment_data', |
| 75 |
'log_comment_cid' => 'comment_log_comment.comment_id', |
| 76 |
'log_user' => 'logging_actor.actor_user', |
| 77 |
'log_user_text' => 'logging_actor.actor_name', |
| 78 |
'log_actor', |
| 79 |
], |
| 80 |
'options' => [], |
| 81 |
'join_conds' => [ |
| 82 |
'user' => [ 'LEFT JOIN', 'user_id=logging_actor.actor_user' ], |
| 83 |
'comment_log_comment' => [ 'JOIN', 'comment_log_comment.comment_id = log_comment_id' ], |
| 84 |
'logging_actor' => [ 'JOIN', 'actor_id=log_actor' ], |
| 85 |
], |
| 86 |
]; |
| 87 |
return [ |
| 88 |
[ |
| 89 |
0, |
| 90 |
$newTables + [ 'conds' => [ 'log_id' => 0 ] ], |
| 91 |
null, |
| 92 |
null |
| 93 |
], |
| 94 |
[ |
| 95 |
123, |
| 96 |
$newTables + [ 'conds' => [ 'log_id' => 123 ] ], |
| 97 |
[ |
| 98 |
'log_id' => 123, |
| 99 |
'log_type' => 'foobarize', |
| 100 |
'log_comment_text' => 'test!', |
| 101 |
'log_comment_data' => null, |
| 102 |
], |
| 103 |
[ 'type' => 'foobarize', 'comment' => 'test!' ] |
| 104 |
], |
| 105 |
[ |
| 106 |
567, |
| 107 |
$newTables + [ 'conds' => [ 'log_id' => 567 ] ], |
| 108 |
[ |
| 109 |
'log_id' => 567, |
| 110 |
'log_type' => 'foobarize', |
| 111 |
'log_comment_text' => 'test!', |
| 112 |
'log_comment_data' => null, |
| 113 |
], |
| 114 |
[ 'type' => 'foobarize', 'comment' => 'test!' ] |
| 115 |
], |
| 116 |
]; |
| 117 |
} |
| 118 |
|
| 119 |
public static function provideGetPerformerIdentity() { |
| 120 |
yield 'registered actor' => [ |
| 121 |
'actor_row_fields' => [ |
| 122 |
'user_id' => 42, |
| 123 |
'log_user_text' => 'Testing', |
| 124 |
'log_actor' => 24, |
| 125 |
], |
| 126 |
UserIdentityValue::newRegistered( 42, 'Testing' ), |
| 127 |
]; |
| 128 |
yield 'anon actor' => [ |
| 129 |
'actor_row_fields' => [ |
| 130 |
'log_user_text' => '127.0.0.1', |
| 131 |
'log_actor' => 24, |
| 132 |
], |
| 133 |
UserIdentityValue::newAnonymous( '127.0.0.1' ), |
| 134 |
]; |
| 135 |
yield 'unknown actor' => [ |
| 136 |
'actor_row_fields' => [], |
| 137 |
new UserIdentityValue( 0, ActorStore::UNKNOWN_USER_NAME ), |
| 138 |
]; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function testGetPerformer( array $actorRowFields, UserIdentity $expected ) { |
| 146 |
$logEntry = DatabaseLogEntry::newFromRow( [ |
| 147 |
'log_id' => 1, |
| 148 |
] + $actorRowFields ); |
| 149 |
$performer = $logEntry->getPerformerIdentity(); |
| 150 |
$this->assertTrue( $expected->equals( $performer ) ); |
| 151 |
} |
| 152 |
} |