You are here

public function LinkitEditorLinkDialogTest::testEditWithDataAttributes in Linkit 8.5

Tests editing a link with data attributes.

File

tests/src/Kernel/LinkitEditorLinkDialogTest.php, line 155

Class

LinkitEditorLinkDialogTest
Tests EditorLinkDialog validation and conversion functionality.

Namespace

Drupal\Tests\linkit\Kernel

Code

public function testEditWithDataAttributes() {
  $entity_label = $this
    ->randomString();

  /** @var \Drupal\Core\Entity\EntityInterface $entity */
  $entity = EntityTest::create([
    'name' => $entity_label,
  ]);
  $entity
    ->save();
  $entity_url = $entity
    ->toUrl('canonical', [
    'path_processing' => FALSE,
  ])
    ->toString();
  $form_object = new EditorLinkDialog();
  $input = [
    'editor_object' => [
      'href' => $entity_url,
      'data-entity-type' => $entity
        ->getEntityTypeId(),
      'data-entity-uuid' => $entity
        ->uuid(),
      'data-entity-substitution' => SubstitutionManagerInterface::DEFAULT_SUBSTITUTION,
    ],
    'dialogOptions' => [
      'title' => 'Edit 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 href field.');
  $this
    ->assertEquals($entity_url, $form['attributes']['href']['#default_value'], 'The href field has the url as default value.');
  $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.');
}