|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Upload Validator Behavior Test |
| 4 | + * |
| 5 | + * @author Florian Krämer |
| 6 | + * @copyright 2012 - 2015 Florian Krämer |
| 7 | + * @license MIT |
| 8 | + */ |
| 9 | +namespace Burzum\HtmlPurifier\Test\TestCase\Model\Behavior; |
| 10 | + |
| 11 | +use Burzum\HtmlPurifier\Lib\Purifier; |
| 12 | +use Cake\Event\Event; |
| 13 | +use Cake\ORM\TableRegistry; |
| 14 | +use Cake\ORM\Table; |
| 15 | +use Cake\Core\Plugin; |
| 16 | +use Cake\TestSuite\TestCase; |
| 17 | + |
| 18 | +/** |
| 19 | + * VoidUploadModel |
| 20 | + */ |
| 21 | +class VoidModel extends Table { |
| 22 | + |
| 23 | + /** |
| 24 | + * name property |
| 25 | + * |
| 26 | + * @var string 'TheVoid' |
| 27 | + */ |
| 28 | + public $name = 'VoidModel'; |
| 29 | + |
| 30 | + /** |
| 31 | + * useTable property |
| 32 | + * |
| 33 | + * @var bool false |
| 34 | + */ |
| 35 | + public $useTable = false; |
| 36 | + |
| 37 | + /** |
| 38 | + * Initialize |
| 39 | + * |
| 40 | + * @param array $config |
| 41 | + * @return void |
| 42 | + */ |
| 43 | + public function initialize(array $config) { |
| 44 | + parent::initialize($config); |
| 45 | + $this->addBehavior('Burzum/HtmlPurifier.HtmlPurifier', [ |
| 46 | + 'fields' => ['field1'] |
| 47 | + ]); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * HtmlPurifierBehaviorTest |
| 53 | + */ |
| 54 | +class HtmlPurifierBehaviorTest extends TestCase { |
| 55 | + |
| 56 | +/** |
| 57 | + * Holds the instance of the table |
| 58 | + * |
| 59 | + * @var mixed |
| 60 | + */ |
| 61 | + public $Article = null; |
| 62 | + |
| 63 | +/** |
| 64 | + * Fixtures |
| 65 | + * |
| 66 | + * @var array |
| 67 | + */ |
| 68 | + public $fixtures = []; |
| 69 | + |
| 70 | +/** |
| 71 | + * startTest |
| 72 | + * |
| 73 | + * @return void |
| 74 | + */ |
| 75 | + public function setUp() { |
| 76 | + parent::setUp(); |
| 77 | + |
| 78 | + Purifier::config('default', [ |
| 79 | + 'HTML.AllowedElements' => 'a, em, blockquote, p, strong, pre, code, span,ul,ol,li,img', |
| 80 | + 'HTML.AllowedAttributes' => 'a.href, a.title, img.src, img.alt, *.style', |
| 81 | + 'CSS.AllowedProperties' => 'text-decoration', |
| 82 | + 'HTML.TidyLevel' => 'heavy', |
| 83 | + 'HTML.Doctype' => 'XHTML 1.0 Transitional' |
| 84 | + ]); |
| 85 | + |
| 86 | + $this->table = new VoidModel(); |
| 87 | + } |
| 88 | + |
| 89 | +/** |
| 90 | + * endTest |
| 91 | + * |
| 92 | + * @return void |
| 93 | + */ |
| 94 | + public function tearDown() { |
| 95 | + unset($this->table); |
| 96 | + } |
| 97 | + |
| 98 | +/** |
| 99 | + * configureUploadValidation |
| 100 | + * |
| 101 | + * @return void |
| 102 | + */ |
| 103 | + public function testBeforeMarshal() { |
| 104 | + $html = '<p style="font-weight: bold;"><script>alert("alert!");</script><span style="text-decoration: line-through;" _mce_style="text-decoration: line-through;">shsfhshs</span></p><p><strong>sdhsdhds</strong></p><p><em>shsdh</em><span style="text-decoration: underline;" _mce_style="text-decoration: underline;">dsh</span></p><ul><li>sdgsgssgd</li><li>sdgdsg</li><li>sdgsdgsg</li><li>sdgdg<br></li></ul>'; |
| 105 | + $expected = '<p><span style="text-decoration:line-through;">shsfhshs</span></p><p><strong>sdhsdhds</strong></p><p><em>shsdh</em><span style="text-decoration:underline;">dsh</span></p><ul><li>sdgsgssgd</li><li>sdgdsg</li><li>sdgsdgsg</li><li>sdgdg</li></ul>'; |
| 106 | + $event = new Event('Model.beforeMarshal'); |
| 107 | + $data = new \ArrayObject([ |
| 108 | + 'field1' => $html, |
| 109 | + 'field2' => '<b>Don\'t change me!</b>' |
| 110 | + ]); |
| 111 | + $options = new \ArrayObject(); |
| 112 | + $this->table->behaviors()->HtmlPurifier->beforeMarshal($event, $data, $options); |
| 113 | + $this->assertEquals($data['field1'], $expected); |
| 114 | + $this->assertEquals($data['field2'], '<b>Don\'t change me!</b>'); |
| 115 | + } |
| 116 | +} |
0 commit comments