We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

oEmbedTest.php 4.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?php

if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
    print "This script must be run from the command line\n";
    exit();
}

define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
define('STATUSNET', true);

require_once INSTALLDIR . '/lib/common.php';

class oEmbedTest extends PHPUnit_Framework_TestCase
{

    public function setup()
    {
18
        $this->old_ohembed = common_config('ohembed', 'endpoint');
19 20 21 22
    }

    public function tearDown()
    {
23
        $GLOBALS['config']['oembed']['endpoint'] = $this->old_ohembed;
24 25 26
    }

    /**
27
     * Test with ohembed DISABLED.
28
     *
29
     * @dataProvider discoverableSources
30 31
     */
    public function testoEmbed($url, $expectedType)
32
    {
33
        $GLOBALS['config']['oembed']['endpoint'] = false;
34 35 36 37 38 39 40 41
        $this->_doTest($url, $expectedType);
    }

    /**
     * Test with oohembed ENABLED.
     *
     * @dataProvider fallbackSources
     */
42
    public function testnoEmbed($url, $expectedType)
43
    {
44
        $GLOBALS['config']['oembed']['endpoint'] = $this->_endpoint();
45 46 47 48
        $this->_doTest($url, $expectedType);
    }

    /**
49
     * Get default oembed endpoint.
50 51 52 53 54 55 56 57
     *
     * @return string
     */
    function _endpoint()
    {
        $default = array();
        $_server = 'localhost'; $_path = '';
        require INSTALLDIR . '/lib/default.php';
58
        return $default['oembed']['endpoint'];
59 60 61 62 63 64 65 66 67
    }

    /**
     * Actually run an individual test.
     *
     * @param string $url
     * @param string $expectedType
     */
    function _doTest($url, $expectedType)
68 69 70 71
    {
        try {
            $data = oEmbedHelper::getObject($url);
            $this->assertEquals($expectedType, $data->type);
72 73 74 75 76 77 78 79 80 81 82 83
            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.');
            } else if ($data->type == 'video') {
                $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.');
            }
84 85 86 87 88 89 90 91 92
        } catch (Exception $e) {
            if ($expectedType == 'none') {
                $this->assertEquals($expectedType, 'none', 'Should not have data for this URL.');
            } else {
                throw $e;
            }
        }
    }

Brion Vibber's avatar
Brion Vibber committed
93 94 95 96 97
    /**
     * Sample oEmbed targets for sites we know ourselves...
     * @return array
     */
    static public function knownSources()
98
    {
Brion Vibber's avatar
Brion Vibber committed
99 100 101 102 103 104 105
        $sources = array(
            array('http://www.flickr.com/photos/brionv/5172500179/', 'photo'),
            array('http://yfrog.com/fy42747177j', 'photo'),
            array('http://twitpic.com/36adw6', 'photo'),
        );
        return $sources;
    }
106

Brion Vibber's avatar
Brion Vibber committed
107 108 109 110 111 112 113 114 115
    /**
     * Sample oEmbed targets that can be found via discovery.
     * Includes also knownSources() output.
     *
     * @return array
     */
    static public function discoverableSources()
    {
        $sources = array(
116

Brion Vibber's avatar
Brion Vibber committed
117 118
            array('http://www.youtube.com/watch?v=eUgLR232Cnw', 'video'),
            array('http://vimeo.com/9283184', 'video'),
119

Brion Vibber's avatar
Brion Vibber committed
120 121 122 123
            // Will fail discovery:
            array('http://leuksman.com/log/2010/10/29/statusnet-0-9-6-release/', 'none'),
        );
        return array_merge(self::knownSources(), $sources);
124 125
    }

Brion Vibber's avatar
Brion Vibber committed
126
    /**
127
     * Sample oEmbed targets that can be found via noembed.com.
Brion Vibber's avatar
Brion Vibber committed
128 129 130 131 132 133
     * Includes also discoverableSources() output.
     *
     * @return array
     */
    static public function fallbackSources()
    {
134

Brion Vibber's avatar
Brion Vibber committed
135
        $sources = array(
136
            array('https://github.com/git/git/commit/85e9c7e1d42849c5c3084a9da748608468310c0e', 'Github Commit'), // @fixme in future there may be a native provider -- will change to 'photo'
Brion Vibber's avatar
Brion Vibber committed
137
        );
138 139 140

        $sources = array();

Brion Vibber's avatar
Brion Vibber committed
141 142
        return array_merge(self::discoverableSources(), $sources);
    }
143
}