Page MenuHomePhabricator

Write PHPUnit tests for Hooks::onArticleViewFooter
Open, Needs TriagePublic

Description

Hooks.php onArticleViewFooter() is loaded on literally every article that's not cached, but has no PHPUnit tests. We should fix that. We should try to get code coverage of it and the methods it uses (Hooks.php shouldShowNoIndex(), Hooks.php shouldNoIndexForNewArticleReasons(), Hooks.php shouldNoIndexForMagicWordReasons(), PageTriageUtil.php PageTriageUtil::isPageUnreviewed()) close to 100%.

https://doc.wikimedia.org/cover-extensions/PageTriage/includes/Hooks.php.html#457

https://coverme.toolforge.org/?repo=Extension%3APageTriage&type=all

image.png (1,833×1,154 px, 353 KB)

Event Timeline

Took a stab at this tonight. I tried this:

	/**
	 * @covers \MediaWiki\Extension\PageTriage\Hooks::onArticleViewFooter
	 * @covers \MediaWiki\Extension\PageTriage\Hooks::shouldShowNoIndex
	 */
	public function testShouldShowNoIndexForUnreviewedNewArticle() {
		$this->overrideConfigValues([
			'PageTriageNoIndexUnreviewedNewArticles' => true,
			'PageTriageMaxAge' => 90,
		]);

		$pageId = $this->insertPage(
			'testUnreviewedPage',
			'Text',
			NS_MAIN,
			$this->getTestUser()->getUser()
		)[ 'id' ];
		$wikipage = $this->getServiceContainer()->getWikiPageFactory()->newFromID( $pageId );
		$this->assertNotNull( $wikipage, 'Page should exist' );

		$status = PageTriageUtil::getStatus( $wikipage );
		$this->assertSame(
			QueueRecord::REVIEW_STATUS_UNREVIEWED,
			$status,
			'Page should be marked as unreviewed'
		);

		$context = RequestContext::getMain();
		$outputPage = $context->getOutput();
		$robotPolicy = $outputPage->getRobotPolicy();
		$this->assertStringContainsString(
			'noindex',
			$robotPolicy,
			'Unreviewed new articles 0 days old should have a noindex meta tag in the HTML'
		);
	}

However the test fails with "Failed asserting that 'index,follow' contains "noindex".", so simply calling $outputPage->getRobotPolicy() doesn't trigger the hook. Will probably need to get the entire page to render HTML and then examine the HTML. The hook onArticleViewFooter() (where the noindex code is) is also part of the skin, so getting HTML from ParserOutput is not sufficient.