silverstripe-iframe/tests/IFramePageTest.php

144 lines
5.0 KiB
PHP
Raw Permalink Normal View History

<?php
2019-11-18 02:47:08 +01:00
namespace SilverStripe\IFrame\Tests;
use SilverStripe\Core\Config\Config;
use SilverStripe\Control\Director;
use SilverStripe\ORM\ValidationException;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\IFrame\IFramePage;
use SilverStripe\IFrame\IFramePageController;
2015-11-18 04:57:44 +01:00
class IFramePageTest extends SapphireTest
{
protected $usesDatabase = true;
2015-11-18 04:57:44 +01:00
public function testGetClass()
{
$iframe = new IFramePage();
$iframe->AutoHeight = 1;
$iframe->getClass();
2021-10-27 07:09:34 +02:00
$this->assertStringContainsString('iframepage-height-auto', $iframe->getClass());
2015-11-18 04:57:44 +01:00
$iframe->AutoHeight = 0;
$iframe->getClass();
2021-10-27 07:09:34 +02:00
$this->assertStringNotContainsString('iframepage-height-auto', $iframe->getClass());
2015-11-18 04:57:44 +01:00
}
public function testGetStyle()
{
$iframe = new IFramePage();
$iframe->FixedHeight = 0;
$iframe->getStyle();
2021-10-27 07:09:34 +02:00
$this->assertStringContainsString('height: 800px', $iframe->getStyle(), 'Height defaults to 800 if not set.');
2015-11-18 04:57:44 +01:00
$iframe->FixedHeight = 100;
$iframe->getStyle();
2021-10-27 07:09:34 +02:00
$this->assertStringContainsString('height: 100px', $iframe->getStyle(), 'Fixed height is settable');
2015-11-18 04:57:44 +01:00
$iframe->AutoWidth = 1;
$iframe->FixedWidth = '200';
2021-10-27 07:09:34 +02:00
$this->assertStringContainsString('width: 100%', $iframe->getStyle(), 'Auto width overrides fixed width');
2015-11-18 04:57:44 +01:00
$iframe->AutoWidth = 0;
$iframe->FixedWidth = '200';
2021-10-27 07:09:34 +02:00
$this->assertStringContainsString('width: 200px', $iframe->getStyle(), 'Fixed width is settable');
2015-11-18 04:57:44 +01:00
}
public function testAllowedUrls()
{
$iframe = new IFramePage();
$tests = array(
'allowed' => array(
'http://anything',
'https://anything',
'page',
'sub-page/link',
'page/link',
'page.html',
'page.htm',
'page.phpissoawesomewhywouldiuseanythingelse',
'//url.com/page',
'/root/page/link',
'http://intranet:8888',
'http://javascript:8080',
'http://username:password@hostname/path?arg=value#anchor'
),
'banned' => array(
'javascript:alert',
'tel:0210001234',
'ftp://url',
'ssh://1.2.3.4',
'ssh://url.com/page'
)
);
foreach ($tests['allowed'] as $url) {
$iframe->IFrameURL = $url;
$iframe->write();
2021-10-27 07:09:34 +02:00
$this->assertStringContainsString($iframe->IFrameURL, $url);
2015-11-18 04:57:44 +01:00
}
foreach ($tests['banned'] as $url) {
$iframe->IFrameURL = $url;
2021-10-27 07:09:34 +02:00
$this->expectException(ValidationException::class);
2015-11-18 04:57:44 +01:00
$iframe->write();
}
}
public function testForceProtocol()
{
$origServer = $_SERVER;
$page = new IFramePage();
$page->URLSegment = 'iframe';
$page->IFrameURL = 'http://target.com';
Config::modify()->set(Director::class, 'alternate_protocol', 'http');
Config::modify()->set(Director::class, 'alternate_base_url', 'http://host.com');
2015-11-18 04:57:44 +01:00
$page->ForceProtocol = '';
$controller = new IFramePageController($page);
$controller->doInit();
$response = $controller->getResponse();
$this->assertNull($response->getHeader('Location'));
2015-11-18 04:57:44 +01:00
Config::modify()->set(Director::class, 'alternate_protocol', 'https');
Config::modify()->set(Director::class, 'alternate_base_url', 'https://host.com');
2015-11-18 04:57:44 +01:00
$page->ForceProtocol = '';
$controller = new IFramePageController($page);
$controller->doInit();
$response = $controller->getResponse();
$this->assertNull($response->getHeader('Location'));
2015-11-18 04:57:44 +01:00
Config::modify()->set(Director::class, 'alternate_protocol', 'http');
Config::modify()->set(Director::class, 'alternate_base_url', 'http://host.com');
2015-11-18 04:57:44 +01:00
$page->ForceProtocol = 'http://';
$controller = new IFramePageController($page);
$controller->doInit();
$response = $controller->getResponse();
$this->assertNull($response->getHeader('Location'));
2015-11-18 04:57:44 +01:00
Config::modify()->set(Director::class, 'alternate_protocol', 'http');
Config::modify()->set(Director::class, 'alternate_base_url', 'http://host.com');
2015-11-18 04:57:44 +01:00
$page->ForceProtocol = 'https://';
$controller = new IFramePageController($page);
$controller->doInit();
$response = $controller->getResponse();
2015-11-18 04:57:44 +01:00
$this->assertEquals($response->getHeader('Location'), 'https://host.com/iframe/');
Config::modify()->set(Director::class, 'alternate_protocol', 'https');
Config::modify()->set(Director::class, 'alternate_base_url', 'https://host.com');
2015-11-18 04:57:44 +01:00
$page->ForceProtocol = 'http://';
$controller = new IFramePageController($page);
$controller->doInit();
$response = $controller->getResponse();
2015-11-18 04:57:44 +01:00
$this->assertEquals($response->getHeader('Location'), 'http://host.com/iframe/');
$_SERVER = $origServer;
}
}