SOC-808 Merge part 1 of 2

This commit is contained in:
Garth Webb 2015-05-19 11:20:44 -07:00
parent 6fefa10110
commit e4afb0c697
9 changed files with 182 additions and 66 deletions

View file

@ -41,6 +41,7 @@ foreach ( $wgInfoboxParserNodes as $parserNode ) {
// helpers // helpers
$wgAutoloadClasses[ 'Wikia\PortableInfobox\Helpers\ImageFilenameSanitizer' ] = $dir . 'services/Helpers/ImageFilenameSanitizer.php'; $wgAutoloadClasses[ 'Wikia\PortableInfobox\Helpers\ImageFilenameSanitizer' ] = $dir . 'services/Helpers/ImageFilenameSanitizer.php';
$wgAutoloadClasses[ 'Wikia\PortableInfobox\Helpers\SimpleXmlUtil' ] = $dir . 'services/Helpers/SimpleXmlUtil.php';
// controller classes // controller classes
$wgAutoloadClasses[ 'PortableInfoboxParserTagController' ] = $dir . 'controllers/PortableInfoboxParserTagController.class.php'; $wgAutoloadClasses[ 'PortableInfoboxParserTagController' ] = $dir . 'controllers/PortableInfoboxParserTagController.class.php';

View file

@ -0,0 +1,37 @@
<?php
namespace Wikia\PortableInfobox\Helpers;
class SimpleXmlUtil {
private static $instance = null;
private function __construct() {
}
/**
* @return null|SimpleXmlUtil
*/
public static function getInstance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Gets contents of SimpleXML node as XML string
* Will return empty string for a non-node argument
*
* @param $node
* @return string
*/
public function getInnerXML( $node ) {
$innerXML = '';
if ( $node instanceof \SimpleXMLElement ) {
foreach ( dom_import_simplexml( $node )->childNodes as $child ) {
$innerXML .= $child->ownerDocument->saveXML( $child );
}
}
return $innerXML;
}
}

View file

@ -46,16 +46,10 @@ class Node {
return [ 'value' => (string)$this->xmlNode ]; return [ 'value' => (string)$this->xmlNode ];
} }
//note that a '0' value cannot be treated like a null
public function isEmpty( $data ) { public function isEmpty( $data ) {
return !( isset( $data[ 'value' ] ) ) || empty( $data[ 'value' ] ); $value = $data[ 'value' ];
} return !( isset( $value ) ) || (empty( $value ) && $value != '0');
protected function getInnerXML( \SimpleXMLElement $node ) {
$innerXML= '';
foreach ( dom_import_simplexml( $node )->childNodes as $child ) {
$innerXML .= $child->ownerDocument->saveXML( $child );
}
return $innerXML;
} }
protected function getValueWithDefault( \SimpleXMLElement $xmlNode ) { protected function getValueWithDefault( \SimpleXMLElement $xmlNode ) {
@ -71,7 +65,9 @@ class Node {
* We should not parse it's contents as XML but return pure text in order to let MediaWiki Parser * We should not parse it's contents as XML but return pure text in order to let MediaWiki Parser
* parse it. * parse it.
*/ */
$value = $this->getInnerXML( $xmlNode->{self::DEFAULT_TAG_NAME} ); $value = \Wikia\PortableInfobox\Helpers\SimpleXmlUtil::getInstance()->getInnerXML(
$xmlNode->{self::DEFAULT_TAG_NAME}
);
$value = $this->getExternalParser()->parseRecursive( $value ); $value = $this->getExternalParser()->parseRecursive( $value );
} }
} }

View file

