5 * Contains \Drupal\linkit\Tests\AttributeCrudTest.
8 namespace Drupal\linkit\Tests;
11 use Drupal\linkit\Entity\Profile;
14 * Tests adding, listing and deleting attributes on a profile.
18 class AttributeCrudTest extends LinkitTestBase {
21 * The attribute manager.
23 * @var \Drupal\linkit\AttributeManager
30 * @var \Drupal\linkit\ProfileInterface
32 protected $linkitProfile;
37 protected function setUp() {
39 $this->manager = $this->container->get('plugin.manager.linkit.attribute');
41 $this->linkitProfile = $this->createProfile();
42 $this->drupalLogin($this->adminUser);
46 * Test the overview page.
48 function testOverview() {
49 $this->drupalGet(Url::fromRoute('linkit.attributes', [
50 'linkit_profile' => $this->linkitProfile->id(),
52 $this->assertText(t('No attributes added.'));
54 $this->assertLinkByHref(Url::fromRoute('linkit.attribute.add', [
55 'linkit_profile' => $this->linkitProfile->id(),
60 * Test adding an attribute to a profile.
63 $this->drupalGet(Url::fromRoute('linkit.attribute.add', [
64 'linkit_profile' => $this->linkitProfile->id(),
67 $this->assertEqual(count($this->manager->getDefinitions()), count($this->xpath('//input[@type="radio"]')), 'All attributes are available.');
70 $edit['plugin'] = 'dummy_attribute';
71 $this->drupalPostForm(NULL, $edit, t('Save and continue'));
73 $this->assertUrl(Url::fromRoute('linkit.attributes', [
74 'linkit_profile' => $this->linkitProfile->id(),
77 $this->assertEqual(1, count($this->xpath('//table/tbody/tr')), 'Attribute added.');
78 $this->assertNoText(t('No attributes added.'));
82 * Test adding a configurable attribute to a profile.
84 function testAddConfigurable() {
85 $this->drupalGet(Url::fromRoute('linkit.attribute.add', [
86 'linkit_profile' => $this->linkitProfile->id(),
89 $this->assertEqual(count($this->manager->getDefinitions()), count($this->xpath('//input[@type="radio"]')), 'All attributes are available.');
92 $edit['plugin'] = 'configurable_dummy_attribute';
93 $this->drupalPostForm(NULL, $edit, t('Save and continue'));
95 $this->assertUrl(Url::fromRoute('linkit.attribute.edit', [
96 'linkit_profile' => $this->linkitProfile->id(),
97 'plugin_instance_id' => 'configurable_dummy_attribute',
100 $this->drupalGet(Url::fromRoute('linkit.attributes', [
101 'linkit_profile' => $this->linkitProfile->id(),
104 $this->assertEqual(1, count($this->xpath('//table/tbody/tr')), 'Attribute added.');
105 $this->assertNoText(t('No attributes added.'));
107 $plugin_url = Url::fromRoute('linkit.attribute.edit', [
108 'linkit_profile' => $this->linkitProfile->id(),
109 'plugin_instance_id' => 'configurable_dummy_attribute',
112 $this->assertLinkByHref($plugin_url->toString());
116 * Test delete an attribute from a profile.
118 function testDelete() {
119 /** @var \Drupal\linkit\AttributeInterface $plugin */
120 $plugin = $this->manager->createInstance('dummy_attribute');
122 $this->linkitProfile->addAttribute($plugin->getConfiguration());
123 $this->linkitProfile->save();
125 // Try delete an attribute that is not attached to the profile.
126 $this->drupalGet(Url::fromRoute('linkit.attribute.delete', [
127 'linkit_profile' => $this->linkitProfile->id(),
128 'plugin_instance_id' => 'doesntexists'
130 $this->assertResponse('404');
132 // Go to the delete page, but press cancel.
133 $this->drupalGet(Url::fromRoute('linkit.attribute.delete', [
134 'linkit_profile' => $this->linkitProfile->id(),
135 'plugin_instance_id' => $plugin->getPluginId(),
137 $this->clickLink(t('Cancel'));
138 $this->assertUrl(Url::fromRoute('linkit.attributes', [
139 'linkit_profile' => $this->linkitProfile->id(),
142 // Delete the attribute from the profile.
143 $this->drupalGet(Url::fromRoute('linkit.attribute.delete', [
144 'linkit_profile' => $this->linkitProfile->id(),
145 'plugin_instance_id' => 'dummy_attribute',
148 $this->drupalPostForm(NULL, [], t('Confirm'));
149 $this->assertRaw(t('The attribute %plugin has been deleted.', ['%plugin' => $plugin->getLabel()]));
150 $this->assertUrl(Url::fromRoute('linkit.attributes', [
151 'linkit_profile' => $this->linkitProfile->id(),
153 $this->assertText(t('No attributes added.'));
155 /** @var \Drupal\linkit\Entity\Profile $updated_profile */
156 $updated_profile = Profile::load($this->linkitProfile->id());
157 $this->assertFalse($updated_profile->getAttributes()->has($plugin->getPluginId()), 'The attribute is deleted from the profile');