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.

110 lines
4.0 KiB

  1. <?php
  2. class LibraryTest extends PHPUnit\Framework\TestCase
  3. {
  4. public function testInputIsParsedMf2Array()
  5. {
  6. $html = '<div class="h-entry"><p class="p-content p-name">Hello World</p><img src="/photo.jpg"></p></div>';
  7. $mf2 = Mf2\parse($html, 'http://example.com/entry');
  8. $xray = new p3k\XRay();
  9. $data = $xray->process('http://example.com/entry', $mf2);
  10. $this->assertEquals('Hello World', $data['data']['content']['text']);
  11. $this->assertEquals('http://example.com/photo.jpg', $data['data']['photo'][0]);
  12. }
  13. public function testInputIsParsedMf2JSON()
  14. {
  15. $html = '<div class="h-entry"><p class="p-content p-name">Hello World</p><img src="/photo.jpg"></p></div>';
  16. $mf2 = Mf2\parse($html, 'http://example.com/entry');
  17. $xray = new p3k\XRay();
  18. $data = $xray->process('http://example.com/entry', json_encode($mf2));
  19. $this->assertEquals('Hello World', $data['data']['content']['text']);
  20. $this->assertEquals('http://example.com/photo.jpg', $data['data']['photo'][0]);
  21. }
  22. public function testInputIsParsedMf2HCard()
  23. {
  24. $url = 'https://waterpigs.co.uk/';
  25. $html = '<a class="h-card" href="https://waterpigs.co.uk/">Barnaby Walters</a>';
  26. $mf2 = Mf2\parse($html, $url);
  27. $xray = new p3k\XRay();
  28. $data = $xray->process($url, $mf2);
  29. $this->assertEquals('card', $data['data']['type']);
  30. $this->assertEquals('Barnaby Walters', $data['data']['name']);
  31. }
  32. public function testNoHEntryMarkupMF2JSON()
  33. {
  34. $url = 'http://example.com/';
  35. $html = '<p><a href="http://target.example.com/">Target</a></p>';
  36. $mf2 = Mf2\parse($html, $url);
  37. $xray = new p3k\XRay();
  38. $data = $xray->process($url, $mf2);
  39. $this->assertEquals('unknown', $data['data']['type']);
  40. }
  41. public function testNoHEntryMarkup()
  42. {
  43. $url = 'http://example.com/';
  44. $html = '<p><a href="http://target.example.com/">Target</a></p>';
  45. $xray = new p3k\XRay();
  46. $data = $xray->parse($url, $html);
  47. $this->assertEquals('unknown', $data['data']['type']);
  48. }
  49. public function testNoHEntryMarkupWithTarget()
  50. {
  51. $url = 'http://example.com/';
  52. $html = '<p><a href="http://target.example.com/">Target</a></p>';
  53. $xray = new p3k\XRay();
  54. $data = $xray->parse($url, $html, ['target' => 'http://target.example.com/']);
  55. $this->assertEquals('unknown', $data['data']['type']);
  56. $this->assertArrayNotHasKey('error', $data);
  57. $this->assertArrayNotHasKey('html', $data);
  58. }
  59. public function testHandlesHCardWithoutURLProperty()
  60. {
  61. $url = 'http://example.com/';
  62. $html = '<p class="h-card">The Mythical URLless Person</p>';
  63. $xray = new p3k\XRay();
  64. $data = $xray->parse($url, $html);
  65. $this->assertEquals('card', $data['data']['type']);
  66. }
  67. public function testDefaultOptionsAreUsed()
  68. {
  69. $url = 'http://example.com/';
  70. $html = '<p class="h-card">A Person</p>';
  71. $defaultOptionsXRay = new p3k\XRay(['include_original' => true]);
  72. $normalXRay = new p3k\XRay();
  73. // Make sure that the options we’re testing with actually result in different values first.
  74. $this->assertNotEquals(
  75. $defaultOptionsXRay->parse($url, $html),
  76. $normalXRay->parse($url, $html)
  77. );
  78. // Make sure that the options are applied in the same way as they would have been if passed to parse()
  79. $this->assertEquals(
  80. $defaultOptionsXRay->parse($url, $html),
  81. $normalXRay->parse($url, $html, ['include_original' => true])
  82. );
  83. // Make sure that the options can be overridden (this doesn’t test on a property-by-property basis but should be good enough.)
  84. $this->assertEquals(
  85. $defaultOptionsXRay->parse($url, $html, ['include_original' => false]),
  86. $normalXRay->parse($url, $html)
  87. );
  88. }
  89. }