@ -5,7 +5,11 @@ class NodeData extends Node {
public function getData() { public function getData() {
return [ return [
'label' => $this->getExternalParser()->parseRecursive( $this->getInnerXML( $this->xmlNode->{self::LABEL_TAG_NAME} ) ), 'label' => $this->getExternalParser()->parseRecursive(
\Wikia\PortableInfobox\Helpers\SimpleXmlUtil::getInstance()->getInnerXML(
$this->xmlNode->{self::LABEL_TAG_NAME}
)
),
'value' => $this->getValueWithDefault( $this->xmlNode ) 'value' => $this->getValueWithDefault( $this->xmlNode )
]; ];
} }

View file

@ -4,11 +4,13 @@ namespace Wikia\PortableInfobox\Parser\Nodes;
class NodeFooter extends Node { class NodeFooter extends Node {
public function getData() { public function getData() {
return [ 'value' => $this->getExternalParser()->parseRecursive( $this->getInnerXML( $this->xmlNode ) ) ]; return [ 'value' => $this->getExternalParser()->parseRecursive(
\Wikia\PortableInfobox\Helpers\SimpleXmlUtil::getInstance()->getInnerXML( $this->xmlNode )
) ];
} }
public function isEmpty( $data ) { public function isEmpty( $data ) {
$links = trim( $data[ 'value' ] ); $links = trim( $data['value'] );
return empty( $links ); return empty( $links );
} }
} }

View file

@ -4,6 +4,8 @@ namespace Wikia\PortableInfobox\Parser\Nodes;
class NodeHeader extends Node { class NodeHeader extends Node {
public function getData() { public function getData() {
return [ 'value' => $this->getExternalParser()->parseRecursive( $this->getInnerXML( $this->xmlNode ) ) ]; return [ 'value' => $this->getExternalParser()->parseRecursive(
\Wikia\PortableInfobox\Helpers\SimpleXmlUtil::getInstance()->getInnerXML( $this->xmlNode )
) ];
} }
} }

70
tests/NodeTest.php Normal file
View file

@ -0,0 +1,70 @@
<?php
class NodeTest extends WikiaBaseTest {
protected function setUp() {
$this->setupFile = dirname( __FILE__ ) . '/../PortableInfobox.setup.php';
parent::setUp();
}
/**
* @dataProvider testIsEmptyDataProvider
*/
public function testIsEmpty( $season, $expectedOutput ) {
$string = '<data source="season"><label>Season</label></data>';
$xml = simplexml_load_string( $string );
$node = new Wikia\PortableInfobox\Parser\Nodes\NodeData( $xml, [ 'season' => $season ] );
$nodeData = $node->getData();
$this->assertTrue( $node->isEmpty( $nodeData ) == $expectedOutput );
}
public function testIsEmptyDataProvider() {
return [
[
'season' => '0',
'expectedOutput' => false
],
[
'season' => '1',
'expectedOutput' => false
],
[
'season' => 'one',
'expectedOutput' => false
],
[
'season' => 'null',
'expectedOutput' => false
],
[
'season' => 'false',
'expectedOutput' => false
],
[
'season' => ' ',
'expectedOutput' => false
],
[
'season' => null,
'expectedOutput' => true
],
[
'season' => [],
'expectedOutput' => true
],
[
'season' => '',
'expectedOutput' => true
],
[
'season' => 5,
'expectedOutput' => false
],
[
'season' => 0,
'expectedOutput' => false
]
];
}
}

View file

@ -41,22 +41,6 @@ class PortableInfoboxParserNodesTest extends WikiaBaseTest {
$this->assertTrue( $nodeDefault->getData()[ 'alt' ] == 'default-alt', 'default alt' ); $this->assertTrue( $nodeDefault->getData()[ 'alt' ] == 'default-alt', 'default alt' );
} }
/**
* @dataProvider testNodeImageVariableReplaceProvider
* @todo rethink this test cause after changes to NodeImage it doesn't make sense in this form
*/
// public function testNodeImageVariableReplace( $xmlString, $data, $expValue ) {
// $xml = simplexml_load_string($xmlString);
// $node = $this->getMockBuilder( 'Wikia\PortableInfobox\Parser\Nodes\NodeImage' )
// ->setConstructorArgs( [ $xml, $data ] )
// ->setMethods( [ 'resolveImageUrl' ] )
// ->getMock();
// $node->expects( $this->any() )->method( 'resolveImageUrl' )->with( $this->equalTo( $expValue ) );
// $externalParser = new \Wikia\PortableInfobox\Parser\DummyParser();
// $node->setExternalParser( $externalParser );
// $node->getData();
// }
public function testNodeHeader() { public function testNodeHeader() {
$string = '<header>Comandantes</header>'; $string = '<header>Comandantes</header>';
$xml = simplexml_load_string( $string ); $xml = simplexml_load_string( $string );
@ -116,40 +100,4 @@ class PortableInfoboxParserNodesTest extends WikiaBaseTest {
$this->assertTrue( $data[ 'value' ][ 0 ]['data']['value'][ 1 ][ 'type' ] == 'data' ); $this->assertTrue( $data[ 'value' ][ 0 ]['data']['value'][ 1 ][ 'type' ] == 'data' );
$this->assertTrue( $data[ 'value' ][ 0 ]['data']['value'][ 2 ][ 'data' ][ 'value' ] == 2 ); $this->assertTrue( $data[ 'value' ][ 0 ]['data']['value'][ 2 ][ 'data' ][ 'value' ] == 2 );
} }
public function testNodeImageVariableReplaceProvider() {
return [
[
'<image source="image2"><alt source="alt-source"><default>default-alt</default></alt></image>',
[
'image2' => 'Wiki-image'
],
'Wiki-image',
'Regular filename should be untouched'
],
[
'<image source="image2"><alt source="alt-source"><default>default-alt</default></alt></image>',
[
'image2' => '[[Wiki-image]]'
],
'[[Wiki-image]]',
'Link to filename should be untouched'
],
[
'<image source="image2"><alt source="alt-source"><default>default-alt</default></alt></image>',
[
'image2' => '[[File:Wiki-image]]'
],
'[[File:Wiki-image]]',
'File invocation in params should be untouched'
],
[
'<image source="image2"><default>[[File:Wiki-image]]</default><alt source="alt-source"><default>default-alt</default></alt></image>',
[ ],
'replaceVariables([[File:Wiki-image]])',
'File invocation in default should invoke replace variables'
],
];
}
} }

