FieldDefaultTokenBasicTest.php in Field default token 8
File
tests/src/Kernel/FieldDefaultTokenBasicTest.php
View source
<?php
namespace Drupal\Tests\field_default_token\Kernel;
use Drupal\entity_test\Entity\EntityTest;
class FieldDefaultTokenBasicTest extends FieldDefaultTokenKernelTestBase {
protected $siteName;
protected $entityTypeId = 'entity_test';
public static $modules = [
'entity_test',
'system',
'user',
];
protected function setUp() {
parent::setUp();
$config_factory = $this->container
->get('config.factory');
$this->siteName = $config_factory
->get('system.site')
->get('name');
}
public function testCallbackNewField() {
$field = $this
->createField();
$field
->setDefaultValue('This is the site name: [site:name]')
->save();
$this
->assertEquals('field_default_token_default_value_callback', $field
->getDefaultValueCallback());
}
public function testCallbackExistingField() {
$field = $this
->createField();
$field
->save();
$this
->assertEquals('', $field
->getDefaultValueCallback());
$field
->setDefaultValue('This is the site name: [site:name]')
->save();
$this
->assertEquals('field_default_token_default_value_callback', $field
->getDefaultValueCallback());
}
public function testCallbackRemoval() {
$field = $this
->createField();
$field
->setDefaultValue('This is the site name: [site:name]')
->save();
$this
->assertEquals('field_default_token_default_value_callback', $field
->getDefaultValueCallback());
$field
->setDefaultValue('There are no tokens to see here, move along')
->save();
$this
->assertNull($field
->getDefaultValueCallback());
}
public function testReplacement() {
$field = $this
->createField();
$field
->setDefaultValue('This is the site name: [site:name]')
->save();
$entity = EntityTest::create();
$entity
->save();
$expected = [
[
'value' => 'This is the site name: ' . $this->siteName,
],
];
$this
->assertEquals($expected, $field
->getDefaultValue($entity));
}
}