Completed
Push — master ( f6659b...84ef1f )
by Jeroen De
14s queued 10s
created

SubPageCountingTest::testPageDefaulting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Tests\System\SubPageList;
4
5
use ParserHooks\FunctionRunner;
6
use PHPUnit\Framework\TestCase;
7
use SubPageList\Extension;
8
use SubPageList\Settings;
9
use Title;
10
11
/**
12
 * @group SubPageList
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class SubPageCountingTest extends TestCase {
17
18
	const CURRENT_PAGE_NAME = 'TempSPLTest:CurrentPage';
19
20
	/**
21
	 * @var Title[]
22
	 */
23
	private $titles = [];
24
25
	public function createPage( $titleText ) {
26
		$title = Title::newFromText( $titleText );
27
		$this->titles[] = $title;
28
29
		$pageCreator = new PageCreator();
30
		$pageCreator->createPage( $title );
31
	}
32
33
	public function tearDown() {
34
		$pageDeleter = new PageDeleter();
35
36
		foreach ( $this->titles as $title ) {
37
			$pageDeleter->deletePage( $title );
38
		}
39
40
		$this->titles = [];
41
	}
42
43
	public function testPageDefaulting() {
44
		$this->createPage( self::CURRENT_PAGE_NAME );
45
		$this->createPage( self::CURRENT_PAGE_NAME . '/SubPage' );
46
47
		$this->assertSubPageCountFor(
48
			'',
49
			1
50
		);
51
	}
52
53
	public function testCountsIndirectChildren() {
54
		$this->createPage( 'TempSPLTest:CountAAA' );
55
		$this->createPage( 'TempSPLTest:CountAAA/AAA' );
56
		$this->createPage( 'TempSPLTest:CountAAA/AAA/AAA' );
57
		$this->createPage( 'TempSPLTest:CountAAA/AAA/BBB' );
58
59
		$this->assertSubPageCountFor(
60
			'TempSPLTest:CountAAA',
61
			3
62
		);
63
	}
64
65
	public function testDoesNotCountParents() {
66
		$this->createPage( 'TempSPLTest:CountAAA' );
67
		$this->createPage( 'TempSPLTest:CountAAA/AAA' );
68
		$this->createPage( 'TempSPLTest:CountAAA/AAA/AAA' );
69
		$this->createPage( 'TempSPLTest:CountAAA/AAA/BBB' );
70
71
		$this->assertSubPageCountFor(
72
			'TempSPLTest:CountAAA/AAA',
73
			2
74
		);
75
	}
76
77
	public function testCountsIndirectChildrenWithoutParent() {
78
		$this->createPage( 'TempSPLTest:CountBBB' );
79
		$this->createPage( 'TempSPLTest:CountBBB/AAA/AAA' );
80
		$this->createPage( 'TempSPLTest:CountBBB/AAA/BBB' );
81
82
		$this->assertSubPageCountFor(
83
			'TempSPLTest:CountBBB',
84
			2
85
		);
86
	}
87
88
	private function assertSubPageCountFor( $pageName, $expectedCount ) {
89
		$this->assertEquals(
90
			$expectedCount,
91
			(int)$this->getSubPageCountFor( $pageName )
92
		);
93
	}
94
95
	private function getSubPageCountFor( $pageName ) {
96
		$extension = new Extension( Settings::newFromGlobals( $GLOBALS ) );
97
98
		$functionRunner = new FunctionRunner(
99
			$extension->getCountHookDefinition(),
100
			$extension->getCountHookHandler()
101
		);
102
103
		$frame = $this->createMock( 'PPFrame' );
104
105
		$frame->expects( $this->once() )
106
			->method( 'expand' )
107
			->will( $this->returnArgument( 0 ) );
108
109
110
		$parser = $this->newParser();
111
		$result = $functionRunner->run(
112
			$parser,
113
			[
114
				'page' => $pageName
115
			],
116
			$frame
117
		);
118
119
		return reset( $result );
120
	}
121
122
	private function newParser() {
123
		$parser = new \Parser();
124
125
		$parser->mOptions = new \ParserOptions();
126
		$parser->clearState();
127
		$parser->setTitle( \Title::newFromText( self::CURRENT_PAGE_NAME ) );
128
129
		return $parser;
130
	}
131
132
}