From 0ce0b500fec160921f20bf5c69a3081d83c7a211 Mon Sep 17 00:00:00 2001 From: Sebastian Marzjan Date: Mon, 28 Dec 2015 11:59:53 +0100 Subject: [PATCH] Use the Builder for Sanitizers. Cover the sanitizer build step in tests too --- PortableInfobox.setup.php | 2 + .../PortableInfoboxRenderService.class.php | 7 +-- services/Sanitizers/NodeSanitizer.php | 26 +--------- services/Sanitizers/PassThroughSanitizer.php | 11 +++++ services/Sanitizers/SanitizerBuilder.php | 27 ++++++++++ tests/sanitizers/NodeDataSanitizerTest.php | 2 +- .../sanitizers/NodeHeroImageSanitizerTest.php | 2 +- .../NodeHorizontalGroupSanitizerTest.php | 2 +- tests/sanitizers/NodeImageSanitizerTest.php | 2 +- tests/sanitizers/NodeTitleSanitizerTest.php | 2 +- tests/sanitizers/PassThroughSanitizerTest.php | 49 +++++++++++++++++++ 11 files changed, 97 insertions(+), 35 deletions(-) create mode 100644 services/Sanitizers/PassThroughSanitizer.php create mode 100644 services/Sanitizers/SanitizerBuilder.php create mode 100644 tests/sanitizers/PassThroughSanitizerTest.php diff --git a/PortableInfobox.setup.php b/PortableInfobox.setup.php index 4edd298..1dd0b98 100644 --- a/PortableInfobox.setup.php +++ b/PortableInfobox.setup.php @@ -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'; diff --git a/services/PortableInfoboxRenderService.class.php b/services/PortableInfoboxRenderService.class.php index f34d06b..9b27ed3 100644 --- a/services/PortableInfoboxRenderService.class.php +++ b/services/PortableInfoboxRenderService.class.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() diff --git a/services/Sanitizers/NodeSanitizer.php b/services/Sanitizers/NodeSanitizer.php index 3659a1e..13be636 100644 --- a/services/Sanitizers/NodeSanitizer.php +++ b/services/Sanitizers/NodeSanitizer.php @@ -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 * diff --git a/services/Sanitizers/PassThroughSanitizer.php b/services/Sanitizers/PassThroughSanitizer.php new file mode 100644 index 0000000..48cac8b --- /dev/null +++ b/services/Sanitizers/PassThroughSanitizer.php @@ -0,0 +1,11 @@ +setupFile = dirname( __FILE__ ) . '/../../PortableInfobox.setup.php'; - $this->sanitizer = new NodeDataSanitizer(); + $this->sanitizer = SanitizerBuilder::createFromType('data'); parent::setUp(); } diff --git a/tests/sanitizers/NodeHeroImageSanitizerTest.php b/tests/sanitizers/NodeHeroImageSanitizerTest.php index a9387d2..1d81175 100644 --- a/tests/sanitizers/NodeHeroImageSanitizerTest.php +++ b/tests/sanitizers/NodeHeroImageSanitizerTest.php @@ -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(); } diff --git a/tests/sanitizers/NodeHorizontalGroupSanitizerTest.php b/tests/sanitizers/NodeHorizontalGroupSanitizerTest.php index 1a975fe..fab3533 100644 --- a/tests/sanitizers/NodeHorizontalGroupSanitizerTest.php +++ b/tests/sanitizers/NodeHorizontalGroupSanitizerTest.php @@ -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(); } diff --git a/tests/sanitizers/NodeImageSanitizerTest.php b/tests/sanitizers/NodeImageSanitizerTest.php index c60ca16..a0a97b8 100644 --- a/tests/sanitizers/NodeImageSanitizerTest.php +++ b/tests/sanitizers/NodeImageSanitizerTest.php @@ -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(); } diff --git a/tests/sanitizers/NodeTitleSanitizerTest.php b/tests/sanitizers/NodeTitleSanitizerTest.php index dd7d215..12adae1 100644 --- a/tests/sanitizers/NodeTitleSanitizerTest.php +++ b/tests/sanitizers/NodeTitleSanitizerTest.php @@ -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(); } diff --git a/tests/sanitizers/PassThroughSanitizerTest.php b/tests/sanitizers/PassThroughSanitizerTest.php new file mode 100644 index 0000000..65b98d1 --- /dev/null +++ b/tests/sanitizers/PassThroughSanitizerTest.php @@ -0,0 +1,49 @@ +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 ' ], + ['value' => 'Test Title ' ], + ], + [ + ['value' => 'Test Title with link'], + ['value' => 'Test Title with link'], + ], + [ + ['value' => 'Real world DBGT Logotitle example'] , + ['value' => 'Real world DBGT Logotitle example'] , + ], + ]; + } +}