2016-01-26 06:38:42 +01:00
|
|
|
<?php
|
|
|
|
|
2016-06-16 06:57:19 +02:00
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Assets\File;
|
|
|
|
use SilverStripe\Control\Session;
|
|
|
|
use SilverStripe\View\Parsers\ShortcodeParser;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-11-15 06:06:31 +01:00
|
|
|
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore;
|
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
class ErrorPageFileExtensionTest extends SapphireTest
|
|
|
|
{
|
2016-08-23 04:36:06 +02:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
protected static $fixture_file = 'ErrorPageTest.yml';
|
2016-01-26 06:38:42 +01:00
|
|
|
|
|
|
|
protected $versionedMode = null;
|
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
public function setUp()
|
|
|
|
{
|
2016-01-26 06:38:42 +01:00
|
|
|
parent::setUp();
|
|
|
|
$this->versionedMode = Versioned::get_reading_mode();
|
2016-03-17 01:02:50 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2016-11-15 06:06:31 +01:00
|
|
|
TestAssetStore::activate('ErrorPageFileExtensionTest');
|
2016-01-26 06:38:42 +01:00
|
|
|
$file = new File();
|
|
|
|
$file->setFromString('dummy', 'dummy.txt');
|
|
|
|
$file->write();
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
public function tearDown()
|
|
|
|
{
|
2016-01-26 06:38:42 +01:00
|
|
|
Versioned::set_reading_mode($this->versionedMode);
|
2016-11-15 06:06:31 +01:00
|
|
|
TestAssetStore::reset();
|
2016-01-26 06:38:42 +01:00
|
|
|
parent::tearDown(); // TODO: Change the autogenerated stub
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
public function testErrorPage()
|
|
|
|
{
|
2016-01-26 06:38:42 +01:00
|
|
|
// Get and publish records
|
2016-07-22 01:32:32 +02:00
|
|
|
$notFoundPage = $this->objFromFixture('SilverStripe\\CMS\\Model\\ErrorPage', '404');
|
2016-04-01 05:17:37 +02:00
|
|
|
$notFoundPage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2016-01-26 06:38:42 +01:00
|
|
|
$notFoundLink = $notFoundPage->Link();
|
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
$disallowedPage = $this->objFromFixture('SilverStripe\\CMS\\Model\\ErrorPage', '403');
|
2016-04-01 05:17:37 +02:00
|
|
|
$disallowedPage->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2016-01-26 06:38:42 +01:00
|
|
|
$disallowedLink = $disallowedPage->Link();
|
|
|
|
|
|
|
|
// Get stage version of file
|
|
|
|
$file = File::get()->first();
|
|
|
|
$fileLink = $file->Link();
|
|
|
|
Session::clear("loggedInAs");
|
|
|
|
|
|
|
|
// Generate shortcode for a file which doesn't exist
|
|
|
|
$shortcode = File::handle_shortcode(array('id' => 9999), null, new ShortcodeParser(), 'file_link');
|
|
|
|
$this->assertEquals($notFoundLink, $shortcode);
|
|
|
|
$shortcode = File::handle_shortcode(array('id' => 9999), 'click here', new ShortcodeParser(), 'file_link');
|
|
|
|
$this->assertEquals(sprintf('<a href="%s">%s</a>', $notFoundLink, 'click here'), $shortcode);
|
|
|
|
|
|
|
|
// Test that user cannot view draft file
|
|
|
|
$shortcode = File::handle_shortcode(array('id' => $file->ID), null, new ShortcodeParser(), 'file_link');
|
|
|
|
$this->assertEquals($disallowedLink, $shortcode);
|
|
|
|
$shortcode = File::handle_shortcode(array('id' => $file->ID), 'click here', new ShortcodeParser(), 'file_link');
|
|
|
|
$this->assertEquals(sprintf('<a href="%s">%s</a>', $disallowedLink, 'click here'), $shortcode);
|
|
|
|
|
|
|
|
// Authenticated users don't get the same error
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$shortcode = File::handle_shortcode(array('id' => $file->ID), null, new ShortcodeParser(), 'file_link');
|
|
|
|
$this->assertEquals($fileLink, $shortcode);
|
|
|
|
}
|
|
|
|
}
|