Passed
Push — pulls/manymanylist-add-callbac... ( 7e0693...0d7c5a )
by Ingo
09:03
created

EmbedShortcodeProviderTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 73
dl 0
loc 133
rs 10
c 2
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A mockRequest() 0 12 1
A testFlushCachedShortcodes() 0 25 1
A testYoutube() 0 33 1
A testSoundcloud() 0 26 1
A assertEqualIgnoringWhitespace() 0 3 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
10
/**
11
 * Class EmbedShortcodeProviderTest
12
 *
13
 * Because Embed/Embed does not have a mockup, the tests have to run against a live environment.
14
 * I've tried to fix it by serializing the data to a file, but to no avail.
15
 * Any improvements on not having to call external resources are welcome.
16
 */
17
class EmbedShortcodeProviderTest extends SapphireTest
18
{
19
20
    /**
21
     * @var string test youtube. The SilverStripe Platform promotion by UncleCheese
22
     */
23
    protected static $test_youtube = 'https://www.youtube.com/watch?v=dM15HfUYwF0';
24
25
    /**
26
     * @var string test Soundcloud. One of my favorite bands, Delain, Suckerpunch.
27
     */
28
    protected static $test_soundcloud = 'http://soundcloud.com/napalmrecords/delain-suckerpunch';
29
30
    public function assertEqualIgnoringWhitespace($a, $b, $message = '')
31
    {
32
        $this->assertEquals(preg_replace('/\s+/', '', $a), preg_replace('/\s+/', '', $b), $message);
33
    }
34
35
    public function testYoutube()
36
    {
37
        /** @var string $result */
38
        $result = $this->mockRequest(
39
            [
40
                'url' => static::$test_youtube,
41
                'caption' => 'A nice video',
42
                'width' => 777,
43
                'height' => 437,
44
            ],
45
            [
46
                'version' => '1.0',
47
                'provider_url' => 'https://www.youtube.com/',
48
                'title' => 'SilverStripe Platform 2 min introduction',
49
                'html' => '<iframe width="480" height="270" src="https://www.youtube.com/embed/dM15HfUYwF0?feature=oembed" frameborder="0" allowfullscreen></iframe>',
50
                'provider_name' => 'YouTube',
51
                'thumbnail_width' => 480,
52
                'type' => 'video',
53
                'thumbnail_url' => 'https://i.ytimg.com/vi/dM15HfUYwF0/hqdefault.jpg',
54
                'thumbnail_height' => 360,
55
                'width' => 480,
56
                'author_url' => 'https://www.youtube.com/user/SilverStripe',
57
                'author_name' => 'SilverStripe',
58
                'height' => 270,
59
            ]
60
        );
61
        $this->assertEqualIgnoringWhitespace(
62
            <<<EOS
63
<div style="width: 777px;"><iframe width="777" height="437" src="https://www.youtube.com/embed/dM15HfUYwF0?feature=oembed" frameborder="0" allowfullscreen></iframe>
64
<p class="caption">A nice video</p></div>
65
EOS
66
            ,
67
            $result
68
        );
69
    }
70
71
    public function testSoundcloud()
72
    {
73
        /** @var string $result */
74
        $result = $this->mockRequest(
75
            ['url' => static::$test_soundcloud],
76
            [
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
        $this->assertEqualIgnoringWhitespace(
92
            <<<EOS
93
<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>
94
EOS
95
            ,
96
            $result
97
        );
98
    }
99
100
    /**
101
     * Mock an oembed request
102
     *
103
     * @param array $arguments Input arguments
104
     * @param array $response JSON response body
105
     * @return string
106
     */
107
    protected function mockRequest($arguments, $response)
108
    {
109
        return EmbedShortcodeProvider::handle_shortcode(
110
            $arguments,
111
            '',
112
            null,
113
            'embed',
114
            [
115
                'resolver' => [
116
                    'class' => MockResolver::class,
117
                    'config' => [
118
                        'expectedContent' => json_encode($response),
119
                    ],
120
                ],
121
            ]
122
        );
123
    }
124
125
    public function testFlushCachedShortcodes()
126
    {
127
        /** @var CacheInterface $cache */
128
        $url = 'http://www.test-service.com/abc123';
129
        $content = '<p>Some content with an [embed url="' . $url . '" thumbnail="https://example.com/mythumb.jpg" ' .
130
            'class="leftAlone ss-htmleditorfield-file embed" width="480" height="270"]' . $url . '[/embed]</p>';
131
        $embedHtml = '<iframe myattr="something" />';
132
        $parser = ShortcodeParser::get('default');
133
134
        // use reflection to access private methods
135
        $provider = new EmbedShortcodeProvider();
136
        $reflector = new \ReflectionClass(EmbedShortcodeProvider::class);
137
        $method = $reflector->getMethod('getCache');
138
        $method->setAccessible(true);
139
        $cache = $method->invokeArgs($provider, []);
140
        $method = $reflector->getMethod('deriveCacheKey');
141
        $method->setAccessible(true);
142
        $key = $method->invokeArgs($provider, [$url]);
143
144
        // assertions
145
        $this->assertEquals('embed-shortcode-httpwwwtest-servicecomabc123', $key);
146
        $cache->set($key, $embedHtml);
147
        $this->assertTrue($cache->has($key));
148
        EmbedShortcodeProvider::flushCachedShortcodes($parser, $content);
149
        $this->assertFalse($cache->has($key));
150
    }
151
}
152