public function LinkitEditorLinkDialogTest::testAdd in Linkit 8.5
Tests adding a link.
File
- tests/
src/ Kernel/ LinkitEditorLinkDialogTest.php, line 92
Class
- LinkitEditorLinkDialogTest
- Tests EditorLinkDialog validation and conversion functionality.
Namespace
Drupal\Tests\linkit\KernelCode
public function testAdd() {
$entity_label = $this
->randomString();
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = EntityTest::create([
'name' => $entity_label,
]);
$entity
->save();
$form_object = new EditorLinkDialog();
$input = [
'editor_object' => [],
'dialogOptions' => [
'title' => 'Add Link',
'dialogClass' => 'editor-link-dialog',
'autoResize' => 'true',
],
'_drupal_ajax' => '1',
'ajax_page_state' => [
'theme' => 'bartik',
'theme_token' => 'some-token',
'libraries' => '',
],
];
$form_state = (new FormState())
->setRequestMethod('POST')
->setUserInput($input)
->addBuildInfo('args', [
$this->editor,
]);
/** @var \Drupal\Core\Form\FormBuilderInterface $form_builder */
$form_builder = $this->container
->get('form_builder');
$form_id = $form_builder
->getFormId($form_object, $form_state);
$form = $form_builder
->retrieveForm($form_id, $form_state);
$form_builder
->prepareForm($form_id, $form, $form_state);
$form_builder
->processForm($form_id, $form, $form_state);
$this
->assertEquals('linkit.autocomplete', $form['attributes']['href']['#autocomplete_route_name'], 'Linkit is enabled on the linkit field.');
$this
->assertEmpty($form['attributes']['href']['#default_value'], 'The linkit field is empty.');
$form_state
->setValue([
'attributes',
'href',
], 'https://example.com/');
$form_state
->setValue('href_dirty_check', '');
$form_state
->setValue([
'attributes',
'data-entity-type',
], $this
->randomString());
$form_state
->setValue([
'attributes',
'data-entity-uuid',
], $this
->randomString());
$form_state
->setValue([
'attributes',
'data-entity-substitution',
], $this
->randomString());
$form_builder
->submitForm($form_object, $form_state);
$this
->assertEmpty($form_state
->getValue([
'attributes',
'data-entity-type',
]));
$this
->assertEmpty($form_state
->getValue([
'attributes',
'data-entity-uuid',
]));
$this
->assertEmpty($form_state
->getValue([
'attributes',
'data-entity-substitution',
]));
$entity_url = $entity
->toUrl('canonical', [
'path_processing' => FALSE,
])
->toString();
$form_state
->setValue([
'attributes',
'href',
], $entity_url);
$form_state
->setValue('href_dirty_check', $entity_url);
$form_state
->setValue([
'attributes',
'data-entity-type',
], $entity
->getEntityTypeId());
$form_state
->setValue([
'attributes',
'data-entity-uuid',
], $entity
->uuid());
$form_state
->setValue([
'attributes',
'data-entity-substitution',
], SubstitutionManagerInterface::DEFAULT_SUBSTITUTION);
$form_builder
->submitForm($form_object, $form_state);
$this
->assertEquals($entity
->getEntityTypeId(), $form_state
->getValue([
'attributes',
'data-entity-type',
]), 'Attribute "data-entity-type" exists and has the correct value.');
$this
->assertEquals($entity
->uuid(), $form_state
->getValue([
'attributes',
'data-entity-uuid',
]), 'Attribute "data-entity-uuid" exists and has the correct value.');
$this
->assertEquals(SubstitutionManagerInterface::DEFAULT_SUBSTITUTION, $form_state
->getValue([
'attributes',
'data-entity-substitution',
]), 'Attribute "data-entity-substitution" exists and has the correct value.');
}