Skip to content

Commit

Permalink
MNT Correct test SilverStripeNavigatorItems.
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed May 6, 2022
1 parent c02549c commit 3a990c9
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 64 deletions.
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
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit 3a990c9

Please sign in to comment.