From 832eb14fad7714901e84a9ca739523f047a45b43 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Wed, 7 Jun 2017 13:27:36 +1200 Subject: [PATCH] Add tests for returning link with content in shortcode handler and error handling --- code/DMSShortcodeHandler.php | 24 ++++------ tests/DMSShortcodeHandlerTest.php | 73 +++++++++++++++++++++++++++++++ tests/DMSShortcodeTest.php | 34 -------------- 3 files changed, 82 insertions(+), 49 deletions(-) create mode 100644 tests/DMSShortcodeHandlerTest.php delete mode 100644 tests/DMSShortcodeTest.php diff --git a/code/DMSShortcodeHandler.php b/code/DMSShortcodeHandler.php index 0af1773..7c1564b 100644 --- a/code/DMSShortcodeHandler.php +++ b/code/DMSShortcodeHandler.php @@ -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,14 +20,14 @@ 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()); - } - - return $document->Link(); } + + if (isset($extra['element'])) { + $extra['element']->setAttribute('data-ext', $document->getExtension()); + $extra['element']->setAttribute('data-size', $document->getFileSizeFormatted()); + } + + return $document->Link(); } } diff --git a/tests/DMSShortcodeHandlerTest.php b/tests/DMSShortcodeHandlerTest.php new file mode 100644 index 0000000..2acbe8e --- /dev/null +++ b/tests/DMSShortcodeHandlerTest.php @@ -0,0 +1,73 @@ +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( + '

Document

', + $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( + 'Some content', + $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'), '')); + } +} diff --git a/tests/DMSShortcodeTest.php b/tests/DMSShortcodeTest.php deleted file mode 100644 index ee7c56b..0000000 --- a/tests/DMSShortcodeTest.php +++ /dev/null @@ -1,34 +0,0 @@ -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( - '

Document

', - $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'); - } -}