Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/field_ui/src/Tests/FieldUiTestTrait.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | 12f9dff5fda9 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\field_ui\Tests; | |
4 | |
5 /** | |
6 * Provides common functionality for the Field UI test classes. | |
7 */ | |
8 trait FieldUiTestTrait { | |
9 | |
10 /** | |
11 * Creates a new field through the Field UI. | |
12 * | |
13 * @param string $bundle_path | |
14 * Admin path of the bundle that the new field is to be attached to. | |
15 * @param string $field_name | |
16 * The field name of the new field storage. | |
17 * @param string $label | |
18 * (optional) The label of the new field. Defaults to a random string. | |
19 * @param string $field_type | |
20 * (optional) The field type of the new field storage. Defaults to | |
21 * 'test_field'. | |
22 * @param array $storage_edit | |
23 * (optional) $edit parameter for drupalPostForm() on the second step | |
24 * ('Storage settings' form). | |
25 * @param array $field_edit | |
26 * (optional) $edit parameter for drupalPostForm() on the third step ('Field | |
27 * settings' form). | |
28 */ | |
29 public function fieldUIAddNewField($bundle_path, $field_name, $label = NULL, $field_type = 'test_field', array $storage_edit = [], array $field_edit = []) { | |
30 $label = $label ?: $this->randomString(); | |
31 $initial_edit = [ | |
32 'new_storage_type' => $field_type, | |
33 'label' => $label, | |
34 'field_name' => $field_name, | |
35 ]; | |
36 | |
37 // Allow the caller to set a NULL path in case they navigated to the right | |
38 // page before calling this method. | |
39 if ($bundle_path !== NULL) { | |
40 $bundle_path = "$bundle_path/fields/add-field"; | |
41 } | |
42 | |
43 // First step: 'Add field' page. | |
44 $this->drupalPostForm($bundle_path, $initial_edit, t('Save and continue')); | |
45 $this->assertRaw(t('These settings apply to the %label field everywhere it is used.', ['%label' => $label]), 'Storage settings page was displayed.'); | |
46 // Test Breadcrumbs. | |
47 $this->assertLink($label, 0, 'Field label is correct in the breadcrumb of the storage settings page.'); | |
48 | |
49 // Second step: 'Storage settings' form. | |
50 $this->drupalPostForm(NULL, $storage_edit, t('Save field settings')); | |
51 $this->assertRaw(t('Updated field %label field settings.', ['%label' => $label]), 'Redirected to field settings page.'); | |
52 | |
53 // Third step: 'Field settings' form. | |
54 $this->drupalPostForm(NULL, $field_edit, t('Save settings')); | |
55 $this->assertRaw(t('Saved %label configuration.', ['%label' => $label]), 'Redirected to "Manage fields" page.'); | |
56 | |
57 // Check that the field appears in the overview form. | |
58 $this->assertFieldByXPath('//table[@id="field-overview"]//tr/td[1]', $label, 'Field was created and appears in the overview page.'); | |
59 } | |
60 | |
61 /** | |
62 * Adds an existing field through the Field UI. | |
63 * | |
64 * @param string $bundle_path | |
65 * Admin path of the bundle that the field is to be attached to. | |
66 * @param string $existing_storage_name | |
67 * The name of the existing field storage for which we want to add a new | |
68 * field. | |
69 * @param string $label | |
70 * (optional) The label of the new field. Defaults to a random string. | |
71 * @param array $field_edit | |
72 * (optional) $edit parameter for drupalPostForm() on the second step | |
73 * ('Field settings' form). | |
74 */ | |
75 public function fieldUIAddExistingField($bundle_path, $existing_storage_name, $label = NULL, array $field_edit = []) { | |
76 $label = $label ?: $this->randomString(); | |
77 $initial_edit = [ | |
78 'existing_storage_name' => $existing_storage_name, | |
79 'existing_storage_label' => $label, | |
80 ]; | |
81 | |
82 // First step: 'Re-use existing field' on the 'Add field' page. | |
83 $this->drupalPostForm("$bundle_path/fields/add-field", $initial_edit, t('Save and continue')); | |
84 // Set the main content to only the content region because the label can | |
85 // contain HTML which will be auto-escaped by Twig. | |
86 $main_content = $this->cssSelect('.region-content'); | |
87 $this->setRawContent(reset($main_content)->asXml()); | |
88 $this->assertRaw('field-config-edit-form', 'The field config edit form is present.'); | |
89 $this->assertNoRaw('&lt;', 'The page does not have double escaped HTML tags.'); | |
90 | |
91 // Second step: 'Field settings' form. | |
92 $this->drupalPostForm(NULL, $field_edit, t('Save settings')); | |
93 $this->assertRaw(t('Saved %label configuration.', ['%label' => $label]), 'Redirected to "Manage fields" page.'); | |
94 | |
95 // Check that the field appears in the overview form. | |
96 $this->assertFieldByXPath('//table[@id="field-overview"]//tr/td[1]', $label, 'Field was created and appears in the overview page.'); | |
97 } | |
98 | |
99 /** | |
100 * Deletes a field through the Field UI. | |
101 * | |
102 * @param string $bundle_path | |
103 * Admin path of the bundle that the field is to be deleted from. | |
104 * @param string $field_name | |
105 * The name of the field. | |
106 * @param string $label | |
107 * The label of the field. | |
108 * @param string $bundle_label | |
109 * The label of the bundle. | |
110 */ | |
111 public function fieldUIDeleteField($bundle_path, $field_name, $label, $bundle_label) { | |
112 // Display confirmation form. | |
113 $this->drupalGet("$bundle_path/fields/$field_name/delete"); | |
114 $this->assertRaw(t('Are you sure you want to delete the field %label', ['%label' => $label]), 'Delete confirmation was found.'); | |
115 | |
116 // Test Breadcrumbs. | |
117 $this->assertLink($label, 0, 'Field label is correct in the breadcrumb of the field delete page.'); | |
118 | |
119 // Submit confirmation form. | |
120 $this->drupalPostForm(NULL, [], t('Delete')); | |
121 $this->assertRaw(t('The field %label has been deleted from the %type content type.', ['%label' => $label, '%type' => $bundle_label]), 'Delete message was found.'); | |
122 | |
123 // Check that the field does not appear in the overview form. | |
124 $this->assertNoFieldByXPath('//table[@id="field-overview"]//span[@class="label-field"]', $label, 'Field does not appear in the overview page.'); | |
125 } | |
126 | |
127 } |