Completed
Push — master ( 32578e...c314fb )
by Damian
27s
created

EmbedShortcodeProviderTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 103
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 2

3 Methods

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