oEmbedTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
  3. print "This script must be run from the command line\n";
  4. exit();
  5. }
  6. define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
  7. define('GNUSOCIAL', true);
  8. define('STATUSNET', true); // compatibility
  9. require_once INSTALLDIR . '/lib/common.php';
  10. class oEmbedTest extends PHPUnit_Framework_TestCase
  11. {
  12. public function setup()
  13. {
  14. $this->old_ohembed = common_config('ohembed', 'endpoint');
  15. }
  16. public function tearDown()
  17. {
  18. $GLOBALS['config']['oembed']['endpoint'] = $this->old_ohembed;
  19. }
  20. /**
  21. * Test with ohembed DISABLED.
  22. *
  23. * @dataProvider discoverableSources
  24. */
  25. public function testoEmbed($url, $expectedType)
  26. {
  27. $GLOBALS['config']['oembed']['endpoint'] = false;
  28. $this->_doTest($url, $expectedType);
  29. }
  30. /**
  31. * Test with oohembed ENABLED.
  32. *
  33. * @dataProvider fallbackSources
  34. */
  35. public function testnoEmbed($url, $expectedType)
  36. {
  37. $GLOBALS['config']['oembed']['endpoint'] = $this->_endpoint();
  38. $this->_doTest($url, $expectedType);
  39. }
  40. /**
  41. * Get default oembed endpoint.
  42. *
  43. * @return string
  44. */
  45. function _endpoint()
  46. {
  47. $default = array();
  48. $_server = 'localhost'; $_path = '';
  49. require INSTALLDIR . '/lib/default.php';
  50. return $default['oembed']['endpoint'];
  51. }
  52. /**
  53. * Actually run an individual test.
  54. *
  55. * @param string $url
  56. * @param string $expectedType
  57. */
  58. function _doTest($url, $expectedType)
  59. {
  60. try {
  61. $data = oEmbedHelper::getObject($url);
  62. $this->assertEquals($expectedType, $data->type);
  63. if ($data->type == 'photo') {
  64. $this->assertTrue(!empty($data->url), 'Photo must have a URL.');
  65. $this->assertTrue(!empty($data->width), 'Photo must have a width.');
  66. $this->assertTrue(!empty($data->height), 'Photo must have a height.');
  67. } else if ($data->type == 'video') {
  68. $this->assertTrue(!empty($data->html), 'Video must have embedding HTML.');
  69. $this->assertTrue(!empty($data->thumbnail_url), 'Video should have a thumbnail.');
  70. }
  71. if (!empty($data->thumbnail_url)) {
  72. $this->assertTrue(!empty($data->thumbnail_width), 'Thumbnail must list a width.');
  73. $this->assertTrue(!empty($data->thumbnail_height), 'Thumbnail must list a height.');
  74. }
  75. } catch (Exception $e) {
  76. if ($expectedType == 'none') {
  77. $this->assertEquals($expectedType, 'none', 'Should not have data for this URL.');
  78. } else {
  79. throw $e;
  80. }
  81. }
  82. }
  83. /**
  84. * Sample oEmbed targets for sites we know ourselves...
  85. * @return array
  86. */
  87. static public function knownSources()
  88. {
  89. $sources = array(
  90. array('https://www.flickr.com/photos/brionv/5172500179/', 'photo'),
  91. );
  92. return $sources;
  93. }
  94. /**
  95. * Sample oEmbed targets that can be found via discovery.
  96. * Includes also knownSources() output.
  97. *
  98. * @return array
  99. */
  100. static public function discoverableSources()
  101. {
  102. $sources = array(
  103. array('http://www.youtube.com/watch?v=eUgLR232Cnw', 'video'),
  104. array('http://vimeo.com/9283184', 'video'),
  105. // Will fail discovery:
  106. array('http://leuksman.com/log/2010/10/29/statusnet-0-9-6-release/', 'none'),
  107. );
  108. return array_merge(self::knownSources(), $sources);
  109. }
  110. /**
  111. * Sample oEmbed targets that can be found via noembed.com.
  112. * Includes also discoverableSources() output.
  113. *
  114. * @return array
  115. */
  116. static public function fallbackSources()
  117. {
  118. $sources = array(
  119. array('https://github.com/git/git/commit/85e9c7e1d42849c5c3084a9da748608468310c0e', 'Github Commit'), // @fixme in future there may be a native provider -- will change to 'photo'
  120. );
  121. $sources = array();
  122. return array_merge(self::discoverableSources(), $sources);
  123. }
  124. }