annotate core/modules/field_ui/src/Tests/FieldUiTestTrait.php @ 19:fa3358dc1485 tip

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