Version 1
[yaffs-website] / web / modules / contrib / linkit / src / Tests / ProfileCrudTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\linkit\Tests\ProfileCreationTest.
6  */
7
8 namespace Drupal\linkit\Tests;
9 use Drupal\Component\Utility\Unicode;
10 use Drupal\Core\Url;
11
12 /**
13  * Tests creating, loading and deleting profiles.
14  *
15  * @group linkit
16  */
17 class ProfileCrudTest extends LinkitTestBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   protected function setUp() {
23     parent::setUp();
24
25     $this->drupalLogin($this->adminUser);
26   }
27
28   /**
29    * Test the overview page.
30    */
31   function testOverview() {
32     // Verify that the profile collection page is not accessible for regular
33     // users.
34     $this->drupalLogin($this->baseUser);
35     $this->drupalGet(Url::fromRoute('entity.linkit_profile.collection'));
36     $this->assertResponse(403);
37     $this->drupalLogout();
38
39     // Verify that the profile collection page is accessible for regular users.
40     $this->drupalLogin($this->adminUser);
41
42     $profiles = [];
43     $profiles[] = $this->createProfile();
44     $profiles[] = $this->createProfile();
45
46     $this->drupalGet(Url::fromRoute('entity.linkit_profile.collection'));
47     $this->assertResponse(200);
48
49     // Assert that the 'Add profile' action exists.
50     $this->assertLinkByHref(Url::fromRoute('entity.linkit_profile.add_form')->toString());
51
52     /** @var \Drupal\linkit\ProfileInterface $profile */
53     foreach ($profiles as $profile) {
54       $this->assertLinkByHref('admin/config/content/linkit/manage/' . $profile->id());
55       $this->assertLinkByHref('admin/config/content/linkit/manage/' . $profile->id() . '/delete');
56     }
57   }
58
59   /**
60    * Creates profile.
61    */
62   function testProfileCreation() {
63     $this->drupalGet(Url::fromRoute('entity.linkit_profile.add_form'));
64     $this->drupalGet('admin/config/content/linkit/add');
65     $this->assertResponse(200);
66
67     // Create a profile.
68     $edit = [];
69     $edit['label'] = Unicode::strtolower($this->randomMachineName());
70     $edit['id'] = Unicode::strtolower($this->randomMachineName());
71     $edit['description'] = $this->randomMachineName(16);
72     $this->drupalPostForm(NULL, $edit, t('Save and manage matchers'));
73
74     $this->assertRaw(t('Created new profile %label.', ['%label' => $edit['label']]));
75
76     $this->drupalGet(Url::fromRoute('entity.linkit_profile.collection'));
77     $this->assertText($edit['label'], 'Profile exists in the profile collection.');
78   }
79
80   /**
81    * Updates a profile.
82    */
83   function testProfileUpdate() {
84     $profile = $this->createProfile();
85     $this->drupalGet(Url::fromRoute('entity.linkit_profile.edit_form', [
86       'linkit_profile' => $profile->id(),
87     ]));
88     $this->assertResponse(200);
89
90     $id_field = $this->xpath('.//input[not(@disabled) and @name="id"]');
91
92     $this->assertTrue(empty($id_field), 'Machine name field is disabled.');
93     $this->assertLinkByHref(Url::fromRoute('entity.linkit_profile.edit_form', [
94       'linkit_profile' => $profile->id(),
95     ])->toString());
96     $this->assertLinkByHref('admin/config/content/linkit/manage/' . $profile->id() . '/delete');
97
98     $edit = [];
99     $edit['label'] = $this->randomMachineName();
100     $edit['description'] = $this->randomMachineName(16);
101     $this->drupalPostForm(NULL, $edit, t('Update profile'));
102
103     $this->assertRaw(t('Updated profile %label.', ['%label' => $edit['label']]));
104
105     $this->drupalGet(Url::fromRoute('entity.linkit_profile.collection'));
106     $this->assertText($edit['label'], 'Updated profile exists in the profile collection.');
107   }
108
109   /**
110    * Delete a profile.
111    */
112   function testProfileDelete() {
113     /** @var \Drupal\linkit\ProfileInterface $profile */
114     $profile = $this->createProfile();
115     $this->drupalGet(Url::fromRoute('entity.linkit_profile.delete_form', [
116       'linkit_profile' => $profile->id(),
117     ]));
118     $this->drupalPostForm(NULL, [], t('Delete'));
119
120     $this->assertRaw(t('The linkit profile %label has been deleted.', ['%label' => $profile->label()]));
121     $this->drupalGet(Url::fromRoute('entity.linkit_profile.collection'));
122     $this->assertNoText($profile->label(), 'Deleted profile does not exists in the profile collection.');
123   }
124
125 }