| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | class OembedTest extends SapphireTest { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | 	public function testGetOembedFromUrl() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | 		Config::inst()->update('Oembed', 'providers', array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | 			'http://*.silverstripe.com/watch*'=>'http://www.silverstripe.com/oembed/' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | 		)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | 		$escapedEndpointURL = urlencode("http://www.silverstripe.com/oembed/"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 		// Test with valid URL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 		$result = Oembed::get_oembed_from_url('http://www.silverstripe.com/watch12345'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 		$this->assertTrue($result!=false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 		$this->assertEquals($result->getOembedURL(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 			'http://www.silverstripe.com/oembed/?format=json&url='.urlencode('http://www.silverstripe.com/watch12345'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 			'Triggers on matching URL'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 		// Test without www. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 		$result = Oembed::get_oembed_from_url('http://silverstripe.com/watch12345'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 		$this->assertTrue($result!=false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		$this->assertEquals($result->getOembedURL(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 			'http://www.silverstripe.com/oembed/?format=json&url='.urlencode('http://silverstripe.com/watch12345'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 			'Triggers on matching URL without www'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		// Test if options make their way to the URL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		$result = Oembed::get_oembed_from_url('http://www.silverstripe.com/watch12345', false, array('foo'=>'bar')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		$this->assertTrue($result!=false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		$this->assertEquals($result->getOembedURL(), 'http://www.silverstripe.com/oembed/?format=json&url=' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 			. urlencode('http://www.silverstripe.com/watch12345').'&foo=bar', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 			'Includes options'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		// Test magic. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		$result = Oembed::get_oembed_from_url('http://www.silverstripe.com/watch12345', false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 			array('height'=>'foo', 'width'=>'bar')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		$this->assertTrue($result!=false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		$urlParts = parse_url($result->getOembedURL()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		parse_str($urlParts['query'], $query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		$this->assertEquals($query['maxheight'], 'foo', 'Magically creates maxheight option'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		$this->assertEquals($query['maxwidth'], 'bar', 'Magically creates maxwidth option'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 	public function testAutodiscover() { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 		// Test href after type tag | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 		$body = <<<EOS | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | <title id="pageTitle">Some content</title> | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | <link rel="search" type="application/opensearchdescription+xml" href="/osd.xml" title="Facebook" /> | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | <link | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 	rel="alternate" type="application/json+oembed" | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  | 	href="https://www.facebook.com/plugins/post/oembed.json/?url=https%3A%2F%2Fwww.facebook.com%2Fsomeusername%2Fposts%2F10209305859558135" | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | 	title="My post" | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  | /> | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  | EOS; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | 		$this->assertEquals( | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | 			'https://www.facebook.com/plugins/post/oembed.json/?url=https%3A%2F%2Fwww.facebook.com%2Fsomeusername%2Fposts%2F10209305859558135', | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  | 			Oembed::autodiscover_from_body($body) | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | 		); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  | 		// Test href before the type tag | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  | 		$body2 = <<<EOS | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | <title id="pageTitle">Some content</title> | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  | <link rel="search" type="application/opensearchdescription+xml" href="/osd.xml" title="Facebook" /> | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  | <link | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  | 	href="https://www.facebook.com/plugins/post/oembed.json/?url=https%3A%2F%2Fwww.facebook.com%2Fsomeusername%2Fposts%2F10209305859558135" | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  | 	rel="alternate" type="application/json+oembed" | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  | 	title="My post" | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | /> | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | EOS; | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  | 		$this->assertEquals( | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | 			'https://www.facebook.com/plugins/post/oembed.json/?url=https%3A%2F%2Fwww.facebook.com%2Fsomeusername%2Fposts%2F10209305859558135', | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 			Oembed::autodiscover_from_body($body2) | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  | 		); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	public function testFindThumbnail() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		$data = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 			"author_name"=> "Some User", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 			"author_url"=> null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			"provider_url" => "https://www.facebook.com", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			"provider_name" => "Facebook", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 			"success" => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			"height" => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 			"html" => "<div />", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 			"type" => "rich", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 			"version" => "1.0", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 			"url" => "https://www.facebook.com/someuser/posts/6465132161654421654", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 			"width" => 552 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		// Test facebook url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		$result = new Oembed_Result('https://www.facebook.com/someuser/posts/6465132161654421654'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		$this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 			"https://graph.facebook.com/6465132161654421654/picture", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			$result->findThumbnail($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		// Test respect existing url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		$data['thumbnail_url'] = 'http://www.silverstripe.com/picture.jpg'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		$this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 			"http://www.silverstripe.com/picture.jpg", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 			$result->findThumbnail($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	public function testRequestProtocolReflectedInGetOembedFromUrl() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		Config::inst()->update('Oembed', 'providers', array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 			'http://*.silverstripe.com/watch*'=> array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 				'http' => 'http://www.silverstripe.com/oembed/', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 				'https' => 'https://www.silverstripe.com/oembed/?scheme=https', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 			), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 			'https://*.silverstripe.com/watch*'=> array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 				'http' => 'http://www.silverstripe.com/oembed/', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 				'https' => 'https://www.silverstripe.com/oembed/?scheme=https', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 			) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 		Config::inst()->update('Director', 'alternate_protocol', 'http'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		foreach(array('http', 'https') as $protocol) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 			$url = $protocol.'://www.silverstripe.com/watch12345'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 			$result = Oembed::get_oembed_from_url($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 			$this->assertInstanceOf('Oembed_Result', $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 			$this->assertEquals($result->getOembedURL(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 				'http://www.silverstripe.com/oembed/?format=json&url='.urlencode($url), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 				'Returns http based URLs when request is over http, regardless of source URL'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		Config::inst()->update('Director', 'alternate_protocol', 'https'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		foreach(array('http', 'https') as $protocol) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 			$url = $protocol.'://www.silverstripe.com/watch12345'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 			$result = Oembed::get_oembed_from_url($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 			$this->assertInstanceOf('Oembed_Result', $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 			$this->assertEquals($result->getOembedURL(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 				'https://www.silverstripe.com/oembed/?scheme=https&format=json&url='.urlencode($url), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 				'Returns https based URLs when request is over https, regardless of source URL'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		Config::inst()->update('Director', 'alternate_protocol', 'foo'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		foreach(array('http', 'https') as $protocol) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 			$url = $protocol.'://www.silverstripe.com/watch12345'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 			$result = Oembed::get_oembed_from_url($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 			$this->assertInstanceOf('Oembed_Result', $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 			$this->assertEquals($result->getOembedURL(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 				'http://www.silverstripe.com/oembed/?format=json&url='.urlencode($url), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 				'When request protocol doesn\'t have specific handler, fall back to first option'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 153 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 154 |  |  |  |