mirror of
https://github.com/Universal-Omega/PortableInfobox.git
synced 2024-11-15 11:59:56 +00:00
Use the Builder for Sanitizers.
Cover the sanitizer build step in tests too
This commit is contained in:
parent
8f437fb1e7
commit
0ce0b500fe
|
@ -50,7 +50,9 @@ $wgAutoloadClasses[ 'Wikia\PortableInfobox\Helpers\PortableInfoboxTemplatesHelpe
|
|||
$wgAutoloadClasses[ 'Wikia\PortableInfobox\Helpers\PagePropsProxy' ] = $dir . 'services/Helpers/PagePropsProxy.php';
|
||||
|
||||
//sanitizers
|
||||
$wgAutoloadClasses[ 'SanitizerBuilder' ] = $dir . 'services/Sanitizers/SanitizerBuilder.php';
|
||||
$wgAutoloadClasses[ 'NodeSanitizer' ] = $dir . 'services/Sanitizers/NodeSanitizer.php';
|
||||
$wgAutoloadClasses[ 'PassThroughSanitizer' ] = $dir . 'services/Sanitizers/PassThroughSanitizer.php';
|
||||
$wgAutoloadClasses[ 'NodeTypeSanitizerInterface' ] = $dir . 'services/Sanitizers/NodeTypeSanitizerInterface.php';
|
||||
$wgAutoloadClasses[ 'NodeDataSanitizer' ] = $dir . 'services/Sanitizers/NodeDataSanitizer.php';
|
||||
$wgAutoloadClasses[ 'NodeHeroImageSanitizer' ] = $dir . 'services/Sanitizers/NodeHeroImageSanitizer.php';
|
||||
|
|
|
@ -22,13 +22,10 @@ class PortableInfoboxRenderService extends WikiaService {
|
|||
'image-collection-mobile' => 'PortableInfoboxItemImageCollectionMobile.mustache'
|
||||
];
|
||||
private $templateEngine;
|
||||
private $nodeSanitizer;
|
||||
|
||||
function __construct() {
|
||||
$this->templateEngine = ( new Wikia\Template\MustacheEngine )
|
||||
->setPrefix( self::getTemplatesDir() );
|
||||
|
||||
$this->nodeSanitizer = new NodeSanitizer();
|
||||
}
|
||||
|
||||
public static function getTemplatesDir() {
|
||||
|
@ -176,7 +173,7 @@ class PortableInfoboxRenderService extends WikiaService {
|
|||
for ( $i = 0; $i < count($data); $i++ ) {
|
||||
$data[$i][ 'context' ] = self::MEDIA_CONTEXT_INFOBOX;
|
||||
$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] ) {
|
||||
$images[] = $data[$i];
|
||||
|
@ -202,7 +199,7 @@ class PortableInfoboxRenderService extends WikiaService {
|
|||
}
|
||||
|
||||
if ( $helper->isWikiaMobile() ) {
|
||||
$data = $this->nodeSanitizer->sanitizeInfoboxFields( $type, $data );
|
||||
$data = SanitizerBuilder::createFromType( $type )->sanitize( $data );
|
||||
}
|
||||
|
||||
return $this->templateEngine->clearData()
|
||||
|
|
|
@ -2,33 +2,9 @@
|
|||
|
||||
use Wikia\Logger\WikiaLogger;
|
||||
|
||||
class NodeSanitizer {
|
||||
abstract class NodeSanitizer {
|
||||
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
|
||||
*
|
||||
|
|
11
services/Sanitizers/PassThroughSanitizer.php
Normal file
11
services/Sanitizers/PassThroughSanitizer.php
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
class PassThroughSanitizer extends NodeSanitizer implements NodeTypeSanitizerInterface {
|
||||
/**
|
||||
* @param $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function sanitize( $data ) {
|
||||
return $data;
|
||||
}
|
||||
}
|
27
services/Sanitizers/SanitizerBuilder.php
Normal file
27
services/Sanitizers/SanitizerBuilder.php
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,7 +6,7 @@ class NodeDataSanitizerTest extends WikiaBaseTest {
|
|||
protected function setUp() {
|
||||
$this->setupFile = dirname( __FILE__ ) . '/../../PortableInfobox.setup.php';
|
||||
|
||||
$this->sanitizer = new NodeDataSanitizer();
|
||||
$this->sanitizer = SanitizerBuilder::createFromType('data');
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ class NodeHeroImageSanitizerTest extends WikiaBaseTest {
|
|||
protected function setUp() {
|
||||
$this->setupFile = dirname( __FILE__ ) . '/../../PortableInfobox.setup.php';
|
||||
|
||||
$this->sanitizer = new NodeHeroImageSanitizer();
|
||||
$this->sanitizer = SanitizerBuilder::createFromType('hero-mobile');
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ class NodeHorizontalGroupSanitizerTest extends WikiaBaseTest {
|
|||
protected function setUp() {
|
||||
$this->setupFile = dirname( __FILE__ ) . '/../../PortableInfobox.setup.php';
|
||||
|
||||
$this->sanitizer = new NodeHorizontalGroupSanitizer();
|
||||
$this->sanitizer = SanitizerBuilder::createFromType('horizontal-group-content');
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ class NodeImageSanitizerTest extends WikiaBaseTest {
|
|||
protected function setUp() {
|
||||
$this->setupFile = dirname( __FILE__ ) . '/../../PortableInfobox.setup.php';
|
||||
|
||||
$this->sanitizer = new NodeImageSanitizer();
|
||||
$this->sanitizer = SanitizerBuilder::createFromType('image');
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ class NodeTitleSanitizerTest extends WikiaBaseTest {
|
|||
protected function setUp() {
|
||||
$this->setupFile = dirname( __FILE__ ) . '/../../PortableInfobox.setup.php';
|
||||
|
||||
$this->sanitizer = new NodeTitleSanitizer();
|
||||
$this->sanitizer = SanitizerBuilder::createFromType('title');
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
|
|
49
tests/sanitizers/PassThroughSanitizerTest.php
Normal file
49
tests/sanitizers/PassThroughSanitizerTest.php
Normal 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'] ,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue