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