From 9727e298fc0645197ae6c28a9704434f6e801003 Mon Sep 17 00:00:00 2001 From: Jon Day Date: Thu, 28 Aug 2014 14:06:41 +0100 Subject: [PATCH 01/15] added allowedClasses to filter purifier --- src/Soflomo/Purifier/Filter/Purifier.php | 25 +++++++++++++++++-- src/Soflomo/Purifier/View/Helper/Purifier.php | 4 ++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Soflomo/Purifier/Filter/Purifier.php b/src/Soflomo/Purifier/Filter/Purifier.php index 3d52597..e0bcefe 100644 --- a/src/Soflomo/Purifier/Filter/Purifier.php +++ b/src/Soflomo/Purifier/Filter/Purifier.php @@ -4,11 +4,17 @@ use HTMLPurifier; use Zend\Filter\FilterInterface; +use Zend\Filter\AbstractFilter; -class Purifier implements FilterInterface +class Purifier extends AbstractFilter implements FilterInterface { protected $purifier; + /** + * @var array + */ + protected $allowedClasses; + public function __construct(HTMLPurifier $purifier) { $this->purifier = $purifier; @@ -16,9 +22,14 @@ public function __construct(HTMLPurifier $purifier) protected function getPurifier() { + if ($this->allowedClasses !== null) { + $config = $this->purifier->config; + exit(var_dump($config)); + } + return $this->purifier; } - + /** * {@inheritdocs} */ @@ -26,4 +37,14 @@ public function filter($value) { return $this->getPurifier()->purify($value); } + + /** + * Array of values to be provided to HTMLPurifier_Config Attr.AllowedClasses + * + * @param array $allowedClasses + */ + public function setAllowedClasses($allowedClasses) + { + $this->allowedClasses = $allowedClasses; + } } \ No newline at end of file diff --git a/src/Soflomo/Purifier/View/Helper/Purifier.php b/src/Soflomo/Purifier/View/Helper/Purifier.php index d3cc5d2..3894294 100644 --- a/src/Soflomo/Purifier/View/Helper/Purifier.php +++ b/src/Soflomo/Purifier/View/Helper/Purifier.php @@ -68,6 +68,8 @@ public function __invoke($html = null) public function purify($html) { - return $this->getPurifier()->purify($html); + + return $this->getPurifier()->purify($html); + } } \ No newline at end of file From 341e6afae6362df4bf2b36cf3e05f979f654375f Mon Sep 17 00:00:00 2001 From: Jon Day Date: Thu, 28 Aug 2014 15:02:58 +0100 Subject: [PATCH 02/15] Moving purify filter over to factory --- Module.php | 4 +--- .../Purifier/Factory/PurifierFilterFactory.php | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 src/Soflomo/Purifier/Factory/PurifierFilterFactory.php diff --git a/Module.php b/Module.php index 01866da..53079c5 100644 --- a/Module.php +++ b/Module.php @@ -79,9 +79,7 @@ public function getFilterConfig() { return array( 'factories' => array( - 'htmlpurifier' => function($sl) { - $purifier = $sl->getServiceLocator()->get('HTMLPurifier'); - return new Filter\Purifier($purifier); + 'htmlpurifier' => 'Soflomo\Purifier\FactoryPurifierFilterFactory'; }, ), ); diff --git a/src/Soflomo/Purifier/Factory/PurifierFilterFactory.php b/src/Soflomo/Purifier/Factory/PurifierFilterFactory.php new file mode 100644 index 0000000..7de0726 --- /dev/null +++ b/src/Soflomo/Purifier/Factory/PurifierFilterFactory.php @@ -0,0 +1,17 @@ +getServiceLocator()->get('HTMLPurifier'); + + return new Filter\Purifier($purifier); + } +} \ No newline at end of file From ff01b2e72e7d9757741c8ce8742e4cd084fb8e4c Mon Sep 17 00:00:00 2001 From: Jon Day Date: Thu, 28 Aug 2014 15:05:44 +0100 Subject: [PATCH 03/15] Fix issue with incorrect closing of array in module.php --- Module.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Module.php b/Module.php index 53079c5..80e9a90 100644 --- a/Module.php +++ b/Module.php @@ -79,8 +79,7 @@ public function getFilterConfig() { return array( 'factories' => array( - 'htmlpurifier' => 'Soflomo\Purifier\FactoryPurifierFilterFactory'; - }, + 'htmlpurifier' => 'Soflomo\Purifier\FactoryPurifierFilterFactory' ), ); } From 6e5ef7a7c3e7bbfee00e44582daf448af674f3cb Mon Sep 17 00:00:00 2001 From: Jon Day Date: Thu, 28 Aug 2014 15:07:02 +0100 Subject: [PATCH 04/15] Fixed issue with namespace in Module.php for new filter factory --- Module.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Module.php b/Module.php index 80e9a90..2f42500 100644 --- a/Module.php +++ b/Module.php @@ -79,7 +79,7 @@ public function getFilterConfig() { return array( 'factories' => array( - 'htmlpurifier' => 'Soflomo\Purifier\FactoryPurifierFilterFactory' + 'htmlpurifier' => 'Soflomo\Purifier\Factory\PurifierFilterFactory' ), ); } From 2c9de2ab2a3be6353c49884fb1c33814cab681fd Mon Sep 17 00:00:00 2001 From: Jon Day Date: Thu, 28 Aug 2014 15:15:39 +0100 Subject: [PATCH 05/15] Moved factory over to FactoryInterface and created create service method. $options should now be available! --- .../Purifier/Factory/PurifierFilterFactory.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Soflomo/Purifier/Factory/PurifierFilterFactory.php b/src/Soflomo/Purifier/Factory/PurifierFilterFactory.php index 7de0726..7002c98 100644 --- a/src/Soflomo/Purifier/Factory/PurifierFilterFactory.php +++ b/src/Soflomo/Purifier/Factory/PurifierFilterFactory.php @@ -1,15 +1,18 @@ getServiceLocator()->get('HTMLPurifier'); return new Filter\Purifier($purifier); From b5eca56fc934d3ab71ae40f2c4037ac2edecc31f Mon Sep 17 00:00:00 2001 From: Jon Day Date: Thu, 28 Aug 2014 15:19:51 +0100 Subject: [PATCH 06/15] Implemeted setting options for Purify Filter in factory --- .../Factory/PurifierFilterFactory.php | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Soflomo/Purifier/Factory/PurifierFilterFactory.php b/src/Soflomo/Purifier/Factory/PurifierFilterFactory.php index 7002c98..5298fec 100644 --- a/src/Soflomo/Purifier/Factory/PurifierFilterFactory.php +++ b/src/Soflomo/Purifier/Factory/PurifierFilterFactory.php @@ -3,18 +3,35 @@ use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; +use Soflomo\Purifier\Filter\Purifier; class PurifierFilterFactory implements FactoryInterface { + /** + * array of options for htmlpurify + * + * @var array + */ + protected $options; + + /** + * @param mixed $options + */ public function __construct($options) { - exit(var_dump($options)); + $this->options = $options; } + /** + * (non-PHPdoc) + * @see \Zend\ServiceManager\FactoryInterface::createService() + */ public function createService(ServiceLocatorInterface $serviceLocator) { $purifier = $serviceLocator->getServiceLocator()->get('HTMLPurifier'); - return new Filter\Purifier($purifier); + $filter = new Purifier($purifier); + + return $filter->setOptions($this->options); } } \ No newline at end of file From 7ec5d7764ca2c37347b0d8d3e657c4358d838ec3 Mon Sep 17 00:00:00 2001 From: Jon Day Date: Thu, 28 Aug 2014 15:27:27 +0100 Subject: [PATCH 07/15] Adding the whitelist to html purify --- src/Soflomo/Purifier/Filter/Purifier.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Soflomo/Purifier/Filter/Purifier.php b/src/Soflomo/Purifier/Filter/Purifier.php index e0bcefe..2233d2d 100644 --- a/src/Soflomo/Purifier/Filter/Purifier.php +++ b/src/Soflomo/Purifier/Filter/Purifier.php @@ -24,7 +24,9 @@ protected function getPurifier() { if ($this->allowedClasses !== null) { $config = $this->purifier->config; - exit(var_dump($config)); + $config->set('AllowedElements', $this->allowedClasses); + + //$this->purifier->config = $config; } return $this->purifier; From 336ef7439df6e8fd228bbe03494e24c05560618d Mon Sep 17 00:00:00 2001 From: Jon Day Date: Thu, 28 Aug 2014 15:31:43 +0100 Subject: [PATCH 08/15] Updated to HTML.AllowedElements in purifier config --- src/Soflomo/Purifier/Filter/Purifier.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Soflomo/Purifier/Filter/Purifier.php b/src/Soflomo/Purifier/Filter/Purifier.php index 2233d2d..e9f9d92 100644 --- a/src/Soflomo/Purifier/Filter/Purifier.php +++ b/src/Soflomo/Purifier/Filter/Purifier.php @@ -24,7 +24,7 @@ protected function getPurifier() { if ($this->allowedClasses !== null) { $config = $this->purifier->config; - $config->set('AllowedElements', $this->allowedClasses); + $config->set('HTML.AllowedElements', $this->allowedClasses); //$this->purifier->config = $config; } From 53faf398ba32967f3c013c4fc9711513281d24ed Mon Sep 17 00:00:00 2001 From: Jon Day Date: Thu, 28 Aug 2014 15:37:15 +0100 Subject: [PATCH 09/15] Updated purify factory to not provide config --- src/Soflomo/Purifier/Factory/HtmlPurifierFactory.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Soflomo/Purifier/Factory/HtmlPurifierFactory.php b/src/Soflomo/Purifier/Factory/HtmlPurifierFactory.php index 180beab..f4283b5 100644 --- a/src/Soflomo/Purifier/Factory/HtmlPurifierFactory.php +++ b/src/Soflomo/Purifier/Factory/HtmlPurifierFactory.php @@ -60,12 +60,14 @@ public function createService(ServiceLocatorInterface $sl) include $config['soflomo_purifier']['standalone_path']; } - $config = HTMLPurifier_Config::createDefault(); + //$config = HTMLPurifier_Config::createDefault(); foreach ($options as $key => $value) { $config->set($key, $value); } - $purifier = new HTMLPurifier($config); + //$purifier = new HTMLPurifier($config); + $purifier = new HTMLPurifier(); + return $purifier; } } From bd0d233898c3da46840bac7cdfa38293a162eb18 Mon Sep 17 00:00:00 2001 From: Jon Day Date: Thu, 28 Aug 2014 15:39:18 +0100 Subject: [PATCH 10/15] Updating the purify filter to use its own config --- src/Soflomo/Purifier/Filter/Purifier.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Soflomo/Purifier/Filter/Purifier.php b/src/Soflomo/Purifier/Filter/Purifier.php index e9f9d92..11e4a56 100644 --- a/src/Soflomo/Purifier/Filter/Purifier.php +++ b/src/Soflomo/Purifier/Filter/Purifier.php @@ -23,10 +23,10 @@ public function __construct(HTMLPurifier $purifier) protected function getPurifier() { if ($this->allowedClasses !== null) { - $config = $this->purifier->config; + $config = \HTMLPurifier_Config::createDefault(); $config->set('HTML.AllowedElements', $this->allowedClasses); - //$this->purifier->config = $config; + $this->purifier->config = $config; } return $this->purifier; From 53b7667dbc5f80fd7b72ab127a3a8f04e45afe64 Mon Sep 17 00:00:00 2001 From: Jon Day Date: Thu, 28 Aug 2014 16:11:59 +0100 Subject: [PATCH 11/15] Adding a default whitelist to test default is inited --- config/module.config.php | 1 + .../Purifier/Factory/HtmlPurifierFactory.php | 7 ++----- src/Soflomo/Purifier/Filter/Purifier.php | 14 ++++++++------ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/config/module.config.php b/config/module.config.php index 7893c10..232f4be 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -45,6 +45,7 @@ 'standalone' => false, 'standalone_path' => 'vendor/ezyang/htmlpurifier/library/HTMLPurifier.standalone.php', 'config' => array( + 'HTML.AllowedElements' => 'p, h1, h2, h3, h4, h5, h6, strong, i, b, ul, ol, li, dl, dd, dt, em, cite, small, a, span. blockquote' ) ), ); \ No newline at end of file diff --git a/src/Soflomo/Purifier/Factory/HtmlPurifierFactory.php b/src/Soflomo/Purifier/Factory/HtmlPurifierFactory.php index f4283b5..a7a3326 100644 --- a/src/Soflomo/Purifier/Factory/HtmlPurifierFactory.php +++ b/src/Soflomo/Purifier/Factory/HtmlPurifierFactory.php @@ -60,14 +60,11 @@ public function createService(ServiceLocatorInterface $sl) include $config['soflomo_purifier']['standalone_path']; } - //$config = HTMLPurifier_Config::createDefault(); + $config = HTMLPurifier_Config::createDefault(); foreach ($options as $key => $value) { $config->set($key, $value); } - //$purifier = new HTMLPurifier($config); - $purifier = new HTMLPurifier(); - - return $purifier; + return new HTMLPurifier($config); } } diff --git a/src/Soflomo/Purifier/Filter/Purifier.php b/src/Soflomo/Purifier/Filter/Purifier.php index 11e4a56..aac0ad1 100644 --- a/src/Soflomo/Purifier/Filter/Purifier.php +++ b/src/Soflomo/Purifier/Filter/Purifier.php @@ -11,9 +11,11 @@ class Purifier extends AbstractFilter implements FilterInterface protected $purifier; /** - * @var array + * Comma seperated values as string + * + * @var string */ - protected $allowedClasses; + protected $allowedElements; public function __construct(HTMLPurifier $purifier) { @@ -22,9 +24,9 @@ public function __construct(HTMLPurifier $purifier) protected function getPurifier() { - if ($this->allowedClasses !== null) { + if ($this->allowedElements !== null) { $config = \HTMLPurifier_Config::createDefault(); - $config->set('HTML.AllowedElements', $this->allowedClasses); + $config->set('HTML.AllowedElements', $this->allowedElements); $this->purifier->config = $config; } @@ -45,8 +47,8 @@ public function filter($value) * * @param array $allowedClasses */ - public function setAllowedClasses($allowedClasses) + public function setAllowedElements($allowedElements) { - $this->allowedClasses = $allowedClasses; + $this->allowedElements = $allowedElements; } } \ No newline at end of file From 9e961d6fdcde23a9b2bd8f832be1a7e8875e816b Mon Sep 17 00:00:00 2001 From: Jon Day Date: Thu, 28 Aug 2014 16:34:39 +0100 Subject: [PATCH 12/15] Added doc blocks to purify filter and removed test config --- config/module.config.php | 1 - .../Factory/PurifierFilterFactory.php | 28 +++++++++++-------- src/Soflomo/Purifier/Filter/Purifier.php | 8 ++++++ 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/config/module.config.php b/config/module.config.php index 232f4be..7893c10 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -45,7 +45,6 @@ 'standalone' => false, 'standalone_path' => 'vendor/ezyang/htmlpurifier/library/HTMLPurifier.standalone.php', 'config' => array( - 'HTML.AllowedElements' => 'p, h1, h2, h3, h4, h5, h6, strong, i, b, ul, ol, li, dl, dd, dt, em, cite, small, a, span. blockquote' ) ), ); \ No newline at end of file diff --git a/src/Soflomo/Purifier/Factory/PurifierFilterFactory.php b/src/Soflomo/Purifier/Factory/PurifierFilterFactory.php index 5298fec..e59b340 100644 --- a/src/Soflomo/Purifier/Factory/PurifierFilterFactory.php +++ b/src/Soflomo/Purifier/Factory/PurifierFilterFactory.php @@ -13,25 +13,29 @@ class PurifierFilterFactory implements FactoryInterface * @var array */ protected $options; - + /** * @param mixed $options */ - public function __construct($options) + public function __construct($options = null) { $this->options = $options; } - + /** * (non-PHPdoc) * @see \Zend\ServiceManager\FactoryInterface::createService() */ - public function createService(ServiceLocatorInterface $serviceLocator) - { - $purifier = $serviceLocator->getServiceLocator()->get('HTMLPurifier'); - - $filter = new Purifier($purifier); - - return $filter->setOptions($this->options); - } -} \ No newline at end of file + public function createService(ServiceLocatorInterface $serviceLocator) + { + $purifier = $serviceLocator->getServiceLocator()->get('HTMLPurifier'); + + $filter = new Purifier($purifier); + + if ($this->options === null || empty($this->options)) { + return $filter; + } else { + return $filter->setOptions($this->options); + } + } +} diff --git a/src/Soflomo/Purifier/Filter/Purifier.php b/src/Soflomo/Purifier/Filter/Purifier.php index aac0ad1..cc88ad6 100644 --- a/src/Soflomo/Purifier/Filter/Purifier.php +++ b/src/Soflomo/Purifier/Filter/Purifier.php @@ -17,11 +17,19 @@ class Purifier extends AbstractFilter implements FilterInterface */ protected $allowedElements; + /** + * @param HTMLPurifier $purifier + */ public function __construct(HTMLPurifier $purifier) { $this->purifier = $purifier; } + /** + * Returns the purifier with config allowed elements added if specified + * + * @return HTMLPurifier + */ protected function getPurifier() { if ($this->allowedElements !== null) { From d7a2ffd077a5b5545c8cda64b342af4d98a293a7 Mon Sep 17 00:00:00 2001 From: Jon Day Date: Fri, 29 Aug 2014 09:55:52 +0100 Subject: [PATCH 13/15] Updated the filter to add the config to purify --- src/Soflomo/Purifier/Filter/Purifier.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Soflomo/Purifier/Filter/Purifier.php b/src/Soflomo/Purifier/Filter/Purifier.php index cc88ad6..6212169 100644 --- a/src/Soflomo/Purifier/Filter/Purifier.php +++ b/src/Soflomo/Purifier/Filter/Purifier.php @@ -32,13 +32,6 @@ public function __construct(HTMLPurifier $purifier) */ protected function getPurifier() { - if ($this->allowedElements !== null) { - $config = \HTMLPurifier_Config::createDefault(); - $config->set('HTML.AllowedElements', $this->allowedElements); - - $this->purifier->config = $config; - } - return $this->purifier; } @@ -47,7 +40,17 @@ protected function getPurifier() */ public function filter($value) { - return $this->getPurifier()->purify($value); + $purifier = $this->getPurifier(); + + if ($this->allowedElements !== null) { + //$config = \HTMLPurifier_Config::createDefault(); + $config = clone $purifier->config; + $config->set('HTML.AllowedElements', $this->allowedElements); + + return $purifier->purify($value, $config); + } + + return $purifier->purify($value); } /** From f78004122046d32bb317563454af9dbc130757ef Mon Sep 17 00:00:00 2001 From: Jon Day Date: Fri, 29 Aug 2014 10:37:01 +0100 Subject: [PATCH 14/15] Removed dead code from Purifier.php --- src/Soflomo/Purifier/Filter/Purifier.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Soflomo/Purifier/Filter/Purifier.php b/src/Soflomo/Purifier/Filter/Purifier.php index 6212169..72e81f4 100644 --- a/src/Soflomo/Purifier/Filter/Purifier.php +++ b/src/Soflomo/Purifier/Filter/Purifier.php @@ -43,7 +43,6 @@ public function filter($value) $purifier = $this->getPurifier(); if ($this->allowedElements !== null) { - //$config = \HTMLPurifier_Config::createDefault(); $config = clone $purifier->config; $config->set('HTML.AllowedElements', $this->allowedElements); From cebd6b23cb943c4128373066cd8c2df8d1c4afac Mon Sep 17 00:00:00 2001 From: Jon Day Date: Fri, 29 Aug 2014 11:57:51 +0100 Subject: [PATCH 15/15] Added some unit tests for purifier filter --- composer.json | 4 + src/Soflomo/Purifier/Filter/Purifier.php | 4 +- .../Purifier/Test/Form/PurifierTestForm.php | 54 +++++++++ src/Soflomo/Purifier/View/Helper/Purifier.php | 24 +++- test/Bootstrap.php | 108 ++++++++++++++++++ test/PurifierTest/Filter/PurifierTest.php | 81 +++++++++++++ test/config/TestConfig.php.dist | 16 +++ test/phpunit.xml.dist | 15 +++ 8 files changed, 302 insertions(+), 4 deletions(-) create mode 100644 src/Soflomo/Purifier/Test/Form/PurifierTestForm.php create mode 100644 test/Bootstrap.php create mode 100644 test/PurifierTest/Filter/PurifierTest.php create mode 100644 test/config/TestConfig.php.dist create mode 100644 test/phpunit.xml.dist diff --git a/composer.json b/composer.json index c96a6d4..9cdcc93 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,10 @@ "zendframework/zendframework": "2.*", "ezyang/htmlpurifier": ">=4.5.0" }, + "require-dev": + { + "phpunit/phpunit": "~3.7" + }, "autoload": { "psr-0": { "Soflomo\\Purifier": "src/" diff --git a/src/Soflomo/Purifier/Filter/Purifier.php b/src/Soflomo/Purifier/Filter/Purifier.php index 72e81f4..a23261e 100644 --- a/src/Soflomo/Purifier/Filter/Purifier.php +++ b/src/Soflomo/Purifier/Filter/Purifier.php @@ -43,7 +43,9 @@ public function filter($value) $purifier = $this->getPurifier(); if ($this->allowedElements !== null) { - $config = clone $purifier->config; + $config = \HTMLPurifier_Config::createDefault(); + + $config = $config->inherit($purifier->config); $config->set('HTML.AllowedElements', $this->allowedElements); return $purifier->purify($value, $config); diff --git a/src/Soflomo/Purifier/Test/Form/PurifierTestForm.php b/src/Soflomo/Purifier/Test/Form/PurifierTestForm.php new file mode 100644 index 0000000..71164b9 --- /dev/null +++ b/src/Soflomo/Purifier/Test/Form/PurifierTestForm.php @@ -0,0 +1,54 @@ +name = 'test'; + parent::__construct($this->name); + } + + /** + * Provide default input rules for this element + * + * Attaches strip tags filter + * + * @return array + */ + public function getInputFilterSpecification() + { + return [ + 'test' => [ + 'required' => false, + 'filters' => [ + array('name' => 'htmlpurifier', + 'options' => [ + 'allowed_elements' => 'p' + ] + ), + ], + ] + ]; + } + + + + public function init() + { + $this->add(array( + 'name' => 'test', + 'type' => 'Textarea', + + )); + } +} \ No newline at end of file diff --git a/src/Soflomo/Purifier/View/Helper/Purifier.php b/src/Soflomo/Purifier/View/Helper/Purifier.php index 3894294..0b277ff 100644 --- a/src/Soflomo/Purifier/View/Helper/Purifier.php +++ b/src/Soflomo/Purifier/View/Helper/Purifier.php @@ -45,18 +45,33 @@ class Purifier extends AbstractHelper { + /** + * @var HTMLPurifier + */ protected $purifier; + /** + * @param HTMLPurifier $purifier + */ public function __construct(HTMLPurifier $purifier) { $this->purifier = $purifier; } + /** + * Getter for purifier + * + * @return HTMLPurifier + */ protected function getPurifier() { return $this->purifier; } + /** + * @param string $html + * @return \Soflomo\Purifier\View\Helper\Purifier|Ambigous + */ public function __invoke($html = null) { if (null === $html) { @@ -66,10 +81,13 @@ public function __invoke($html = null) return $this->purify($html); } + /** + * Purifies the html string + * + * @param string $html + */ public function purify($html) { - - return $this->getPurifier()->purify($html); - + return $this->getPurifier()->purify($html); } } \ No newline at end of file diff --git a/test/Bootstrap.php b/test/Bootstrap.php new file mode 100644 index 0000000..4ec3789 --- /dev/null +++ b/test/Bootstrap.php @@ -0,0 +1,108 @@ + array( + 'module_paths' => explode(PATH_SEPARATOR, $zf2ModulePaths), + ), + ); + + $config = ArrayUtils::merge($baseConfig, $testConfig); + //exit(var_dump($config)); + $serviceManager = new ServiceManager(new ServiceManagerConfig()); + $serviceManager->setService('ApplicationConfig', $config); + $serviceManager->get('ModuleManager')->loadModules(); + + static::$serviceManager = $serviceManager; + static::$config = $config; + } + + public static function getServiceManager() + { + return static::$serviceManager; + } + + public static function getConfig() + { + return static::$config; + } + + protected static function initAutoloader() + { + $vendorPath = static::findParentPath('vendor'); + + if (is_readable($vendorPath . '/autoload.php')) { + $loader = include $vendorPath . '/autoload.php'; + } else { + $zf2Path = getenv('ZF2_PATH') ?: (defined('ZF2_PATH') ? ZF2_PATH : (is_dir($vendorPath . '/ZF2/library') ? $vendorPath . '/ZF2/library' : false)); + + if (!$zf2Path) { + throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.'); + } + + include $zf2Path . '/Zend/Loader/AutoloaderFactory.php'; + + } + + } + + protected static function findParentPath($path) + { + $dir = __DIR__; + $previousDir = '.'; + while (!is_dir($dir . '/' . $path)) { + $dir = dirname($dir); + if ($previousDir === $dir) return false; + $previousDir = $dir; + } + return $dir . '/' . $path; + } + +} + +Bootstrap::init(); \ No newline at end of file diff --git a/test/PurifierTest/Filter/PurifierTest.php b/test/PurifierTest/Filter/PurifierTest.php new file mode 100644 index 0000000..0efd9a7 --- /dev/null +++ b/test/PurifierTest/Filter/PurifierTest.php @@ -0,0 +1,81 @@ +Paragraph

Whitehat Header

Blackhat Header

'; + + /** + * Setup the filter and services as close as posisble to + * during an application request + */ + public function setUp() + { + parent::setUp(); + $init = \Zend\Mvc\Application::init(\PurifierTest\Bootstrap::getConfig()); + + $serviceManager = \PurifierTest\Bootstrap::getServiceManager(); + $filterChain = new \Zend\Filter\FilterChain(); + $this->filterManager = $filterChain->getPluginManager(); + $filterManager = $filterChain->getPluginManager(); + $this->filterManager->setServiceLocator($serviceManager); + + $filter = $serviceManager->get('htmlpurifier'); + + $this->filterManager->setFactory('htmlpurifier', 'Soflomo\Purifier\Factory\PurifierFilterFactory'); + $this->filter = $this->filterManager->get('htmlpurifier'); + + $this->formManager = $serviceManager->get('FormElementManager'); + } + + public function testCanAcceptAllowedElements() + { + $this->filter->setOptions(['allowed_elements' => 'p']); + $result = $this->filter->filter($this->htmlTestString); + $this->assertEquals("

Paragraph

Whitehat HeaderBlackhat Header", $result); + + $result = $this->filter->filter($this->htmlTestString); + } + + public function testConfigAndFilterState() + { + $this->filter->setOptions(['allowed_elements' => 'p']); + $result = $this->filter->filter($this->htmlTestString); + $this->assertEquals("

Paragraph

Whitehat HeaderBlackhat Header", $result); + + $this->filterTwo = $this->filterManager->get('htmlpurifier'); + + $result = $this->filterTwo->filter($this->htmlTestString); + $this->assertEquals("

Paragraph

Whitehat Header

Blackhat Header

", $result); + } + + public function testFormManagerInit() + { + $this->formManager->setInvokableClass('TestForm', '\Soflomo\Purifier\Test\Form\PurifierTestForm'); + + $form = $this->formManager->get('TestForm'); + + $form->setData(['test' => $this->htmlTestString]); + $form->isValid(); + + $this->assertEquals("

Paragraph

Whitehat HeaderBlackhat Header", $form->getData()['test']); + } +} \ No newline at end of file diff --git a/test/config/TestConfig.php.dist b/test/config/TestConfig.php.dist new file mode 100644 index 0000000..8a475fd --- /dev/null +++ b/test/config/TestConfig.php.dist @@ -0,0 +1,16 @@ + array( + 'Soflomo\Purifier' + ), + 'module_listener_options' => array( + 'config_glob_paths' => array( + __DIR__.'/{,*.}{global,local}.php', + __DIR__.'/../../config/{*}.php', + ), + 'module_paths' => array( + 'module', + 'vendor', + ), + ), +); \ No newline at end of file diff --git a/test/phpunit.xml.dist b/test/phpunit.xml.dist new file mode 100644 index 0000000..7e119f7 --- /dev/null +++ b/test/phpunit.xml.dist @@ -0,0 +1,15 @@ + + + + + + ./PurifierTest + + + + + + ../src/Purifier + + + \ No newline at end of file