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

EmbedShortcodeProviderTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 115
dl 0
loc 177
rs 10
c 1
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A assertEqualIgnoringWhitespace() 0 3 1
A testFlickr() 0 25 1
A getShortcodeHtml() 0 11 1
A testFlushCachedShortcodes() 0 28 1
A testVimeo() 0 21 1
A testYoutube() 0 24 1
A testSoundcloud() 0 21 1
A testAudio() 0 23 1
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\EmbedUnitTest;
10
11
class EmbedShortcodeProviderTest extends EmbedUnitTest
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(
19
        string $urlA,
20
        string $urlB,
21
        string $firstResponse,
22
        string $secondResponse,
23
        array $arguments
24
    ): string {
25
        $firstResponse = str_replace("\n", '', $firstResponse);
26
        $secondResponse = str_replace("\n", '', $secondResponse);
27
        $embedContainer = $this->createEmbedContainer($urlA, $urlB, $firstResponse, $secondResponse);
28
        return EmbedShortcodeProvider::handle_shortcode($arguments, '', null, '', ['Embeddable' => $embedContainer]);
29
    }
30
31
    public function testYoutube()
32
    {
33
        $url = 'https://www.youtube.com/watch?v=dM15HfUYwF0';
34
        $html = $this->getShortcodeHtml(
35
            $url,
36
            $url,
37
            <<<EOT
38
            <link rel="alternate" type="application/json+oembed" href="https://www.youtube.com/oembed?format=json&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Da2tDOYkFCYo" title="The flying car completes first ever inter-city flight (Official Video)">
39
            EOT,
40
            <<<EOT
41
            {"title":"The flying car completes first ever inter-city flight (Official Video)","author_name":"KleinVision","author_url":"https://www.youtube.com/channel/UCCHAHvcO7KSNmgXVRIJLNkw","type":"video","height":113,"width":200,"version":"1.0","provider_name":"YouTube","provider_url":"https://www.youtube.com/","thumbnail_height":360,"thumbnail_width":480,"thumbnail_url":"https://i.ytimg.com/vi/a2tDOYkFCYo/hqdefault.jpg","html":"\u003ciframe width=\u0022200\u0022 height=\u0022113\u0022 src=\u0022https://www.youtube.com/embed/a2tDOYkFCYo?feature=oembed\u0022 frameborder=\u00220\u0022 allow=\u0022accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\u0022 allowfullscreen\u003e\u003c/iframe\u003e"}
42
            EOT,
43
            [
44
                'url' => $url,
45
                'caption' => 'A nice video',
46
                'width' => 777,
47
                'height' => 437,
48
            ],
49
        );
50
        $this->assertEqualIgnoringWhitespace(
51
            <<<EOT
52
            <div style="width:777px;"><iframe width="777" height="437" src="https://www.youtube.com/embed/a2tDOYkFCYo?feature=oembed" frameborder="0" allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture" allowfullscreen></iframe><p class="caption">A nice video</p></div>
53
            EOT,
54
            $html
55
        );
56
    }
57
58
    public function testSoundcloud()
59
    {
60
        $url = 'https://soundcloud.com/napalmrecords/delain-suckerpunch';
61
        $html = $this->getShortcodeHtml(
62
            $url,
63
            $url,
64
            <<<EOT
65
            <link rel="alternate" type="text/json+oembed" href="https://soundcloud.com/oembed?url=https%3A%2F%2Fsoundcloud.com%2Fnapalmrecords%2Fdelain-suckerpunch&amp;format=json">
66
            EOT,
67
            <<<EOT
68
            {"version":1.0,"type":"rich","provider_name":"SoundCloud","provider_url":"https://soundcloud.com","height":400,"width":"100%","title":"DELAIN - Suckerpunch by Napalm Records","description":"Taken from the EP \"Lunar Prelude\": https://shop.napalmrecords.com/delain","thumbnail_url":"https://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=https%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F242518079&show_artwork=true\"></iframe>","author_name":"Napalm Records","author_url":"https://soundcloud.com/napalmrecords"}
69
            EOT,
70
            [
71
                'url' => $url
72
            ],
73
        );
74
        $this->assertEqualIgnoringWhitespace(
75
            <<<EOT
76
            <div style="width:100px;"><iframe width="100%" height="400" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?visual=true&url=https%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F242518079&show_artwork=true"></iframe></div>
77
            EOT,
78
            $html
79
        );
80
    }
