Passed
Pull Request — 4 (#10244)
by Steve
08:11
created

testFlushCachedShortcodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 21
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 28
rs 9.584
1
<?php
2
3
namespace SilverStripe\View\Tests\Shortcodes;
4
5
use Psr\SimpleCache\CacheInterface;
6
use SilverStripe\View\Parsers\ShortcodeParser;
7
use SilverStripe\View\Shortcodes\EmbedShortcodeProvider;
8
use SilverStripe\Dev\SapphireTest;
9
use SilverStripe\View\Tests\Embed\UnitTestEmbed;
10
11
class EmbedShortcodeProviderTest extends UnitTestEmbed
12
{
13
    public function assertEqualIgnoringWhitespace($a, $b, $message = '')
14
    {
15
        $this->assertEquals(preg_replace('/\s+/', '', $a), preg_replace('/\s+/', '', $b), $message);
16
    }
17
18
    private function getShortcodeHtml(string $url, string $firstResponse, string $secondResponse, array $arguments)
19
    {
20
        $embedContainer = $this->createEmbedContainer($url, $firstResponse, $secondResponse);
21
        return EmbedShortcodeProvider::handle_shortcode($arguments, '', null, '', ['Embeddable' => $embedContainer]);
22
    }
23
24
    public function testYoutube()
25
    {
26
        $url = 'https://www.youtube.com/watch?v=dM15HfUYwF0';
27
        $html = $this->getShortcodeHtml(
28
            $url,
29
            implode('', [
30
                '<html><link rel="alternate" type="application/json+oembed" ',
31
                'href="https://www.youtube.com/oembed?format=json&amp;',
32
                'url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%dM15HfUYwF0" ',
33
                'title="SilverStripe Platform 2 min introduction"></html>'
34
            ]),
35
            json_encode([
36
                'version' => '1.0',
37
                'provider_url' => 'https://www.youtube.com/',
38
                'title' => 'SilverStripe Platform 2 min introduction',
39
                'html' => '<iframe width="480" height="270" src="https://www.youtube.com/embed/dM15HfUYwF0?feature=oembed" frameborder="0" allowfullscreen></iframe>',
40
                'provider_name' => 'YouTube',
41
                'thumbnail_width' => 480,
42
                'type' => 'video',
43
                'thumbnail_url' => 'https://i.ytimg.com/vi/dM15HfUYwF0/hqdefault.jpg',
44
                'thumbnail_height' => 360,
45
                'width' => 480,
46
                'author_url' => 'https://www.youtube.com/user/SilverStripe',
47
                'author_name' => 'SilverStripe',
48
                'height' => 270,
49
            ]),
50
            [
51
                'url' => $url,
52
                'caption' => 'A nice video',
53
                'width' => 777,
54
                'height' => 437,
55
            ],
56
        );
57
        $this->assertEqualIgnoringWhitespace(
58
            <<<EOS
59
<div style="width: 777px;"><iframe width="777" height="437" src="https://www.youtube.com/embed/dM15HfUYwF0?feature=oembed" frameborder="0" allowfullscreen></iframe>
60
<p class="caption">A nice video</p></div>
61
EOS
62
            ,
63
            $html
64
        );
65
    }
66
67
    public function testSoundcloud()
68
    {
69
        $url = 'http://soundcloud.com/napalmrecords/delain-suckerpunch';
70
        $html = $this->getShortcodeHtml(
71
            $url,
72
            implode('', [
73
                '<html><link rel="alternate" type="text/json+oembed" ',
74
                'href="https://soundcloud.com/oembed?url=https%3A%2F%2Fsoundcloud.','com%2Fnapalmrecords%2Fdelain-suckerpunch&amp;format=json"></html>'
75
            ]),
76
            json_encode([
77
                'version' => 1,
78
                'type' => 'rich',
79
                'provider_name' => 'SoundCloud',
80
                'provider_url' => 'http://soundcloud.com',
81
                'height' => 400,
82
                'width' => '100%',
83
                'title' => 'DELAIN - Suckerpunch by Napalm Records',
84
                'description' => 'Taken from the EP "Lunar Prelude": http://shop.napalmrecords.com/delain',
85
                'thumbnail_url' => 'http://i1.sndcdn.com/artworks-000143578557-af0v6l-t500x500.jpg',
86
                '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>',
87
                'author_name' => 'Napalm Records',
88
                'author_url' => 'http://soundcloud.com/napalmrecords',
89
            ]),
90
            [
91
                'url' => $url
92
            ],
93
        );
94
        $this->assertEqualIgnoringWhitespace(
95
            <<<EOS
96
<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>
97
EOS
98
            ,
99
            $html
100
        );
101
    }
102
103
    public function testFlushCachedShortcodes()
104
    {
105
        /** @var CacheInterface $cache */
106
        $url = 'http://www.test-service.com/abc123';
107
        $content = '<p>Some content with an [embed url="' . $url . '" thumbnail="https://example.com/mythumb.jpg" ' .
108
            'class="leftAlone ss-htmleditorfield-file embed" width="480" height="270"]' . $url . '[/embed]</p>';
109
        $embedHtml = '<iframe myattr="something" />';
110
        $parser = ShortcodeParser::get('default');
111
112
        // use reflection to access private methods
113
        $provider = new EmbedShortcodeProvider();
114
        $reflector = new \ReflectionClass(EmbedShortcodeProvider::class);
115
        $method = $reflector->getMethod('getCache');
116
        $method->setAccessible(true);
117
        $cache = $method->invokeArgs($provider, []);
118
        $method = $reflector->getMethod('deriveCacheKey');
119
        $method->setAccessible(true);
120
        $class = 'leftAlone ss-htmleditorfield-file embed';
121
        $width = '480';
122
        $height = '270';
123
        $key = $method->invokeArgs($provider, [$url, $class, $width, $height]);
124
125
        // assertions
126
        $this->assertEquals('embed-shortcode-httpwwwtest-servicecomabc123-leftAloness-htmleditorfield-fileembed-480-270', $key);
127
        $cache->set($key, $embedHtml);
128
        $this->assertTrue($cache->has($key));
129
        EmbedShortcodeProvider::flushCachedShortcodes($parser, $content);
130
        $this->assertFalse($cache->has($key));
131
    }
132
}
133