Skip to content

Commit

Permalink
Merge pull request #2737 from creative-commoners/pulls/4.11/allow-emp…
Browse files Browse the repository at this point in the history
…ty-preview-urls

Issue was fixed
  • Loading branch information
sabina-talipova authored May 11, 2022
2 parents f72f5dc + 83104da commit 1897cb3
Show file tree
Hide file tree
Showing 9 changed files with 319 additions and 72 deletions.
7 changes: 3 additions & 4 deletions code/Controllers/SilverStripeNavigatorItem_ArchiveLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ public function getMessage()

public function getLink()
{
return Controller::join_links(
$this->record->PreviewLink(),
'?archiveDate=' . urlencode($this->record->LastEdited ?? '')
);
$link = $this->record->PreviewLink();
return $link ? Controller::join_links($link, '?archiveDate=' . urlencode($this->record->LastEdited ?? '')) : '';
}

public function canView($member = null)
Expand All @@ -62,6 +60,7 @@ public function canView($member = null)
&& $this->isArchived()
// Don't follow redirects in preview, they break the CMS editing form
&& !($record instanceof RedirectorPage)
&& $this->getLink()
);
}

Expand Down
4 changes: 3 additions & 1 deletion code/Controllers/SilverStripeNavigatorItem_LiveLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public function getMessage()

public function getLink()
{
return Controller::join_links($this->getLivePage()->PreviewLink(), '?stage=Live');
$link = $this->getLivePage()->PreviewLink();
return $link ? Controller::join_links($link, '?stage=Live') : '';
}

public function canView($member = null)
Expand All @@ -60,6 +61,7 @@ public function canView($member = null)
&& $this->showLiveLink()
&& $record->hasStages()
&& $this->getLivePage()
&& $this->getLink()
);
}

Expand Down
7 changes: 6 additions & 1 deletion code/Controllers/SilverStripeNavigatorItem_StageLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ public function getMessage()
public function getLink()
{
$date = Versioned::current_archived_date();
$link = $this->record->PreviewLink();
if (!$link) {
return '';
}
return Controller::join_links(
$this->record->PreviewLink(),
$link,
'?stage=Stage',
$date ? '?archiveDate=' . $date : null
);
Expand All @@ -66,6 +70,7 @@ public function canView($member = null)
&& $this->showStageLink()
&& $record->hasStages()
&& $this->getDraftPage()
&& $this->getLink()
);
}

Expand Down
17 changes: 15 additions & 2 deletions code/Controllers/SilverStripeNavigatorItem_Unversioned.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function getHTML()

public function getLink()
{
return $this->getRecord()->PreviewLink();
return $this->getRecord()->PreviewLink() ?? '';
}

public function getTitle()
Expand All @@ -40,11 +40,24 @@ public function getTitle()
public function canView($member = null)
{
return (
!$this->getRecord()->hasExtension(Versioned::class)
$this->recordIsUnversioned()
&& $this->showUnversionedLink()
&& $this->getLink()
);
}

private function recordIsUnversioned(): bool
{
$record = $this->getRecord();
// If the record has the Versioned extension, it can be considered unversioned
// for the purposes of this class if it has no stages and is not archived.
if ($record->hasExtension(Versioned::class)) {
return (!$record->hasStages()) && !$this->isArchived();
}
// Completely unversioned.
return true;
}

