]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - tests/oEmbedTest.php
Test oEmbed lookups with oohembed both on and off explicitly
[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         } catch (Exception $e) {
73             if ($expectedType == 'none') {
74                 $this->assertEquals($expectedType, 'none', 'Should not have data for this URL.');
75             } else {
76                 throw $e;
77             }
78         }
79     }
80
81     /**
82      * Sample oEmbed targets for sites we know ourselves...
83      * @return array
84      */
85     static public function knownSources()
86     {
87         $sources = array(
88             array('http://www.flickr.com/photos/brionv/5172500179/', 'photo'),
89             array('http://yfrog.com/fy42747177j', 'photo'),
90             array('http://twitpic.com/36adw6', 'photo'),
91         );
92         return $sources;
93     }
94
95     /**
96      * Sample oEmbed targets that can be found via discovery.
97      * Includes also knownSources() output.
98      *
99      * @return array
100      */
101     static public function discoverableSources()
102     {
103         $sources = array(
104             array('http://identi.ca/attachment/34437400', 'photo'),
105
106             array('http://www.youtube.com/watch?v=eUgLR232Cnw', 'video'),
107             array('http://vimeo.com/9283184', 'video'),
108
109             // Will fail discovery:
110             array('http://leuksman.com/log/2010/10/29/statusnet-0-9-6-release/', 'none'),
111         );
112         return array_merge(self::knownSources(), $sources);
113     }
114
115     /**
116      * Sample oEmbed targets that can be found via oohembed.com.
117      * Includes also discoverableSources() output.
118      *
119      * @return array
120      */
121     static public function fallbackSources()
122     {
123         $sources = array(
124             array('http://en.wikipedia.org/wiki/File:Wiki.png', 'link'), // @fixme in future there may be a native provider -- will change to 'photo'
125         );
126         return array_merge(self::discoverableSources(), $sources);
127     }
128 }