You are here

function AttributeCrudTest::testDelete in Linkit 8.4

Test delete an attribute from a profile.

File

src/Tests/AttributeCrudTest.php, line 118
Contains \Drupal\linkit\Tests\AttributeCrudTest.

Class

AttributeCrudTest
Tests adding, listing and deleting attributes on a profile.

Namespace

Drupal\linkit\Tests

Code

function testDelete() {

  /** @var \Drupal\linkit\AttributeInterface $plugin */
  $plugin = $this->manager
    ->createInstance('dummy_attribute');
  $this->linkitProfile
    ->addAttribute($plugin
    ->getConfiguration());
  $this->linkitProfile
    ->save();

  // Try delete an attribute that is not attached to the profile.
  $this
    ->drupalGet(Url::fromRoute('linkit.attribute.delete', [
    'linkit_profile' => $this->linkitProfile
      ->id(),
    'plugin_instance_id' => 'doesntexists',
  ]));
  $this
    ->assertResponse('404');

  // Go to the delete page, but press cancel.
  $this
    ->drupalGet(Url::fromRoute('linkit.attribute.delete', [
    'linkit_profile' => $this->linkitProfile
      ->id(),
    'plugin_instance_id' => $plugin
      ->getPluginId(),
  ]));
  $this
    ->clickLink(t('Cancel'));
  $this
    ->assertUrl(Url::fromRoute('linkit.attributes', [
    'linkit_profile' => $this->linkitProfile
      ->id(),
  ]));

  // Delete the attribute from the profile.
  $this
    ->drupalGet(Url::fromRoute('linkit.attribute.delete', [
    'linkit_profile' => $this->linkitProfile
      ->id(),
    'plugin_instance_id' => 'dummy_attribute',
  ]));
  $this
    ->drupalPostForm(NULL, [], t('Confirm'));
  $this
    ->assertRaw(t('The attribute %plugin has been deleted.', [
    '%plugin' => $plugin
      ->getLabel(),
  ]));
  $this
    ->assertUrl(Url::fromRoute('linkit.attributes', [
    'linkit_profile' => $this->linkitProfile
      ->id(),
  ]));
  $this
    ->assertText(t('No attributes added.'));

  /** @var \Drupal\linkit\Entity\Profile $updated_profile */
  $updated_profile = Profile::load($this->linkitProfile
    ->id());
  $this
    ->assertFalse($updated_profile
    ->getAttributes()
    ->has($plugin
    ->getPluginId()), 'The attribute is deleted from the profile');
}