/**
* True if the record is configured to display this item.
*
Expand Down
236 changes: 204 additions & 32 deletions tests/php/Controllers/SilverStripeNavigatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use SilverStripe\CMS\Controllers\SilverStripeNavigatorItem_ArchiveLink;
use SilverStripe\CMS\Controllers\SilverStripeNavigatorItem_LiveLink;
use SilverStripe\CMS\Controllers\SilverStripeNavigatorItem_StageLink;
use SilverStripe\CMS\Controllers\SilverStripeNavigatorItem_Unversioned;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Security\Member;

Expand All @@ -15,53 +16,224 @@ class SilverStripeNavigatorTest extends SapphireTest

protected static $extra_dataobjects = [
SilverStripeNavigatorTest\UnstagedRecord::class,
SilverStripeNavigatorTest\UnversionedRecord::class,
SilverStripeNavigatorTest\VersionedRecord::class,
];

public function testGetItems()
public function testGetItemsAutoDiscovery(): void
{
$page = $this->objFromFixture('Page', 'page1');
$navigator = new SilverStripeNavigator($page);

$items = $navigator->getItems();
$classes = array_map('get_class', $items->toArray() ?? []);
$this->assertContains(
SilverStripeNavigatorItem_StageLink::class,
$classes,
'Adds default classes'
);
$record = new SilverStripeNavigatorTest\VersionedRecord();
$record->PreviewLinkTestProperty = 'some-value';
$record->write();
$navigator = new SilverStripeNavigator($record);
$classes = array_map('get_class', $navigator->getItems()->toArray());

$this->assertContains(
SilverStripeNavigatorTest_TestItem::class,
$classes,
'Autodiscovers new classes'
);
}

public function testGetItemsPublished(): void
{
$record = new SilverStripeNavigatorTest\VersionedRecord();
$record->PreviewLinkTestProperty = 'some-value';
$record->write();
$record->publishRecursive();
$navigator = new SilverStripeNavigator($record);
$classes = array_map('get_class', $navigator->getItems()->toArray());

// Has the live and staged links
$this->assertContains(SilverStripeNavigatorItem_LiveLink::class, $classes);
$this->assertContains(SilverStripeNavigatorItem_StageLink::class, $classes);

// Does not have the other links
$this->assertNotContains(SilverStripeNavigatorItem_ArchiveLink::class, $classes);
$this->assertNotContains(SilverStripeNavigatorItem_Unversioned::class, $classes);
}

public function testGetItemsStaged(): void
{
$record = new SilverStripeNavigatorTest\VersionedRecord();
$record->PreviewLinkTestProperty = 'some-value';
$record->write();
$navigator = new SilverStripeNavigator($record);
$classes = array_map('get_class', $navigator->getItems()->toArray());

// Has the stage link
$this->assertContains(SilverStripeNavigatorItem_StageLink::class, $classes);

// Does not have the other links
$this->assertNotContains(SilverStripeNavigatorItem_ArchiveLink::class, $classes);
$this->assertNotContains(SilverStripeNavigatorItem_LiveLink::class, $classes);
$this->assertNotContains(SilverStripeNavigatorItem_Unversioned::class, $classes);
}

public function testGetItemsArchived(): void
{
$record = new SilverStripeNavigatorTest\VersionedRecord();
$record->PreviewLinkTestProperty = 'some-value';
$record->write();
$record->doArchive();
$navigator = new SilverStripeNavigator($record);
$classes = array_map('get_class', $navigator->getItems()->toArray());

// Non-versioned items don't have stage / live
// Has the archived link
$this->assertContains(SilverStripeNavigatorItem_ArchiveLink::class, $classes);

// Does not have the other links
$this->assertNotContains(SilverStripeNavigatorItem_LiveLink::class, $classes);
$this->assertNotContains(SilverStripeNavigatorItem_StageLink::class, $classes);
$this->assertNotContains(SilverStripeNavigatorItem_UnversionedLink::class, $classes);
}

public function testCanView()
public function testGetItemsUnstaged(): void
{
$page = $this->objFromFixture('Page', 'page1');
$admin = $this->objFromFixture(Member::class, 'admin');
$navigator = new SilverStripeNavigator($page);

// TODO Shouldn't be necessary but SapphireTest logs in as ADMIN by default
$this->logInWithPermission('CMS_ACCESS_CMSMain');
$items = $navigator->getItems();
$classes = array_map('get_class', $items->toArray() ?? []);
$this->assertNotContains(SilverStripeNavigatorTest_ProtectedTestItem::class, $classes);

$this->logInWithPermission('ADMIN');
$items = $navigator->getItems();
$classes = array_map('get_class', $items->toArray() ?? []);
$this->assertContains(SilverStripeNavigatorTest_ProtectedTestItem::class, $classes);

// Unversioned record shouldn't be viewable in stage / live specific views
$unversioned = new SilverStripeNavigatorTest\UnstagedRecord();
$navigator2 = new SilverStripeNavigator($unversioned);
$classes = array_map('get_class', $navigator2->getItems()->toArray() ?? []);
$record = new SilverStripeNavigatorTest\UnstagedRecord();
$record->previewLinkTestProperty = 'some-value';
$record->write();
$navigator = new SilverStripeNavigator($record);
$classes = array_map('get_class', $navigator->getItems()->toArray());

// Has the unversioned link
$this->assertContains(SilverStripeNavigatorItem_Unversioned::class, $classes);

// Does not have the other links
$this->assertNotContains(SilverStripeNavigatorItem_ArchiveLink::class, $classes);
$this->assertNotContains(SilverStripeNavigatorItem_LiveLink::class, $classes);
$this->assertNotContains(SilverStripeNavigatorItem_StageLink::class, $classes);
}

public function testGetItemsUnversioned(): void
{
$record = new SilverStripeNavigatorTest\UnversionedRecord();
$record->previewLinkTestProperty = 'some-value';
$record->write();
$navigator = new SilverStripeNavigator($record);
$classes = array_map('get_class', $navigator->getItems()->toArray());

// Has the unversioned link
$this->assertContains(SilverStripeNavigatorItem_Unversioned::class, $classes);

// Does not have the other links
$this->assertNotContains(SilverStripeNavigatorItem_ArchiveLink::class, $classes);
$this->assertNotContains(SilverStripeNavigatorItem_LiveLink::class, $classes);
$this->assertNotContains(SilverStripeNavigatorItem_StageLink::class, $classes);
}

public function testCanViewPublished(): void
{
$record = new SilverStripeNavigatorTest\VersionedRecord();
$record->write();
$record->publishRecursive();
$liveLinkItem = new SilverStripeNavigatorItem_LiveLink($record);
$stagedLinkItem = new SilverStripeNavigatorItem_StageLink($record);
$archivedLinkItem = new SilverStripeNavigatorItem_ArchiveLink($record);
$unversionedLinkItem = new SilverStripeNavigatorItem_Unversioned($record);

// Cannot view staged and live links when there's no preview link
$this->assertFalse($liveLinkItem->canView());
$this->assertFalse($stagedLinkItem->canView());

$record->PreviewLinkTestProperty = 'some-value';
$record->write();
$record->publishRecursive();

// Can view staged and live links
$this->assertTrue($liveLinkItem->canView());
$this->assertTrue($stagedLinkItem->canView());
// Cannot view the other links
$this->assertFalse($archivedLinkItem->canView());
$this->assertFalse($unversionedLinkItem->canView());
}

public function testCanViewStaged(): void
{
$record = new SilverStripeNavigatorTest\VersionedRecord();
$record->write();
$liveLinkItem = new SilverStripeNavigatorItem_LiveLink($record);
$stagedLinkItem = new SilverStripeNavigatorItem_StageLink($record);
$archivedLinkItem = new SilverStripeNavigatorItem_ArchiveLink($record);
$unversionedLinkItem = new SilverStripeNavigatorItem_Unversioned($record);

// Cannot view staged link when there's no preview link
$this->assertFalse($stagedLinkItem->canView());

$record->PreviewLinkTestProperty = 'some-value';

// Can view staged link
$this->assertTrue($stagedLinkItem->canView());
// Cannot view the other links
$this->assertFalse($liveLinkItem->canView());
$this->assertFalse($archivedLinkItem->canView());
$this->assertFalse($unversionedLinkItem->canView());
}

public function testCanViewArchived(): void
{
$record = new SilverStripeNavigatorTest\VersionedRecord();
$record->write();
$record->doArchive();
$liveLinkItem = new SilverStripeNavigatorItem_LiveLink($record);
$stagedLinkItem = new SilverStripeNavigatorItem_StageLink($record);
$archivedLinkItem = new SilverStripeNavigatorItem_ArchiveLink($record);
$unversionedLinkItem = new SilverStripeNavigatorItem_Unversioned($record);

// Cannot view archived link when there's no preview link
$this->assertFalse($archivedLinkItem->canView());

$record->PreviewLinkTestProperty = 'some-value';

// Can view archived link
$this->assertTrue($archivedLinkItem->canView());
// Cannot view the other links
$this->assertFalse($liveLinkItem->canView());
$this->assertFalse($stagedLinkItem->canView());
$this->assertFalse($unversionedLinkItem->canView());
}

public function testCanViewUnstaged(): void
{
$record = new SilverStripeNavigatorTest\UnstagedRecord();
$record->write();
$liveLinkItem = new SilverStripeNavigatorItem_LiveLink($record);
$stagedLinkItem = new SilverStripeNavigatorItem_StageLink($record);
$archivedLinkItem = new SilverStripeNavigatorItem_ArchiveLink($record);
$unversionedLinkItem = new SilverStripeNavigatorItem_Unversioned($record);

// Cannot view unversioned link when there's no preview link
$this->assertFalse($unversionedLinkItem->canView());

$record->previewLinkTestProperty = 'some-value';

// Can view unversioned link
$this->assertTrue($unversionedLinkItem->canView());
// Cannot view the other links
$this->assertFalse($liveLinkItem->canView());
$this->assertFalse($stagedLinkItem->canView());
$this->assertFalse($archivedLinkItem->canView());
}

public function testCanViewUnversioned(): void
{
$record = new SilverStripeNavigatorTest\UnversionedRecord();
$record->write();
$liveLinkItem = new SilverStripeNavigatorItem_LiveLink($record);
$stagedLinkItem = new SilverStripeNavigatorItem_StageLink($record);
$archivedLinkItem = new SilverStripeNavigatorItem_ArchiveLink($record);
$unversionedLinkItem = new SilverStripeNavigatorItem_Unversioned($record);

// Cannot view unversioned link when there's no preview link
$this->assertFalse($unversionedLinkItem->canView());

$record->previewLinkTestProperty = 'some-value';

// Can view unversioned link
$this->assertTrue($unversionedLinkItem->canView());
// Cannot view the other links
$this->assertFalse($liveLinkItem->canView());
$this->assertFalse($stagedLinkItem->canView());
$this->assertFalse($archivedLinkItem->canView());
}
}
12 changes: 10 additions & 2 deletions tests/php/Controllers/SilverStripeNavigatorTest/UnstagedRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@
*/
class UnstagedRecord extends DataObject implements TestOnly, CMSPreviewable
{
private static $table_name = 'SilverStripeNavigatorTest_UnversionedRecord';
private static $table_name = 'SilverStripeNavigatorTest_UnstagedRecord';

private static $show_stage_link = true;

private static $show_live_link = true;

private static $show_unversioned_preview_link = true;

private static $extensions = [
Versioned::class . '.versioned',
];

public $previewLinkTestProperty = null;

public function PreviewLink($action = null)
{
return null;
return $this->previewLinkTestProperty;
}

/**
Expand Down
Loading

0 comments on commit 1897cb3

Please sign in to comment.