Skip to content

Commit

Permalink
Merge pull request #982 from creative-commoners/pulls/4.9/allow-empty…
Browse files Browse the repository at this point in the history
…-preview

Issue was solved
  • Loading branch information
sabina-talipova authored May 12, 2022
2 parents a847d96 + 24f6cde commit 38bd12b
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 8 deletions.
22 changes: 17 additions & 5 deletions src/Models/BaseElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ public function AbsoluteLink($action = null)

/**
* @param string|null $action
* @return string
* @return string|null
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws \SilverStripe\ORM\ValidationException
*/
Expand All @@ -721,16 +721,28 @@ public function Link($action = null)

/**
* @param string|null $action
* @return string
* @return string|null
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws \SilverStripe\ORM\ValidationException
*/
public function PreviewLink($action = null)
{
$action = $action . '?ElementalPreview=' . mt_rand();
$link = $this->Link($action);
$this->extend('updatePreviewLink', $link);
$link = null;
if ($page = $this->getPage()) {
if (ClassInfo::hasMethod($page, 'Link')) {
$link = $page->Link($action);
}
if (!$link && ($page instanceof CMSPreviewable)) {
$link = $page->PreviewLink($action);
}
if ($link) {
// The ElementalPreview getvar is used in ElementalPageExtension
// The anchor must be at the end of the URL to function correctly
$link .= '?ElementalPreview=' . mt_rand() . '#' . $this->getAnchor();
}
}

$this->extend('updatePreviewLink', $link);
return $link;
}

Expand Down
46 changes: 46 additions & 0 deletions tests/BaseElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,50 @@ public function testAbsoluteLink(string $class, string $element, ?string $link)
$absoluteLink = $object->AbsoluteLink();
$this->assertEquals($link, $absoluteLink);
}

public function previewLinksProvider()
{
return [
// Element on Page
[
ElementContent::class,
'content1',
'/test-elemental/',
],
// Element in DataObject WITHOUT PreviewLink or Link
[
TestElement::class,
'elementDataObject2',
null,
],
// Element in DataObject WITH PreviewLink WITHOUT Link
[
TestElement::class,
'elementDataObject3',
'preview-link',
],
// Element in DataObject WITH PreviewLink AND Link (different paths)
[
TestElement::class,
'elementDataObject4',
'base-link',
],
];
}

/**
* @dataProvider previewLinksProvider
*/
public function testPreviewLink(string $class, string $elementIdentifier, ?string $link)
{
/** @var BaseElement $element */
$element = $this->objFromFixture($class, $elementIdentifier);

if ($link) {
$regex = '/^' . preg_quote($link . '?ElementalPreview=', '/') .'\d*#' . $element->getAnchor() . '$/';
$this->assertTrue((bool)preg_match($regex, $element->PreviewLink()));
} else {
$this->assertSame($link, $element->PreviewLink());
}
}
}
26 changes: 25 additions & 1 deletion tests/ElementalAreaDataObjectTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ DNADesign\Elemental\Models\ElementalArea:
areaDataObject2:
Title: Area 2
OwnerClassName: DNADesign\Elemental\Tests\Src\TestDataObject
areaDataObject3:
Title: Area 3
OwnerClassName: DNADesign\Elemental\Tests\Src\TestPreviewableDataObject
areaDataObject4:
Title: Area 4
OwnerClassName: DNADesign\Elemental\Tests\Src\TestPreviewableDataObjectWithLink

DNADesign\Elemental\Tests\Src\TestDataObjectWithCMSEditLink:
dataObject1:
Expand All @@ -13,9 +19,19 @@ DNADesign\Elemental\Tests\Src\TestDataObjectWithCMSEditLink:

DNADesign\Elemental\Tests\Src\TestDataObject:
dataObject2:
Title: DataObject without CMSEditLink method
Title: DataObject without CMSEditLink, Link, or PreviewLink methods
ElementalAreaID: =>DNADesign\Elemental\Models\ElementalArea.areaDataObject2

DNADesign\Elemental\Tests\Src\TestPreviewableDataObject:
dataObject3:
Title: DataObject with PreviewLink method
ElementalAreaID: =>DNADesign\Elemental\Models\ElementalArea.areaDataObject3

DNADesign\Elemental\Tests\Src\TestPreviewableDataObjectWithLink:
dataObject4:
Title: DataObject with PreviewLink and Link methods
ElementalAreaID: =>DNADesign\Elemental\Models\ElementalArea.areaDataObject4

DNADesign\Elemental\Tests\Src\TestElement:
elementDataObject1:
Title: Element 1
Expand All @@ -25,6 +41,14 @@ DNADesign\Elemental\Tests\Src\TestElement:
Title: Element 2
TestValue: 'Hello Test'
ParentID: =>DNADesign\Elemental\Models\ElementalArea.areaDataObject2
elementDataObject3:
Title: Element 3
TestValue: 'Hello Test'
ParentID: =>DNADesign\Elemental\Models\ElementalArea.areaDataObject3
elementDataObject4:
Title: Element 4
TestValue: 'Hello Test'
ParentID: =>DNADesign\Elemental\Models\ElementalArea.areaDataObject4

DNADesign\Elemental\Tests\Src\TestElementDataObject:
testElementDataObject1:
Expand Down
2 changes: 0 additions & 2 deletions tests/Src/TestDataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace DNADesign\Elemental\Tests\Src;

use DNADesign\Elemental\Models\ElementalArea;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\ORM\DataObject;
use SilverStripe\Dev\TestOnly;

Expand Down
26 changes: 26 additions & 0 deletions tests/Src/TestPreviewableDataObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace DNADesign\Elemental\Tests\Src;

use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\CMSPreviewable;

class TestPreviewableDataObject extends TestDataObject implements TestOnly, CMSPreviewable
{
private static $table_name = 'TestPreviewableDataObject';

public function PreviewLink($action = null)
{
return 'preview-link';
}

public function getMimeType()
{
return null;
}

public function CMSEditLink()
{
return null;
}
}
15 changes: 15 additions & 0 deletions tests/Src/TestPreviewableDataObjectWithLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace DNADesign\Elemental\Tests\Src;

use SilverStripe\Dev\TestOnly;

class TestPreviewableDataObjectWithLink extends TestPreviewableDataObject implements TestOnly
{
private static $table_name = 'TestPreviewableDataObjectWithLink';

public function Link($action = null)
{
return 'base-link';
}
}

0 comments on commit 38bd12b

Please sign in to comment.