Completed
Push — master ( e3eece...4cf24a )
by Damian
02:28
created

IFramePageTest::testAllowedUrls()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 41
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 41
rs 8.8571
cc 3
eloc 31
nc 4
nop 0
1
<?php
2
3
class IFramePageTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    public function setUp()
6
    {
7
        parent::setUp();
8
        Config::nest();
9
    }
10
11
    public function tearDown()
12
    {
13
        Config::unnest();
14
        parent::tearDown();
15
    }
16
17
    public function testGetClass()
18
    {
19
        $iframe = new IFramePage();
20
        $iframe->AutoHeight = 1;
21
        $iframe->getClass();
0 ignored issues
show
Unused Code introduced by
The call to the method IFramePage::getClass() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
22
23
        $this->assertContains('iframepage-height-auto', $iframe->getClass());
24
25
        $iframe->AutoHeight = 0;
26
        $iframe->getClass();
0 ignored issues
show
Unused Code introduced by
The call to the method IFramePage::getClass() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
27
28
        $this->assertNotContains('iframepage-height-auto', $iframe->getClass());
29
    }
30
31
    public function testGetStyle()
32
    {
33
        $iframe = new IFramePage();
34
35
        $iframe->FixedHeight = 0;
36
        $iframe->getStyle();
0 ignored issues
show
Unused Code introduced by
The call to the method IFramePage::getStyle() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
37
        $this->assertContains('height: 800px', $iframe->getStyle(), 'Height defaults to 800 if not set.');
38
39
        $iframe->FixedHeight = 100;
40
        $iframe->getStyle();
0 ignored issues
show
Unused Code introduced by
The call to the method IFramePage::getStyle() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
41
        $this->assertContains('height: 100px', $iframe->getStyle(), 'Fixed height is settable');
42
43
        $iframe->AutoWidth = 1;
44
        $iframe->FixedWidth = '200';
45
        $this->assertContains('width: 100%', $iframe->getStyle(), 'Auto width overrides fixed width');
46
47
        $iframe->AutoWidth = 0;
48
        $iframe->FixedWidth = '200';
49
        $this->assertContains('width: 200px', $iframe->getStyle(), 'Fixed width is settable');
50
    }
51
52
    public function testAllowedUrls()
53
    {
54
        $iframe = new IFramePage();
55
56
        $tests = array(
57
            'allowed' => array(
58
                'http://anything',
59
                'https://anything',
60
                'page',
61
                'sub-page/link',
62
                'page/link',
63
                'page.html',
64
                'page.htm',
65
                'page.phpissoawesomewhywouldiuseanythingelse',
66
                '//url.com/page',
67
                '/root/page/link',
68
                'http://intranet:8888',
69
                'http://javascript:8080',
70
                'http://username:password@hostname/path?arg=value#anchor'
71
            ),
72
            'banned' => array(
73
                'javascript:alert',
74
                'tel:0210001234',
75
                'ftp://url',
76
                'ssh://1.2.3.4',
77
                'ssh://url.com/page'
78
            )
79
        );
80
81
        foreach ($tests['allowed'] as $url) {
82
            $iframe->IFrameURL = $url;
83
            $iframe->write();
84
            $this->assertContains($iframe->IFrameURL, $url);
85
        }
86
87
        foreach ($tests['banned'] as $url) {
88
            $iframe->IFrameURL = $url;
89
            $this->setExpectedException('ValidationException');
0 ignored issues
show
Bug introduced by
The method setExpectedException() does not seem to exist on object<IFramePageTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
            $iframe->write();
91
        }
92
    }
93
94
    public function testForceProtocol()
0 ignored issues
show
Coding Style introduced by
testForceProtocol uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
95
    {
96
        $origServer = $_SERVER;
97
98
        $page = new IFramePage();
99
        $page->URLSegment = 'iframe';
100
        $page->IFrameURL = 'http://target.com';
101
102
        Config::inst()->update('Director', 'alternate_protocol', 'http');
103
        Config::inst()->update('Director', 'alternate_base_url', 'http://host.com');
104
        $page->ForceProtocol = '';
105
        $controller = new IFramePage_Controller($page);
106
        $response = $controller->init();
107
        $this->assertNull($response);
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<IFramePageTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
109
        Config::inst()->update('Director', 'alternate_protocol', 'https');
110
        Config::inst()->update('Director', 'alternate_base_url', 'https://host.com');
111
        $page->ForceProtocol = '';
112
        $controller = new IFramePage_Controller($page);
113
        $response = $controller->init();
114
        $this->assertNull($response);
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<IFramePageTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
116
        Config::inst()->update('Director', 'alternate_protocol', 'http');
117
        Config::inst()->update('Director', 'alternate_base_url', 'http://host.com');
118
        $page->ForceProtocol = 'http://';
119
        $controller = new IFramePage_Controller($page);
120
        $response = $controller->init();
121
        $this->assertNull($response);
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<IFramePageTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
123
        Config::inst()->update('Director', 'alternate_protocol', 'http');
124
        Config::inst()->update('Director', 'alternate_base_url', 'http://host.com');
125
        $page->ForceProtocol = 'https://';
126
        $controller = new IFramePage_Controller($page);
127
        $response = $controller->init();
128
        $this->assertEquals($response->getHeader('Location'), 'https://host.com/iframe/');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<IFramePageTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
130
        Config::inst()->update('Director', 'alternate_protocol', 'https');
131
        Config::inst()->update('Director', 'alternate_base_url', 'https://host.com');
132
        $page->ForceProtocol = 'http://';
133
        $controller = new IFramePage_Controller($page);
134
        $response = $controller->init();
135
        $this->assertEquals($response->getHeader('Location'), 'http://host.com/iframe/');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<IFramePageTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
136
137
        $_SERVER = $origServer;
138
    }
139
}
140