mirror of
https://github.com/silverstripe/silverstripe-dms
synced 2024-10-22 12:05:56 +00:00
Added test
This commit is contained in:
parent
f7f5f115ee
commit
97ec75df59
@ -1278,6 +1278,7 @@ class DMSDocument_Controller extends Controller
|
|||||||
/**
|
/**
|
||||||
* Returns the document object from the request object's ID parameter.
|
* Returns the document object from the request object's ID parameter.
|
||||||
* Returns null, if no document found
|
* Returns null, if no document found
|
||||||
|
* @return DMSDocument|null
|
||||||
*/
|
*/
|
||||||
protected function getDocumentFromID($request)
|
protected function getDocumentFromID($request)
|
||||||
{
|
{
|
||||||
@ -1380,10 +1381,29 @@ class DMSDocument_Controller extends Controller
|
|||||||
//its ViewCount should be increased by 1 just before the browser sending the file to front.
|
//its ViewCount should be increased by 1 just before the browser sending the file to front.
|
||||||
$doc->trackView();
|
$doc->trackView();
|
||||||
|
|
||||||
|
$this->sendFile($path, $mime, $doc->getFilenameWithoutID(), $disposition);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self::$testMode) {
|
||||||
|
return 'This asset does not exist.';
|
||||||
|
}
|
||||||
|
$this->httpError(404, 'This asset does not exist.');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $path File path
|
||||||
|
* @param string $mime File mime type
|
||||||
|
* @param string $name File name
|
||||||
|
* @param string $disposition Content dispositon
|
||||||
|
*/
|
||||||
|
protected function sendFile($path, $mime, $name, $disposition) {
|
||||||
header('Content-Type: ' . $mime);
|
header('Content-Type: ' . $mime);
|
||||||
header('Content-Length: ' . filesize($path), null);
|
header('Content-Length: ' . filesize($path), null);
|
||||||
if (!empty($mime) && $mime != "text/html") {
|
if (!empty($mime) && $mime != "text/html") {
|
||||||
header('Content-Disposition: '.$disposition.'; filename="'.$doc->getFilenameWithoutID().'"');
|
header('Content-Disposition: '.$disposition.'; filename="'.addslashes($name).'"');
|
||||||
}
|
}
|
||||||
header('Content-transfer-encoding: 8bit');
|
header('Content-transfer-encoding: 8bit');
|
||||||
header('Expires: 0');
|
header('Expires: 0');
|
||||||
@ -1394,11 +1414,3 @@ class DMSDocument_Controller extends Controller
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (self::$testMode) {
|
|
||||||
return 'This asset does not exist.';
|
|
||||||
}
|
|
||||||
$this->httpError(404, 'This asset does not exist.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
54
tests/DMSDocumentControllerTest.php
Normal file
54
tests/DMSDocumentControllerTest.php
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class DMSDocumentControllerTest
|
||||||
|
*/
|
||||||
|
class DMSDocumentControllerTest extends SapphireTest {
|
||||||
|
|
||||||
|
protected static $fixture_file = "dmstest.yml";
|
||||||
|
|
||||||
|
public function testDownloadBehaviourOpen() {
|
||||||
|
DMS::$dmsFolder = DMS_DIR; //sneakily setting the DMS folder to the folder where the test file lives
|
||||||
|
|
||||||
|
$this->logInWithPermission('ADMIN');
|
||||||
|
/** @var DMSDocument_Controller $controller */
|
||||||
|
$controller = $this->getMockBuilder('DMSDocument_Controller')
|
||||||
|
->setMethods(array('sendFile'))->getMock();
|
||||||
|
$self = $this;
|
||||||
|
$controller->expects($this->once())->method('sendFile')->will($this->returnCallback(function($path, $mime, $name, $disposition) use($self) {
|
||||||
|
$self->assertEquals('inline', $disposition);
|
||||||
|
}));
|
||||||
|
$openDoc = new DMSDocument();
|
||||||
|
$openDoc->Filename = "DMS-test-lorum-file.pdf";
|
||||||
|
$openDoc->Folder = "tests";
|
||||||
|
$openDoc->DownloadBehavior = 'open';
|
||||||
|
$openDoc->clearEmbargo(false);
|
||||||
|
$openDoc->write();
|
||||||
|
$request = new SS_HTTPRequest('GET', 'index/' . $openDoc->ID);
|
||||||
|
$request->match('index/$ID');
|
||||||
|
$controller->index($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testDownloadBehaviourDownload() {
|
||||||
|
DMS::$dmsFolder = DMS_DIR; //sneakily setting the DMS folder to the folder where the test file lives
|
||||||
|
|
||||||
|
$this->logInWithPermission('ADMIN');
|
||||||
|
/** @var DMSDocument_Controller $controller */
|
||||||
|
$controller = $this->getMockBuilder('DMSDocument_Controller')
|
||||||
|
->setMethods(array('sendFile'))->getMock();
|
||||||
|
$self = $this;
|
||||||
|
$controller->expects($this->once())->method('sendFile')->will($this->returnCallback(function($path, $mime, $name, $disposition) use($self) {
|
||||||
|
$self->assertEquals('attachment', $disposition);
|
||||||
|
}));
|
||||||
|
$openDoc = new DMSDocument();
|
||||||
|
$openDoc->Filename = "DMS-test-lorum-file.pdf";
|
||||||
|
$openDoc->Folder = "tests";
|
||||||
|
$openDoc->DownloadBehavior = 'download';
|
||||||
|
$openDoc->clearEmbargo(false);
|
||||||
|
$openDoc->write();
|
||||||
|
$request = new SS_HTTPRequest('GET', 'index/' . $openDoc->ID);
|
||||||
|
$request->match('index/$ID');
|
||||||
|
$controller->index($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -2,7 +2,7 @@
|
|||||||
class DMSDocumentTest extends SapphireTest
|
class DMSDocumentTest extends SapphireTest
|
||||||
{
|
{
|
||||||
|
|
||||||
public static $fixture_file = "dms/tests/dmstest.yml";
|
protected static $fixture_file = "dms/tests/dmstest.yml";
|
||||||
|
|
||||||
public function tearDownOnce()
|
public function tearDownOnce()
|
||||||
{
|
{
|
||||||
@ -157,4 +157,16 @@ class DMSDocumentTest extends SapphireTest
|
|||||||
."associated with causes that document to be deleted as well"
|
."associated with causes that document to be deleted as well"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testDefaultDownloadBehabiourCMSFields() {
|
||||||
|
$document = singleton('DMSDocument');
|
||||||
|
Config::inst()->update('DMSDocument', 'default_download_behaviour', 'open');
|
||||||
|
$cmsFields = $document->getCMSFields();
|
||||||
|
$this->assertEquals('open', $cmsFields->dataFieldByName('DownloadBehavior')->Value());
|
||||||
|
|
||||||
|
|
||||||
|
Config::inst()->update('DMSDocument', 'default_download_behaviour', 'download');
|
||||||
|
$cmsFields = $document->getCMSFields();
|
||||||
|
$this->assertEquals('download', $cmsFields->dataFieldByName('DownloadBehavior')->Value());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,7 @@ class DMSEmbargoTest extends SapphireTest
|
|||||||
public function testBasicEmbargo()
|
public function testBasicEmbargo()
|
||||||
{
|
{
|
||||||
$oldDMSFolder = DMS::$dmsFolder;
|
$oldDMSFolder = DMS::$dmsFolder;
|
||||||
|
$oldTestMode = DMSDocument_Controller::$testMode;
|
||||||
DMS::$dmsFolder = DMS_DIR; //sneakily setting the DMS folder to the folder where the test file lives
|
DMS::$dmsFolder = DMS_DIR; //sneakily setting the DMS folder to the folder where the test file lives
|
||||||
|
|
||||||
$doc = new DMSDocument();
|
$doc = new DMSDocument();
|
||||||
@ -54,6 +55,7 @@ class DMSEmbargoTest extends SapphireTest
|
|||||||
$this->assertNotEquals($doc->getFullPath(), $result, "File no longer returned (in test mode) when switching to other user group");
|
$this->assertNotEquals($doc->getFullPath(), $result, "File no longer returned (in test mode) when switching to other user group");
|
||||||
|
|
||||||
DMS::$dmsFolder = $oldDMSFolder;
|
DMS::$dmsFolder = $oldDMSFolder;
|
||||||
|
DMSDocument_Controller::$testMode = $oldTestMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testEmbargoIndefinitely()
|
public function testEmbargoIndefinitely()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user