-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2bf91ca
commit a4fb268
Showing
8 changed files
with
136 additions
and
89 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace SilverStripe\LinkField\Tests\Form; | ||
|
||
use SilverStripe\Dev\SapphireTest; | ||
use SilverStripe\Forms\FieldList; | ||
use SilverStripe\LinkField\Form\LinkField; | ||
use SilverStripe\LinkField\Tests\Form\AbstractLinkFieldTest\TestBlock; | ||
use SilverStripe\LinkField\Tests\Controllers\LinkFieldControllerTest\TestPhoneLink; | ||
use SilverStripe\Forms\Form; | ||
use ReflectionObject; | ||
use SilverStripe\Core\ClassInfo; | ||
use SilverStripe\Core\Config\Config; | ||
use SilverStripe\LinkField\Models\Link; | ||
use SilverStripe\LinkField\Models\PhoneLink; | ||
use SilverStripe\LinkField\Models\EmailLink; | ||
|
||
class AbstractLinkFieldTest extends SapphireTest | ||
{ | ||
protected static $fixture_file = 'AbstractLinkFieldTest.yml'; | ||
|
||
protected static $extra_dataobjects = [ | ||
TestBlock::class, | ||
TestPhoneLink::class, | ||
]; | ||
|
||
public function testElementalNamespaceRemoved(): void | ||
{ | ||
$form = new Form(); | ||
$field = new LinkField('PageElements_1_MyLink'); | ||
$form->setFields(new FieldList([$field])); | ||
$block = $this->objFromFixture(TestBlock::class, 'TestBlock01'); | ||
$form->loadDataFrom($block); | ||
$reflector = new ReflectionObject($field); | ||
$method = $reflector->getMethod('getOwnerFields'); | ||
$method->setAccessible(true); | ||
$res = $method->invoke($field); | ||
$this->assertEquals([ | ||
'ID' => $block->ID, | ||
'Class' => TestBlock::class, | ||
'Relation' => 'MyLink', | ||
], $res); | ||
} | ||
|
||
public function testAllowedLinks(): void | ||
{ | ||
// Ensure only default link subclasses are included this test | ||
foreach (ClassInfo::subclassesFor(Link::class) as $className) { | ||
if (strpos($className, 'SilverStripe\\LinkField\\Models\\') !== 0) { | ||
Config::modify()->set($className, 'allowed_by_default', false); | ||
} | ||
} | ||
$field = new LinkField('MyLink'); | ||
$keys = $this->getKeysForAllowedTypes($field); | ||
$this->assertSame(['email', 'external', 'file', 'phone', 'sitetree'], $keys); | ||
// Test can disallow globally | ||
Config::modify()->set(PhoneLink::class, 'allowed_by_default', false); | ||
$keys = $this->getKeysForAllowedTypes($field); | ||
$this->assertSame(['email', 'external', 'file', 'sitetree'], $keys); | ||
// Can override with setAllowedTypes() | ||
$field->setAllowedTypes([EmailLink::class, PhoneLink::class]); | ||
$keys = $this->getKeysForAllowedTypes($field); | ||
$this->assertSame(['email', 'phone'], $keys); | ||
} | ||
|
||
private function getKeysForAllowedTypes(LinkField $field) | ||
{ | ||
$rawJson = $field->getTypesProp(); | ||
$types = json_decode($rawJson, true); | ||
$allowedTypes = array_filter($types, fn($type) => $type['allowed']); | ||
$keys = array_column($allowedTypes, 'key'); | ||
sort($keys); | ||
return $keys; | ||
} | ||
} |
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
4 changes: 2 additions & 2 deletions
4
tests/php/Form/LinkFieldTest/TestBlock.php → .../Form/AbstractLinkFieldTest/TestBlock.php
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 was deleted.
Oops, something went wrong.