View file

@ -0,0 +1,56 @@
<?php
class SimpleXmlUtilTest extends WikiaBaseTest {
private $simpleXmlUtil;
protected function setUp() {
$this->setupFile = dirname( __FILE__ ) . '/../PortableInfobox.setup.php';
parent::setUp();
$this->simpleXmlUtil = \Wikia\PortableInfobox\Helpers\SimpleXmlUtil::getInstance();
}
/**
* @dataProvider testGetInnerXMLDataProvider
*/
public function testGetInnerXML( $xmlString, $expValue, $message ) {
$xml = simplexml_load_string( $xmlString );
$this->assertEquals( $expValue, $this->simpleXmlUtil->getInnerXML( $xml ), $message );
}
public function testGetInnerXMLDataProvider() {
return [
[
'<data source="name"><default><gallery orientation="mosaic">TestFile.png</gallery></default></data>',
'<default><gallery orientation="mosaic">TestFile.png</gallery></default>',
'Valid gallery tag in default'
],
[
'<data source="name"><default>Test <ref name="multiple" /></default></data>',
'<default>Test <ref name="multiple"/></default>',
'Text + XML'
],
[
'<data source="name"><default><ref name="multiple" /></default></data>',
'<default><ref name="multiple"/></default>',
'Valid self-closing tag'
],
[
'<data source="name"><default><ref name="multiple" ></default></data>',
'',
'Invalid inner XML'
],
[
'<data source="name"><default></data>',
'',
'Invalid inner XML'
],
[
'<data source="name" />',
'',
'No inner XML'
]
];
}
}