Passed
Pull Request — 4 (#10244)
by Steve
15:32
created

EmbedContainerTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 76
dl 0
loc 129
rs 10
c 1
b 0
f 0
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testValidate() 0 6 1
A testGetType() 0 30 1
A testGetName() 0 4 1
A testGetPreviewURL() 0 7 2
A testOptions() 0 7 1
A testGetExtractor() 0 6 1
A testGetDimensions() 0 8 1
A getEmbedContainer() 0 31 2
A getFallbackEmbedContainer() 0 3 1
A testCrawler() 0 5 1
1
<?php
2
3
namespace SilverStripe\View\Tests\Embed;
4
5
use Embed\Extractor;
6
use Embed\Http\Crawler;
7
use SilverStripe\View\Embed\EmbedContainer;
8
use SilverStripe\AssetAdmin\Controller\AssetAdmin;
0 ignored issues
show
Bug introduced by
The type SilverStripe\AssetAdmin\Controller\AssetAdmin was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
class EmbedContainerTest extends EmbedUnitTest
11
{
12
    public function testGetDimensions()
13
    {
14
        $container = $this->getEmbedContainer();
15
        $this->assertSame(480, $container->getWidth());
16
        $this->assertSame(270, $container->getHeight());
17
        $container = $this->getFallbackEmbedContainer();
18
        $this->assertSame(100, $container->getWidth());
19
        $this->assertSame(100, $container->getHeight());
20
    }
21
22
    public function testGetPreviewURL()
23
    {
24
        $container = $this->getEmbedContainer();
25
        $this->assertSame('https://www.youtube.com/watch?v=iRXJXaLV0n4', $container->getPreviewURL());
26
        $container = $this->getFallbackEmbedContainer();
27
        if (class_exists(AssetAdmin::class)) {
28
            $this->assertStringContainsString('client/dist/images/icon_file.png', $container->getPreviewURL());
29
        }
30
    }
31
32
    public function testGetName()
33
    {
34
        $container = $this->getEmbedContainer();
35
        $this->assertSame('Try to stay SERIOUS -The most popular CAT videos', $container->getName());
36
    }
37
38
    public function testGetType()
39
    {
40
        $container = $this->getEmbedContainer();
41
        $this->assertSame('rich', $container->getType());
42
        $container = $this->getEmbedContainer(
43
            <<<EOT
44
            <video width="320" height="240" controls>
45
                <source src="movie.ogg" type="video/ogg">
46
                Your browser does not support the video tag.
47
            </video> 
48
            EOT
49
        );
50
        $this->assertSame('video', $container->getType());
51
        $container = $this->getEmbedContainer(
52
            <<<EOT
53
            <audio controls>
54
                <source src="horse.ogg" type="audio/ogg">
55
                Your browser does not support the audio element.
56
            </audio> 
57
            EOT
58
        );
59
        $this->assertSame('audio', $container->getType());
60
        $container = $this->getEmbedContainer(
61
            <<<EOT
62
            <a data-flickr-embed="true" href="https://www.flickr.com/photos/philocycler/32119532132/"><img src="https://live.staticflickr.com/759/32119532132_50c3f7933f_b.jpg" width="1024" height="742" alt="bird"></a>
63
            EOT
64
        );
65
        $this->assertSame('photo', $container->getType());
66
        $container = $this->getEmbedContainer('<p>Lorem ipsum</p>');
67
        $this->assertSame('link', $container->getType());
68
    }
69
70
    public function testValidate()
71
    {
72
        $container = $this->getEmbedContainer();
73
        $this->assertTrue($container->validate());
74
        $container = $this->getFallbackEmbedContainer();
75
        $this->assertFalse($container->validate());
76
    }
77
78
79
    public function testOptions()
80
    {
81
        $options = ['foo' => 'bar'];
82
        $container = $this->getEmbedContainer();
83
        $this->assertSame([], $container->getOptions());
84
        $container->setOptions($options);
85
        $this->assertSame($options, $container->getOptions());
86
    }
87
88
    public function testGetExtractor()
89
    {
90
        $container = $this->getEmbedContainer();
91
        $extractor = $container->getExtractor();
92
        $this->assertTrue($extractor instanceof Extractor);
93
        $this->assertSame('Try to stay SERIOUS -The most popular CAT videos', $extractor->title);
94
    }
95
96
    public function testCrawler()
97
    {
98
        $container = $this->getEmbedContainer();
99
        $crawler = new Crawler();
0 ignored issues
show
Unused Code introduced by
The assignment to $crawler is dead and can be removed.
Loading history...
100
        $this->assertTrue($container->getCrawler() instanceof Crawler);
101
    }
102
103
    private function getFallbackEmbedContainer()
104
    {
105
        return $this->createEmbedContainer('', '', '', '');
106
    }
107
108
    private function getEmbedContainer(string $htmlOverride = '')
109
    {
110
        $html = $htmlOverride ?: implode('', [
111
            '<iframe width="480" height="270" src="https://www.youtube.com/embed/iRXJXaLV0n4?feature=oembed" ',
112
            'frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>'
113
        ]);
114
        $url = 'https://www.youtube.com/watch?v=iRXJXaLV0n4';
115
        return $this->createEmbedContainer(
116
            $url,
117
            $url,
118
            implode('', [
119
                '<html><link rel="alternate" type="application/json+oembed" ',
120
                'href="https://www.youtube.com/oembed?format=json&amp;',
121
                'url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DiRXJXaLV0n4" ',
122
                'title="Try to stay SERIOUS -The most popular CAT videos"></html>'
123
            ]),
124
            json_encode([
125
                'author_url' => 'https://www.youtube.com/channel/UCR2KG2dK1tAkwZZjm7rAiSg',
126
                'thumbnail_width' => 480,
127
                'title' => 'Try to stay SERIOUS -The most popular CAT videos',
128
                'width' => 480,
129
                'provider_name' => 'YouTube',
130
                'author_name' => 'Tiger Funnies',
131
                'height' => 270,
132
                'version' => '1.0',
133
                'type' => 'video',
134
                // phpcs:ignore
135
                'html' => $html,
136
                'provider_url' => 'https://www.youtube.com/',
137
                'thumbnail_height' => 360,
138
                'thumbnail_url' => 'https://i.ytimg.com/vi/iRXJXaLV0n4/hqdefault.jpg',
139
            ])
140
        );
141
    }
142
}
143