81
82
    public function testVimeo()
83
    {
84
        $url = 'https://vimeo.com/680885625';
85
        $html = $this->getShortcodeHtml(
86
            $url,
87
            $url,
88
            <<<EOT
89
            <link rel="alternate" href="https://vimeo.com/api/oembed.json?url=https%3A%2F%2Fvimeo.com%2F680885625%3Fh%3D0cadf1a475" type="application/json+oembed" title="Mount Rainier National Park - 2021 - Episode 01">
90
            EOT,
91
            <<<EOT
92
            {"type":"video","version":"1.0","provider_name":"Vimeo","provider_url":"https:\/\/vimeo.com\/","title":"Mount Rainier National Park - 2021 - Episode 01","author_name":"Altered Stag Productions","author_url":"https:\/\/vimeo.com\/alteredstag","is_plus":"0","account_type":"pro","html":"<iframe src=\"https:\/\/player.vimeo.com\/video\/680885625?h=0cadf1a475&amp;app_id=122963\" width=\"640\" height=\"360\" frameborder=\"0\" allow=\"autoplay; fullscreen; picture-in-picture\" allowfullscreen title=\"Mount Rainier National Park - 2021 - Episode 01\"><\/iframe>","width":640,"height":360,"duration":60,"description":"Mount Rainier was the first national park I ever visited so it was definitely exciting to be back with refined skills and better equipment. Here is a quick cap of the trip with more segments on the way.\n\nSong: And What Now of the Birds for Ben by David Jennings - March 3, 2021.","thumbnail_url":"https:\/\/i.vimeocdn.com\/video\/1380153025-d3b1840ae521cd936bdaaafaef280b9c0634e729c6b09bca7767792b553a5220-d_640","thumbnail_width":640,"thumbnail_height":360,"thumbnail_url_with_play_button":"https:\/\/i.vimeocdn.com\/filter\/overlay?src0=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F1380153025-d3b1840ae521cd936bdaaafaef280b9c0634e729c6b09bca7767792b553a5220-d_640&src1=http%3A%2F%2Ff.vimeocdn.com%2Fp%2Fimages%2Fcrawler_play.png","upload_date":"2022-02-23 08:54:15","video_id":680885625,"uri":"\/videos\/680885625"}
93
            EOT,
94
            [
95
                'url' => $url
96
            ],
97
        );
98
        $this->assertEqualIgnoringWhitespace(
99
            <<<EOT
100
            <div style="width: 640px;"><iframe src="https://player.vimeo.com/video/680885625?h=0cadf1a475&amp;app_id=122963" width="640" height="360" frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen title="Mount Rainier National Park - 2021 - Episode 01"></iframe></div>
101
            EOT,
102
            $html
103
        );
104
    }
105
106
    public function testFlickr()
