5 * Contains \Drupal\linkit\Tests\MatcherCrudTest.
8 namespace Drupal\linkit\Tests;
10 use Drupal\linkit\Entity\Profile;
13 * Tests adding, listing, updating and deleting matchers on a profile.
17 class MatcherCrudTest extends LinkitTestBase {
20 * The attribute manager.
22 * @var \Drupal\linkit\MatcherManager
29 * @var \Drupal\linkit\ProfileInterface
31 protected $linkitProfile;
36 protected function setUp() {
38 $this->manager = $this->container->get('plugin.manager.linkit.matcher');
40 $this->linkitProfile = $this->createProfile();
41 $this->drupalLogin($this->adminUser);
45 * Test the overview page.
47 function testOverview() {
48 $this->drupalGet(Url::fromRoute('linkit.matchers', [
49 'linkit_profile' => $this->linkitProfile->id(),
51 $this->assertText(t('No matchers added.'));
53 $this->assertLinkByHref(Url::fromRoute('linkit.matcher.add', [
54 'linkit_profile' => $this->linkitProfile->id(),
59 * Test adding a matcher to a profile.
62 $this->drupalGet(Url::fromRoute('linkit.matcher.add', [
63 'linkit_profile' => $this->linkitProfile->id(),
67 $edit['plugin'] = 'dummy_matcher';
68 $this->drupalPostForm(NULL, $edit, t('Save and continue'));
70 // Load the saved profile.
71 $this->linkitProfile = Profile::load($this->linkitProfile->id());
73 $matcher_ids = $this->linkitProfile->getMatchers()->getInstanceIds();
74 /** @var \Drupal\linkit\MatcherInterface $plugin */
75 $plugin = $this->linkitProfile->getMatcher(current($matcher_ids));
77 $this->assertRaw(t('Added %label matcher.', ['%label' => $plugin->getLabel()]));
78 $this->assertNoText(t('No matchers added.'));
82 * Test adding a configurable attribute to a profile.
84 function testAddConfigurable() {
85 $this->drupalGet(Url::fromRoute('linkit.matcher.add', [
86 'linkit_profile' => $this->linkitProfile->id(),
90 $edit['plugin'] = 'configurable_dummy_matcher';
91 $this->drupalPostForm(NULL, $edit, t('Save and continue'));
93 // Load the saved profile.
94 $this->linkitProfile = Profile::load($this->linkitProfile->id());
96 $matcher_ids = $this->linkitProfile->getMatchers()->getInstanceIds();
97 /** @var \Drupal\linkit\MatcherInterface $plugin */
98 $plugin = $this->linkitProfile->getMatcher(current($matcher_ids));
100 $this->assertUrl(Url::fromRoute('linkit.matcher.edit', [
101 'linkit_profile' => $this->linkitProfile->id(),
102 'plugin_instance_id' => $plugin->getUuid(),
105 $this->drupalGet(Url::fromRoute('linkit.matchers', [
106 'linkit_profile' => $this->linkitProfile->id(),
109 $this->assertNoText(t('No matchers added.'));
113 * Test delete a matcher from a profile.
115 function testDelete() {
116 /** @var \Drupal\linkit\AttributeInterface $plugin */
117 $plugin = $this->manager->createInstance('dummy_matcher');
119 $profile = $this->createProfile();
120 $plugin_uuid = $profile->addMatcher($plugin->getConfiguration());
123 // Try delete a matcher that is not attached to the profile.
124 $this->drupalGet(Url::fromRoute('linkit.matcher.delete', [
125 'linkit_profile' => $profile->id(),
126 'plugin_instance_id' => 'doesntexists'
128 $this->assertResponse('404');
130 // Go to the delete page, but press cancel.
131 $this->drupalGet(Url::fromRoute('linkit.matcher.delete', [
132 'linkit_profile' => $profile->id(),
133 'plugin_instance_id' => $plugin_uuid,
135 $this->clickLink(t('Cancel'));
136 $this->assertUrl(Url::fromRoute('linkit.matchers', [
137 'linkit_profile' => $profile->id(),
140 // Delete the matcher from the profile.
141 $this->drupalGet(Url::fromRoute('linkit.matcher.delete', [
142 'linkit_profile' => $profile->id(),
143 'plugin_instance_id' => $plugin_uuid,
146 $this->drupalPostForm(NULL, [], t('Confirm'));
147 $this->assertRaw(t('The matcher %plugin has been deleted.', ['%plugin' => $plugin->getLabel()]));
148 $this->assertUrl(Url::fromRoute('linkit.matchers', [
149 'linkit_profile' => $profile->id(),
151 $this->assertText(t('No matchers added.'));
153 /** @var \Drupal\linkit\Entity\Profile $updated_profile */
154 $updated_profile = Profile::load($profile->id());
155 $this->assertFalse($updated_profile->getMatchers()->has($plugin_uuid), 'The user matcher is deleted from the profile');