Passed
Pull Request — master (#104)
by
unknown
02:44
created

URLSanitisationServiceTest::testTransformation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 16
rs 9.9666
1
<?php
2
3
namespace SilverStripe\StaticPublishQueue\Test;
4
5
use SilverStripe\Core\Config\Config;
6
use SilverStripe\Dev\SapphireTest;
7
use SilverStripe\StaticPublishQueue\Service\URLSanitisationService;
8
9
/**
10
 * Class URLSanitisationServiceTest
11
 *
12
 * @package SilverStripe\StaticPublishQueue\Test
13
 */
14
class URLSanitisationServiceTest extends SapphireTest
15
{
16
    /**
17
     * @param bool $formatted
18
     * @param array $urls
19
     * @param array $expected
20
     * @dataProvider formattedUrlsProvider
21
     */
22
    public function testFormatting($formatted, array $urls, array $expected)
23
    {
24
        $urlsService = URLSanitisationService::create();
25
        $urlsService->addURLs($urls);
26
        $urls = $urlsService->getURLs($formatted);
27
28
        $this->assertEquals($expected, $urls);
29
    }
30
31
    /**
32
     * @param bool $ssl
33
     * @param array $urls
34
     * @param array $expected
35
     * @dataProvider transformationUrlsProvider
36
     */
37
    public function testTransformation($ssl, array $urls, array $expected)
38
    {
39
        if ($ssl) {
40
            Config::modify()->set(URLSanitisationService::class, 'force_ssl', true);
41
        }
42
43
        // normal transformation (protocol change)
44
        $urlsService = URLSanitisationService::create();
45
        $urlsService->addURLs($urls);
46
        $urls = $urlsService->getURLs();
47
48
        $this->assertEquals($expected, $urls);
49
50
        $urlsService = URLSanitisationService::create();
51
        $urlsService->addURLs($urls);
52
        $this->assertEquals($expected, $urlsService->getURLs());
53
    }
54
55
    public function transformationUrlsProvider()
56
    {
57
        return [
58
            [
59
                // protocol change
60
                false,
61
                [
62
                    'http://some-locale/some-page/',
63
                ],
64
                [
65
                    'http://some-locale/some-page/',
66
                ],
67
            ],
68
            [
69
                // protocol change
70
                true,
71
                [
72
                    'http://some-locale/some-page/',
73
                ],
74
                [
75
                    'https://some-locale/some-page/',
76
                ],
77
            ],
78
            [
79
                // enforce trailing slash
80
                false,
81
                [
82
                    'http://some-locale/some-page',
83
                ],
84
                [
85
                    'http://some-locale/some-page/',
86
                ],
87
            ],
88
            [
89
                // enforce trailing slash
90
                true,
91
                [
92
                    'http://some-locale/some-page',
93
                ],
94
                [
95
                    'https://some-locale/some-page/',
96
                ],
97
            ],
98
        ];
99
    }
100
101
    public function formattedUrlsProvider()
102
    {
103
        return [
104
            [
105
                false,
106
                [
107
                    'http://some-locale/some-page/',
108
                    'http://some-locale/some-other-page/',
109
                ],
110
                [
111
                    'http://some-locale/some-page/',
112
                    'http://some-locale/some-other-page/',
113
                ]
114
            ],
115
            [
116
                true,
117
                [
118
                    'http://some-locale/some-page/',
119
                    'http://some-locale/some-other-page/',
120
                ],
121
                [
122
                    'http://some-locale/some-page/' => 0,
123
                    'http://some-locale/some-other-page/' => 1,
124
                ],
125
            ],
126
        ];
127
    }
128
}
129