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