2016-05-27 03:09:03 +02:00
|
|
|
<?php
|
|
|
|
|
2017-06-29 08:45:17 +02:00
|
|
|
namespace SilverStripe\View\Tests\Shortcodes;
|
2016-05-27 03:09:03 +02:00
|
|
|
|
2017-06-29 08:45:17 +02:00
|
|
|
use SilverStripe\View\Shortcodes\EmbedShortcodeProvider;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2016-05-27 03:09:03 +02:00
|
|
|
/**
|
|
|
|
* Class EmbedShortcodeProviderTest
|
|
|
|
*
|
|
|
|
* Because Embed/Embed does not have a mockup, the tests have to run against a live environment.
|
|
|
|
* I've tried to fix it by serializing the data to a file, but to no avail.
|
|
|
|
* Any improvements on not having to call external resources are welcome.
|
|
|
|
*/
|
|
|
|
class EmbedShortcodeProviderTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* @var string test youtube. The SilverStripe Platform promotion by UncleCheese
|
|
|
|
*/
|
|
|
|
protected static $test_youtube = 'https://www.youtube.com/watch?v=dM15HfUYwF0';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string test Soundcloud. One of my favorite bands, Delain, Suckerpunch.
|
|
|
|
*/
|
|
|
|
protected static $test_soundcloud = 'http://soundcloud.com/napalmrecords/delain-suckerpunch';
|
|
|
|
|
2019-08-19 17:35:40 +02:00
|
|
|
public function assertEqualIgnoringWhitespace($a, $b, $message = '')
|
|
|
|
{
|
|
|
|
$this->assertEquals(preg_replace('/\s+/', '', $a), preg_replace('/\s+/', '', $b), $message);
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testYoutube()
|
|
|
|
{
|
2017-04-03 06:30:01 +02:00
|
|
|
/** @var string $result */
|
|
|
|
$result = $this->mockRequest(
|
|
|
|
[
|
|
|
|
'url' => static::$test_youtube,
|
|
|
|
'caption' => 'A nice video',
|
2020-07-27 08:04:03 +02:00
|
|
|
'width' => 777,
|
|
|
|
'height' => 437,
|
2017-04-03 06:30:01 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'version' => '1.0',
|
|
|
|
'provider_url' => 'https://www.youtube.com/',
|
|
|
|
'title' => 'SilverStripe Platform 2 min introduction',
|
|
|
|
'html' => '<iframe width="480" height="270" src="https://www.youtube.com/embed/dM15HfUYwF0?feature=oembed" frameborder="0" allowfullscreen></iframe>',
|
|
|
|
'provider_name' => 'YouTube',
|
|
|
|
'thumbnail_width' => 480,
|
|
|
|
'type' => 'video',
|
|
|
|
'thumbnail_url' => 'https://i.ytimg.com/vi/dM15HfUYwF0/hqdefault.jpg',
|
|
|
|
'thumbnail_height' => 360,
|
|
|
|
'width' => 480,
|
|
|
|
'author_url' => 'https://www.youtube.com/user/SilverStripe',
|
|
|
|
'author_name' => 'SilverStripe',
|
|
|
|
'height' => 270,
|
|
|
|
]
|
|
|
|
);
|
2019-08-19 17:35:40 +02:00
|
|
|
$this->assertEqualIgnoringWhitespace(
|
2017-04-03 06:30:01 +02:00
|
|
|
<<<EOS
|
2020-07-27 08:04:03 +02:00
|
|
|
<div style="width: 777px;"><iframe width="777" height="437" src="https://www.youtube.com/embed/dM15HfUYwF0?feature=oembed" frameborder="0" allowfullscreen></iframe>
|
2017-04-03 06:30:01 +02:00
|
|
|
<p class="caption">A nice video</p></div>
|
|
|
|
EOS
|
|
|
|
,
|
|
|
|
$result
|
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSoundcloud()
|
|
|
|
{
|
2017-04-03 06:30:01 +02:00
|
|
|
/** @var string $result */
|
|
|
|
$result = $this->mockRequest(
|
|
|
|
['url' => static::$test_soundcloud],
|
|
|
|
[
|
|
|
|
'version' => 1,
|
|
|
|
'type' => 'rich',
|
|
|
|
'provider_name' => 'SoundCloud',
|
|
|
|
'provider_url' => 'http://soundcloud.com',
|
|
|
|
'height' => 400,
|
|
|
|
'width' => '100%',
|
|
|
|
'title' => 'DELAIN - Suckerpunch by Napalm Records',
|
|
|
|
'description' => 'Taken from the EP "Lunar Prelude": http://shop.napalmrecords.com/delain',
|
|
|
|
'thumbnail_url' => 'http://i1.sndcdn.com/artworks-000143578557-af0v6l-t500x500.jpg',
|
|
|
|
'html' => '<iframe width="100%" height="400" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?visual=true&url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F242518079&show_artwork=true"></iframe>',
|
|
|
|
'author_name' => 'Napalm Records',
|
|
|
|
'author_url' => 'http://soundcloud.com/napalmrecords',
|
|
|
|
]
|
|
|
|
);
|
2019-08-19 17:35:40 +02:00
|
|
|
$this->assertEqualIgnoringWhitespace(
|
2017-04-03 06:30:01 +02:00
|
|
|
<<<EOS
|
|
|
|
<div style="width: 100px;"><iframe width="100%" height="400" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?visual=true&url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F242518079&show_artwork=true"></iframe></div>
|
|
|
|
EOS
|
|
|
|
,
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mock an oembed request
|
|
|
|
*
|
|
|
|
* @param array $arguments Input arguments
|
|
|
|
* @param array $response JSON response body
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function mockRequest($arguments, $response)
|
|
|
|
{
|
|
|
|
return EmbedShortcodeProvider::handle_shortcode(
|
|
|
|
$arguments,
|
|
|
|
'',
|
|
|
|
null,
|
|
|
|
'embed',
|
|
|
|
[
|
|
|
|
'resolver' => [
|
|
|
|
'class' => MockResolver::class,
|
|
|
|
'config' => [
|
|
|
|
'expectedContent' => json_encode($response),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2016-05-27 03:09:03 +02:00
|
|
|
}
|