2010-11-15 19:55:28 +00:00
< ? php
2018-07-18 05:31:24 +01:00
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
/**
* OembedPlugin implementation for GNU social
*
* @ package GNUsocial
* @ author Mikael Nordfeldth
* @ author Diogo Cordeiro < diogo @ fc . up . pt >
* @ copyright 2019 Free Software Foundation , Inc http :// www . fsf . org
* @ license https :// www . gnu . org / licenses / agpl . html GNU AGPL v3 or later
*/
2010-11-15 19:55:28 +00:00
if ( isset ( $_SERVER ) && array_key_exists ( 'REQUEST_METHOD' , $_SERVER )) {
print " This script must be run from the command line \n " ;
exit ();
}
2018-07-18 05:31:24 +01:00
define ( 'INSTALLDIR' , realpath ( __DIR__ . '/../../..' ));
2013-09-28 14:20:10 +01:00
define ( 'GNUSOCIAL' , true );
define ( 'STATUSNET' , true ); // compatibility
2010-11-15 19:55:28 +00:00
require_once INSTALLDIR . '/lib/common.php' ;
class oEmbedTest extends PHPUnit_Framework_TestCase
{
public function setup ()
{
2011-09-30 20:51:23 +01:00
$this -> old_ohembed = common_config ( 'ohembed' , 'endpoint' );
2010-11-15 19:55:28 +00:00
}
public function tearDown ()
{
2011-09-30 20:51:23 +01:00
$GLOBALS [ 'config' ][ 'oembed' ][ 'endpoint' ] = $this -> old_ohembed ;
2010-11-15 19:55:28 +00:00
}
/**
2011-09-30 20:51:23 +01:00
* Test with ohembed DISABLED .
2010-11-15 19:55:28 +00:00
*
2010-11-15 20:32:29 +00:00
* @ dataProvider discoverableSources
2010-11-15 19:55:28 +00:00
*/
public function testoEmbed ( $url , $expectedType )
2010-11-15 20:32:29 +00:00
{
2011-09-30 20:51:23 +01:00
$GLOBALS [ 'config' ][ 'oembed' ][ 'endpoint' ] = false ;
2010-11-15 20:32:29 +00:00
$this -> _doTest ( $url , $expectedType );
}
/**
* Test with oohembed ENABLED .
*
* @ dataProvider fallbackSources
*/
2011-09-30 20:51:23 +01:00
public function testnoEmbed ( $url , $expectedType )
2010-11-15 20:32:29 +00:00
{
2011-09-30 20:51:23 +01:00
$GLOBALS [ 'config' ][ 'oembed' ][ 'endpoint' ] = $this -> _endpoint ();
2010-11-15 20:32:29 +00:00
$this -> _doTest ( $url , $expectedType );
}
/**
2011-09-30 20:51:23 +01:00
* Get default oembed endpoint .
2010-11-15 20:32:29 +00:00
*
* @ return string
*/
2018-07-18 05:31:24 +01:00
public function _endpoint ()
2010-11-15 20:32:29 +00:00
{
$default = array ();
2018-07-18 05:31:24 +01:00
$_server = 'localhost' ;
$_path = '' ;
2010-11-15 20:32:29 +00:00
require INSTALLDIR . '/lib/default.php' ;
2011-09-30 20:51:23 +01:00
return $default [ 'oembed' ][ 'endpoint' ];
2010-11-15 20:32:29 +00:00
}
/**
* Actually run an individual test .
*
* @ param string $url
* @ param string $expectedType
*/
2018-07-18 05:31:24 +01:00
public function _doTest ( $url , $expectedType )
2010-11-15 19:55:28 +00:00
{
try {
$data = oEmbedHelper :: getObject ( $url );
$this -> assertEquals ( $expectedType , $data -> type );
2010-11-15 20:58:00 +00:00
if ( $data -> type == 'photo' ) {
$this -> assertTrue ( ! empty ( $data -> url ), 'Photo must have a URL.' );
$this -> assertTrue ( ! empty ( $data -> width ), 'Photo must have a width.' );
$this -> assertTrue ( ! empty ( $data -> height ), 'Photo must have a height.' );
2018-07-18 05:31:24 +01:00
} elseif ( $data -> type == 'video' ) {
2010-11-15 20:58:00 +00:00
$this -> assertTrue ( ! empty ( $data -> html ), 'Video must have embedding HTML.' );
$this -> assertTrue ( ! empty ( $data -> thumbnail_url ), 'Video should have a thumbnail.' );
}
if ( ! empty ( $data -> thumbnail_url )) {
$this -> assertTrue ( ! empty ( $data -> thumbnail_width ), 'Thumbnail must list a width.' );
$this -> assertTrue ( ! empty ( $data -> thumbnail_height ), 'Thumbnail must list a height.' );
}
2010-11-15 19:55:28 +00:00
} catch ( Exception $e ) {
if ( $expectedType == 'none' ) {
$this -> assertEquals ( $expectedType , 'none' , 'Should not have data for this URL.' );
} else {
throw $e ;
}
}
}
2010-11-15 20:25:44 +00:00
/**
* Sample oEmbed targets for sites we know ourselves ...
* @ return array
*/
2018-07-18 05:31:24 +01:00
public static function knownSources ()
2010-11-15 19:55:28 +00:00
{
2010-11-15 20:25:44 +00:00
$sources = array (
2015-01-17 11:07:57 +00:00
array ( 'https://www.flickr.com/photos/brionv/5172500179/' , 'photo' ),
2010-11-15 20:25:44 +00:00
);
return $sources ;
}
2010-11-15 19:55:28 +00:00
2010-11-15 20:25:44 +00:00
/**
* Sample oEmbed targets that can be found via discovery .
* Includes also knownSources () output .
*
* @ return array
*/
2018-07-18 05:31:24 +01:00
public static function discoverableSources ()
2010-11-15 20:25:44 +00:00
{
$sources = array (
2010-11-15 19:55:28 +00:00
2010-11-15 20:25:44 +00:00
array ( 'http://www.youtube.com/watch?v=eUgLR232Cnw' , 'video' ),
array ( 'http://vimeo.com/9283184' , 'video' ),
2010-11-15 19:55:28 +00:00
2010-11-15 20:25:44 +00:00
// Will fail discovery:
array ( 'http://leuksman.com/log/2010/10/29/statusnet-0-9-6-release/' , 'none' ),
);
return array_merge ( self :: knownSources (), $sources );
2010-11-15 19:55:28 +00:00
}
2010-11-15 20:25:44 +00:00
/**
2011-09-30 20:51:23 +01:00
* Sample oEmbed targets that can be found via noembed . com .
2010-11-15 20:25:44 +00:00
* Includes also discoverableSources () output .
*
* @ return array
*/
2018-07-18 05:31:24 +01:00
public static function fallbackSources ()
2010-11-15 20:25:44 +00:00
{
$sources = array (
2011-09-30 20:51:23 +01:00
array ( 'https://github.com/git/git/commit/85e9c7e1d42849c5c3084a9da748608468310c0e' , 'Github Commit' ), // @fixme in future there may be a native provider -- will change to 'photo'
2010-11-15 20:25:44 +00:00
);
2011-09-30 20:51:23 +01:00
$sources = array ();
2010-11-15 20:25:44 +00:00
return array_merge ( self :: discoverableSources (), $sources );
}
2010-11-15 19:55:28 +00:00
}