RemoteFileFormFactoryTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 61
c 4
b 0
f 0
dl 0
loc 149
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A providerTestRejectedURLs() 0 42 1
A providerTestAcceptedURLs() 0 36 1
A setUp() 0 16 1
A testRejectedURLS() 0 14 2
A testAcceptedURLs() 0 13 2
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Tests\Forms;
4
5
use Embed\Exceptions\InvalidUrlException;
6
use SilverStripe\AssetAdmin\Forms\RemoteFileFormFactory;
7
use SilverStripe\AssetAdmin\Tests\Forms\RemoteFileFormFactoryTest\MockEmbed;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\Forms\Form;
11
use SilverStripe\View\Embed\Embeddable;
12
13
class RemoteFileFormFactoryTest extends SapphireTest
14
{
15
16
    protected function setUp() : void
17
    {
18
        parent::setUp();
19
20
        // Reset all configs
21
        RemoteFileFormFactory::config()->set('fileurl_scheme_whitelist', []);
22
        RemoteFileFormFactory::config()->set('fileurl_scheme_blacklist', []);
23
        RemoteFileFormFactory::config()->set('fileurl_port_whitelist', []);
24
        RemoteFileFormFactory::config()->set('fileurl_port_blacklist', []);
25
        RemoteFileFormFactory::config()->set('fileurl_domain_whitelist', []);
26
        RemoteFileFormFactory::config()->set('fileurl_domain_blacklist', []);
27
28
        // mock embed
29
        Injector::inst()->load([
30
            Embeddable::class => [
31
                'class' => MockEmbed::class,
32
            ]
33
        ]);
34
    }
35
36
    public function providerTestAcceptedURLs()
37
    {
38
        return [
39
            [
40
                ['fileurl_scheme_blacklist' => ['https']],
41
                'http://www.google.com',
42
            ],
43
            [
44
                ['fileurl_scheme_whitelist' => ['http']],
45
                'http://www.google.com',
46
            ],
47
            [
48
                ['fileurl_domain_blacklist' => ['www.amazon.com']],
49
                'http://www.google.com',
50
            ],
51
            [
52
                ['fileurl_domain_whitelist' => ['www.google.com']],
53
                'http://www.google.com',
54
            ],
55
            [
56
                # port-ommitted urls ignored. Needs schema blacklist also
57
                ['fileurl_port_blacklist' => [80]],
58
                'http://www.google.com',
59
            ],
60
            [
61
                # port-ommitted urls ignored
62
                ['fileurl_port_whitelist' => [80]],
63
                'http://www.google.com',
64
            ],
65
            [
66
                ['fileurl_port_blacklist' => [443]],
67
                'http://www.google.com:80',
68
            ],
69
            [
70
                ['fileurl_port_whitelist' => [443]],
71
                'https://www.google.com:443',
72
            ],
73
        ];
74
    }
75
76
    /**
77
     * @dataProvider providerTestAcceptedURLs
78
     * @param array $config Config to merge
79
     * @param string $acceptedURL OK url
80
     */
81
    public function testAcceptedURLs($config, $acceptedURL)
82
    {
83
        foreach ($config as $key => $value) {
84
            RemoteFileFormFactory::config()->set($key, $value);
85
        }
86
87
        // Should pass
88
        $builder = new RemoteFileFormFactory();
89
        $fields = $builder->getForm(null, 'Form', [
90
            'type' => 'edit',
91
            'url' => $acceptedURL,
92
        ]);
93
        $this->assertInstanceOf(Form::class, $fields);
94
    }
95
96
    public function providerTestRejectedURLs()
97
    {
98
        return [
99
            [
100
                ['fileurl_scheme_blacklist' => ['https']],
101
                'https://www.google.com'
102
            ],
103
            [
104
                ['fileurl_scheme_whitelist' => ['http']],
105
                'https://www.google.com'
106
            ],
107
            [
108
                ['fileurl_domain_blacklist' => ['www.amazon.com']],
109
                'http://www.amazon.com'
110
            ],
111
            [
112
                ['fileurl_domain_whitelist' => ['www.google.com']],
113
                'http://www.amazon.com'
114
            ],
115
            [
116
                # ipv4 blacklist
117
                ['fileurl_domain_blacklist' => ['127.0.0.1']],
118
                'http://127.0.0.1/'
119
            ],
120
            [
121
                # ipv6 blacklist
122
                ['fileurl_domain_blacklist' => ['[0:0:0:0:0:0:0:1]']],
123
                'http://[0:0:0:0:0:0:0:1]/'
124
            ],
125
            [
126
                # ipv6 blacklist
127
                ['fileurl_domain_blacklist' => ['[::1]']],
128
                'http://[::1]/'
129
            ],
130
            [
131
                ['fileurl_port_blacklist' => [80]],
132
                'http://www.google.com:80'
133
            ],
134
            [
135
                # port-ommitted urls ignored
136
                ['fileurl_port_whitelist' => [80]],
137
                'http://www.google.com:443'
138
            ],
139
        ];
140
    }
141
142
    /**
143
     * @dataProvider providerTestRejectedURLs
144
     * @param array $config Config to merge
145
     * @param string $rejectedURL rejected url
146
     * @param string $rejectedMessage
147
     */
148
    public function testRejectedURLS($config, $rejectedURL)
149
    {
150
        $this->expectException(InvalidUrlException::class);
151
152
        // Set config
153
        foreach ($config as $key => $value) {
154
            RemoteFileFormFactory::config()->set($key, $value);
155
        }
156
157
        // Should throw error
158
        $builder = new RemoteFileFormFactory();
159
        $builder->getForm(null, 'Form', [
160
            'type' => 'edit',
161
            'url' => $rejectedURL,
162
        ]);
163
    }
164
}
165