Passed
Push — 4 ( 233e0e...9a76d4 )
by Robbie
07:47 queued 11s
created

EmbedShortcodeProviderTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
dl 0
loc 102
rs 10
c 1
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A mockRequest() 0 12 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 SilverStripe\View\Shortcodes\EmbedShortcodeProvider;
6
use SilverStripe\Dev\SapphireTest;
7
8
/**
9
 * Class EmbedShortcodeProviderTest
10
 *
11
 * Because Embed/Embed does not have a mockup, the tests have to run against a live environment.
12
 * I've tried to fix it by serializing the data to a file, but to no avail.
13
 * Any improvements on not having to call external resources are welcome.
14
 */
15
class EmbedShortcodeProviderTest extends SapphireTest
16
{
17
18
    /**
19
     * @var string test youtube. The SilverStripe Platform promotion by UncleCheese
20
     */
21
    protected static $test_youtube = 'https://www.youtube.com/watch?v=dM15HfUYwF0';
22
23
    /**
24
     * @var string test Soundcloud. One of my favorite bands, Delain, Suckerpunch.
25
     */
26
    protected static $test_soundcloud = 'http://soundcloud.com/napalmrecords/delain-suckerpunch';
27
28
    public function assertEqualIgnoringWhitespace($a, $b, $message = '')
29
    {
30
        $this->assertEquals(preg_replace('/\s+/', '', $a), preg_replace('/\s+/', '', $b), $message);
31
    }
32
33
    public function testYoutube()
34
    {
35
        /** @var string $result */
36
        $result = $this->mockRequest(
37
            [
38
                'url' => static::$test_youtube,
39
                'caption' => 'A nice video',
40
                'width' => 480,
41
                'height' => 360,
42
            ],
43
            [
44
                'version' => '1.0',
45
                'provider_url' => 'https://www.youtube.com/',
46
                'title' => 'SilverStripe Platform 2 min introduction',
47
                'html' => '<iframe width="480" height="270" src="https://www.youtube.com/embed/dM15HfUYwF0?feature=oembed" frameborder="0" allowfullscreen></iframe>',
48
                'provider_name' => 'YouTube',
49
                'thumbnail_width' => 480,
50
                'type' => 'video',
51
                'thumbnail_url' => 'https://i.ytimg.com/vi/dM15HfUYwF0/hqdefault.jpg',
52
                'thumbnail_height' => 360,
53
                'width' => 480,
54
                'author_url' => 'https://www.youtube.com/user/SilverStripe',
55
                'author_name' => 'SilverStripe',
56
                'height' => 270,
57
            ]
58
        );
59
        $this->assertEqualIgnoringWhitespace(
60
            <<<EOS
61
<div style="width: 480px;"><iframe width="480" height="270" src="https://www.youtube.com/embed/dM15HfUYwF0?feature=oembed" frameborder="0" allowfullscreen></iframe>
62
<p class="caption">A nice video</p></div>
63
EOS
64
            ,
65
            $result
66
        );
67
    }
68
69
    public function testSoundcloud()
70
    {
71
        /** @var string $result */
72
        $result = $this->mockRequest(
73
            ['url' => static::$test_soundcloud],
74
            [
75
                'version' => 1,
76
                'type' => 'rich',
77
                'provider_name' => 'SoundCloud',
78
                'provider_url' => 'http://soundcloud.com',
79
                'height' => 400,
80
                'width' => '100%',
81
                'title' => 'DELAIN - Suckerpunch by Napalm Records',
82
                'description' => 'Taken from the EP "Lunar Prelude": http://shop.napalmrecords.com/delain',
83
                'thumbnail_url' => 'http://i1.sndcdn.com/artworks-000143578557-af0v6l-t500x500.jpg',
84
                '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>',
85
                'author_name' => 'Napalm Records',
86
                'author_url' => 'http://soundcloud.com/napalmrecords',
87
            ]
88
        );
89
        $this->assertEqualIgnoringWhitespace(
90
            <<<EOS
91
<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>
92
EOS
93
            ,
94
            $result
95
        );
96
    }
97
98
    /**
99
     * Mock an oembed request
100
     *
101
     * @param array $arguments Input arguments
102
     * @param array $response JSON response body
103
     * @return string
104
     */
105
    protected function mockRequest($arguments, $response)
106
    {
107
        return EmbedShortcodeProvider::handle_shortcode(
108
            $arguments,
109
            '',
110
            null,
111
            'embed',
112
            [
113
                'resolver' => [
114
                    'class' => MockResolver::class,
115
                    'config' => [
116
                        'expectedContent' => json_encode($response),
117
                    ],
118
                ],
119
            ]
120
        );
121
    }
122
}
123