Skip to content

Commit

Permalink
Only add tags during view action
Browse files Browse the repository at this point in the history
  • Loading branch information
mwjames committed Feb 27, 2015
1 parent 9668ecf commit d0b8088
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/HookRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function register() {

$outputPageTagFormatter = new OutputPageTagFormatter( $outputPage );
$outputPageTagFormatter->setMetaTagsBlacklist( $configuration['metaTagsBlacklist'] );
$outputPageTagFormatter->setViewActionState( \Action::getActionName( $outputPage->getContext() ) );

$propertyValuesContentFetcher = new PropertyValuesContentFetcher( $fallbackSemanticDataFetcher );
$propertyValuesContentFetcher->useFallbackChainForMultipleProperties( $configuration['metaTagsFallbackUseForMultipleProperties'] );
Expand Down
16 changes: 15 additions & 1 deletion src/OutputPageTagFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class OutputPageTagFormatter {
*/
private $usedOpenGraphProtocolMarkup = false;

/**
* @var boolean
*/
private $isViewAction = false;

/**
* @since 1.0
*
Expand All @@ -45,14 +50,23 @@ public function setMetaTagsBlacklist( array $metaTagsBlacklist ) {
$this->metaTagsBlacklist = array_flip( $metaTagsBlacklist );
}

/**
* @since 1.0
*
* @param string $actionName
*/
public function setViewActionState( $actionName ) {
$this->isViewAction = $actionName === 'view';
}

/**
* @since 1.0
*
* @return boolean
*/
public function canUseOutputPage() {

if ( $this->outputPage->getTitle() === null || $this->outputPage->getTitle()->isSpecialPage() ) {
if ( $this->outputPage->getTitle() === null || $this->outputPage->getTitle()->isSpecialPage() || !$this->isViewAction ) {
return false;
}

Expand Down
16 changes: 16 additions & 0 deletions tests/phpunit/Unit/HookRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ public function doTestOutputPageParserOutput( $instance ) {

$title = Title::newFromText( __METHOD__ );

$webRequest = $this->getMockBuilder( '\WebRequest' )
->disableOriginalConstructor()
->getMock();

$context = $this->getMockBuilder( '\IContextSource' )
->disableOriginalConstructor()
->getMock();

$context->expects( $this->atLeastOnce() )
->method( 'getRequest' )
->will( $this->returnValue( $webRequest ) );

$outputPage = $this->getMockBuilder( '\OutputPage' )
->disableOriginalConstructor()
->getMock();
Expand All @@ -61,6 +73,10 @@ public function doTestOutputPageParserOutput( $instance ) {
->method( 'getTitle' )
->will( $this->returnValue( $title ) );

$outputPage->expects( $this->atLeastOnce() )
->method( 'getContext' )
->will( $this->returnValue( $context ) );

$parserOutput = $this->getMockBuilder( '\ParserOutput' )
->disableOriginalConstructor()
->getMock();
Expand Down
31 changes: 30 additions & 1 deletion tests/phpunit/Unit/OutputPageTagFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testCanConstruct() {
);
}

public function testTryToUseOutputPage() {
public function testTryToUseOutputPageForSpecialPage() {

$title = $this->getMockBuilder( '\Title' )
->disableOriginalConstructor()
Expand Down Expand Up @@ -56,6 +56,35 @@ public function testTryToUseOutputPage() {
);
}

public function testTryToUseOutputPageForNonViewAction() {

$title = $this->getMockBuilder( '\Title' )
->disableOriginalConstructor()
->getMock();

$title->expects( $this->any() )
->method( 'isSpecialPage' )
->will( $this->returnValue( false ) );

$outputPage = $this->getMockBuilder( '\OutputPage' )
->disableOriginalConstructor()
->getMock();

$outputPage->expects( $this->never() )
->method( 'addMeta' );

$outputPage->expects( $this->atLeastOnce() )
->method( 'getTitle' )
->will( $this->returnValue( $title ) );

$instance = new OutputPageTagFormatter( $outputPage );
$instance->setViewActionState( 'foo' );

$this->assertFalse(
$instance->canUseOutputPage()
);
}

public function testTryToAddContentForBlacklistedTag() {

$outputPage = $this->getMockBuilder( '\OutputPage' )
Expand Down

0 comments on commit d0b8088

Please sign in to comment.