Completed
Push — master ( df6bd2...c3da4e )
by Robbie
12s
created

IFramePageTest::testForceProtocol()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 40
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 49
rs 9.2258
1
<?php
2
namespace SilverStripe\IFrame\Tests;
3
4
use SilverStripe\Core\Config\Config;
5
use SilverStripe\Control\Director;
6
use SilverStripe\ORM\ValidationException;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\IFrame\IFramePage;
9
use SilverStripe\IFrame\IFramePageController;
10
11
class IFramePageTest extends SapphireTest
12
{
13
    protected $usesDatabase = true;
14
15
    public function testGetClass()
16
    {
17
        $iframe = new IFramePage();
18
        $iframe->AutoHeight = 1;
19
        $iframe->getClass();
20
21
        $this->assertContains('iframepage-height-auto', $iframe->getClass());
22
23
        $iframe->AutoHeight = 0;
24
        $iframe->getClass();
25
26
        $this->assertNotContains('iframepage-height-auto', $iframe->getClass());
27
    }
28
29
    public function testGetStyle()
30
    {
31
        $iframe = new IFramePage();
32
33
        $iframe->FixedHeight = 0;
34
        $iframe->getStyle();
35
        $this->assertContains('height: 800px', $iframe->getStyle(), 'Height defaults to 800 if not set.');
36
37
        $iframe->FixedHeight = 100;
38
        $iframe->getStyle();
39
        $this->assertContains('height: 100px', $iframe->getStyle(), 'Fixed height is settable');
40
41
        $iframe->AutoWidth = 1;
42
        $iframe->FixedWidth = '200';
43
        $this->assertContains('width: 100%', $iframe->getStyle(), 'Auto width overrides fixed width');
44
45
        $iframe->AutoWidth = 0;
46
        $iframe->FixedWidth = '200';
47
        $this->assertContains('width: 200px', $iframe->getStyle(), 'Fixed width is settable');
48
    }
49
50
    public function testAllowedUrls()
51
    {
52
        $iframe = new IFramePage();
53
54
        $tests = array(
55
            'allowed' => array(
56
                'http://anything',
57
                'https://anything',
58
                'page',
59
                'sub-page/link',
60
                'page/link',
61
                'page.html',
62
                'page.htm',
63
                'page.phpissoawesomewhywouldiuseanythingelse',
64
                '//url.com/page',
65
                '/root/page/link',
66
                'http://intranet:8888',
67
                'http://javascript:8080',
68
                'http://username:password@hostname/path?arg=value#anchor'
69
            ),
70
            'banned' => array(
71
                'javascript:alert',
72
                'tel:0210001234',
73
                'ftp://url',
74
                'ssh://1.2.3.4',
75
                'ssh://url.com/page'
76
            )
77
        );
78
79
        foreach ($tests['allowed'] as $url) {
80
            $iframe->IFrameURL = $url;
81
            $iframe->write();
82
            $this->assertContains($iframe->IFrameURL, $url);
83
        }
84
85
        foreach ($tests['banned'] as $url) {
86
            $iframe->IFrameURL = $url;
87
            $this->setExpectedException(ValidationException::class);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit_Framework_TestCase::setExpectedException() has been deprecated: Method deprecated since Release 5.2.0; use expectException() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

87
            /** @scrutinizer ignore-deprecated */ $this->setExpectedException(ValidationException::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
88
            $iframe->write();
89
        }
90
    }
91
92
    public function testForceProtocol()
93
    {
94
        $origServer = $_SERVER;
95
96
        $page = new IFramePage();
97
        $page->URLSegment = 'iframe';
98
        $page->IFrameURL = 'http://target.com';
99
100
        Config::modify()->set(Director::class, 'alternate_protocol', 'http');
101
        Config::modify()->set(Director::class, 'alternate_base_url', 'http://host.com');
102
        $page->ForceProtocol = '';
103
        $controller = new IFramePageController($page);
104
        $controller->doInit();
105
        $response = $controller->getResponse();
106
        $this->assertNull($response->getHeader('Location'));
107
108
        Config::modify()->set(Director::class, 'alternate_protocol', 'https');
109
        Config::modify()->set(Director::class, 'alternate_base_url', 'https://host.com');
110
        $page->ForceProtocol = '';
111
        $controller = new IFramePageController($page);
112
        $controller->doInit();
113
        $response = $controller->getResponse();
114
        $this->assertNull($response->getHeader('Location'));
115
116
        Config::modify()->set(Director::class, 'alternate_protocol', 'http');
117
        Config::modify()->set(Director::class, 'alternate_base_url', 'http://host.com');
118
        $page->ForceProtocol = 'http://';
119
        $controller = new IFramePageController($page);
120
        $controller->doInit();
121
        $response = $controller->getResponse();
122
        $this->assertNull($response->getHeader('Location'));
123
124
        Config::modify()->set(Director::class, 'alternate_protocol', 'http');
125
        Config::modify()->set(Director::class, 'alternate_base_url', 'http://host.com');
126
        $page->ForceProtocol = 'https://';
127
        $controller = new IFramePageController($page);
128
        $controller->doInit();
129
        $response = $controller->getResponse();
130
        $this->assertEquals($response->getHeader('Location'), 'https://host.com/iframe/');
131
132
        Config::modify()->set(Director::class, 'alternate_protocol', 'https');
133
        Config::modify()->set(Director::class, 'alternate_base_url', 'https://host.com');
134
        $page->ForceProtocol = 'http://';
135
        $controller = new IFramePageController($page);
136
        $controller->doInit();
137
        $response = $controller->getResponse();
138
        $this->assertEquals($response->getHeader('Location'), 'http://host.com/iframe/');
139
140
        $_SERVER = $origServer;
141
    }
142
}
143