EmbedTest.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * OembedPlugin implementation for GNU social
  18. *
  19. * @package GNUsocial
  20. * @author Mikael Nordfeldth
  21. * @author Diogo Cordeiro <diogo@fc.up.pt>
  22. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. // namespace Tests\Unit;
  26. use PHPUnit\Framework\TestCase;
  27. define('INSTALLDIR', realpath(__DIR__ . '/../../..'));
  28. if (!defined('GNUSOCIAL')) {
  29. define('GNUSOCIAL', true);
  30. }
  31. if (!defined('STATUSNET')) { // Compatibility
  32. define('STATUSNET', true);
  33. }
  34. require_once INSTALLDIR . '/lib/util/common.php';
  35. final class oEmbedTest extends TestCase
  36. {
  37. /**
  38. * Run tests
  39. *
  40. * @param string $url
  41. * @param string $expectedType
  42. * @dataProvider sources
  43. */
  44. public function testEmbed($url, $expectedType)
  45. {
  46. try {
  47. $data = EmbedHelper::getObject($url);
  48. $this->assertEquals($expectedType, $data->type);
  49. if ($data->type == 'photo') {
  50. $this->assertTrue(!empty($data->thumbnail_url), 'Photo must have a URL.');
  51. $this->assertTrue(!empty($data->thumbnail_width), 'Photo must have a width.');
  52. $this->assertTrue(!empty($data->thumbnail_height), 'Photo must have a height.');
  53. } elseif ($data->type == 'video') {
  54. $this->assertTrue(!empty($data->html), 'Video must have embedding HTML.');
  55. $this->assertTrue(!empty($data->thumbnail_url), 'Video should have a thumbnail.');
  56. } else {
  57. $this->assertTrue(!empty($data->title), 'Page must have a title');
  58. $this->assertTrue(!empty($data->url), 'Page must have a URL');
  59. }
  60. if (!empty($data->thumbnail_url)) {
  61. $this->assertTrue(!empty($data->thumbnail_width), 'Thumbnail must list a width.');
  62. $this->assertTrue(!empty($data->thumbnail_height), 'Thumbnail must list a height.');
  63. }
  64. } catch (Exception $e) {
  65. if ($expectedType == 'none') {
  66. $this->assertEquals($expectedType, 'none', 'Should not have data for this URL.');
  67. } else {
  68. throw $e;
  69. }
  70. }
  71. }
  72. public static function sources() {
  73. return [
  74. ['https://notabug.org/', 'link'],
  75. ['http://www.youtube.com/watch?v=eUgLR232Cnw', 'video'],
  76. [GNUSOCIAL_ENGINE_URL, 'link'],
  77. ['https://www.gnu.org/graphics/heckert_gnu.transp.small.png', 'photo'],
  78. ['http://vimeo.com/9283184', 'video'],
  79. ['http://leuksman.com/log/2010/10/29/statusnet-0-9-6-release/', 'none'],
  80. ['https://github.com/git/git/commit/85e9c7e1d42849c5c3084a9da748608468310c0e', 'link']
  81. ];
  82. }
  83. }