View source
<?php
namespace Drupal\Tests\fixed_text_link_formatter\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\link\LinkItemInterface;
use Drupal\Tests\BrowserTestBase;
class FixedTextLinkTest extends BrowserTestBase {
public static $modules = [
'node',
'fixed_text_link_formatter',
];
private $nodeType;
private $fieldStorage;
private $field;
protected $defaultTheme = 'classy';
private $entityDisplayRepository;
private $linkFieldName;
public function setUp() {
parent::setUp();
$this->entityDisplayRepository = $this->container
->get('entity_display.repository');
$this->nodeType = $this
->drupalCreateContentType();
$this->linkFieldName = mb_strtolower($this
->randomMachineName());
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $this->linkFieldName,
'entity_type' => 'node',
'type' => 'link',
]);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => $this->nodeType
->id(),
'settings' => [
'title' => DRUPAL_DISABLED,
'link_type' => LinkItemInterface::LINK_GENERIC,
],
]);
$this->field
->save();
$this->entityDisplayRepository
->getFormDisplay('node', $this->nodeType
->id(), 'default')
->setComponent($this->linkFieldName, [
'type' => 'link_default',
'settings' => [
'placeholder_url' => 'http://example.com',
],
])
->save();
}
public function testFormatsWithConfiguredText() {
$linkText = 'View our amazing website';
$linkClass = 'test-class';
$this->entityDisplayRepository
->getViewDisplay('node', $this->nodeType
->id(), 'full')
->setComponent($this->linkFieldName, [
'type' => 'fixed_text_link',
'settings' => [
'link_text' => $linkText,
'link_class' => $linkClass,
],
])
->save();
$node = $this
->createNode([
'type' => $this->nodeType
->id(),
$this->linkFieldName => [
'uri' => 'http://example.com/test',
],
]);
$this
->getSession()
->visit($node
->toUrl()
->toString());
$this
->assertSession()
->linkExists($linkText);
$this
->assertSession()
->elementExists('css', ".{$linkClass}");
}
public function testRegression3054339() {
$linkText = 'View amazing website';
$this->entityDisplayRepository
->getViewDisplay('node', $this->nodeType
->id(), 'full')
->setComponent($this->linkFieldName, [
'type' => 'fixed_text_link',
'settings' => [
'link_text' => $linkText,
'allow_override' => TRUE,
],
])
->save();
$overrideText = 'Override test';
$node = $this
->createNode([
'type' => $this->nodeType
->id(),
$this->linkFieldName => [
'uri' => 'http://example.com/test',
'title' => $overrideText,
],
]);
$this
->getSession()
->visit($node
->toUrl()
->toString());
$this
->assertSession()
->linkExists($overrideText);
$this
->assertSession()
->pageTextNotContains($linkText);
}
}