Merge pull request #163 from creative-commoners/pulls/2.0/add-more-tests

Add some more tests
This commit is contained in:
Daniel Hensby 2017-06-07 21:39:18 +01:00 committed by GitHub
commit bacc3db4ef
6 changed files with 167 additions and 63 deletions

View File

@ -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();
}
}

View File

@ -65,7 +65,7 @@ class DMSDocumentAddController extends LeftAndMain
public function getCurrentDocumentSet()
{
if ($id = $this->getRequest()->getVar('dsid')) {
return DMSDocumentSet::get()->byid($id);
return DMSDocumentSet::get()->byId($id);
}
return singleton('DMSDocumentSet');
}

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

View File

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

View File

@ -4,22 +4,45 @@ class DMSDocumentAddControllerTest extends FunctionalTest
{
protected static $fixture_file = 'dms/tests/dmstest.yml';
/**
* @var DMSDocumentAddController
*/
protected $controller;
public function setUp()
{
parent::setUp();
$this->logInWithPermission();
$this->controller = new DMSDocumentAddController;
$this->controller->init();
}
/**
* Ensure that if no ID is provided then a SiteTree singleton is returned (which will not have an ID). If one is
* provided then it should be loaded from the database via versioning.
*/
public function testCurrentPageReturnsSiteTree()
{
$controller = new DMSDocumentAddController;
$this->assertInstanceOf('SiteTree', $controller->currentPage());
$page = $this->objFromFixture('SiteTree', 's1');
$this->assertInstanceOf('SiteTree', $this->controller->currentPage());
$this->assertEmpty($this->controller->currentPage()->ID);
$this->controller->setRequest(new SS_HTTPRequest('GET', '/', array('ID' => $page->ID)));
$this->assertEquals($page->ID, $this->controller->currentPage()->ID, 'Specified page is loaded and returned');
}
/**
* Ensure that if no "dsid" is given a singleton is returned (which will not have an ID). If one is provided
* it should be loaded from the database
*/
public function testGetCurrentDocumentSetReturnsDocumentSet()
{
$controller = new DMSDocumentAddController;
$this->assertInstanceOf('DMSDocumentSet', $controller->getCurrentDocumentSet());
$set = $this->objFromFixture('DMSDocumentSet', 'ds1');
$this->assertInstanceOf('DMSDocumentSet', $this->controller->getCurrentDocumentSet());
$this->assertEmpty($this->controller->getCurrentDocumentSet()->ID, 'Singleton does not have an ID');
$this->controller->setRequest(new SS_HTTPRequest('GET', '/', array('dsid' => $set->ID)));
$this->assertEquals($set->ID, $this->controller->getCurrentDocumentSet()->ID, 'Specified document set is returned');
}
/**
@ -27,13 +50,12 @@ class DMSDocumentAddControllerTest extends FunctionalTest
*/
public function testGetAllowedExtensions()
{
$controller = new DMSDocumentAddController;
Config::inst()->remove('File', 'allowed_extensions');
Config::inst()->update('File', 'allowed_extensions', array('jpg', 'gif'));
$this->assertSame(array('jpg', 'gif'), $controller->getAllowedExtensions());
$this->assertSame(array('jpg', 'gif'), $this->controller->getAllowedExtensions());
Config::inst()->update('DMSDocumentAddController', 'allowed_extensions', array('php', 'php5'));
$this->assertSame(array('jpg', 'gif', 'php', 'php5'), $controller->getAllowedExtensions());
$this->assertSame(array('jpg', 'gif', 'php', 'php5'), $this->controller->getAllowedExtensions());
}
/**
@ -42,13 +64,27 @@ class DMSDocumentAddControllerTest extends FunctionalTest
*/
public function testBacklink()
{
$controller = new DMSDocumentAddController;
$controller->init();
$this->assertContains('admin/documents', $controller->Backlink());
$this->assertContains('admin/documents', $this->controller->Backlink());
$request = new SS_HTTPRequest('GET', '/', array('dsid' => 123));
$controller->setRequest($request);
$this->assertContains('EditForm', $controller->Backlink());
$this->assertContains('123', $controller->Backlink());
$this->controller->setRequest($request);
$this->assertContains('EditForm', $this->controller->Backlink());
$this->assertContains('123', $this->controller->Backlink());
}
/**
* Test that the document autocomplete endpoint returns JSON, matching on ID, title or filename (case insensitive)
*/
public function testDocumentAutocomplete()
{
$result = (string) $this->get('admin/pages/adddocument/documentautocomplete?term=EXIST')->getBody();
$this->assertJson($result, 'Autocompleter should return JSON');
$this->assertContains("File That Doesn't Exist (Title)", $result);
$this->assertContains('test-file-file-doesnt-exist-1', $result);
$this->assertNotContains('doc-logged-in-users', $result);
$document = $this->objFromFixture('DMSDocument', 'd2');
$result = (string) $this->get('admin/pages/adddocument/documentautocomplete?term=' . $document->ID)->getBody();
$this->assertContains($document->ID . " - File That Doesn't Exist (Title)", $result);
}
}

View File

@ -0,0 +1,35 @@
<?php
class DMSUploadField_ItemHandlerTest extends SapphireTest
{
protected static $fixture_file = 'dms/tests/dmstest.yml';
/**
* @var DMSDocument
*/
protected $document;
public function setUp()
{
parent::setUp();
$this->document = $this->objFromFixture('DMSDocument', 'd1');
}
public function testGetItem()
{
$handler = new DMSUploadField_ItemHandler(DMSUploadField::create('Test'), $this->document->ID);
$result = $handler->getItem();
$this->assertSame($result->ID, $this->document->ID, 'getItem returns the correct document from the database');
}
public function testEditForm()
{
$handler = new DMSUploadField_ItemHandler(DMSUploadField::create('Test'), $this->document->ID);
$result = $handler->EditForm();
$this->assertInstanceOf('Form', $result);
$this->assertInstanceOf('DMSDocument', $result->getRecord());
$this->assertSame($this->document->ID, $result->getRecord()->ID);
}
}