Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 75d2a51

Browse files
author
Florian Krämer
committed
Adding a test for the behavior
1 parent 5d5f3cc commit 75d2a51

File tree

3 files changed

+117
-112
lines changed

3 files changed

+117
-112
lines changed

Test/Case/Model/Behavior/HtmlPurifierBehaviorTest.php

Lines changed: 0 additions & 111 deletions
This file was deleted.

src/Model/Behavior/HtmlPurifierBehavior.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @copyright 2012 - 2015 Florian Krämer
77
* @license MIT
88
*/
9-
namespace App\Model\Behavior;
9+
namespace Burzum\HtmlPurifier\Model\Behavior;
1010

1111
use ArrayObject;
1212
use Burzum\HtmlPurifier\Lib\PurifierTrait;
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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

Comments
 (0)