[3.1.0] Split out VarParser from ConfigSchema
[htmlpurifier.git] / tests / HTMLPurifier / VarParserTest.php
blob84137e34003b21fef939aea3d646b22f8e627496
1 <?php
3 class HTMLPurifier_VarParserTest extends UnitTestCase
6 protected $parser;
8 public function setup() {
9 $this->parser = new HTMLPurifier_VarParser();
12 function assertValid($var, $type, $ret = null) {
13 $ret = ($ret === null) ? $var : $ret;
14 $this->assertIdentical($this->parser->parse($var, $type), $ret);
17 function assertInvalid($var, $type, $msg = null) {
18 $caught = false;
19 try {
20 $this->parser->parse($var, $type);
21 } catch (HTMLPurifier_VarParserException $e) {
22 $caught = true;
23 if ($msg !== null) $this->assertIdentical($e->getMessage(), $msg);
25 if (!$caught) {
26 $this->fail('Did not catch expected error');
30 function testValidate() {
32 $this->assertValid('foobar', 'string');
33 $this->assertValid('foobar', 'text'); // aliases, lstring = long string
34 $this->assertValid('FOOBAR', 'istring', 'foobar');
35 $this->assertValid('FOOBAR', 'itext', 'foobar');
37 $this->assertValid(34, 'int');
39 $this->assertValid(3.34, 'float');
41 $this->assertValid(false, 'bool');
42 $this->assertValid(0, 'bool', false);
43 $this->assertValid(1, 'bool', true);
44 $this->assertValid('true', 'bool', true);
45 $this->assertValid('false', 'bool', false);
46 $this->assertValid('1', 'bool', true);
47 $this->assertInvalid(34, 'bool');
48 $this->assertInvalid(null, 'bool');
50 $this->assertValid(array('1', '2', '3'), 'list');
51 $this->assertValid('foo,bar, cow', 'list', array('foo', 'bar', 'cow'));
52 $this->assertValid('', 'list', array());
53 $this->assertValid("foo\nbar", 'list', array('foo', 'bar'));
54 $this->assertValid("foo\nbar,baz", 'list', array('foo', 'bar', 'baz'));
56 $this->assertValid(array('1' => true, '2' => true), 'lookup');
57 $this->assertValid(array('1', '2'), 'lookup', array('1' => true, '2' => true));
58 $this->assertValid('foo,bar', 'lookup', array('foo' => true, 'bar' => true));
59 $this->assertValid("foo\nbar", 'lookup', array('foo' => true, 'bar' => true));
60 $this->assertValid("foo\nbar,baz", 'lookup', array('foo' => true, 'bar' => true, 'baz' => true));
61 $this->assertValid('', 'lookup', array());
62 $this->assertValid(array(), 'lookup');
64 $this->assertValid(array('foo' => 'bar'), 'hash');
65 $this->assertValid(array(1 => 'moo'), 'hash');
66 $this->assertInvalid(array(0 => 'moo'), 'hash');
67 $this->assertValid('', 'hash', array());
68 $this->assertValid('foo:bar,too:two', 'hash', array('foo' => 'bar', 'too' => 'two'));
69 $this->assertValid("foo:bar\ntoo:two,three:free", 'hash', array('foo' => 'bar', 'too' => 'two', 'three' => 'free'));
70 $this->assertValid('foo:bar,too', 'hash', array('foo' => 'bar'));
71 $this->assertValid('foo:bar,', 'hash', array('foo' => 'bar'));
72 $this->assertValid('foo:bar:baz', 'hash', array('foo' => 'bar:baz'));
74 $this->assertValid(23, 'mixed');
78 function testValidate_null() {
79 $this->assertIdentical($this->parser->parse(null, 'string', true), null);
80 $this->expectException('HTMLPurifier_VarParserException');
81 $this->parser->parse(null, 'string', false);