Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #134 from creative-commoners/pulls/1.4/add-to-camp…
…aign-extension NEW Add to Campaign button in SiteTree now lives in campaign-admin
- Loading branch information
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace SilverStripe\CampaignAdmin; | ||
|
||
use SilverStripe\Forms\FieldList; | ||
use SilverStripe\Forms\Tab; | ||
use SilverStripe\ORM\DataExtension; | ||
use SilverStripe\Security\Permission; | ||
|
||
/** | ||
* Handles adding the "Add to Campaign" button to a page's secondary actions menu | ||
*/ | ||
class SiteTreeExtension extends DataExtension | ||
{ | ||
public function updateCMSActions(FieldList $actions) | ||
{ | ||
// Add to campaign option if not-archived and has publish permission | ||
if ((!$this->owner->isPublished() && !$this->owner->isOnDraft()) | ||
|| !$this->owner->canPublish() | ||
|| !Permission::check('CMS_ACCESS_CampaignAdmin') | ||
) { | ||
return; | ||
} | ||
|
||
/** @var Tab $moreOptions */ | ||
$moreOptions = $actions->fieldByName('ActionMenus.MoreOptions'); | ||
if (!$moreOptions) { | ||
return; | ||
} | ||
|
||
$moreOptions->insertAfter( | ||
'Information', | ||
AddToCampaignHandler_FormAction::create() | ||
->removeExtraClass('btn-primary') | ||
->addExtraClass('btn-secondary') | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace SilverStripe\CampaignAdmin\Tests; | ||
|
||
use SilverStripe\CampaignAdmin\SiteTreeExtension; | ||
use SilverStripe\CMS\Model\SiteTree; | ||
use SilverStripe\Dev\SapphireTest; | ||
use SilverStripe\Versioned\Versioned; | ||
|
||
class SiteTreeExtensionTest extends SapphireTest | ||
{ | ||
protected $usesDatabase = true; | ||
|
||
protected static $required_extensions = [ | ||
SiteTree::class => [ | ||
SiteTreeExtension::class, | ||
], | ||
]; | ||
|
||
public function testAddToCampaignButtonIsAdded() | ||
{ | ||
$this->logInWithPermission(); | ||
|
||
$page = new SiteTree(); | ||
$page->CanEditType = 'LoggedInUsers'; | ||
$page->write(); | ||
$page->publishRecursive(); | ||
$actions = $page->getCMSActions(); | ||
|
||
$addToCampaignAction = $actions->dataFieldByName('action_addtocampaign'); | ||
$this->assertNotNull($addToCampaignAction, 'Add To Campaign button should have been added'); | ||
} | ||
|
||
public function testAddToCampaignButtonIsNotAddedWhenUserDoesNotHavePermission() | ||
{ | ||
$this->logInWithPermission('EDIT_PERMISSIONS'); | ||
|
||
$page = new SiteTree(); | ||
$page->CanEditType = 'LoggedInUsers'; | ||
$page->write(); | ||
$page->publishRecursive(); | ||
$actions = $page->getCMSActions(); | ||
|
||
$addToCampaignAction = $actions->dataFieldByName('action_addtocampaign'); | ||
$this->assertNull( | ||
$addToCampaignAction, | ||
'Add To Campaign button should not be shown to users without permission' | ||
); | ||
} | ||
|
||
public function testActionsDeletedFromStageRecord() | ||
{ | ||
$this->logInWithPermission(); | ||
|
||
$page = new SiteTree(); | ||
$page->CanEditType = 'LoggedInUsers'; | ||
$pageID = $page->write(); | ||
$page->publishRecursive(); | ||
$page->deleteFromStage('Stage'); | ||
|
||
// Get the live version of the page | ||
$page = Versioned::get_one_by_stage(SiteTree::class, "Live", "\"SiteTree\".\"ID\" = $pageID"); | ||
$this->assertInstanceOf(SiteTree::class, $page); | ||
|
||
$actions = $page->getCMSActions(); | ||
|
||
// Theoretically allow deletions to be staged via add to campaign | ||
$this->assertNotNull($actions->dataFieldByName('action_addtocampaign')); | ||
} | ||
|
||
public function testActionsChangedOnStageRecord() | ||
{ | ||
$this->logInWithPermission(); | ||
|
||
$page = new SiteTree(); | ||
$page->CanEditType = 'LoggedInUsers'; | ||
$page->write(); | ||
$page->publishRecursive(); | ||
$page->Content = 'Changed on Stage'; | ||
$page->write(); | ||
$page->flushCache(); | ||
|
||
// Reload latest version | ||
$page = SiteTree::get()->byID($page->ID); | ||
|
||
$actions = $page->getCMSActions(); | ||
$this->assertNotNull($actions->dataFieldByName('action_addtocampaign')); | ||
} | ||
} |