Merge pull request #2737 from creative-commoners/pulls/4.11/allow-empty-preview-urls

Issue was fixed
This commit is contained in:
Sabina Talipova 2022-05-11 14:45:34 +12:00 committed by GitHub
commit 1897cb3360
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 317 additions and 70 deletions

View File

@ -46,10 +46,8 @@ class SilverStripeNavigatorItem_ArchiveLink extends SilverStripeNavigatorItem
public function getLink() public function getLink()
{ {
return Controller::join_links( $link = $this->record->PreviewLink();
$this->record->PreviewLink(), return $link ? Controller::join_links($link, '?archiveDate=' . urlencode($this->record->LastEdited ?? '')) : '';
'?archiveDate=' . urlencode($this->record->LastEdited ?? '')
);
} }
public function canView($member = null) public function canView($member = null)
@ -62,6 +60,7 @@ class SilverStripeNavigatorItem_ArchiveLink extends SilverStripeNavigatorItem
&& $this->isArchived() && $this->isArchived()
// Don't follow redirects in preview, they break the CMS editing form // Don't follow redirects in preview, they break the CMS editing form
&& !($record instanceof RedirectorPage) && !($record instanceof RedirectorPage)
&& $this->getLink()
); );
} }

View File

@ -48,7 +48,8 @@ class SilverStripeNavigatorItem_LiveLink extends SilverStripeNavigatorItem
public function getLink() 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) public function canView($member = null)
@ -60,6 +61,7 @@ class SilverStripeNavigatorItem_LiveLink extends SilverStripeNavigatorItem
&& $this->showLiveLink() && $this->showLiveLink()
&& $record->hasStages() && $record->hasStages()
&& $this->getLivePage() && $this->getLivePage()
&& $this->getLink()
); );
} }

View File

@ -50,8 +50,12 @@ class SilverStripeNavigatorItem_StageLink extends SilverStripeNavigatorItem
public function getLink() public function getLink()
{ {
$date = Versioned::current_archived_date(); $date = Versioned::current_archived_date();
$link = $this->record->PreviewLink();
if (!$link) {
return '';
}
return Controller::join_links( return Controller::join_links(
$this->record->PreviewLink(), $link,
'?stage=Stage', '?stage=Stage',
$date ? '?archiveDate=' . $date : null $date ? '?archiveDate=' . $date : null
); );
@ -66,6 +70,7 @@ class SilverStripeNavigatorItem_StageLink extends SilverStripeNavigatorItem
&& $this->showStageLink() && $this->showStageLink()
&& $record->hasStages() && $record->hasStages()
&& $this->getDraftPage() && $this->getDraftPage()
&& $this->getLink()
); );
} }

View File

