LangTest.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. class HTMLPurifier_AttrDef_LangTest extends HTMLPurifier_AttrDefHarness
  3. {
  4. function test() {
  5. $this->def = new HTMLPurifier_AttrDef_Lang();
  6. // basic good uses
  7. $this->assertDef('en');
  8. $this->assertDef('en-us');
  9. $this->assertDef(' en ', 'en'); // trim
  10. $this->assertDef('EN', 'en'); // case insensitivity
  11. // (thanks Eugen Pankratz for noticing the typos!)
  12. $this->assertDef('En-Us-Edison', 'en-us-edison'); // complex ci
  13. $this->assertDef('fr en', false); // multiple languages
  14. $this->assertDef('%', false); // bad character
  15. // test overlong language according to syntax
  16. $this->assertDef('thisistoolongsoitgetscut', false);
  17. // primary subtag rules
  18. // I'm somewhat hesitant to allow x and i as primary language codes,
  19. // because they usually are never used in real life. However,
  20. // theoretically speaking, having them alone is permissable, so
  21. // I'll be lenient. No XML parser is going to complain anyway.
  22. $this->assertDef('x');
  23. $this->assertDef('i');
  24. // real world use-cases
  25. $this->assertDef('x-klingon');
  26. $this->assertDef('i-mingo');
  27. // because the RFC only defines two and three letter primary codes,
  28. // anything with a length of four or greater is invalid, despite
  29. // the syntax stipulation of 1 to 8 characters. Because the RFC
  30. // specifically states that this reservation is in order to allow
  31. // for future versions to expand, the adoption of a new RFC will
  32. // require these test cases to be rewritten, even if backwards-
  33. // compatibility is largely retained (i.e. this is not forwards
  34. // compatible)
  35. $this->assertDef('four', false);
  36. // for similar reasons, disallow any other one character language
  37. $this->assertDef('f', false);
  38. // second subtag rules
  39. // one letter subtags prohibited until revision. This is, however,
  40. // less volatile than the restrictions on the primary subtags.
  41. // Also note that this test-case tests fix-behavior: chop
  42. // off subtags until you get a valid language code.
  43. $this->assertDef('en-a', 'en');
  44. // however, x is a reserved single-letter subtag that is allowed
  45. $this->assertDef('en-x', 'en-x');
  46. // 2-8 chars are permitted, but have special meaning that cannot
  47. // be checked without maintaining country code lookup tables (for
  48. // two characters) or special registration tables (for all above).
  49. $this->assertDef('en-uk', true);
  50. // further subtag rules: only syntactic constraints
  51. $this->assertDef('en-us-edison');
  52. $this->assertDef('en-us-toolonghaha', 'en-us');
  53. $this->assertDef('en-us-a-silly-long-one');
  54. // rfc 3066 stipulates that if a three letter and a two letter code
  55. // are available, the two letter one MUST be used. Without a language
  56. // code lookup table, we cannot implement this functionality.
  57. // although the HTML protocol, technically speaking, allows you to
  58. // omit language tags, this implicitly means that the parent element's
  59. // language is the one applicable, which, in some cases, is incorrect.
  60. // Thus, we allow und, only slightly defying the RFC's SHOULD NOT
  61. // designation.
  62. $this->assertDef('und');
  63. // because attributes only allow one language, mul is allowed, complying
  64. // with the RFC's SHOULD NOT designation.
  65. $this->assertDef('mul');
  66. }
  67. }
  68. // vim: et sw=4 sts=4