Use the Builder for Sanitizers.

Cover the sanitizer build step in tests too
This commit is contained in:
Sebastian Marzjan 2015-12-28 11:59:53 +01:00
parent 8f437fb1e7
commit 0ce0b500fe
11 changed files with 97 additions and 35 deletions

View file

@ -50,7 +50,9 @@ $wgAutoloadClasses[ 'Wikia\PortableInfobox\Helpers\PortableInfoboxTemplatesHelpe
$wgAutoloadClasses[ 'Wikia\PortableInfobox\Helpers\PagePropsProxy' ] = $dir . 'services/Helpers/PagePropsProxy.php'; $wgAutoloadClasses[ 'Wikia\PortableInfobox\Helpers\PagePropsProxy' ] = $dir . 'services/Helpers/PagePropsProxy.php';
//sanitizers //sanitizers
$wgAutoloadClasses[ 'SanitizerBuilder' ] = $dir . 'services/Sanitizers/SanitizerBuilder.php';
$wgAutoloadClasses[ 'NodeSanitizer' ] = $dir . 'services/Sanitizers/NodeSanitizer.php'; $wgAutoloadClasses[ 'NodeSanitizer' ] = $dir . 'services/Sanitizers/NodeSanitizer.php';
$wgAutoloadClasses[ 'PassThroughSanitizer' ] = $dir . 'services/Sanitizers/PassThroughSanitizer.php';
$wgAutoloadClasses[ 'NodeTypeSanitizerInterface' ] = $dir . 'services/Sanitizers/NodeTypeSanitizerInterface.php'; $wgAutoloadClasses[ 'NodeTypeSanitizerInterface' ] = $dir . 'services/Sanitizers/NodeTypeSanitizerInterface.php';
$wgAutoloadClasses[ 'NodeDataSanitizer' ] = $dir . 'services/Sanitizers/NodeDataSanitizer.php'; $wgAutoloadClasses[ 'NodeDataSanitizer' ] = $dir . 'services/Sanitizers/NodeDataSanitizer.php';
$wgAutoloadClasses[ 'NodeHeroImageSanitizer' ] = $dir . 'services/Sanitizers/NodeHeroImageSanitizer.php'; $wgAutoloadClasses[ 'NodeHeroImageSanitizer' ] = $dir . 'services/Sanitizers/NodeHeroImageSanitizer.php';

View file

@ -22,13 +22,10 @@ class PortableInfoboxRenderService extends WikiaService {
'image-collection-mobile' => 'PortableInfoboxItemImageCollectionMobile.mustache' 'image-collection-mobile' => 'PortableInfoboxItemImageCollectionMobile.mustache'
]; ];
private $templateEngine; private $templateEngine;
private $nodeSanitizer;
function __construct() { function __construct() {
$this->templateEngine = ( new Wikia\Template\MustacheEngine ) $this->templateEngine = ( new Wikia\Template\MustacheEngine )
->setPrefix( self::getTemplatesDir() ); ->setPrefix( self::getTemplatesDir() );
$this->nodeSanitizer = new NodeSanitizer();
} }
public static function getTemplatesDir() { public static function getTemplatesDir() {
@ -176,7 +173,7 @@ class PortableInfoboxRenderService extends WikiaService {
for ( $i = 0; $i < count($data); $i++ ) { for ( $i = 0; $i < count($data); $i++ ) {
$data[$i][ 'context' ] = self::MEDIA_CONTEXT_INFOBOX; $data[$i][ 'context' ] = self::MEDIA_CONTEXT_INFOBOX;
$data[$i] = $helper->extendImageData( $data[$i] ); $data[$i] = $helper->extendImageData( $data[$i] );
$data[$i] = $this->nodeSanitizer->sanitizeInfoboxFields( $type, $data[$i] ); $data[$i] = SanitizerBuilder::createFromType( $type )->sanitize( $data[$i] );
if ( !!$data[$i] ) { if ( !!$data[$i] ) {
$images[] = $data[$i]; $images[] = $data[$i];
@ -202,7 +199,7 @@ class PortableInfoboxRenderService extends WikiaService {
} }
if ( $helper->isWikiaMobile() ) { if ( $helper->isWikiaMobile() ) {
$data = $this->nodeSanitizer->sanitizeInfoboxFields( $type, $data ); $data = SanitizerBuilder::createFromType( $type )->sanitize( $data );
} }
return $this->templateEngine->clearData() return $this->templateEngine->clearData()

View file

@ -2,33 +2,9 @@
use Wikia\Logger\WikiaLogger; use Wikia\Logger\WikiaLogger;
class NodeSanitizer { abstract class NodeSanitizer {
private $uniqTag = 'uniq'; private $uniqTag = 'uniq';
/**
* @desc call proper for given type sanitizer class
*
* @param $type
* @param $data
* @return mixed
*/
public function sanitizeInfoboxFields( $type, $data ) {
switch ( $type ) {
case 'data':
return ( new NodeDataSanitizer() )->sanitize( $data );
case 'horizontal-group-content':
return ( new NodeHorizontalGroupSanitizer() )->sanitize( $data );
case 'title':
return ( new NodeTitleSanitizer() )->sanitize( $data );
case 'image':
return ( new NodeImageSanitizer() )->sanitize( $data );
case 'hero-mobile':
return ( new NodeHeroImageSanitizer() )->sanitize( $data );
}
return $data;
}
/** /**
* process single title or label * process single title or label
* *

View file

@ -0,0 +1,11 @@
<?php
class PassThroughSanitizer extends NodeSanitizer implements NodeTypeSanitizerInterface {
/**
* @param $data
* @return mixed
*/
public function sanitize( $data ) {
return $data;
}
}

View file

@ -0,0 +1,27 @@
<?php
class SanitizerBuilder {
/**
* @desc provide sanitizer for a given node type
*
* @param $type
* @return NodeSanitizer
*/
static public function createFromType( $type ) {
switch ( $type ) {
case 'data':
return new NodeDataSanitizer();
case 'horizontal-group-content':
return new NodeHorizontalGroupSanitizer();
case 'title':
return new NodeTitleSanitizer();
case 'image':
return new NodeImageSanitizer();
case 'hero-mobile':
return new NodeHeroImageSanitizer();
default:
return new PassThroughSanitizer();
}
}
}

View file

@ -6,7 +6,7 @@ class NodeDataSanitizerTest extends WikiaBaseTest {
protected function setUp() { protected function setUp() {
$this->setupFile = dirname( __FILE__ ) . '/../../PortableInfobox.setup.php'; $this->setupFile = dirname( __FILE__ ) . '/../../PortableInfobox.setup.php';
$this->sanitizer = new NodeDataSanitizer(); $this->sanitizer = SanitizerBuilder::createFromType('data');
parent::setUp(); parent::setUp();
} }

View file

@ -6,7 +6,7 @@ class NodeHeroImageSanitizerTest extends WikiaBaseTest {
protected function setUp() { protected function setUp() {
$this->setupFile = dirname( __FILE__ ) . '/../../PortableInfobox.setup.php'; $this->setupFile = dirname( __FILE__ ) . '/../../PortableInfobox.setup.php';
$this->sanitizer = new NodeHeroImageSanitizer(); $this->sanitizer = SanitizerBuilder::createFromType('hero-mobile');
parent::setUp(); parent::setUp();
} }

View file

@ -6,7 +6,7 @@ class NodeHorizontalGroupSanitizerTest extends WikiaBaseTest {
protected function setUp() { protected function setUp() {
$this->setupFile = dirname( __FILE__ ) . '/../../PortableInfobox.setup.php'; $this->setupFile = dirname( __FILE__ ) . '/../../PortableInfobox.setup.php';
$this->sanitizer = new NodeHorizontalGroupSanitizer(); $this->sanitizer = SanitizerBuilder::createFromType('horizontal-group-content');
parent::setUp(); parent::setUp();
} }

View file

@ -6,7 +6,7 @@ class NodeImageSanitizerTest extends WikiaBaseTest {
protected function setUp() { protected function setUp() {
$this->setupFile = dirname( __FILE__ ) . '/../../PortableInfobox.setup.php'; $this->setupFile = dirname( __FILE__ ) . '/../../PortableInfobox.setup.php';
$this->sanitizer = new NodeImageSanitizer(); $this->sanitizer = SanitizerBuilder::createFromType('image');
parent::setUp(); parent::setUp();
} }

View file

@ -6,7 +6,7 @@ class NodeTitleSanitizerTest extends WikiaBaseTest {
protected function setUp() { protected function setUp() {
$this->setupFile = dirname( __FILE__ ) . '/../../PortableInfobox.setup.php'; $this->setupFile = dirname( __FILE__ ) . '/../../PortableInfobox.setup.php';
$this->sanitizer = new NodeTitleSanitizer(); $this->sanitizer = SanitizerBuilder::createFromType('title');
parent::setUp(); parent::setUp();
} }

View file

@ -0,0 +1,49 @@
<?php
class PassThroughSanitizerTest extends WikiaBaseTest {
private $sanitizer;
protected function setUp() {
$this->setupFile = dirname( __FILE__ ) . '/../../PortableInfobox.setup.php';
$this->sanitizer = SanitizerBuilder::createFromType('invalid-type');
parent::setUp();
}
/**
* @param $data
* @param $expected
* @dataProvider testSanitizeDataProvider
*/
function testSanitize( $data, $expected ) {
$this->assertEquals(
$expected,
$this->sanitizer->sanitize( $data )
);
}
function testSanitizeDataProvider() {
return [
[
['value' => 'Test Title' ],
[ 'value' => 'Test Title' ]
],
[
['value' => ' Test Title '],
['value' => ' Test Title '],
],
[
['value' => 'Test Title <img src=\'data:image/gif;base64,R0lGODlhAQABAIABAAAAAP///yH5BAEAAAEALAAAAAABAAEAQAICTAEAOw%3D%3D\' class=\'article-media\' data-ref=\'1\' width=\'400\' height=\'100\' /> ' ],
['value' => 'Test Title <img src=\'data:image/gif;base64,R0lGODlhAQABAIABAAAAAP///yH5BAEAAAEALAAAAAABAAEAQAICTAEAOw%3D%3D\' class=\'article-media\' data-ref=\'1\' width=\'400\' height=\'100\' /> ' ],
],
[
['value' => 'Test Title <a href="example.com">with link</a>'],
['value' => 'Test Title <a href="example.com">with link</a>'],
],
[
['value' => 'Real world <a href="http://vignette-poz.wikia-dev.com/mediawiki116/images/b/b6/DBGT_Logo.svg/revision/latest?cb=20150601155347" class="image image-thumbnail" ><img src="http://vignette-poz.wikia-dev.com/mediawiki116/images/b/b6/DBGT_Logo.svg/revision/latest/scale-to-width-down/30?cb=20150601155347" alt="DBGT Logo" class="" data-image-key="DBGT_Logo.svg" data-image-name="DBGT Logo.svg" width="30" height="18" ></a>title example'] ,
['value' => 'Real world <a href="http://vignette-poz.wikia-dev.com/mediawiki116/images/b/b6/DBGT_Logo.svg/revision/latest?cb=20150601155347" class="image image-thumbnail" ><img src="http://vignette-poz.wikia-dev.com/mediawiki116/images/b/b6/DBGT_Logo.svg/revision/latest/scale-to-width-down/30?cb=20150601155347" alt="DBGT Logo" class="" data-image-key="DBGT_Logo.svg" data-image-name="DBGT Logo.svg" width="30" height="18" ></a>title example'] ,
],
];
}
}