4 * @todo Fix usage of HTMLPurifier_Language->_loaded using something else
6 class HTMLPurifier_LanguageTest
extends HTMLPurifier_Harness
11 protected function generateEnLanguage()
13 $factory = HTMLPurifier_LanguageFactory
::instance();
14 $config = HTMLPurifier_Config
::create(array('Core.Language' => 'en'));
15 $context = new HTMLPurifier_Context();
16 return $factory->create($config, $context);
19 public function test_getMessage()
21 $config = HTMLPurifier_Config
::createDefault();
22 $context = new HTMLPurifier_Context();
23 $lang = new HTMLPurifier_Language($config, $context);
24 $lang->_loaded
= true;
25 $lang->messages
['HTMLPurifier'] = 'HTML Purifier';
26 $this->assertIdentical($lang->getMessage('HTMLPurifier'), 'HTML Purifier');
27 $this->assertIdentical($lang->getMessage('LanguageTest: Totally non-existent key'), '[LanguageTest: Totally non-existent key]');
30 public function test_formatMessage()
32 $config = HTMLPurifier_Config
::createDefault();
33 $context = new HTMLPurifier_Context();
34 $lang = new HTMLPurifier_Language($config, $context);
35 $lang->_loaded
= true;
36 $lang->messages
['LanguageTest: Error'] = 'Error is $1 on line $2';
37 $this->assertIdentical($lang->formatMessage('LanguageTest: Error', array(1=>'fatal', 32)), 'Error is fatal on line 32');
40 public function test_formatMessage_tokenParameter()
42 $config = HTMLPurifier_Config
::createDefault();
43 $context = new HTMLPurifier_Context();
44 $generator = new HTMLPurifier_Generator($config, $context); // replace with mock if this gets icky
45 $context->register('Generator', $generator);
46 $lang = new HTMLPurifier_Language($config, $context);
47 $lang->_loaded
= true;
48 $lang->messages
['LanguageTest: Element info'] = 'Element Token: $1.Name, $1.Serialized, $1.Compact, $1.Line';
49 $lang->messages
['LanguageTest: Data info'] = 'Data Token: $1.Data, $1.Serialized, $1.Compact, $1.Line';
50 $this->assertIdentical($lang->formatMessage('LanguageTest: Element info',
51 array(1=>new HTMLPurifier_Token_Start('a', array('href'=>'http://example.com'), 18))),
52 'Element Token: a, <a href="http://example.com">, <a>, 18');
53 $this->assertIdentical($lang->formatMessage('LanguageTest: Data info',
54 array(1=>new HTMLPurifier_Token_Text('data>', 23))),
55 'Data Token: data>, data>, data>, 23');
58 public function test_listify()
60 $lang = $this->generateEnLanguage();
61 $this->assertEqual($lang->listify(array('Item')), 'Item');
62 $this->assertEqual($lang->listify(array('Item', 'Item2')), 'Item and Item2');
63 $this->assertEqual($lang->listify(array('Item', 'Item2', 'Item3')), 'Item, Item2 and Item3');
66 public function test_formatMessage_arrayParameter()
68 $lang = $this->generateEnLanguage();
70 $array = array('Item1', 'Item2', 'Item3');
71 $this->assertIdentical(
72 $lang->formatMessage('LanguageTest: List', array(1=>$array)),
73 'Item1, Item2 and Item3'
76 $array = array('Key1' => 'Value1', 'Key2' => 'Value2');
77 $this->assertIdentical(
78 $lang->formatMessage('LanguageTest: Hash', array(1=>$array)),
79 'Key1 and Key2; Value1 and Value2'