]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - tests/oEmbedTest.php
Merge branch 'master' into testing
[quix0rs-gnu-social.git] / 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('STATUSNET', true);
10
11 require_once INSTALLDIR . '/lib/common.php';
12
13 class oEmbedTest extends PHPUnit_Framework_TestCase
14 {
15
16     public function setup()
17     {
18         $this->old_oohembed = common_config('oohembed', 'endpoint');
19     }
20
21     public function tearDown()
22     {
23         $GLOBALS['config']['oohembed']['endpoint'] = $this->old_oohembed;
24     }
25
26     /**
27      * Test with oohembed DISABLED.
28      *
29      * @dataProvider discoverableSources
30      */
31     public function testoEmbed($url, $expectedType)
32     {
33         $GLOBALS['config']['oohembed']['endpoint'] = false;
34         $this->_doTest($url, $expectedType);
35     }
36
37     /**
38      * Test with oohembed ENABLED.
39      *
40      * @dataProvider fallbackSources
41      */
42     public function testoohEmbed($url, $expectedType)
43     {
44         $GLOBALS['config']['oohembed']['endpoint'] = $this->_endpoint();
45         $this->_doTest($url, $expectedType);
46     }
47
48     /**
49      * Get default oohembed endpoint.
50      *
51      * @return string
52      */
53     function _endpoint()
54     {
55         $default = array();
56         $_server = 'localhost'; $_path = '';
57         require INSTALLDIR . '/lib/default.php';
58         return $default['oohembed']['endpoint'];
59     }
60
61     /**
62      * Actually run an individual test.
63      *
64      * @param string $url
65      * @param string $expectedType
66      */
67     function _doTest($url, $expectedType)
68     {
69         try {
70             $data = oEmbedHelper::getObject($url);
71             $this->assertEquals($expectedType, $data->type);
72             if ($data->type == 'photo') {
73                 $this->assertTrue(!empty($data->url), 'Photo must have a URL.');
74                 $this->assertTrue(!empty($data->width), 'Photo must have a width.');
75                 $this->assertTrue(!empty($data->height), 'Photo must have a height.');
76             } else if ($data->type == 'video') {
77                 $this->assertTrue(!empty($data->html), 'Video must have embedding HTML.');
78                 $this->assertTrue(!empty($data->thumbnail_url), 'Video should have a thumbnail.');
79             }
80             if (!empty($data->thumbnail_url)) {
81                 $this->assertTrue(!empty($data->thumbnail_width), 'Thumbnail must list a width.');
82                 $this->assertTrue(!empty($data->thumbnail_height), 'Thumbnail must list a height.');
83             }
84         } catch (Exception $e) {
85             if ($expectedType == 'none') {
86                 $this->assertEquals($expectedType, 'none', 'Should not have data for this URL.');
87             } else {
88                 throw $e;
89             }
90         }
91     }
92
93     /**
94      * Sample oEmbed targets for sites we know ourselves...
95      * @return array
96      */
97     static public function knownSources()
98     {
99         $sources = array(
100             array('http://www.flickr.com/photos/brionv/5172500179/', 'photo'),
101             array('http://yfrog.com/fy42747177j', 'photo'),
102             array('http://twitpic.com/36adw6', 'photo'),
103         );
104         return $sources;
105     }
106
107     /**
108      * Sample oEmbed targets that can be found via discovery.
109      * Includes also knownSources() output.
110      *
111      * @return array
112      */
113     static public function discoverableSources()
114     {
115         $sources = array(
116             array('http://identi.ca/attachment/34437400', 'photo'),
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 oohembed.com.
129      * Includes also discoverableSources() output.
130      *
131      * @return array
132      */
133     static public function fallbackSources()
134     {
135         $sources = array(
136             array('http://en.wikipedia.org/wiki/File:Wiki.png', 'link'), // @fixme in future there may be a native provider -- will change to 'photo'
137         );
138         return array_merge(self::discoverableSources(), $sources);
139     }
140 }