Completed
Push — master ( 15b4cf...ad9d4e )
by
unknown
08:03
created

EmbedShortcodeProviderTest::mockRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 2
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
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 testYoutube()
29
    {
30
        /** @var string $result */
31
        $result = $this->mockRequest(
32
            [
33
                'url' => static::$test_youtube,
34
                'caption' => 'A nice video',
35
                'width' => 480,
36
                'height' => 360,
37
            ],
38
            [
39
                'version' => '1.0',
40
                'provider_url' => 'https://www.youtube.com/',
41
                'title' => 'SilverStripe Platform 2 min introduction',
42
                'html' => '<iframe width="480" height="270" src="https://www.youtube.com/embed/dM15HfUYwF0?feature=oembed" frameborder="0" allowfullscreen></iframe>',
43
                'provider_name' => 'YouTube',
44
                'thumbnail_width' => 480,
45
                'type' => 'video',
46
                'thumbnail_url' => 'https://i.ytimg.com/vi/dM15HfUYwF0/hqdefault.jpg',
47
                'thumbnail_height' => 360,
48
                'width' => 480,
49
                'author_url' => 'https://www.youtube.com/user/SilverStripe',
50
                'author_name' => 'SilverStripe',
51
                'height' => 270,
52
            ]
53
        );
54
        $this->assertEquals(
55
            <<<EOS
56
<div style="width: 480px;"><iframe width="480" height="270" src="https://www.youtube.com/embed/dM15HfUYwF0?feature=oembed" frameborder="0" allowfullscreen></iframe>
57
<p class="caption">A nice video</p></div>
58
EOS
59
            ,
60
            $result
61
        );
62
    }
63
64
    public function testSoundcloud()
65
    {
66
        /** @var string $result */
67
        $result = $this->mockRequest(
68
            ['url' => static::$test_soundcloud],
69
            [
70
                'version' => 1,
71
                'type' => 'rich',
72
                'provider_name' => 'SoundCloud',
73
                'provider_url' => 'http://soundcloud.com',
74
                'height' => 400,
75
                'width' => '100%',
76
                'title' => 'DELAIN - Suckerpunch by Napalm Records',
77
                'description' => 'Taken from the EP "Lunar Prelude": http://shop.napalmrecords.com/delain',
78
                'thumbnail_url' => 'http://i1.sndcdn.com/artworks-000143578557-af0v6l-t500x500.jpg',
79
                '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>',
80
                'author_name' => 'Napalm Records',
81
                'author_url' => 'http://soundcloud.com/napalmrecords',
82
            ]
83
        );
84
        $this->assertEquals(
85
            <<<EOS
86
<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>
87
EOS
88
            ,
89
            $result
90
        );
91
    }
92
93
    /**
94
     * Mock an oembed request
95
     *
96
     * @param array $arguments Input arguments
97
     * @param array $response JSON response body
98
     * @return string
99
     */
100
    protected function mockRequest($arguments, $response)
101
    {
102
        return EmbedShortcodeProvider::handle_shortcode(
103
            $arguments,
104
            '',
105
            null,
106
            'embed',
107
            [
108
                'resolver' => [
109
                    'class' => MockResolver::class,
110
                    'config' => [
111
                        'expectedContent' => json_encode($response),
112
                    ],
113
                ],
114
            ]
115
        );
116
    }
117
}
118