1<?php
2
3namespace MediaWiki\Tests\Log;
4
5use DatabaseLogEntry;
6use LogPage;
7use MediaWiki\User\UserIdentityValue;
8use MockTitleTrait;
9
10/**
11 * @group Database
12 * @coversDefaultClass LogPage
13 * @package MediaWiki\Tests\Log
14 */
15class LogPageTest extends \MediaWikiIntegrationTestCase {
16	use MockTitleTrait;
17
18	protected function setUp(): void {
19		parent::setUp();
20
21		$this->setMwGlobals( [
22			'wgLogNames' => [
23				'test_test' => 'testing-log-message'
24			],
25			'wgLogHeaders' => [
26				'test_test' => 'testing-log-header'
27			],
28			'wgLogRestrictions' => [
29				'test_test' => 'testing-log-restriction'
30			]
31		] );
32		$this->tablesUsed[] = 'logging';
33	}
34
35	/**
36	 * @covers ::__construct
37	 * @covers ::getName
38	 * @covers ::getDescription
39	 * @covers ::getRestriction
40	 * @covers ::isRestricted
41	 */
42	public function testConstruct() {
43		$logPage = new LogPage( 'test_test' );
44		$this->assertSame( 'testing-log-message', $logPage->getName()->getKey() );
45		$this->assertSame( 'testing-log-header', $logPage->getDescription()->getKey() );
46		$this->assertSame( 'testing-log-restriction', $logPage->getRestriction() );
47		$this->assertTrue( $logPage->isRestricted() );
48	}
49
50	/**
51	 * @covers ::addEntry
52	 * @covers ::getComment
53	 * @covers ::getRcComment
54	 * @covers ::getRcCommentIRC
55	 */
56	public function testAddEntrySetsProperties() {
57		$logPage = new LogPage( 'test_test' );
58		$user = new UserIdentityValue( 0, '127.0.0.1' );
59		$logPage->addEntry(
60			'test_action',
61			$this->makeMockTitle( __METHOD__ ),
62			'testing_comment',
63			[ 'param_one', 'param_two' ],
64			$user
65		);
66		$this->assertSame( 'testing_comment', $logPage->getComment() );
67		$this->assertStringContainsString( 'testing_comment', $logPage->getRcComment() );
68		$this->assertStringContainsString( 'testing_comment', $logPage->getRcCommentIRC() );
69	}
70
71	/**
72	 * @covers ::addEntry
73	 */
74	public function testAddEntrySave() {
75		$logPage = new LogPage( 'test_test' );
76		$user = new UserIdentityValue( 0, '127.0.0.1' );
77		$title = $this->makeMockTitle( __METHOD__ );
78		$id = $logPage->addEntry(
79			'test_action',
80			$title,
81			'testing_comment',
82			[ 'param_one', 'param_two' ],
83			$user
84		);
85
86		$savedLogEntry = DatabaseLogEntry::newFromId( $id, $this->db );
87		$this->assertNotNull( $savedLogEntry );
88		$this->assertSame( 'test_test', $savedLogEntry->getType() );
89		$this->assertSame( 'test_action', $savedLogEntry->getSubtype() );
90		$this->assertSame( 'testing_comment', $savedLogEntry->getComment() );
91		$this->assertArrayEquals( [ 'param_one', 'param_two' ], $savedLogEntry->getParameters() );
92		$this->assertTrue( $title->equals( $savedLogEntry->getTarget() ) );
93		$this->assertTrue( $user->equals( $savedLogEntry->getPerformerIdentity() ) );
94	}
95}
96