Blame view

framework/tests/oembed/OembedTest.php 3.86 KB
0084d336   Administrator   Importers CRUD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  <?php
  
  class OembedTest extends SapphireTest {
  	public function setUp() {
  		parent::setUp();
  		Config::nest();
  	}
  
  	public function tearDown() {
  		Config::unnest();
  		parent::tearDown();
  	}
  
  	public function testGetOembedFromUrl() {
  		Config::inst()->update('Oembed', 'providers', array(
  			'http://*.silverstripe.com/watch*'=>'http://www.silverstripe.com/oembed/'
  		));
  		$escapedEndpointURL = urlencode("http://www.silverstripe.com/oembed/");
  
  		// Test with valid URL
  		$result = Oembed::get_oembed_from_url('http://www.silverstripe.com/watch12345');
  		$this->assertTrue($result!=false);
  		$this->assertEquals($result->getOembedURL(),
  			'http://www.silverstripe.com/oembed/?format=json&url='.urlencode('http://www.silverstripe.com/watch12345'),
  			'Triggers on matching URL');
  
  		// Test without www.
  		$result = Oembed::get_oembed_from_url('http://silverstripe.com/watch12345');
  		$this->assertTrue($result!=false);
  		$this->assertEquals($result->getOembedURL(),
  			'http://www.silverstripe.com/oembed/?format=json&url='.urlencode('http://silverstripe.com/watch12345'),
  			'Triggers on matching URL without www');
  
  		// Test if options make their way to the URL
  		$result = Oembed::get_oembed_from_url('http://www.silverstripe.com/watch12345', false, array('foo'=>'bar'));
  		$this->assertTrue($result!=false);
  		$this->assertEquals($result->getOembedURL(), 'http://www.silverstripe.com/oembed/?format=json&url='
  			. urlencode('http://www.silverstripe.com/watch12345').'&foo=bar', 
  			'Includes options');
  
  		// Test magic.
  		$result = Oembed::get_oembed_from_url('http://www.silverstripe.com/watch12345', false,
  			array('height'=>'foo', 'width'=>'bar'));
  		$this->assertTrue($result!=false);
  		$urlParts = parse_url($result->getOembedURL());
  		parse_str($urlParts['query'], $query);
  		$this->assertEquals($query['maxheight'], 'foo', 'Magically creates maxheight option');
  		$this->assertEquals($query['maxwidth'], 'bar', 'Magically creates maxwidth option');
  	}
  
  	public function testRequestProtocolReflectedInGetOembedFromUrl() {
  		Config::inst()->update('Oembed', 'providers', array(
  			'http://*.silverstripe.com/watch*'=> array(
  				'http' => 'http://www.silverstripe.com/oembed/',
  				'https' => 'https://www.silverstripe.com/oembed/?scheme=https',
  			),
  			'https://*.silverstripe.com/watch*'=> array(
  				'http' => 'http://www.silverstripe.com/oembed/',
  				'https' => 'https://www.silverstripe.com/oembed/?scheme=https',
  			)
  		));
  
  		Config::inst()->update('Director', 'alternate_protocol', 'http');
  
  		foreach(array('http', 'https') as $protocol) {
  			$url = $protocol.'://www.silverstripe.com/watch12345';
  			$result = Oembed::get_oembed_from_url($url);
  
  			$this->assertInstanceOf('Oembed_Result', $result);
  			$this->assertEquals($result->getOembedURL(),
  				'http://www.silverstripe.com/oembed/?format=json&url='.urlencode($url),
  				'Returns http based URLs when request is over http, regardless of source URL');
  		}
  
  		Config::inst()->update('Director', 'alternate_protocol', 'https');
  
  		foreach(array('http', 'https') as $protocol) {
  			$url = $protocol.'://www.silverstripe.com/watch12345';
  			$result = Oembed::get_oembed_from_url($url);
  
  			$this->assertInstanceOf('Oembed_Result', $result);
  			$this->assertEquals($result->getOembedURL(),
  				'https://www.silverstripe.com/oembed/?scheme=https&format=json&url='.urlencode($url),
  				'Returns https based URLs when request is over https, regardless of source URL');
  		}
  
  		Config::inst()->update('Director', 'alternate_protocol', 'foo');
  
  		foreach(array('http', 'https') as $protocol) {
  			$url = $protocol.'://www.silverstripe.com/watch12345';
  			$result = Oembed::get_oembed_from_url($url);
  
  			$this->assertInstanceOf('Oembed_Result', $result);
  			$this->assertEquals($result->getOembedURL(),
  				'http://www.silverstripe.com/oembed/?format=json&url='.urlencode($url),
  				'When request protocol doesn\'t have specific handler, fall back to first option');
  		}
  	}
  }