| 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 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 * @var Title[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	private static $titles = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	public static function createPage( $titleText ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		$title = Title::newFromText( $titleText ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		self::$titles[] = $title; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		$pageCreator = new PageCreator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 		$pageCreator->createPage( $title ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	public static function tearDownAfterClass() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		$pageDeleter = new PageDeleter(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		foreach ( self::$titles as $title ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 			$pageDeleter->deletePage( $title ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	public function testPageDefaulting() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		self::createPage( 'TempSPLTest:CountZZZ/ABC' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		self::createPage( 'TempSPLTest:CountZZZ' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		$this->assertSubPageCountFor( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 			'', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 			1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	public function testCountsIndirectChildren() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		self::createPage( 'TempSPLTest:CountAAA' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		self::createPage( 'TempSPLTest:CountAAA/AAA' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		self::createPage( 'TempSPLTest:CountAAA/AAA/AAA' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		self::createPage( 'TempSPLTest:CountAAA/AAA/BBB' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		$this->assertSubPageCountFor( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 			'TempSPLTest:CountAAA', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 * @depends testCountsIndirectChildren | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	public function testDoesNotCountParents() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		$this->assertSubPageCountFor( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			'TempSPLTest:CountAAA/AAA', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 			2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	public function testCountsIndirectChildrenWithoutParent() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		self::createPage( 'TempSPLTest:CountBBB' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		self::createPage( 'TempSPLTest:CountBBB/AAA/AAA' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		self::createPage( 'TempSPLTest:CountBBB/AAA/BBB' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		$this->assertSubPageCountFor( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 			'TempSPLTest:CountBBB', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 			2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  | 	private function assertSubPageCountFor( $pageName, $expectedCount ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  | 		$this->assertEquals( | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  | 			$expectedCount, | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  | 			(int)$this->getSubPageCountFor( $pageName ) | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  | 		); | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	private function getSubPageCountFor( $pageName ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		$extension = new Extension( Settings::newFromGlobals( $GLOBALS ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		$functionRunner = new FunctionRunner( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 			$extension->getCountHookDefinition(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			$extension->getCountHookHandler() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		$frame = $this->createMock( 'PPFrame' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		$frame->expects( $this->once() ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 			->method( 'expand' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 			->will( $this->returnArgument( 0 ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		$parser = $this->newParser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		$result = $functionRunner->run( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 			$parser, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 			[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 				'page' => $pageName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 			], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 			$frame | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		return reset( $result ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	private function newParser() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 		$parser = new \Parser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		$parser->mOptions = new \ParserOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		$parser->clearState(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 		$parser->setTitle( \Title::newMainPage() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		return $parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 126 |  |  | } |