Version 1
[yaffs-website] / web / modules / contrib / linkit / src / Tests / AttributeCrudTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\linkit\Tests\AttributeCrudTest.
6  */
7
8 namespace Drupal\linkit\Tests;
9
10 use Drupal\Core\Url;
11 use Drupal\linkit\Entity\Profile;
12
13 /**
14  * Tests adding, listing and deleting attributes on a profile.
15  *
16  * @group linkit
17  */
18 class AttributeCrudTest extends LinkitTestBase {
19
20   /**
21    * The attribute manager.
22    *
23    * @var \Drupal\linkit\AttributeManager
24    */
25   protected $manager;
26
27   /**
28    * The linkit profile.
29    *
30    * @var \Drupal\linkit\ProfileInterface
31    */
32   protected $linkitProfile;
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp() {
38     parent::setUp();
39     $this->manager = $this->container->get('plugin.manager.linkit.attribute');
40
41     $this->linkitProfile = $this->createProfile();
42     $this->drupalLogin($this->adminUser);
43   }
44
45   /**
46    * Test the overview page.
47    */
48   function testOverview() {
49     $this->drupalGet(Url::fromRoute('linkit.attributes', [
50       'linkit_profile' => $this->linkitProfile->id(),
51     ]));
52     $this->assertText(t('No attributes added.'));
53
54     $this->assertLinkByHref(Url::fromRoute('linkit.attribute.add', [
55       'linkit_profile' => $this->linkitProfile->id(),
56     ])->toString());
57   }
58
59   /**
60    * Test adding an attribute to a profile.
61    */
62   function testAdd() {
63     $this->drupalGet(Url::fromRoute('linkit.attribute.add', [
64       'linkit_profile' => $this->linkitProfile->id(),
65     ]));
66
67     $this->assertEqual(count($this->manager->getDefinitions()), count($this->xpath('//input[@type="radio"]')), 'All attributes are available.');
68
69     $edit = array();
70     $edit['plugin'] = 'dummy_attribute';
71     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
72
73     $this->assertUrl(Url::fromRoute('linkit.attributes', [
74       'linkit_profile' => $this->linkitProfile->id(),
75     ]));
76
77     $this->assertEqual(1, count($this->xpath('//table/tbody/tr')), 'Attribute added.');
78     $this->assertNoText(t('No attributes added.'));
79   }
80
81   /**
82    * Test adding a configurable attribute to a profile.
83    */
84   function testAddConfigurable() {
85     $this->drupalGet(Url::fromRoute('linkit.attribute.add', [
86       'linkit_profile' => $this->linkitProfile->id(),
87     ]));
88
89     $this->assertEqual(count($this->manager->getDefinitions()), count($this->xpath('//input[@type="radio"]')), 'All attributes are available.');
90
91     $edit = array();
92     $edit['plugin'] = 'configurable_dummy_attribute';
93     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
94
95     $this->assertUrl(Url::fromRoute('linkit.attribute.edit', [
96       'linkit_profile' => $this->linkitProfile->id(),
97       'plugin_instance_id' => 'configurable_dummy_attribute',
98     ]));
99
100     $this->drupalGet(Url::fromRoute('linkit.attributes', [
101       'linkit_profile' => $this->linkitProfile->id(),
102     ]));
103
104     $this->assertEqual(1, count($this->xpath('//table/tbody/tr')), 'Attribute added.');
105     $this->assertNoText(t('No attributes added.'));
106
107     $plugin_url = Url::fromRoute('linkit.attribute.edit', [
108       'linkit_profile' => $this->linkitProfile->id(),
109       'plugin_instance_id' => 'configurable_dummy_attribute',
110     ]);
111
112     $this->assertLinkByHref($plugin_url->toString());
113   }
114
115   /**
116    * Test delete an attribute from a profile.
117    */
118   function testDelete() {
119     /** @var \Drupal\linkit\AttributeInterface $plugin */
120     $plugin = $this->manager->createInstance('dummy_attribute');
121
122     $this->linkitProfile->addAttribute($plugin->getConfiguration());
123     $this->linkitProfile->save();
124
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'
129     ]));
130     $this->assertResponse('404');
131
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(),
136     ]));
137     $this->clickLink(t('Cancel'));
138     $this->assertUrl(Url::fromRoute('linkit.attributes', [
139       'linkit_profile' => $this->linkitProfile->id(),
140     ]));
141
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',
146     ]));
147
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(),
152     ]));
153     $this->assertText(t('No attributes added.'));
154
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');
158   }
159
160 }