-
Notifications
You must be signed in to change notification settings - Fork 15
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 #126 from creative-commoners/pulls/4/default-link-…
…title NEW Default link title for each link type
- Loading branch information
Showing
9 changed files
with
189 additions
and
21 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
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
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 |
---|---|---|
@@ -1 +1 @@ | ||
<a href="$URL" <% if $OpenInNew %>target="_blank" rel="noopener noreferrer"<% end_if %>>$Title</a> | ||
<a href="$URL" <% if $OpenInNew %>target="_blank" rel="noopener noreferrer"<% end_if %>>$DisplayTitle</a> |
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,14 @@ | ||
<?php | ||
|
||
namespace SilverStripe\LinkField\Tests\Extensions; | ||
|
||
use SilverStripe\Dev\TestOnly; | ||
use SilverStripe\ORM\DataExtension; | ||
|
||
class ExternalLinkExtension extends DataExtension implements TestOnly | ||
{ | ||
public function updateDefaultLinkTitle(&$defaultLinkTitle): void | ||
{ | ||
$defaultLinkTitle = sprintf('External Link: %s', $this->owner->getURL()); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
use SilverStripe\ORM\DataObject; | ||
use SilverStripe\ORM\ValidationException; | ||
use SilverStripe\Versioned\Versioned; | ||
use SilverStripe\LinkField\Tests\Extensions\ExternalLinkExtension; | ||
|
||
class LinkTest extends SapphireTest | ||
{ | ||
|
@@ -28,6 +29,12 @@ class LinkTest extends SapphireTest | |
*/ | ||
protected static $fixture_file = 'LinkTest.yml'; | ||
|
||
protected static $required_extensions = [ | ||
ExternalLink::class => [ | ||
ExternalLinkExtension::class, | ||
], | ||
]; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
@@ -78,8 +85,7 @@ public function testSiteTreeLinkTitleFallback(): void | |
|
||
// The actual Database Title field should still be null | ||
$this->assertNull($model->getField('Title')); | ||
// But when we fetch the field (ViewableData) it should return the value from getTitle() | ||
$this->assertEquals($page->Title, $model->Title, 'We expect to get the linked Page title'); | ||
$this->assertEquals(null, $model->Title, 'We expect that link does not have a title'); | ||
|
||
$customTitle = 'My custom title'; | ||
$model->Title = $customTitle; | ||
|
@@ -329,4 +335,76 @@ public function linkUrlCasesDataProvider(): array | |
], | ||
]; | ||
} | ||
|
||
function linkDefaultTitleDataProvider(): array | ||
{ | ||
return [ | ||
'page link' => [ | ||
'identifier' => 'page-link-1', | ||
'class' => SiteTreeLink::class, | ||
'expected' => 'PageLink1' | ||
], | ||
'email link' => [ | ||
'identifier' => 'email-link-with-email', | ||
'class' => EmailLink::class, | ||
'expected' => 'EmailLinkWithEmail' | ||
], | ||
'external link' => [ | ||
'identifier' => 'external-link-with-url', | ||
'class' => ExternalLink::class, | ||
'expected' => 'ExternalLinkWithUrl' | ||
], | ||
'phone link' => [ | ||
'identifier' => 'phone-link-with-phone', | ||
'class' => PhoneLink::class, | ||
'expected' => 'PhoneLinkWithPhone' | ||
], | ||
'file link' => [ | ||
'identifier' => 'file-link-no-image', | ||
'class' => FileLink::class, | ||
'expected' => 'File missing' | ||
], | ||
'page link with default title' => [ | ||
'identifier' => 'page-link-with-default-title', | ||
'class' => SiteTreeLink::class, | ||
'expected' => 'Page1' | ||
], | ||
'page link no page default title' => [ | ||
'identifier' => 'page-link-no-page-default-title', | ||
'class' => SiteTreeLink::class, | ||
'expected' => 'Page missing' | ||
], | ||
'email link with default title' => [ | ||
'identifier' => 'email-link-with-default-title', | ||
'class' => EmailLink::class, | ||
'expected' => '[email protected]' | ||
], | ||
'external link with default title' => [ | ||
'identifier' => 'external-link-with-default-title', | ||
'class' => ExternalLink::class, | ||
'expected' => 'External Link: https://google.com' | ||
], | ||
'phone link with default title' => [ | ||
'identifier' => 'phone-link-with-default-title', | ||
'class' => PhoneLink::class, | ||
'expected' => '+64 4 978 7330' | ||
], | ||
'file link with default title' => [ | ||
'identifier' => 'file-link-with-default-title', | ||
'class' => FileLink::class, | ||
'expected' => '600x400.png' | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider linkDefaultTitleDataProvider | ||
*/ | ||
public function testDefaultLinkTitle(string $identifier, string $class, string $expected): void | ||
{ | ||
/** @var Link $link */ | ||
$link = $this->objFromFixture($class, $identifier); | ||
|
||
$this->assertEquals($expected, $link->getDisplayTitle()); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ SilverStripe\CMS\Model\SiteTree: | |
SilverStripe\Assets\Image: | ||
image-1: | ||
Title: 'Image1' | ||
image-2: null | ||
|
||
SilverStripe\LinkField\Models\Link: | ||
link-1: | ||
|
@@ -36,31 +37,45 @@ SilverStripe\LinkField\Models\SiteTreeLink: | |
QueryString: 'param1=value1¶m2=option2' | ||
Anchor: 'my-anchor' | ||
Page: =>SilverStripe\CMS\Model\SiteTree.page-1 | ||
page-link-with-default-title: | ||
Page: =>SilverStripe\CMS\Model\SiteTree.page-1 | ||
page-link-no-page-default-title: | ||
Page: null | ||
|
||
SilverStripe\LinkField\Models\EmailLink: | ||
email-link-with-email: | ||
Title: 'EmailLinkWithEmail' | ||
Email: '[email protected]' | ||
email-link-no-email: | ||
Title: 'EmailLinkNoEmail' | ||
email-link-with-default-title: | ||
Email: '[email protected]' | ||
|
||
SilverStripe\LinkField\Models\ExternalLink: | ||
external-link-with-url: | ||
Title: 'ExternalLinkWithUrl' | ||
ExternalUrl: 'https://google.com' | ||
external-link-no-url: | ||
Title: 'ExternalLinkNoUrl' | ||
external-link-with-default-title: | ||
ExternalUrl: 'https://google.com' | ||
|
||
SilverStripe\LinkField\Models\PhoneLink: | ||
phone-link-with-phone: | ||
Title: 'PhoneLinkWithPhone' | ||
Phone: '+64 4 978 7330' | ||
phone-link-no-phone: | ||
Title: 'PhoneLinkNoPhone' | ||
phone-link-with-default-title: | ||
Phone: '+64 4 978 7330' | ||
|
||
SilverStripe\LinkField\Models\FileLink: | ||
file-link-with-image: | ||
Title: 'FileLinkWithImage' | ||
File: =>SilverStripe\Assets\Image.image-1 | ||
file-link-no-image: | ||
Title: 'FileLinkNoImage' | ||
Title: null | ||
File: =>SilverStripe\Assets\Image.image-2 | ||
OpenInNew: true | ||
file-link-with-default-title: | ||
File: =>SilverStripe\Assets\Image.image-1 |