LinkitEditorLinkDialogTest.php 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

namespace Drupal\Tests\linkit\Kernel;

use Drupal\Core\Form\FormState;
use Drupal\editor\Entity\Editor;
use Drupal\editor\Form\EditorLinkDialog;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\filter\Entity\FilterFormat;
10
use Drupal\linkit\SubstitutionManagerInterface;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
use Drupal\linkit\Tests\ProfileCreationTrait;

/**
 * Tests EditorLinkDialog validation and conversion functionality.
 *
 * @group linkit
 */
class LinkitEditorLinkDialogTest extends LinkitKernelTestBase {

  use ProfileCreationTrait;

  /**
   * The linkit profile.
   *
   * @var \Drupal\linkit\ProfileInterface
   */
  protected $linkitProfile;

  /**
   * Filter format for testing.
   *
   * @var \Drupal\filter\FilterFormatInterface
   */
  protected $format;

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = ['editor', 'ckeditor', 'entity_test'];

  /**
   * Sets up the test.
   */
  protected function setUp() {
    parent::setUp();

    $this->installEntitySchema('entity_test');
50
    $this->installSchema('system', ['key_value_expire']);
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

    // Create a profile.
    $this->linkitProfile = $this->createProfile();

    /** @var \Drupal\linkit\MatcherManager $matcherManager */
    $matcherManager = $this->container->get('plugin.manager.linkit.matcher');

    // Add the entity_test matcher to the profile.
    /** @var \Drupal\linkit\MatcherInterface $plugin */
    $plugin = $matcherManager->createInstance('entity:entity_test');
    $this->linkitProfile->addMatcher($plugin->getConfiguration());
    $this->linkitProfile->save();

    // Add a text format.
    $this->format = FilterFormat::create([
      'format' => 'filtered_html',
      'name' => 'Filtered HTML',
      'weight' => 0,
      'filters' => [],
    ]);
    $this->format->save();

    // Set up editor.
    $editor = Editor::create([
      'format' => 'filtered_html',
      'editor' => 'ckeditor',
    ]);
    $editor->setSettings([
      'plugins' => [
        'drupallink' => [
          'linkit_enabled' => TRUE,
          'linkit_profile' => $this->linkitProfile->id(),
        ],
      ],
    ]);
    $editor->save();
  }

  /**
   * Tests adding a link.
   */
  public function testAdd() {
    $entity_label = $this->randomString();
    $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->format]);

    /** @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);

125
    $this->assertEquals('linkit.autocomplete', $form['linkit']['#autocomplete_route_name'], 'Linkit is enabled on the linkit field.');
anon's avatar
anon committed
126
    $this->assertEmpty($form['linkit']['#default_value'], 'The linkit field is empty.');
127

128
    $form_state->setValue('linkit', 'url_without_schema');
129 130
    $form_builder->submitForm($form_object, $form_state);
    $this->assertEmpty($form_state->getErrors(), 'Got no validation errors for url without schema.');
131 132
    $this->assertEmpty($form_state->getValue(['attributes', 'data-entity-type']));
    $this->assertEmpty($form_state->getValue(['attributes', 'data-entity-uuid']));
anon's avatar
anon committed
133
    $this->assertEmpty($form_state->getValue(['attributes', 'data-entity-substitution']));
134 135 136 137

    $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);
138 139 140 141 142

    $form_builder->submitForm($form_object, $form_state);
    $this->assertEmpty($form_state->getErrors(), 'Got no validation errors for correct URI.');
    $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.');
143
    $this->assertEquals(SubstitutionManagerInterface::DEFAULT_SUBSTITUTION, $form_state->getValue(['attributes', 'data-entity-substitution']), 'Attribute "data-entity-substitution" exists and has the correct value.');
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  }

  /**
   * Tests editing a link with data attributes.
   */
  public function testEditWithDataAttributes() {
    $entity_label = $this->randomString();
    $entity = EntityTest::create(['name' => $entity_label]);
    $entity->save();

    $entity_no_access = EntityTest::create(['name' => 'forbid_access']);
    $entity_no_access->save();

    $form_object = new EditorLinkDialog();

    $input = [
      'editor_object' => [
161
        'href' => '#',
162 163
        'data-entity-type' => $entity->getEntityTypeId(),
        'data-entity-uuid' => $entity->uuid(),
164
        'data-entity-substitution' => SubstitutionManagerInterface::DEFAULT_SUBSTITUTION,
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
      ],
      '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->format]);

    /** @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);

190 191 192 193 194
    $this->assertEquals('linkit.autocomplete', $form['linkit']['#autocomplete_route_name'], 'Linkit is enabled on the linkit field.');
    $this->assertEquals($entity->label(), $form['linkit']['#default_value'], 'The linkit field has the label 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.');
195 196 197 198 199

    // Make sure the dialog don't display entity labels for inaccessible
    // entities.
    $input = [
      'editor_object' => [
200
        'href' => '#',
201 202
        'data-entity-type' => $entity_no_access->getEntityTypeId(),
        'data-entity-uuid' => $entity_no_access->uuid(),
203
        'data-entity-substitution' => SubstitutionManagerInterface::DEFAULT_SUBSTITUTION,
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
      ],
      '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->format]);

    /** @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);

229 230
    $this->assertEquals('linkit.autocomplete', $form['linkit']['#autocomplete_route_name'], 'Linkit is enabled on the linkit field.');
    $this->assertEmpty($form['linkit']['#default_value']);
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
  }

  /**
   * Tests editing a link without data attributes.
   */
  public function testEditWithoutDataAttributes() {
    $form_object = new EditorLinkDialog();

    $input = [
      'editor_object' => [
        'href' => 'http://example.com/',
      ],
      '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->format]);

    /** @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);

267 268 269 270 271
    $this->assertEquals('linkit.autocomplete', $form['linkit']['#autocomplete_route_name'], 'Linkit is enabled on the href field.');
    $this->assertEquals('http://example.com/', $form['linkit']['#default_value'], 'The linkit field default value is the external URI.');
    $this->assertEmpty($form['attributes']['data-entity-type']['#default_value']);
    $this->assertEmpty($form['attributes']['data-entity-uuid']['#default_value']);
    $this->assertEmpty($form['attributes']['data-entity-substitution']['#default_value']);
272 273 274
  }

}