mirror of
https://github.com/silverstripe/silverstripe-dms
synced 2024-10-22 14:05:56 +02:00
Add tests for returning link with content in shortcode handler and error handling
This commit is contained in:
parent
0368293e74
commit
832eb14fad
@ -8,14 +8,8 @@
|
||||
*/
|
||||
class DMSShortcodeHandler
|
||||
{
|
||||
|
||||
public static function handle(
|
||||
$arguments,
|
||||
$content,
|
||||
ShortcodeParser $parser,
|
||||
$tag,
|
||||
array $extra = array()
|
||||
) {
|
||||
public static function handle($arguments, $content, ShortcodeParser $parser, $tag, array $extra = array())
|
||||
{
|
||||
if (!empty($arguments['id'])) {
|
||||
$document = DMSDocument::get()->byID($arguments['id']);
|
||||
|
||||
@ -26,7 +20,8 @@ class DMSShortcodeHandler
|
||||
$document->Link(),
|
||||
$parser->parse($content)
|
||||
);
|
||||
} else {
|
||||
}
|
||||
|
||||
if (isset($extra['element'])) {
|
||||
$extra['element']->setAttribute('data-ext', $document->getExtension());
|
||||
$extra['element']->setAttribute('data-size', $document->getFileSizeFormatted());
|
||||
@ -35,7 +30,6 @@ class DMSShortcodeHandler
|
||||
return $document->Link();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$error = ErrorPage::get()->filter('ErrorCode', '404')->First();
|
||||
|
||||
|
73
tests/DMSShortcodeHandlerTest.php
Normal file
73
tests/DMSShortcodeHandlerTest.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests DMS shortcode linking functionality.
|
||||
*
|
||||
* @package dms
|
||||
* @subpackage tests
|
||||
*/
|
||||
class DMSShortcodeHandlerTest extends SapphireTest
|
||||
{
|
||||
protected static $fixture_file = 'dmstest.yml';
|
||||
|
||||
public function testShortcodeOperation()
|
||||
{
|
||||
Config::inst()->update('DMS', 'folder_name', 'assets/_unit-test-123');
|
||||
|
||||
$file = 'dms/tests/DMS-test-lorum-file.pdf';
|
||||
$document = DMS::inst()->storeDocument($file);
|
||||
|
||||
$result = ShortcodeParser::get('default')->parse(sprintf(
|
||||
'<p><a href="[dms_document_link id=\'%d\']">Document</a></p>',
|
||||
$document->ID
|
||||
));
|
||||
|
||||
$value = Injector::inst()->create('HTMLValue', $result);
|
||||
$link = $value->query('//a')->item(0);
|
||||
|
||||
$this->assertStringEndsWith(
|
||||
'/dmsdocument/' . $document->ID . '-dms-test-lorum-file-pdf',
|
||||
$link->getAttribute('href')
|
||||
);
|
||||
$this->assertEquals($document->getExtension(), $link->getAttribute('data-ext'));
|
||||
$this->assertEquals($document->getFileSizeFormatted(), $link->getAttribute('data-size'));
|
||||
|
||||
DMSFilesystemTestHelper::delete('assets/_unit-test-123');
|
||||
}
|
||||
|
||||
/**
|
||||
* When the document is valid, the correct arguments are provided and some content is given, the content should
|
||||
* be parsed and added into an anchor to the document
|
||||
*/
|
||||
public function testShortcodeWithContentReturnsParsedContentInLink()
|
||||
{
|
||||
$document = $this->objFromFixture('DMSDocument', 'd1');
|
||||
$arguments = array('id' => $document->ID);
|
||||
$result = DMSShortcodeHandler::handle($arguments, 'Some content', ShortcodeParser::get('default'), '');
|
||||
|
||||
$this->assertSame(
|
||||
'<a href="/dmsdocument/' . $document->ID . '-test-file-file-doesnt-exist-1">Some content</a>',
|
||||
$result
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* An error page link should be returned if the arguments are not valid, empty or the document is not available etc.
|
||||
*
|
||||
* This only applies when an error page with a 404 error code exists.
|
||||
*/
|
||||
public function testReturnErrorPageWhenIdIsEmpty()
|
||||
{
|
||||
ErrorPage::create(array('URLSegment' => 'testing', 'ErrorCode' => '404'))->write();
|
||||
$result = DMSShortcodeHandler::handle(array(), '', ShortcodeParser::get('default'), '');
|
||||
$this->assertContains('testing', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* When invalid or no data is available to return from the arguments and no error page exists to use for a link,
|
||||
* return a blank string
|
||||
*/
|
||||
public function testReturnEmptyStringWhenNoErrorPageExistsAndIdIsEmpty()
|
||||
{
|
||||
$this->assertSame('', DMSShortcodeHandler::handle(array(), '', ShortcodeParser::get('default'), ''));
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests DMS shortcode linking functionality.
|
||||
*
|
||||
* @package dms
|
||||
* @subpackage tests
|
||||
*/
|
||||
class DMSShortcodeTest extends SapphireTest
|
||||
{
|
||||
public function testShortcodeOperation()
|
||||
{
|
||||
Config::inst()->update('DMS', 'folder_name', 'assets/_unit-test-123');
|
||||
|
||||
$file = 'dms/tests/DMS-test-lorum-file.pdf';
|
||||
$document = DMS::inst()->storeDocument($file);
|
||||
|
||||
$result = ShortcodeParser::get('default')->parse(sprintf(
|
||||
'<p><a href="[dms_document_link id=\'%d\']">Document</a></p>',
|
||||
$document->ID
|
||||
));
|
||||
|
||||
$value = Injector::inst()->create('HTMLValue', $result);
|
||||
$link = $value->query('//a')->item(0);
|
||||
|
||||
$this->assertStringEndsWith(
|
||||
'/dmsdocument/' . $document->ID . '-dms-test-lorum-file-pdf',
|
||||
$link->getAttribute('href')
|
||||
);
|
||||
$this->assertEquals($document->getExtension(), $link->getAttribute('data-ext'));
|
||||
$this->assertEquals($document->getFileSizeFormatted(), $link->getAttribute('data-size'));
|
||||
|
||||
DMSFilesystemTestHelper::delete('assets/_unit-test-123');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user