Version 1
[yaffs-website] / web / modules / contrib / linkit / src / Tests / MatcherCrudTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\linkit\Tests\MatcherCrudTest.
6  */
7
8 namespace Drupal\linkit\Tests;
9 use Drupal\Core\Url;
10 use Drupal\linkit\Entity\Profile;
11
12 /**
13  * Tests adding, listing, updating and deleting matchers on a profile.
14  *
15  * @group linkit
16  */
17 class MatcherCrudTest extends LinkitTestBase {
18
19   /**
20    * The attribute manager.
21    *
22    * @var \Drupal\linkit\MatcherManager
23    */
24   protected $manager;
25
26   /**
27    * The linkit profile.
28    *
29    * @var \Drupal\linkit\ProfileInterface
30    */
31   protected $linkitProfile;
32
33   /**
34    * {@inheritdoc}
35    */
36   protected function setUp() {
37     parent::setUp();
38     $this->manager = $this->container->get('plugin.manager.linkit.matcher');
39
40     $this->linkitProfile = $this->createProfile();
41     $this->drupalLogin($this->adminUser);
42   }
43
44   /**
45    * Test the overview page.
46    */
47   function testOverview() {
48     $this->drupalGet(Url::fromRoute('linkit.matchers', [
49       'linkit_profile' => $this->linkitProfile->id(),
50     ]));
51     $this->assertText(t('No matchers added.'));
52
53     $this->assertLinkByHref(Url::fromRoute('linkit.matcher.add', [
54       'linkit_profile' => $this->linkitProfile->id(),
55     ])->toString());
56   }
57
58   /**
59    * Test adding a matcher to a profile.
60    */
61   function testAdd() {
62     $this->drupalGet(Url::fromRoute('linkit.matcher.add', [
63       'linkit_profile' => $this->linkitProfile->id(),
64     ]));
65
66     $edit = array();
67     $edit['plugin'] = 'dummy_matcher';
68     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
69
70     // Load the saved profile.
71     $this->linkitProfile = Profile::load($this->linkitProfile->id());
72
73     $matcher_ids = $this->linkitProfile->getMatchers()->getInstanceIds();
74     /** @var \Drupal\linkit\MatcherInterface $plugin */
75     $plugin = $this->linkitProfile->getMatcher(current($matcher_ids));
76
77     $this->assertRaw(t('Added %label matcher.', ['%label' => $plugin->getLabel()]));
78     $this->assertNoText(t('No matchers added.'));
79   }
80
81   /**
82    * Test adding a configurable attribute to a profile.
83    */
84   function testAddConfigurable() {
85     $this->drupalGet(Url::fromRoute('linkit.matcher.add', [
86       'linkit_profile' => $this->linkitProfile->id(),
87     ]));
88
89     $edit = array();
90     $edit['plugin'] = 'configurable_dummy_matcher';
91     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
92
93     // Load the saved profile.
94     $this->linkitProfile = Profile::load($this->linkitProfile->id());
95
96     $matcher_ids = $this->linkitProfile->getMatchers()->getInstanceIds();
97     /** @var \Drupal\linkit\MatcherInterface $plugin */
98     $plugin = $this->linkitProfile->getMatcher(current($matcher_ids));
99
100     $this->assertUrl(Url::fromRoute('linkit.matcher.edit', [
101       'linkit_profile' => $this->linkitProfile->id(),
102       'plugin_instance_id' => $plugin->getUuid(),
103     ]));
104
105     $this->drupalGet(Url::fromRoute('linkit.matchers', [
106       'linkit_profile' => $this->linkitProfile->id(),
107     ]));
108
109     $this->assertNoText(t('No matchers added.'));
110   }
111
112   /**
113    * Test delete a matcher from a profile.
114    */
115   function testDelete() {
116     /** @var \Drupal\linkit\AttributeInterface $plugin */
117     $plugin = $this->manager->createInstance('dummy_matcher');
118
119     $profile = $this->createProfile();
120     $plugin_uuid = $profile->addMatcher($plugin->getConfiguration());
121     $profile->save();
122
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'
127     ]));
128     $this->assertResponse('404');
129
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,
134     ]));
135     $this->clickLink(t('Cancel'));
136     $this->assertUrl(Url::fromRoute('linkit.matchers', [
137       'linkit_profile' => $profile->id(),
138     ]));
139
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,
144     ]));
145
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(),
150     ]));
151     $this->assertText(t('No matchers added.'));
152
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');
156   }
157
158 }