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.

137 lines
5.5 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. public function testDefaultOptionsAreUsedForFetching()
  92. {
  93. // LibraryTest::testDefaultOptionsAreUsed can only test that default options are merged and passed
  94. // to the relevant format handler. To test that they’re additionally passed to the fetcher currently
  95. // requires a network request to the twitter API for an auth error.
  96. // A potential future improvement for this would be to make a new mock HTTP client object which
  97. // accepts a callback, which gets passed the request it would send. We can then check that the
  98. // request has the parameters we want without having to actually hit the network.
  99. $url = 'https://twitter.com/BarnabyWalters/status/990659593561952256';
  100. // Confirm the expected behaviour.
  101. $xray = new p3k\XRay();
  102. $result = $xray->parse($url);
  103. $this->assertEquals('missing_parameters', $result['error']);
  104. $xray = new p3k\XRay([
  105. 'twitter_api_key' => 'extremely real API credentials',
  106. 'twitter_api_secret' => 'extremely real API credentials',
  107. 'twitter_access_token' => 'extremely real API credentials',
  108. 'twitter_access_token_secret' => 'extremely real API credentials'
  109. ]);
  110. $result = $xray->parse($url);
  111. $this->assertEquals('twitter_error', $result['error']);
  112. }
  113. }