107
    {
108
        $urlA = 'https://www.flickr.com/photos/philocycler/32119532132/in/photolist-QWhZSL-DFFK9V-JcDYRD-S5ksMB-KPznfz-dT81te-2aqUUb1-Gur1ok-cgfEL1-dUu2Cv-8iqmZ9-z5ktAq-z5mCCE-9FmXnE-UH4Y1d-VZsXJn-22zGNHz-e1mzTR-22uVLSo-VJJWsE-VJJJQG-8in8np-agL5ae-9KKkAe-29if7Rt';
109
        $urlB = 'https://live.staticflickr.com/759/32119532132_50c3f7933f_b.jpg';
110
        $html = $this->getShortcodeHtml(
111
            $urlA,
112
            $urlB,
113
            <<<EOT
114
            <link   rel="alternative" type="application/json+oembed"  href="https://www.flickr.com/services/oembed?url&#x3D;https://www.flickr.com/photos/philocycler/32119532132&amp;format&#x3D;json" data-dynamic-added-by="bb44774707b5780000000000000000000000000000000" data-dynamic="true" />
115
            EOT,
116
            <<<EOT
117
            {"type":"photo","flickr_type":"photo","title":"bird","author_name":"Philocycler","author_url":"https:\/\/www.flickr.com\/photos\/philocycler\/","width":1024,"height":742,"url":"https:\/\/live.staticflickr.com\/759\/32119532132_50c3f7933f_b.jpg","web_page":"https:\/\/www.flickr.com\/photos\/philocycler\/32119532132\/","thumbnail_url":"https:\/\/live.staticflickr.com\/759\/32119532132_50c3f7933f_q.jpg","thumbnail_width":150,"thumbnail_height":150,"web_page_short_url":"https:\/\/flic.kr\/p\/QWhZSL","license":"All Rights Reserved","license_id":0,"html":"<a data-flickr-embed=\"true\" href=\"https:\/\/www.flickr.com\/photos\/philocycler\/32119532132\/\" title=\"bird by Philocycler, on Flickr\"><img src=\"https:\/\/live.staticflickr.com\/759\/32119532132_50c3f7933f_b.jpg\" width=\"1024\" height=\"742\" alt=\"bird\"><\/a><script async src=\"https:\/\/embedr.flickr.com\/assets\/client-code.js\" charset=\"utf-8\"><\/script>","version":"1.0","cache_age":3600,"provider_name":"Flickr","provider_url":"https:\/\/www.flickr.com\/"}
118
            EOT,
119
            [
120
                'url' => $urlB,
121
                'width' => 1024,
122
                'height' => 742,
123
                'caption' => 'Birdy'
124
            ],
125
        );
126
        $this->assertEqualIgnoringWhitespace(
127
            <<<EOT
128
            <div style="width:1024px;"><a data-flickr-embed="true" href="https://www.flickr.com/photos/philocycler/32119532132/" title="birdbyPhilocycler,onFlickr"><img src="https://live.staticflickr.com/759/32119532132_50c3f7933f_b.jpg" width="1024" height="742" alt="bird"></a><script asyncsrc="https://embedr.flickr.com/assets/client-code.js" charset="utf-8"></script><p class="caption">Birdy</p></div>
129
            EOT,
130
            $html
131
        );
132
    }
133
134
    public function testAudio()
135
    {
136
        // not implemented in Silerstripe so will fallback to a link to $urlA
137
        $urlA = 'https://www.someaudioplace.com/12345';
138
        $urlB = 'https://www.someaudioplace.com/listen/12345';
139
        $html = $this->getShortcodeHtml(
140
            $urlA,
141
            $urlB,
142
            <<<EOT
143
            <link rel="alternative" type="application/json+oembed"  href="https://www.someaudioplace.com/oembed?a=12345" data-dynamic="true" />
144
            EOT,
145
            <<<EOT
146
            {"type":"audio","title":"Some music","author_name":"bob","html":"<audio controls><source src="https://www.someaudioplace.com/listen/12345" type="audio/ogg"></audio>"}
147
            EOT,
148
            [
149
                'url' => $urlB,
150
            ],
151
        );
152
        $this->assertEqualIgnoringWhitespace(
153
            <<<EOT
154
            <a href="https://www.someaudioplace.com/12345"></a>
155
            EOT,
156
            $html
157
        );
158
    }
159
160
    public function testFlushCachedShortcodes()
161
    {
162
        /** @var CacheInterface $cache */
163
        $url = 'http://www.test-service.com/abc123';
164
        $content = '<p>Some content with an [embed url="' . $url . '" thumbnail="https://example.com/mythumb.jpg" ' .
165
            'class="leftAlone ss-htmleditorfield-file embed" width="480" height="270"]' . $url . '[/embed]</p>';
166
        $embedHtml = '<iframe myattr="something" />';
167
        $parser = ShortcodeParser::get('default');
168
169
        // use reflection to access private methods
170
        $provider = new EmbedShortcodeProvider();
171
        $reflector = new \ReflectionClass(EmbedShortcodeProvider::class);
172
        $method = $reflector->getMethod('getCache');
173
        $method->setAccessible(true);
174
        $cache = $method->invokeArgs($provider, []);
175
        $method = $reflector->getMethod('deriveCacheKey');
176
        $method->setAccessible(true);
177
        $class = 'leftAlone ss-htmleditorfield-file embed';
178
        $width = '480';
179
        $height = '270';
180
        $key = $method->invokeArgs($provider, [$url, $class, $width, $height]);
181
182
        // assertions
183
        $this->assertEquals('embed-shortcode-httpwwwtest-servicecomabc123-leftAloness-htmleditorfield-fileembed-480-270', $key);
184
        $cache->set($key, $embedHtml);
185
        $this->assertTrue($cache->has($key));
186
        EmbedShortcodeProvider::flushCachedShortcodes($parser, $content);
187
        $this->assertFalse($cache->has($key));
188
    }
189
}
190