1<?php
2
3/**
4 * @group Database
5 */
6class ArticleTest extends \MediaWikiIntegrationTestCase {
7
8	/**
9	 * @var Title
10	 */
11	private $title;
12	/**
13	 * @var Article
14	 */
15	private $article;
16
17	/** creates a title object and its article object */
18	protected function setUp(): void {
19		parent::setUp();
20		$this->title = Title::makeTitle( NS_MAIN, 'SomePage' );
21		$this->article = new Article( $this->title );
22	}
23
24	/**
25	 * @covers Article::__get
26	 */
27	public function testImplementsGetMagic() {
28		$this->filterDeprecated( '/Accessing Article::\$mLatest/' );
29		$this->assertFalse( $this->article->mLatest, "Article __get magic" );
30	}
31
32	/**
33	 * @depends testImplementsGetMagic
34	 * @covers Article::__set
35	 */
36	public function testImplementsSetMagic() {
37		$this->filterDeprecated( '/Accessing Article::\$mLatest/' );
38		$this->filterDeprecated( '/Setting Article::\$mLatest/' );
39		$this->article->mLatest = 2;
40		$this->assertEquals( 2, $this->article->mLatest, "Article __set magic" );
41	}
42
43	/**
44	 * @covers Article::__get
45	 * @covers Article::__set
46	 */
47	public function testGetOrSetOnNewProperty() {
48		$this->filterDeprecated(
49			'/Accessing Article::\$ext_someNewProperty/'
50		);
51		$this->filterDeprecated(
52			'/Setting Article::\$ext_someNewProperty/'
53		);
54		$this->article->ext_someNewProperty = 12;
55		$this->assertEquals( 12, $this->article->ext_someNewProperty,
56			"Article get/set magic on new field" );
57		$this->assertEquals( 12, $this->article->getPage()->ext_someNewProperty,
58			"Article get/set magic on new field" );
59		$this->article->ext_someNewProperty = -8;
60		$this->assertEquals( -8, $this->article->ext_someNewProperty,
61			"Article get/set magic on update to new field" );
62		$this->assertEquals( -8, $this->article->getPage()->ext_someNewProperty,
63			"Article get/set magic on new field" );
64	}
65
66	/**
67	 * @covers Article::__sleep
68	 */
69	public function testSerialization_fails() {
70		$this->expectException( LogicException::class );
71		serialize( $this->article );
72	}
73
74	/**
75	 * Tests that missing article page shows parser contents
76	 * of the well-known system message for NS_MEDIAWIKI pages
77	 * @covers Article::showMissingArticle
78	 */
79	public function testMissingArticleMessage() {
80		// Use a well-known system message
81		$title = Title::makeTitle( NS_MEDIAWIKI, 'Uploadedimage' );
82		$article = new Article( $title, 0 );
83		$article->getContext()->getOutput()->setTitle( $title );
84		$article->showMissingArticle();
85		$output = $article->getContext()->getOutput();
86		$this->assertStringContainsString(
87			Message::newFromKey( 'uploadedimage' )->parse(),
88			$output->getHTML()
89		);
90	}
91
92	/**
93	 * Test if patrol footer is possible to show
94	 * @covers Article::showPatrolFooter
95	 * @dataProvider provideShowPatrolFooter
96	 */
97	public function testShowPatrolFooter( $group, $title, $editPageText, $isEditedBySameUser, $expectedResult ) {
98		$context = new RequestContext();
99		$article = new Article( $title );
100		$user1 = $this->getTestUser( $group )->getUser();
101		$user2 = $this->getTestUser()->getUser();
102		$context->setUser( $user1 );
103		$article->setContext( $context );
104		if ( $editPageText !== null ) {
105			$editedUser = $isEditedBySameUser ? $user1 : $user2;
106			$editIsGood = $this->editPage( $article->getPage(), $editPageText, '', NS_MAIN, $editedUser )->isGood();
107			$this->assertTrue( $editIsGood, 'Sanity: edited a page' );
108		}
109		$this->assertSame( $expectedResult, $article->showPatrolFooter() );
110	}
111
112	public function provideShowPatrolFooter() {
113		yield 'UserAllowedRevExist' => [
114			'sysop',
115			Title::makeTitle( NS_MAIN, 'Page1' ),
116			'EditPage1',
117			false,
118			true
119		];
120
121		yield 'UserNotAllowedRevExist' => [
122			null,
123			Title::makeTitle( NS_MAIN, 'Page2' ),
124			'EditPage2',
125			false,
126			false
127		];
128
129		yield 'UserAllowedNoRev' => [
130			'sysop',
131			Title::makeTitle( NS_MAIN, 'Page3' ),
132			null,
133			false,
134			false
135		];
136
137		yield 'UserAllowedRevExistBySameUser' => [
138			'sysop',
139			Title::makeTitle( NS_MAIN, 'Page4' ),
140			'EditPage4',
141			true,
142			false
143		];
144	}
145}
146