LanguageTest.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * @todo Fix usage of HTMLPurifier_Language->_loaded using something else
  4. */
  5. class HTMLPurifier_LanguageTest extends HTMLPurifier_Harness
  6. {
  7. protected $lang;
  8. protected function generateEnLanguage() {
  9. $factory = HTMLPurifier_LanguageFactory::instance();
  10. $config = HTMLPurifier_Config::create(array('Core.Language' => 'en'));
  11. $context = new HTMLPurifier_Context();
  12. return $factory->create($config, $context);
  13. }
  14. function test_getMessage() {
  15. $config = HTMLPurifier_Config::createDefault();
  16. $context = new HTMLPurifier_Context();
  17. $lang = new HTMLPurifier_Language($config, $context);
  18. $lang->_loaded = true;
  19. $lang->messages['HTMLPurifier'] = 'HTML Purifier';
  20. $this->assertIdentical($lang->getMessage('HTMLPurifier'), 'HTML Purifier');
  21. $this->assertIdentical($lang->getMessage('LanguageTest: Totally non-existent key'), '[LanguageTest: Totally non-existent key]');
  22. }
  23. function test_formatMessage() {
  24. $config = HTMLPurifier_Config::createDefault();
  25. $context = new HTMLPurifier_Context();
  26. $lang = new HTMLPurifier_Language($config, $context);
  27. $lang->_loaded = true;
  28. $lang->messages['LanguageTest: Error'] = 'Error is $1 on line $2';
  29. $this->assertIdentical($lang->formatMessage('LanguageTest: Error', array(1=>'fatal', 32)), 'Error is fatal on line 32');
  30. }
  31. function test_formatMessage_tokenParameter() {
  32. $config = HTMLPurifier_Config::createDefault();
  33. $context = new HTMLPurifier_Context();
  34. $generator = new HTMLPurifier_Generator($config, $context); // replace with mock if this gets icky
  35. $context->register('Generator', $generator);
  36. $lang = new HTMLPurifier_Language($config, $context);
  37. $lang->_loaded = true;
  38. $lang->messages['LanguageTest: Element info'] = 'Element Token: $1.Name, $1.Serialized, $1.Compact, $1.Line';
  39. $lang->messages['LanguageTest: Data info'] = 'Data Token: $1.Data, $1.Serialized, $1.Compact, $1.Line';
  40. $this->assertIdentical($lang->formatMessage('LanguageTest: Element info',
  41. array(1=>new HTMLPurifier_Token_Start('a', array('href'=>'http://example.com'), 18))),
  42. 'Element Token: a, <a href="http://example.com">, <a>, 18');
  43. $this->assertIdentical($lang->formatMessage('LanguageTest: Data info',
  44. array(1=>new HTMLPurifier_Token_Text('data>', 23))),
  45. 'Data Token: data>, data&gt;, data&gt;, 23');
  46. }
  47. function test_listify() {
  48. $lang = $this->generateEnLanguage();
  49. $this->assertEqual($lang->listify(array('Item')), 'Item');
  50. $this->assertEqual($lang->listify(array('Item', 'Item2')), 'Item and Item2');
  51. $this->assertEqual($lang->listify(array('Item', 'Item2', 'Item3')), 'Item, Item2 and Item3');
  52. }
  53. function test_formatMessage_arrayParameter() {
  54. $lang = $this->generateEnLanguage();
  55. $array = array('Item1', 'Item2', 'Item3');
  56. $this->assertIdentical(
  57. $lang->formatMessage('LanguageTest: List', array(1=>$array)),
  58. 'Item1, Item2 and Item3'
  59. );
  60. $array = array('Key1' => 'Value1', 'Key2' => 'Value2');
  61. $this->assertIdentical(
  62. $lang->formatMessage('LanguageTest: Hash', array(1=>$array)),
  63. 'Key1 and Key2; Value1 and Value2'
  64. );
  65. }
  66. }
  67. // vim: et sw=4 sts=4