@ -19,7 +19,7 @@ class SilverStripeNavigatorItem_Unversioned extends SilverStripeNavigatorItem
public function getLink() public function getLink()
{ {
return $this->getRecord()->PreviewLink(); return $this->getRecord()->PreviewLink() ?? '';
} }
public function getTitle() public function getTitle()
@ -40,11 +40,24 @@ class SilverStripeNavigatorItem_Unversioned extends SilverStripeNavigatorItem
public function canView($member = null) public function canView($member = null)
{ {
return ( return (
!$this->getRecord()->hasExtension(Versioned::class) $this->recordIsUnversioned()
&& $this->showUnversionedLink() && $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. * True if the record is configured to display this item.
* *

View File

@ -6,6 +6,7 @@ use SilverStripe\CMS\Controllers\SilverStripeNavigator;
use SilverStripe\CMS\Controllers\SilverStripeNavigatorItem_ArchiveLink; use SilverStripe\CMS\Controllers\SilverStripeNavigatorItem_ArchiveLink;
use SilverStripe\CMS\Controllers\SilverStripeNavigatorItem_LiveLink; use SilverStripe\CMS\Controllers\SilverStripeNavigatorItem_LiveLink;
use SilverStripe\CMS\Controllers\SilverStripeNavigatorItem_StageLink; use SilverStripe\CMS\Controllers\SilverStripeNavigatorItem_StageLink;
use SilverStripe\CMS\Controllers\SilverStripeNavigatorItem_Unversioned;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
@ -15,53 +16,224 @@ class SilverStripeNavigatorTest extends SapphireTest
protected static $extra_dataobjects = [ protected static $extra_dataobjects = [
SilverStripeNavigatorTest\UnstagedRecord::class, SilverStripeNavigatorTest\UnstagedRecord::class,
SilverStripeNavigatorTest\UnversionedRecord::class,
SilverStripeNavigatorTest\VersionedRecord::class,
]; ];
public function testGetItems() public function testGetItemsAutoDiscovery(): void
{ {
$page = $this->objFromFixture('Page', 'page1'); $record = new SilverStripeNavigatorTest\VersionedRecord();
$navigator = new SilverStripeNavigator($page); $record->PreviewLinkTestProperty = 'some-value';
$record->write();
$items = $navigator->getItems(); $navigator = new SilverStripeNavigator($record);
$classes = array_map('get_class', $items->toArray() ?? []); $classes = array_map('get_class', $navigator->getItems()->toArray());
$this->assertContains(
SilverStripeNavigatorItem_StageLink::class,
$classes,
'Adds default classes'
);
$this->assertContains( $this->assertContains(
SilverStripeNavigatorTest_TestItem::class, SilverStripeNavigatorTest_TestItem::class,
$classes, $classes,
'Autodiscovers new classes' 'Autodiscovers new classes'
); );
// Non-versioned items don't have stage / live
} }
public function testCanView() public function testGetItemsPublished(): void
{ {
$page = $this->objFromFixture('Page', 'page1'); $record = new SilverStripeNavigatorTest\VersionedRecord();
$admin = $this->objFromFixture(Member::class, 'admin'); $record->PreviewLinkTestProperty = 'some-value';
$navigator = new SilverStripeNavigator($page); $record->write();
$record->publishRecursive();
$navigator = new SilverStripeNavigator($record);
$classes = array_map('get_class', $navigator->getItems()->toArray());
// TODO Shouldn't be necessary but SapphireTest logs in as ADMIN by default // Has the live and staged links
$this->logInWithPermission('CMS_ACCESS_CMSMain'); $this->assertContains(SilverStripeNavigatorItem_LiveLink::class, $classes);
$items = $navigator->getItems(); $this->assertContains(SilverStripeNavigatorItem_StageLink::class, $classes);
$classes = array_map('get_class', $items->toArray() ?? []);
$this->assertNotContains(SilverStripeNavigatorTest_ProtectedTestItem::class, $classes);
$this->logInWithPermission('ADMIN'); // Does not have the other links
$items = $navigator->getItems(); $this->assertNotContains(SilverStripeNavigatorItem_ArchiveLink::class, $classes);
$classes = array_map('get_class', $items->toArray() ?? []); $this->assertNotContains(SilverStripeNavigatorItem_Unversioned::class, $classes);
$this->assertContains(SilverStripeNavigatorTest_ProtectedTestItem::class, $classes); }
// Unversioned record shouldn't be viewable in stage / live specific views public function testGetItemsStaged(): void
$unversioned = new SilverStripeNavigatorTest\UnstagedRecord(); {
$navigator2 = new SilverStripeNavigator($unversioned); $record = new SilverStripeNavigatorTest\VersionedRecord();
$classes = array_map('get_class', $navigator2->getItems()->toArray() ?? []); $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());
// 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_LiveLink::class, $classes);
$this->assertNotContains(SilverStripeNavigatorItem_StageLink::class, $classes); $this->assertNotContains(SilverStripeNavigatorItem_StageLink::class, $classes);
$this->assertNotContains(SilverStripeNavigatorItem_UnversionedLink::class, $classes);
}
public function testGetItemsUnstaged(): void
{
$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_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());
} }
} }

View File

@ -12,15 +12,23 @@ use SilverStripe\Versioned\Versioned;
*/ */
class UnstagedRecord extends DataObject implements TestOnly, CMSPreviewable 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 = [ private static $extensions = [
Versioned::class . '.versioned', Versioned::class . '.versioned',
]; ];
public $previewLinkTestProperty = null;
public function PreviewLink($action = null) public function PreviewLink($action = null)
{ {
return null; return $this->previewLinkTestProperty;
} }
/** /**

View File

@ -0,0 +1,36 @@
<?php
namespace SilverStripe\CMS\Tests\Controllers\SilverStripeNavigatorTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\CMSPreviewable;
use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned;
class UnversionedRecord extends DataObject implements TestOnly, CMSPreviewable
{
private static $table_name = 'SilverStripeNavigatorTest_UnversionedRecord';
private static $show_stage_link = true;
private static $show_live_link = true;
private static $show_unversioned_preview_link = true;
public $previewLinkTestProperty = null;
public function PreviewLink($action = null)
{
return $this->previewLinkTestProperty;
}
public function getMimeType()
{
return 'text/html';
}
public function CMSEditLink()
{
return null;
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace SilverStripe\CMS\Tests\Controllers\SilverStripeNavigatorTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\CMSPreviewable;
use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned;
class VersionedRecord extends DataObject implements TestOnly, CMSPreviewable
{
private static $table_name = 'SilverStripeNavigatorTest_VersionedRecord';
private static $show_stage_link = true;
private static $show_live_link = true;
private static $show_unversioned_preview_link = true;
private static $db = [
'PreviewLinkTestProperty' => 'Text',
];
private static $extensions = [
Versioned::class,
];
public function PreviewLink($action = null)
{
return $this->PreviewLinkTestProperty;
}
public function getMimeType()
{
return 'text/html';
}
public function CMSEditLink()
{
return null;
}
}

View File

@ -1,30 +0,0 @@
<?php
namespace SilverStripe\CMS\Tests\Controllers;
use SilverStripe\CMS\Controllers\SilverStripeNavigatorItem;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
class SilverStripeNavigatorTest_ProtectedTestItem extends SilverStripeNavigatorItem implements TestOnly
{
public function getTitle()
{
return self::class;
}
public function getHTML()
{
return null;
}
public function canView($member = null)
{
if (!$member) {
$member = Security::getCurrentUser();
}
return Permission::checkMember($member, 'ADMIN');
}
}