You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.4KB

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