Skip to content

Commit

Permalink
ENHANCEMENT: remove dependencies between framework tests and cms module.
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr authored and chillu committed Jun 20, 2012
1 parent 8b43780 commit d82b67c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 28 deletions.
16 changes: 9 additions & 7 deletions filesystem/File.php
Expand Up @@ -168,11 +168,14 @@ class File extends DataObject {
public static function link_shortcode_handler($arguments, $content = null, $parser = null) {
if(!isset($arguments['id']) || !is_numeric($arguments['id'])) return;

if (
!($record = DataObject::get_by_id('File', $arguments['id'])) // Get the file by ID.
&& !($record = DataObject::get_one('ErrorPage', '"ErrorCode" = \'404\'')) // Link to 404 page directly.
) {
return; // There were no suitable matches at all.
$record = DataObject::get_by_id('File', $arguments['id']);

if (!$record) {
if(class_exists('ErrorPage')) {
$record = DataObject::get_one('ErrorPage', '"ErrorCode" = \'404\'');
}

if (!$record) return; // There were no suitable matches at all.
}

// build the HTML tag
Expand Down Expand Up @@ -360,8 +363,7 @@ function getCMSFields() {
sprintf('<a href="%s" target="_blank">%s</a>', $this->Link(), $this->RelativeLink())
),
new DateField_Disabled("Created", _t('AssetTableField.CREATED','First uploaded') . ':'),
new DateField_Disabled("LastEdited", _t('AssetTableField.LASTEDIT','Last changed') . ':'),
new ReadonlyField('BackLinkCount', _t('AssetTableField.BACKLINKCOUNT', 'Used on:'), $this->BackLinkTracking()->Count() . ' ' . _t('AssetTableField.PAGES', 'page(s)'))
new DateField_Disabled("LastEdited", _t('AssetTableField.LASTEDIT','Last changed') . ':')
)
)->setName("FilePreviewData")->addExtraClass('cms-file-info-data')
)->setName("FilePreview")->addExtraClass('cms-file-info');
Expand Down
6 changes: 5 additions & 1 deletion security/Member.php
Expand Up @@ -1037,7 +1037,11 @@ public static function mapInCMSGroups($groups = null) {
if(!$groups || $groups->Count() == 0) {
$perms = array('ADMIN', 'CMS_ACCESS_AssetAdmin');

$cmsPerms = singleton('CMSMain')->providePermissions();
if(class_exists('CMSMain')) {
$cmsPerms = singleton('CMSMain')->providePermissions();
} else {
$cmsPerms = singleton('LeftAndMain')->providePermissions();
}

if(!empty($cmsPerms)) {
$perms = array_unique(array_merge($perms, array_keys($cmsPerms)));
Expand Down
33 changes: 26 additions & 7 deletions tests/filesystem/FileTest.php
Expand Up @@ -11,7 +11,6 @@ class FileTest extends SapphireTest {

public function testLinkShortcodeHandler() {
$testFile = $this->objFromFixture('File', 'asdf');
$errorPage = $this->objFromFixture('ErrorPage', '404');

$parser = new ShortcodeParser();
$parser->register('file_link', array('File', 'link_shortcode_handler'));
Expand All @@ -30,15 +29,25 @@ public function testLinkShortcodeHandler() {
$fileShortcode = '[file_link id="-1"]';
$fileEnclosed = '[file_link id="-1"]Example Content[/file_link]';

$fileShortcodeExpected = $errorPage->Link();
$fileEnclosedExpected = sprintf('<a href="%s">Example Content</a>', $errorPage->Link());

$this->assertEquals($fileShortcodeExpected, $parser->parse($fileShortcode), 'Test link to 404 page if no suitable matches.');
$this->assertEquals($fileEnclosedExpected, $parser->parse($fileEnclosed));

$this->assertEquals('', $parser->parse('[file_link]'), 'Test that invalid ID attributes are not parsed.');
$this->assertEquals('', $parser->parse('[file_link id="text"]'));
$this->assertEquals('', $parser->parse('[file_link]Example Content[/file_link]'));

if(class_exists('ErrorPage')) {
$errorPage = ErrorPage::get()->filter('ErrorCode', 404)->First();
$this->assertEquals(
$errorPage->Link(),
$parser->parse($fileShortcode),
'Test link to 404 page if no suitable matches.'
);
$this->assertEquals(
sprintf('<a href="%s">Example Content</a>', $errorPage->Link()),
$parser->parse($fileEnclosed)
);
} else {
$this->assertEquals('', $parser->parse($fileShortcode), 'Short code is removed if file record is not present.');
$this->assertEquals('', $parser->parse($fileEnclosed));
}
}

function testCreateWithFilenameWithSubfolder() {
Expand Down Expand Up @@ -372,6 +381,16 @@ function setUp() {
fwrite($fh, str_repeat('x',1000000));
fclose($fh);
}

// Conditional fixture creation in case the 'cms' module is installed
if(class_exists('ErrorPage')) {
$page = new ErrorPage(array(
'Title' => 'Page not Found',
'ErrorCode' => 404
));
$page->write();
$page->publish('Stage', 'Live');
}
}

function tearDown() {
Expand Down
4 changes: 0 additions & 4 deletions tests/filesystem/FileTest.yml
Expand Up @@ -28,7 +28,3 @@ File:
Filename: assets/FileTest-folder1/File1.txt
Name: File1.txt
ParentID: =>Folder.folder1
ErrorPage:
404:
Title: Page not Found
ErrorCode: 404
15 changes: 6 additions & 9 deletions tests/security/PermissionTest.php
Expand Up @@ -68,16 +68,13 @@ function testGettingMembersByPermission() {

function testHiddenPermissions(){
$permissionCheckboxSet = new PermissionCheckboxSetField('Permissions','Permissions','Permission','GroupID');
$this->assertContains('CMS_ACCESS_CMSMain', $permissionCheckboxSet->Field());
$this->assertContains('CMS_ACCESS_AssetAdmin', $permissionCheckboxSet->Field());
$this->assertContains('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field());

Permission::add_to_hidden_permissions('CMS_ACCESS_CMSMain');
Permission::add_to_hidden_permissions('CMS_ACCESS_AssetAdmin');
$this->assertNotContains('CMS_ACCESS_CMSMain', $permissionCheckboxSet->Field());
$this->assertNotContains('CMS_ACCESS_AssetAdmin', $permissionCheckboxSet->Field());
Permission::add_to_hidden_permissions('CMS_ACCESS_LeftAndMain');

$this->assertNotContains('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field());

Permission::remove_from_hidden_permissions('CMS_ACCESS_AssetAdmin');
$this->assertContains('CMS_ACCESS_AssetAdmin', $permissionCheckboxSet->Field());
Permission::remove_from_hidden_permissions('CMS_ACCESS_CMSMain');
Permission::remove_from_hidden_permissions('CMS_ACCESS_LeftAndMain');
$this->assertContains('CMS_ACCESS_LeftAndMain', $permissionCheckboxSet->Field());
}
}

0 comments on commit d82b67c

Please sign in to comment.