IPTCTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * @group Media
  4. */
  5. class IPTCTest extends MediaWikiTestCase {
  6. /**
  7. * @covers IPTC::getCharset
  8. */
  9. public function testRecognizeUtf8() {
  10. // utf-8 is the only one used in practise.
  11. $res = IPTC::getCharset( "\x1b%G" );
  12. $this->assertEquals( 'UTF-8', $res );
  13. }
  14. /**
  15. * @covers IPTC::parse
  16. */
  17. public function testIPTCParseNoCharset88591() {
  18. // basically IPTC for keyword with value of 0xBC which is 1/4 in iso-8859-1
  19. // This data doesn't specify a charset. We're supposed to guess
  20. // (which basically means utf-8 if valid, windows 1252 (iso 8859-1) if not)
  21. $iptcData = "Photoshop 3.0\08BIM\4\4\0\0\0\0\0\x06\x1c\x02\x19\x00\x01\xBC";
  22. $res = IPTC::parse( $iptcData );
  23. $this->assertEquals( [ '¼' ], $res['Keywords'] );
  24. }
  25. /**
  26. * @covers IPTC::parse
  27. */
  28. public function testIPTCParseNoCharset88591b() {
  29. /* This one contains a sequence that's valid iso 8859-1 but not valid utf8 */
  30. /* \xC3 = Ã, \xB8 = ¸ */
  31. $iptcData = "Photoshop 3.0\08BIM\4\4\0\0\0\0\0\x09\x1c\x02\x19\x00\x04\xC3\xC3\xC3\xB8";
  32. $res = IPTC::parse( $iptcData );
  33. $this->assertEquals( [ 'ÃÃø' ], $res['Keywords'] );
  34. }
  35. /**
  36. * Same as testIPTCParseNoCharset88591b, but forcing the charset to utf-8.
  37. * What should happen is the first "\xC3\xC3" should be dropped as invalid,
  38. * leaving \xC3\xB8, which is ø
  39. * @covers IPTC::parse
  40. */
  41. public function testIPTCParseForcedUTFButInvalid() {
  42. $iptcData = "Photoshop 3.0\08BIM\4\4\0\0\0\0\0\x11\x1c\x02\x19\x00\x04\xC3\xC3\xC3\xB8"
  43. . "\x1c\x01\x5A\x00\x03\x1B\x25\x47";
  44. $res = IPTC::parse( $iptcData );
  45. $this->assertEquals( [ 'ø' ], $res['Keywords'] );
  46. }
  47. /**
  48. * @covers IPTC::parse
  49. */
  50. public function testIPTCParseNoCharsetUTF8() {
  51. $iptcData = "Photoshop 3.0\08BIM\4\4\0\0\0\0\0\x07\x1c\x02\x19\x00\x02¼";
  52. $res = IPTC::parse( $iptcData );
  53. $this->assertEquals( [ '¼' ], $res['Keywords'] );
  54. }
  55. /**
  56. * Testing something that has 2 values for keyword
  57. * @covers IPTC::parse
  58. */
  59. public function testIPTCParseMulti() {
  60. $iptcData = /* identifier */ "Photoshop 3.0\08BIM\4\4"
  61. /* length */ . "\0\0\0\0\0\x0D"
  62. . "\x1c\x02\x19" . "\x00\x01" . "\xBC"
  63. . "\x1c\x02\x19" . "\x00\x02" . "\xBC\xBD";
  64. $res = IPTC::parse( $iptcData );
  65. $this->assertEquals( [ '¼', '¼½' ], $res['Keywords'] );
  66. }
  67. /**
  68. * @covers IPTC::parse
  69. */
  70. public function testIPTCParseUTF8() {
  71. // This has the magic "\x1c\x01\x5A\x00\x03\x1B\x25\x47" which marks content as UTF8.
  72. $iptcData =
  73. "Photoshop 3.0\08BIM\4\4\0\0\0\0\0\x0F\x1c\x02\x19\x00\x02¼\x1c\x01\x5A\x00\x03\x1B\x25\x47";
  74. $res = IPTC::parse( $iptcData );
  75. $this->assertEquals( [ '¼' ], $res['Keywords'] );
  76. }
  77. }