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
|
|
|
|
2020-08-12 02:14:05 +02:00
|
|
|
use Psr\SimpleCache\CacheInterface;
|
|
|
|
use SilverStripe\View\Parsers\ShortcodeParser;
|
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
|
|
|
}
|
2020-08-12 02:14:05 +02:00
|
|
|
|
|
|
|
public function testFlushCachedShortcodes()
|
|
|
|
{
|
|
|
|
/** @var CacheInterface $cache */
|
|
|
|
$url = 'http://www.test-service.com/abc123';
|
|
|
|
$content = '<p>Some content with an [embed url="' . $url . '" thumbnail="https://example.com/mythumb.jpg" ' .
|
|
|
|
'class="leftAlone ss-htmleditorfield-file embed" width="480" height="270"]' . $url . '[/embed]</p>';
|
|
|
|
$embedHtml = '<iframe myattr="something" />';
|
|
|
|
$parser = ShortcodeParser::get('default');
|
|
|
|
|
|
|
|
// use reflection to access private methods
|
|
|
|
$provider = new EmbedShortcodeProvider();
|
|
|
|
$reflector = new \ReflectionClass(EmbedShortcodeProvider::class);
|
|
|
|
$method = $reflector->getMethod('getCache');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
$cache = $method->invokeArgs($provider, []);
|
|
|
|
$method = $reflector->getMethod('deriveCacheKey');
|
|
|
|
$method->setAccessible(true);
|
2021-06-29 02:17:07 +02:00
|
|
|
$class = 'leftAlone ss-htmleditorfield-file embed';
|
|
|
|
$width = '480';
|
|
|
|
$height = '270';
|
|
|
|
$key = $method->invokeArgs($provider, [$url, $class, $width, $height]);
|
2020-08-12 02:14:05 +02:00
|
|
|
|
|
|
|
// assertions
|
2021-06-29 02:17:07 +02:00
|
|
|
$this->assertEquals('embed-shortcode-httpwwwtest-servicecomabc123-leftAloness-htmleditorfield-fileembed-480-270', $key);
|
2020-08-12 02:14:05 +02:00
|
|
|
$cache->set($key, $embedHtml);
|
|
|
|
$this->assertTrue($cache->has($key));
|
|
|
|
EmbedShortcodeProvider::flushCachedShortcodes($parser, $content);
|
|
|
|
$this->assertFalse($cache->has($key));
|
|
|
|
}
|
2016-05-27 03:09:03 +02:00
|
|
|
}
|