52e75cfc98976d6153538af6e85145df814191c7
[yaffs-website] / web / core / modules / field_ui / tests / src / Traits / FieldUiTestTrait.php
1 <?php
2
3 namespace Drupal\Tests\field_ui\Traits;
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     $this->assertRaw('field-config-edit-form', 'The field config edit form is present.');
87     $this->assertNoRaw('&amp;lt;', 'The page does not have double escaped HTML tags.');
88
89     // Second step: 'Field settings' form.
90     $this->drupalPostForm(NULL, $field_edit, t('Save settings'));
91     $this->assertRaw(t('Saved %label configuration.', ['%label' => $label]), 'Redirected to "Manage fields" page.');
92
93     // Check that the field appears in the overview form.
94     $this->assertFieldByXPath('//table[@id="field-overview"]//tr/td[1]', $label, 'Field was created and appears in the overview page.');
95   }
96
97   /**
98    * Deletes a field through the Field UI.
99    *
100    * @param string $bundle_path
101    *   Admin path of the bundle that the field is to be deleted from.
102    * @param string $field_name
103    *   The name of the field.
104    * @param string $label
105    *   The label of the field.
106    * @param string $bundle_label
107    *   The label of the bundle.
108    */
109   public function fieldUIDeleteField($bundle_path, $field_name, $label, $bundle_label) {
110     // Display confirmation form.
111     $this->drupalGet("$bundle_path/fields/$field_name/delete");
112     $this->assertRaw(t('Are you sure you want to delete the field %label', ['%label' => $label]), 'Delete confirmation was found.');
113
114     // Test Breadcrumbs.
115     $this->assertLink($label, 0, 'Field label is correct in the breadcrumb of the field delete page.');
116
117     // Submit confirmation form.
118     $this->drupalPostForm(NULL, [], t('Delete'));
119     $this->assertRaw(t('The field %label has been deleted from the %type content type.', ['%label' => $label, '%type' => $bundle_label]), 'Delete message was found.');
120
121     // Check that the field does not appear in the overview form.
122     $this->assertNoFieldByXPath('//table[@id="field-overview"]//span[@class="label-field"]', $label, 'Field does not appear in the overview page.');
123   }
124
125 }