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.

111 lines
4.1 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. // On pages where the h-card is the main data but lacks a URL property, it will be filled with the page URL.
  67. $this->assertEquals($url, $data['data']['url']);
  68. }
  69. public function testDefaultOptionsAreUsed()
  70. {
  71. $url = 'http://example.com/';
  72. $html = '<p class="h-card">A Person</p>';
  73. $defaultOptionsXRay = new p3k\XRay(['include_original' => true]);
  74. $normalXRay = new p3k\XRay();
  75. // Make sure that the options we’re testing with actually result in different values first.
  76. $this->assertNotEquals(
  77. $defaultOptionsXRay->parse($url, $html),
  78. $normalXRay->parse($url, $html)
  79. );
  80. // Make sure that the options are applied in the same way as they would have been if passed to parse()
  81. $this->assertEquals(
  82. $defaultOptionsXRay->parse($url, $html),
  83. $normalXRay->parse($url, $html, ['include_original' => true])
  84. );
  85. // Make sure that the options can be overridden (this doesn’t test on a property-by-property basis but should be good enough.)
  86. $this->assertEquals(
  87. $defaultOptionsXRay->parse($url, $html, ['include_original' => false]),
  88. $normalXRay->parse($url, $html)
  89. );
  90. }
  91. }