diff --git a/core/modules/datetime/src/Tests/DateTimeItemTest.php b/core/modules/datetime/src/Tests/DateTimeItemTest.php index e12e6f59f6bb7c8ed56156fcaea4365baf842301..63aa0faba37caa7fc8e25d67f16016dd815d03d4 100644 --- a/core/modules/datetime/src/Tests/DateTimeItemTest.php +++ b/core/modules/datetime/src/Tests/DateTimeItemTest.php @@ -29,21 +29,21 @@ public function setUp() { parent::setUp(); // Create a field with settings to validate. - $this->fieldStorage = entity_create('field_storage_config', array( + $field_storage = entity_create('field_storage_config', array( 'name' => 'field_datetime', 'type' => 'datetime', 'entity_type' => 'entity_test', 'settings' => array('datetime_type' => 'date'), )); - $this->fieldStorage->save(); - $this->instance = entity_create('field_instance_config', array( - 'field_storage' => $this->fieldStorage, + $field_storage->save(); + $instance = entity_create('field_instance_config', array( + 'field_storage' => $field_storage, 'bundle' => 'entity_test', 'settings' => array( 'default_value' => 'blank', ), )); - $this->instance->save(); + $instance->save(); } /** diff --git a/core/modules/editor/src/Tests/QuickEditIntegrationTest.php b/core/modules/editor/src/Tests/QuickEditIntegrationTest.php index 531f1ac0c5c1f73836c6ddd5d6ab64306f653f8c..a8f81e286cb341ff7fa4e04c67d68a22710be79e 100644 --- a/core/modules/editor/src/Tests/QuickEditIntegrationTest.php +++ b/core/modules/editor/src/Tests/QuickEditIntegrationTest.php @@ -64,7 +64,6 @@ public function setUp() { // Install the Filter module. $this->installSchema('system', 'url_alias'); - $this->enableModules(array('user', 'filter')); // Enable the Text Editor and Text Editor Test module. $this->enableModules(array('editor', 'editor_test')); @@ -130,23 +129,23 @@ public function testEditorSelection() { $this->editorSelector = $this->container->get('quickedit.editor.selector'); // Create an entity with values for this text field. - $this->entity = entity_create('entity_test'); - $this->entity->{$this->field_name}->value = 'Hello, world!'; - $this->entity->{$this->field_name}->format = 'filtered_html'; - $this->entity->save(); + $entity = entity_create('entity_test'); + $entity->{$this->field_name}->value = 'Hello, world!'; + $entity->{$this->field_name}->format = 'filtered_html'; + $entity->save(); // Editor selection w/ cardinality 1, text format w/o associated text editor. - $this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $this->field_name), "With cardinality 1, and the filtered_html text format, the 'form' editor is selected."); + $this->assertEqual('form', $this->getSelectedEditor($entity->id(), $this->field_name), "With cardinality 1, and the filtered_html text format, the 'form' editor is selected."); // Editor selection w/ cardinality 1, text format w/ associated text editor. - $this->entity->{$this->field_name}->format = 'full_html'; - $this->entity->save(); - $this->assertEqual('editor', $this->getSelectedEditor($this->entity->id(), $this->field_name), "With cardinality 1, and the full_html text format, the 'editor' editor is selected."); + $entity->{$this->field_name}->format = 'full_html'; + $entity->save(); + $this->assertEqual('editor', $this->getSelectedEditor($entity->id(), $this->field_name), "With cardinality 1, and the full_html text format, the 'editor' editor is selected."); // Editor selection with text processing, cardinality >1 - $this->field_textarea_field_storage->cardinality = 2; - $this->field_textarea_field_storage->save(); - $this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $this->field_name), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected."); + $this->fields->field_textarea_field_storage->cardinality = 2; + $this->fields->field_textarea_field_storage->save(); + $this->assertEqual('form', $this->getSelectedEditor($entity->id(), $this->field_name), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected."); } /** @@ -159,11 +158,11 @@ public function testMetadata() { $this->metadataGenerator = new MetadataGenerator($this->accessChecker, $this->editorSelector, $this->editorManager); // Create an entity with values for the field. - $this->entity = entity_create('entity_test'); - $this->entity->{$this->field_name}->value = 'Test'; - $this->entity->{$this->field_name}->format = 'full_html'; - $this->entity->save(); - $entity = entity_load('entity_test', $this->entity->id()); + $entity = entity_create('entity_test'); + $entity->{$this->field_name}->value = 'Test'; + $entity->{$this->field_name}->format = 'full_html'; + $entity->save(); + $entity = entity_load('entity_test', $entity->id()); // Verify metadata. $items = $entity->getTranslation(LanguageInterface::LANGCODE_NOT_SPECIFIED)->get($this->field_name); @@ -197,11 +196,11 @@ public function testAttachments() { */ public function testGetUntransformedTextCommand() { // Create an entity with values for the field. - $this->entity = entity_create('entity_test'); - $this->entity->{$this->field_name}->value = 'Test'; - $this->entity->{$this->field_name}->format = 'full_html'; - $this->entity->save(); - $entity = entity_load('entity_test', $this->entity->id()); + $entity = entity_create('entity_test'); + $entity->{$this->field_name}->value = 'Test'; + $entity->{$this->field_name}->format = 'full_html'; + $entity->save(); + $entity = entity_load('entity_test', $entity->id()); // Verify AJAX response. $controller = new EditorController(); diff --git a/core/modules/field/src/Tests/ConfigFieldDefinitionTest.php b/core/modules/field/src/Tests/ConfigFieldDefinitionTest.php index 7c1d320c4f446d2d7115cec7c219264ea3a5900e..dcd558c084600ebec40de1818f3ca95568a778b3 100644 --- a/core/modules/field/src/Tests/ConfigFieldDefinitionTest.php +++ b/core/modules/field/src/Tests/ConfigFieldDefinitionTest.php @@ -24,6 +24,16 @@ class ConfigFieldDefinitionTest extends FieldUnitTestBase { */ protected $entityManager; + /** + * @var string + */ + private $entityType; + + /** + * @var string + */ + private $bundle; + /** * {@inheritdoc} */ @@ -46,10 +56,10 @@ public function setUp() { */ public function testBundleFieldDefinition() { $definitions = $this->entityManager->getFieldDefinitions($this->entityType, $this->bundle); - $this->assertTrue(isset($definitions[$this->instance->getName()])); - $this->assertTrue($definitions[$this->instance->getName()] instanceof FieldDefinitionInterface); + $this->assertTrue(isset($definitions[$this->fieldTestData->instance->getName()])); + $this->assertTrue($definitions[$this->fieldTestData->instance->getName()] instanceof FieldDefinitionInterface); // Make sure no field for the instance on another entity type is exposed. - $this->assertFalse(isset($definitions[$this->instance_rev->getName()])); + $this->assertFalse(isset($definitions[$this->fieldTestData->instance_rev->getName()])); } /** @@ -57,11 +67,11 @@ public function testBundleFieldDefinition() { */ public function testFieldStorageDefinition() { $field_storage_definitions = $this->entityManager->getFieldStorageDefinitions($this->entityType); - $this->assertTrue(isset($field_storage_definitions[$this->instance->getName()])); - $this->assertTrue($field_storage_definitions[$this->instance->getName()] instanceof FieldStorageDefinitionInterface); + $this->assertTrue(isset($field_storage_definitions[$this->fieldTestData->instance->getName()])); + $this->assertTrue($field_storage_definitions[$this->fieldTestData->instance->getName()] instanceof FieldStorageDefinitionInterface); // Make sure no storage field for the instance on another entity type is // exposed. - $this->assertFalse(isset($field_storage_definitions[$this->instance_rev->getName()])); + $this->assertFalse(isset($field_storage_definitions[$this->fieldTestData->instance_rev->getName()])); } } diff --git a/core/modules/field/src/Tests/CrudTest.php b/core/modules/field/src/Tests/CrudTest.php index a7dbb19d84dd4426a31ee584a0527115ecd3ea8a..1feb64f5da94bb0703081c4b60cf07d33680bed2 100644 --- a/core/modules/field/src/Tests/CrudTest.php +++ b/core/modules/field/src/Tests/CrudTest.php @@ -278,66 +278,66 @@ function testDelete() { // TODO: Also test deletion of the data stored in the field ? // Create two fields (so we can test that only one is deleted). - $this->field = array( + $field = array( 'name' => 'field_1', 'type' => 'test_field', 'entity_type' => 'entity_test', ); - entity_create('field_storage_config', $this->field)->save(); - $this->another_field = array( + entity_create('field_storage_config', $field)->save(); + $another_field = array( 'name' => 'field_2', 'type' => 'test_field', 'entity_type' => 'entity_test', ); - entity_create('field_storage_config', $this->another_field)->save(); + entity_create('field_storage_config', $another_field)->save(); // Create instances for each. - $this->instance_definition = array( - 'field_name' => $this->field['name'], + $instance_definition = array( + 'field_name' => $field['name'], 'entity_type' => 'entity_test', 'bundle' => 'entity_test', ); - entity_create('field_instance_config', $this->instance_definition)->save(); - $another_instance_definition = $this->instance_definition; - $another_instance_definition['field_name'] = $this->another_field['name']; + entity_create('field_instance_config', $instance_definition)->save(); + $another_instance_definition = $instance_definition; + $another_instance_definition['field_name'] = $another_field['name']; entity_create('field_instance_config', $another_instance_definition)->save(); // Test that the first field is not deleted, and then delete it. - $field_storage = current(entity_load_multiple_by_properties('field_storage_config', array('field_name' => $this->field['name'], 'include_deleted' => TRUE))); + $field_storage = current(entity_load_multiple_by_properties('field_storage_config', array('field_name' => $field['name'], 'include_deleted' => TRUE))); $this->assertTrue(!empty($field_storage) && empty($field_storage->deleted), 'A new field is not marked for deletion.'); - FieldStorageConfig::loadByName('entity_test', $this->field['name'])->delete(); + FieldStorageConfig::loadByName('entity_test', $field['name'])->delete(); // Make sure that the field is marked as deleted when it is specifically // loaded. - $field_storage = current(entity_load_multiple_by_properties('field_storage_config', array('field_name' => $this->field['name'], 'include_deleted' => TRUE))); + $field_storage = current(entity_load_multiple_by_properties('field_storage_config', array('field_name' => $field['name'], 'include_deleted' => TRUE))); $this->assertTrue(!empty($field_storage->deleted), 'A deleted field is marked for deletion.'); // Make sure that this field's instance is marked as deleted when it is // specifically loaded. - $instance = current(entity_load_multiple_by_properties('field_instance_config', array('entity_type' => 'entity_test', 'field_name' => $this->instance_definition['field_name'], 'bundle' => $this->instance_definition['bundle'], 'include_deleted' => TRUE))); + $instance = current(entity_load_multiple_by_properties('field_instance_config', array('entity_type' => 'entity_test', 'field_name' => $instance_definition['field_name'], 'bundle' => $instance_definition['bundle'], 'include_deleted' => TRUE))); $this->assertTrue(!empty($instance->deleted), 'An instance for a deleted field is marked for deletion.'); // Try to load the field normally and make sure it does not show up. - $field_storage = entity_load('field_storage_config', 'entity_test.' . $this->field['name']); + $field_storage = entity_load('field_storage_config', 'entity_test.' . $field['name']); $this->assertTrue(empty($field_storage), 'A deleted field is not loaded by default.'); // Try to load the instance normally and make sure it does not show up. - $instance = entity_load('field_instance_config', 'entity_test.' . '.' . $this->instance_definition['bundle'] . '.' . $this->instance_definition['field_name']); + $instance = entity_load('field_instance_config', 'entity_test.' . '.' . $instance_definition['bundle'] . '.' . $instance_definition['field_name']); $this->assertTrue(empty($instance), 'An instance for a deleted field is not loaded by default.'); // Make sure the other field (and its field instance) are not deleted. - $another_field_storage = entity_load('field_storage_config', 'entity_test.' . $this->another_field['name']); + $another_field_storage = entity_load('field_storage_config', 'entity_test.' . $another_field['name']); $this->assertTrue(!empty($another_field_storage) && empty($another_field_storage->deleted), 'A non-deleted field is not marked for deletion.'); $another_instance = entity_load('field_instance_config', 'entity_test.' . $another_instance_definition['bundle'] . '.' . $another_instance_definition['field_name']); $this->assertTrue(!empty($another_instance) && empty($another_instance->deleted), 'An instance of a non-deleted field is not marked for deletion.'); // Try to create a new field the same name as a deleted field and // write data into it. - entity_create('field_storage_config', $this->field)->save(); - entity_create('field_instance_config', $this->instance_definition)->save(); - $field_storage = entity_load('field_storage_config', 'entity_test.' . $this->field['name']); + entity_create('field_storage_config', $field)->save(); + entity_create('field_instance_config', $instance_definition)->save(); + $field_storage = entity_load('field_storage_config', 'entity_test.' . $field['name']); $this->assertTrue(!empty($field_storage) && empty($field_storage->deleted), 'A new field with a previously used name is created.'); - $instance = entity_load('field_instance_config', 'entity_test.' . $this->instance_definition['bundle'] . '.' . $this->instance_definition['field_name'] ); + $instance = entity_load('field_instance_config', 'entity_test.' . $instance_definition['bundle'] . '.' . $instance_definition['field_name'] ); $this->assertTrue(!empty($instance) && empty($instance->deleted), 'A new instance for a previously used field name is created.'); // Save an entity with data for the field diff --git a/core/modules/field/src/Tests/FieldAttachOtherTest.php b/core/modules/field/src/Tests/FieldAttachOtherTest.php index a74e4d28653fd92034df5f00205f20616fffae96..4df1e0f3a218d281dda3d5d3788f865010bbb1a3 100644 --- a/core/modules/field/src/Tests/FieldAttachOtherTest.php +++ b/core/modules/field/src/Tests/FieldAttachOtherTest.php @@ -18,21 +18,7 @@ */ class FieldAttachOtherTest extends FieldUnitTestBase { - /** - * Field name to use in the test. - * - * @var string - */ - protected $field_name; - - /** - * Field name to use in the test. - * - * @var string - */ - protected $field_name_2; - - public function setUp() { + protected function setUp() { parent::setUp(); $this->installEntitySchema('entity_test_rev'); $this->createFieldWithInstance(); @@ -48,10 +34,10 @@ function testEntityDisplayBuild() { $entity_init = entity_create($entity_type); // Populate values to be displayed. - $values = $this->_generateTestFieldValues($this->field_storage->getCardinality()); - $entity_init->{$this->field_name}->setValue($values); - $values_2 = $this->_generateTestFieldValues($this->field_storage_2->getCardinality()); - $entity_init->{$this->field_name_2}->setValue($values_2); + $values = $this->_generateTestFieldValues($this->fieldTestData->field_storage->getCardinality()); + $entity_init->{$this->fieldTestData->field_name}->setValue($values); + $values_2 = $this->_generateTestFieldValues($this->fieldTestData->field_storage_2->getCardinality()); + $entity_init->{$this->fieldTestData->field_name_2}->setValue($values_2); // Simple formatter, label displayed. $entity = clone($entity_init); @@ -65,7 +51,7 @@ function testEntityDisplayBuild() { 'test_formatter_setting' => $formatter_setting, ), ); - $display->setComponent($this->field_name, $display_options); + $display->setComponent($this->fieldTestData->field_name, $display_options); $formatter_setting_2 = $this->randomName(); $display_options_2 = array( @@ -75,16 +61,16 @@ function testEntityDisplayBuild() { 'test_formatter_setting' => $formatter_setting_2, ), ); - $display->setComponent($this->field_name_2, $display_options_2); + $display->setComponent($this->fieldTestData->field_name_2, $display_options_2); // View all fields. $content = $display->build($entity); $this->render($content); - $this->assertRaw($this->instance->getLabel(), "First field's label is displayed."); + $this->assertRaw($this->fieldTestData->instance->getLabel(), "First field's label is displayed."); foreach ($values as $delta => $value) { $this->assertRaw("$formatter_setting|{$value['value']}", "Value $delta is displayed, formatter settings are applied."); } - $this->assertRaw($this->instance_2->getLabel(), "Second field's label is displayed."); + $this->assertRaw($this->fieldTestData->instance_2->getLabel(), "Second field's label is displayed."); foreach ($values_2 as $delta => $value) { $this->assertRaw("$formatter_setting_2|{$value['value']}", "Value $delta is displayed, formatter settings are applied."); } @@ -92,17 +78,17 @@ function testEntityDisplayBuild() { // Label hidden. $entity = clone($entity_init); $display_options['label'] = 'hidden'; - $display->setComponent($this->field_name, $display_options); + $display->setComponent($this->fieldTestData->field_name, $display_options); $content = $display->build($entity); $this->render($content); - $this->assertNoRaw($this->instance->getLabel(), "Hidden label: label is not displayed."); + $this->assertNoRaw($this->fieldTestData->instance->getLabel(), "Hidden label: label is not displayed."); // Field hidden. $entity = clone($entity_init); - $display->removeComponent($this->field_name); + $display->removeComponent($this->fieldTestData->field_name); $content = $display->build($entity); $this->render($content); - $this->assertNoRaw($this->instance->getLabel(), "Hidden field: label is not displayed."); + $this->assertNoRaw($this->fieldTestData->instance->getLabel(), "Hidden field: label is not displayed."); foreach ($values as $delta => $value) { $this->assertNoRaw("$formatter_setting|{$value['value']}", "Hidden field: value $delta is not displayed."); } @@ -110,7 +96,7 @@ function testEntityDisplayBuild() { // Multiple formatter. $entity = clone($entity_init); $formatter_setting = $this->randomName(); - $display->setComponent($this->field_name, array( + $display->setComponent($this->fieldTestData->field_name, array( 'label' => 'above', 'type' => 'field_test_multiple', 'settings' => array( @@ -128,7 +114,7 @@ function testEntityDisplayBuild() { // Test a formatter that uses hook_field_formatter_prepare_view(). $entity = clone($entity_init); $formatter_setting = $this->randomName(); - $display->setComponent($this->field_name, array( + $display->setComponent($this->fieldTestData->field_name, array( 'label' => 'above', 'type' => 'field_test_with_prepare_view', 'settings' => array( @@ -152,21 +138,21 @@ function testEntityDisplayBuild() { function testEntityDisplayViewMultiple() { // Use a formatter that has a prepareView() step. $display = entity_get_display('entity_test', 'entity_test', 'full') - ->setComponent($this->field_name, array( + ->setComponent($this->fieldTestData->field_name, array( 'type' => 'field_test_with_prepare_view', )); // Create two entities. $entity1 = entity_create('entity_test', array('id' => 1, 'type' => 'entity_test')); - $entity1->{$this->field_name}->setValue($this->_generateTestFieldValues(1)); + $entity1->{$this->fieldTestData->field_name}->setValue($this->_generateTestFieldValues(1)); $entity2 = entity_create('entity_test', array('id' => 2, 'type' => 'entity_test')); - $entity2->{$this->field_name}->setValue($this->_generateTestFieldValues(1)); + $entity2->{$this->fieldTestData->field_name}->setValue($this->_generateTestFieldValues(1)); // Run buildMultiple(), and check that the entities come out as expected. $display->buildMultiple(array($entity1, $entity2)); - $item1 = $entity1->{$this->field_name}[0]; + $item1 = $entity1->{$this->fieldTestData->field_name}[0]; $this->assertEqual($item1->additional_formatter_value, $item1->value + 1, 'Entity 1 ran through the prepareView() formatter method.'); - $item2 = $entity2->{$this->field_name}[0]; + $item2 = $entity2->{$this->fieldTestData->field_name}[0]; $this->assertEqual($item2->additional_formatter_value, $item2->value + 1, 'Entity 2 ran through the prepareView() formatter method.'); } @@ -178,8 +164,8 @@ function testEntityDisplayViewMultiple() { */ function testEntityCache() { // Initialize random values and a test entity. - $entity_init = entity_create('entity_test', array('type' => $this->instance->bundle)); - $values = $this->_generateTestFieldValues($this->field_storage->getCardinality()); + $entity_init = entity_create('entity_test', array('type' => $this->fieldTestData->instance->bundle)); + $values = $this->_generateTestFieldValues($this->fieldTestData->field_storage->getCardinality()); // Non-cacheable entity type. $entity_type = 'entity_test'; @@ -190,7 +176,7 @@ function testEntityCache() { // Save, and check that no cache entry is present. $entity = clone($entity_init); - $entity->{$this->field_name}->setValue($values); + $entity->{$this->fieldTestData->field_name}->setValue($values); $entity = $this->entitySaveReload($entity); $cid = "values:$entity_type:" . $entity->id(); $this->assertFalse(\Drupal::cache('entity')->get($cid), 'Non-cached: no cache entry on insert and load'); @@ -209,7 +195,7 @@ function testEntityCache() { // Save, and check that no cache entry is present. $entity = clone($entity_init); - $entity->{$this->field_name_2} = $values; + $entity->{$this->fieldTestData->field_name_2} = $values; $entity->save(); $cid = "values:$entity_type:" . $entity->id(); @@ -222,8 +208,8 @@ function testEntityCache() { $this->assertEqual($cache->data, $cached_entity, 'Cached: correct cache entry on load'); // Update with different values, and check that the cache entry is wiped. - $values = $this->_generateTestFieldValues($this->field_storage_2->getCardinality()); - $entity->{$this->field_name_2} = $values; + $values = $this->_generateTestFieldValues($this->fieldTestData->field_storage_2->getCardinality()); + $entity->{$this->fieldTestData->field_name_2} = $values; $entity->save(); $this->assertFalse(\Drupal::cache('entity')->get($cid), 'Cached: no cache entry on update'); @@ -234,8 +220,8 @@ function testEntityCache() { $this->assertEqual($cache->data, $cached_entity, 'Cached: correct cache entry on load'); // Create a new revision, and check that the cache entry is wiped. - $values = $this->_generateTestFieldValues($this->field_storage_2->getCardinality()); - $entity->{$this->field_name_2} = $values; + $values = $this->_generateTestFieldValues($this->fieldTestData->field_storage_2->getCardinality()); + $entity->{$this->fieldTestData->field_name_2} = $values; $entity->setNewRevision(); $entity->save(); $this->assertFalse(\Drupal::cache('entity')->get($cid), 'Cached: no cache entry on new revision creation'); @@ -261,29 +247,29 @@ function testEntityFormDisplayBuildForm() { $this->createFieldWithInstance('_2'); $entity_type = 'entity_test'; - $entity = entity_create($entity_type, array('id' => 1, 'revision_id' => 1, 'type' => $this->instance->bundle)); + $entity = entity_create($entity_type, array('id' => 1, 'revision_id' => 1, 'type' => $this->fieldTestData->instance->bundle)); // Test generating widgets for all fields. - $display = entity_get_form_display($entity_type, $this->instance->bundle, 'default'); + $display = entity_get_form_display($entity_type, $this->fieldTestData->instance->bundle, 'default'); $form = array(); $form_state = new FormState(); $display->buildForm($entity, $form, $form_state); - $this->assertEqual($form[$this->field_name]['widget']['#title'], $this->instance->getLabel(), "First field's form title is {$this->instance->getLabel()}"); - $this->assertEqual($form[$this->field_name_2]['widget']['#title'], $this->instance_2->getLabel(), "Second field's form title is {$this->instance_2->getLabel()}"); - for ($delta = 0; $delta < $this->field_storage->getCardinality(); $delta++) { + $this->assertEqual($form[$this->fieldTestData->field_name]['widget']['#title'], $this->fieldTestData->instance->getLabel(), "First field's form title is {$this->fieldTestData->instance->getLabel()}"); + $this->assertEqual($form[$this->fieldTestData->field_name_2]['widget']['#title'], $this->fieldTestData->instance_2->getLabel(), "Second field's form title is {$this->fieldTestData->instance_2->getLabel()}"); + for ($delta = 0; $delta < $this->fieldTestData->field_storage->getCardinality(); $delta++) { // field_test_widget uses 'textfield' - $this->assertEqual($form[$this->field_name]['widget'][$delta]['value']['#type'], 'textfield', "First field's form delta $delta widget is textfield"); + $this->assertEqual($form[$this->fieldTestData->field_name]['widget'][$delta]['value']['#type'], 'textfield', "First field's form delta $delta widget is textfield"); } - for ($delta = 0; $delta < $this->field_storage_2->getCardinality(); $delta++) { + for ($delta = 0; $delta < $this->fieldTestData->field_storage_2->getCardinality(); $delta++) { // field_test_widget uses 'textfield' - $this->assertEqual($form[$this->field_name_2]['widget'][$delta]['value']['#type'], 'textfield', "Second field's form delta $delta widget is textfield"); + $this->assertEqual($form[$this->fieldTestData->field_name_2]['widget'][$delta]['value']['#type'], 'textfield', "Second field's form delta $delta widget is textfield"); } // Test generating widgets for all fields. - $display = entity_get_form_display($entity_type, $this->instance->bundle, 'default'); + $display = entity_get_form_display($entity_type, $this->fieldTestData->instance->bundle, 'default'); foreach ($display->getComponents() as $name => $options) { - if ($name != $this->field_name_2) { + if ($name != $this->fieldTestData->field_name_2) { $display->removeComponent($name); } } @@ -291,11 +277,11 @@ function testEntityFormDisplayBuildForm() { $form_state = new FormState(); $display->buildForm($entity, $form, $form_state); - $this->assertFalse(isset($form[$this->field_name]), 'The first field does not exist in the form'); - $this->assertEqual($form[$this->field_name_2]['widget']['#title'], $this->instance_2->getLabel(), "Second field's form title is {$this->instance_2->getLabel()}"); - for ($delta = 0; $delta < $this->field_storage_2->getCardinality(); $delta++) { + $this->assertFalse(isset($form[$this->fieldTestData->field_name]), 'The first field does not exist in the form'); + $this->assertEqual($form[$this->fieldTestData->field_name_2]['widget']['#title'], $this->fieldTestData->instance_2->getLabel(), "Second field's form title is {$this->fieldTestData->instance_2->getLabel()}"); + for ($delta = 0; $delta < $this->fieldTestData->field_storage_2->getCardinality(); $delta++) { // field_test_widget uses 'textfield' - $this->assertEqual($form[$this->field_name_2]['widget'][$delta]['value']['#type'], 'textfield', "Second field's form delta $delta widget is textfield"); + $this->assertEqual($form[$this->fieldTestData->field_name_2]['widget'][$delta]['value']['#type'], 'textfield', "Second field's form delta $delta widget is textfield"); } } @@ -306,10 +292,10 @@ function testEntityFormDisplayExtractFormValues() { $this->createFieldWithInstance('_2'); $entity_type = 'entity_test'; - $entity_init = entity_create($entity_type, array('id' => 1, 'revision_id' => 1, 'type' => $this->instance->bundle)); + $entity_init = entity_create($entity_type, array('id' => 1, 'revision_id' => 1, 'type' => $this->fieldTestData->instance->bundle)); // Build the form for all fields. - $display = entity_get_form_display($entity_type, $this->instance->bundle, 'default'); + $display = entity_get_form_display($entity_type, $this->fieldTestData->instance->bundle, 'default'); $form = array(); $form_state = new FormState(); $display->buildForm($entity_init, $form, $form_state); @@ -318,11 +304,11 @@ function testEntityFormDisplayExtractFormValues() { // First field. $values = array(); $weights = array(); - for ($delta = 0; $delta < $this->field_storage->getCardinality(); $delta++) { + for ($delta = 0; $delta < $this->fieldTestData->field_storage->getCardinality(); $delta++) { $values[$delta]['value'] = mt_rand(1, 127); // Assign random weight. do { - $weight = mt_rand(0, $this->field_storage->getCardinality()); + $weight = mt_rand(0, $this->fieldTestData->field_storage->getCardinality()); } while (in_array($weight, $weights)); $weights[$delta] = $weight; $values[$delta]['_weight'] = $weight; @@ -332,11 +318,11 @@ function testEntityFormDisplayExtractFormValues() { // Second field. $values_2 = array(); $weights_2 = array(); - for ($delta = 0; $delta < $this->field_storage_2->getCardinality(); $delta++) { + for ($delta = 0; $delta < $this->fieldTestData->field_storage_2->getCardinality(); $delta++) { $values_2[$delta]['value'] = mt_rand(1, 127); // Assign random weight. do { - $weight = mt_rand(0, $this->field_storage_2->getCardinality()); + $weight = mt_rand(0, $this->fieldTestData->field_storage_2->getCardinality()); } while (in_array($weight, $weights_2)); $weights_2[$delta] = $weight; $values_2[$delta]['_weight'] = $weight; @@ -348,8 +334,8 @@ function testEntityFormDisplayExtractFormValues() { $form_state['build_info']['callback_object'] = \Drupal::entityManager()->getFormObject($entity_type, 'default'); \Drupal::formBuilder()->prepareForm('field_test_entity_form', $form, $form_state); drupal_process_form('field_test_entity_form', $form, $form_state); - $form_state['values'][$this->field_name] = $values; - $form_state['values'][$this->field_name_2] = $values_2; + $form_state['values'][$this->fieldTestData->field_name] = $values; + $form_state['values'][$this->fieldTestData->field_name_2] = $values_2; // Extract values for all fields. $entity = clone($entity_init); @@ -364,17 +350,17 @@ function testEntityFormDisplayExtractFormValues() { $expected_values[] = array('value' => $values[$key]['value']); } } - $this->assertIdentical($entity->{$this->field_name}->getValue(), $expected_values, 'Submit filters empty values'); + $this->assertIdentical($entity->{$this->fieldTestData->field_name}->getValue(), $expected_values, 'Submit filters empty values'); foreach ($weights_2 as $key => $value) { if ($key != 1) { $expected_values_2[] = array('value' => $values_2[$key]['value']); } } - $this->assertIdentical($entity->{$this->field_name_2}->getValue(), $expected_values_2, 'Submit filters empty values'); + $this->assertIdentical($entity->{$this->fieldTestData->field_name_2}->getValue(), $expected_values_2, 'Submit filters empty values'); // Call EntityFormDisplayInterface::extractFormValues() for a single field (the second field). foreach ($display->getComponents() as $name => $options) { - if ($name != $this->field_name_2) { + if ($name != $this->fieldTestData->field_name_2) { $display->removeComponent($name); } } @@ -386,8 +372,8 @@ function testEntityFormDisplayExtractFormValues() { $expected_values_2[] = array('value' => $values_2[$key]['value']); } } - $this->assertTrue($entity->{$this->field_name}->isEmpty(), 'The first field is empty in the entity object'); - $this->assertIdentical($entity->{$this->field_name_2}->getValue(), $expected_values_2, 'Submit filters empty values'); + $this->assertTrue($entity->{$this->fieldTestData->field_name}->isEmpty(), 'The first field is empty in the entity object'); + $this->assertIdentical($entity->{$this->fieldTestData->field_name_2}->getValue(), $expected_values_2, 'Submit filters empty values'); } } diff --git a/core/modules/field/src/Tests/FieldAttachStorageTest.php b/core/modules/field/src/Tests/FieldAttachStorageTest.php index 05db62ed4af4df9b76a6ba4ae49c7ab9ba9bd657..a8aa9766dea517b6270f7f8b04a048dc53d860fc 100644 --- a/core/modules/field/src/Tests/FieldAttachStorageTest.php +++ b/core/modules/field/src/Tests/FieldAttachStorageTest.php @@ -16,20 +16,6 @@ */ class FieldAttachStorageTest extends FieldUnitTestBase { - /** - * The field instance. - * - * @var \Drupal\field\Entity\FieldInstanceConfig - */ - protected $instance; - - /** - * Field name to use in the test. - * - * @var string - */ - protected $field_name; - public function setUp() { parent::setUp(); $this->installEntitySchema('entity_test_rev'); @@ -44,7 +30,7 @@ public function setUp() { function testFieldAttachSaveLoad() { $entity_type = 'entity_test_rev'; $this->createFieldWithInstance('', $entity_type); - $cardinality = $this->field_storage->getCardinality(); + $cardinality = $this->fieldTestData->field_storage->getCardinality(); // TODO : test empty values filtering and "compression" (store consecutive deltas). // Preparation: create three revisions and store them in $revision array. @@ -53,7 +39,7 @@ function testFieldAttachSaveLoad() { for ($revision_id = 0; $revision_id < 3; $revision_id++) { // Note: we try to insert one extra value. $current_values = $this->_generateTestFieldValues($cardinality + 1); - $entity->{$this->field_name}->setValue($current_values); + $entity->{$this->fieldTestData->field_name}->setValue($current_values); $entity->setNewRevision(); $entity->save(); $entity_id = $entity->id(); @@ -66,20 +52,20 @@ function testFieldAttachSaveLoad() { $entity = $storage->load($entity_id); // Confirm current revision loads the correct data. // Number of values per field loaded equals the field cardinality. - $this->assertEqual(count($entity->{$this->field_name}), $cardinality, 'Current revision: expected number of values'); + $this->assertEqual(count($entity->{$this->fieldTestData->field_name}), $cardinality, 'Current revision: expected number of values'); for ($delta = 0; $delta < $cardinality; $delta++) { // The field value loaded matches the one inserted or updated. - $this->assertEqual($entity->{$this->field_name}[$delta]->value , $values[$current_revision][$delta]['value'], format_string('Current revision: expected value %delta was found.', array('%delta' => $delta))); + $this->assertEqual($entity->{$this->fieldTestData->field_name}[$delta]->value , $values[$current_revision][$delta]['value'], format_string('Current revision: expected value %delta was found.', array('%delta' => $delta))); } // Confirm each revision loads the correct data. foreach (array_keys($values) as $revision_id) { $entity = $storage->loadRevision($revision_id); // Number of values per field loaded equals the field cardinality. - $this->assertEqual(count($entity->{$this->field_name}), $cardinality, format_string('Revision %revision_id: expected number of values.', array('%revision_id' => $revision_id))); + $this->assertEqual(count($entity->{$this->fieldTestData->field_name}), $cardinality, format_string('Revision %revision_id: expected number of values.', array('%revision_id' => $revision_id))); for ($delta = 0; $delta < $cardinality; $delta++) { // The field value loaded matches the one inserted or updated. - $this->assertEqual($entity->{$this->field_name}[$delta]->value, $values[$revision_id][$delta]['value'], format_string('Revision %revision_id: expected value %delta was found.', array('%revision_id' => $revision_id, '%delta' => $delta))); + $this->assertEqual($entity->{$this->fieldTestData->field_name}[$delta]->value, $values[$revision_id][$delta]['value'], format_string('Revision %revision_id: expected value %delta was found.', array('%revision_id' => $revision_id, '%delta' => $delta))); } } } @@ -165,10 +151,10 @@ function testFieldAttachSaveEmptyData() { // Insert: Field is NULL. $entity = clone $entity_init; - $entity->{$this->field_name} = NULL; + $entity->{$this->fieldTestData->field_name} = NULL; $entity->enforceIsNew(); $entity = $this->entitySaveReload($entity); - $this->assertTrue($entity->{$this->field_name}->isEmpty(), 'Insert: NULL field results in no value saved'); + $this->assertTrue($entity->{$this->fieldTestData->field_name}->isEmpty(), 'Insert: NULL field results in no value saved'); // All saves after this point should be updates, not inserts. $entity_init->enforceIsNew(FALSE); @@ -176,28 +162,28 @@ function testFieldAttachSaveEmptyData() { // Add some real data. $entity = clone($entity_init); $values = $this->_generateTestFieldValues(1); - $entity->{$this->field_name} = $values; + $entity->{$this->fieldTestData->field_name} = $values; $entity = $this->entitySaveReload($entity); - $this->assertEqual($entity->{$this->field_name}->getValue(), $values, 'Field data saved'); + $this->assertEqual($entity->{$this->fieldTestData->field_name}->getValue(), $values, 'Field data saved'); // Update: Field is NULL. Data should be wiped. $entity = clone($entity_init); - $entity->{$this->field_name} = NULL; + $entity->{$this->fieldTestData->field_name} = NULL; $entity = $this->entitySaveReload($entity); - $this->assertTrue($entity->{$this->field_name}->isEmpty(), 'Update: NULL field removes existing values'); + $this->assertTrue($entity->{$this->fieldTestData->field_name}->isEmpty(), 'Update: NULL field removes existing values'); // Re-add some data. $entity = clone($entity_init); $values = $this->_generateTestFieldValues(1); - $entity->{$this->field_name} = $values; + $entity->{$this->fieldTestData->field_name} = $values; $entity = $this->entitySaveReload($entity); - $this->assertEqual($entity->{$this->field_name}->getValue(), $values, 'Field data saved'); + $this->assertEqual($entity->{$this->fieldTestData->field_name}->getValue(), $values, 'Field data saved'); // Update: Field is empty array. Data should be wiped. $entity = clone($entity_init); - $entity->{$this->field_name} = array(); + $entity->{$this->fieldTestData->field_name} = array(); $entity = $this->entitySaveReload($entity); - $this->assertTrue($entity->{$this->field_name}->isEmpty(), 'Update: empty array removes existing values'); + $this->assertTrue($entity->{$this->fieldTestData->field_name}->isEmpty(), 'Update: empty array removes existing values'); } /** @@ -208,25 +194,25 @@ function testFieldAttachSaveEmptyDataDefaultValue() { $this->createFieldWithInstance('', $entity_type); // Add a default value function. - $this->instance->default_value_function = 'field_test_default_value'; - $this->instance->save(); + $this->fieldTestData->instance->default_value_function = 'field_test_default_value'; + $this->fieldTestData->instance->save(); // Verify that fields are populated with default values. $entity_init = entity_create($entity_type, array('id' => 1, 'revision_id' => 1)); - $default = field_test_default_value($entity_init, $this->instance); - $this->assertEqual($entity_init->{$this->field_name}->getValue(), $default, 'Default field value correctly populated.'); + $default = field_test_default_value($entity_init, $this->fieldTestData->instance); + $this->assertEqual($entity_init->{$this->fieldTestData->field_name}->getValue(), $default, 'Default field value correctly populated.'); // Insert: Field is NULL. $entity = clone($entity_init); - $entity->{$this->field_name} = NULL; + $entity->{$this->fieldTestData->field_name} = NULL; $entity->enforceIsNew(); $entity = $this->entitySaveReload($entity); - $this->assertTrue($entity->{$this->field_name}->isEmpty(), 'Insert: NULL field results in no value saved'); + $this->assertTrue($entity->{$this->fieldTestData->field_name}->isEmpty(), 'Insert: NULL field results in no value saved'); // Verify that prepopulated field values are not overwritten by defaults. $value = array(array('value' => $default[0]['value'] - mt_rand(1, 127))); - $entity = entity_create($entity_type, array('type' => $entity_init->bundle(), $this->field_name => $value)); - $this->assertEqual($entity->{$this->field_name}->getValue(), $value, 'Prepopulated field value correctly maintained.'); + $entity = entity_create($entity_type, array('type' => $entity_init->bundle(), $this->fieldTestData->field_name => $value)); + $this->assertEqual($entity->{$this->fieldTestData->field_name}->getValue(), $value, 'Prepopulated field value correctly maintained.'); } /** @@ -235,13 +221,13 @@ function testFieldAttachSaveEmptyDataDefaultValue() { function testFieldAttachDelete() { $entity_type = 'entity_test_rev'; $this->createFieldWithInstance('', $entity_type); - $cardinality = $this->field_storage->getCardinality(); - $entity = entity_create($entity_type, array('type' => $this->instance->bundle)); + $cardinality = $this->fieldTestData->field_storage->getCardinality(); + $entity = entity_create($entity_type, array('type' => $this->fieldTestData->instance->bundle)); $vids = array(); // Create revision 0 $values = $this->_generateTestFieldValues($cardinality); - $entity->{$this->field_name} = $values; + $entity->{$this->fieldTestData->field_name} = $values; $entity->save(); $vids[] = $entity->getRevisionId(); @@ -260,7 +246,7 @@ function testFieldAttachDelete() { // Confirm each revision loads foreach ($vids as $vid) { $revision = $controller->loadRevision($vid); - $this->assertEqual(count($revision->{$this->field_name}), $cardinality, "The test entity revision $vid has $cardinality values."); + $this->assertEqual(count($revision->{$this->fieldTestData->field_name}), $cardinality, "The test entity revision $vid has $cardinality values."); } // Delete revision 1, confirm the other two still load. @@ -269,13 +255,13 @@ function testFieldAttachDelete() { foreach (array(0, 2) as $key) { $vid = $vids[$key]; $revision = $controller->loadRevision($vid); - $this->assertEqual(count($revision->{$this->field_name}), $cardinality, "The test entity revision $vid has $cardinality values."); + $this->assertEqual(count($revision->{$this->fieldTestData->field_name}), $cardinality, "The test entity revision $vid has $cardinality values."); } // Confirm the current revision still loads $controller->resetCache(); $current = $controller->load($entity->id()); - $this->assertEqual(count($current->{$this->field_name}), $cardinality, "The test entity current revision has $cardinality values."); + $this->assertEqual(count($current->{$this->fieldTestData->field_name}), $cardinality, "The test entity current revision has $cardinality values."); // Delete all field data, confirm nothing loads $entity->delete(); @@ -293,38 +279,38 @@ function testFieldAttachDelete() { function testEntityCreateRenameBundle() { $entity_type = 'entity_test_rev'; $this->createFieldWithInstance('', $entity_type); - $cardinality = $this->field_storage->getCardinality(); + $cardinality = $this->fieldTestData->field_storage->getCardinality(); // Create a new bundle. $new_bundle = 'test_bundle_' . drupal_strtolower($this->randomName()); entity_test_create_bundle($new_bundle, NULL, $entity_type); // Add an instance to that bundle. - $this->instance_definition['bundle'] = $new_bundle; - entity_create('field_instance_config', $this->instance_definition)->save(); + $this->fieldTestData->instance_definition['bundle'] = $new_bundle; + entity_create('field_instance_config', $this->fieldTestData->instance_definition)->save(); // Save an entity with data in the field. - $entity = entity_create($entity_type, array('type' => $this->instance->bundle)); + $entity = entity_create($entity_type, array('type' => $this->fieldTestData->instance->bundle)); $values = $this->_generateTestFieldValues($cardinality); - $entity->{$this->field_name} = $values; + $entity->{$this->fieldTestData->field_name} = $values; // Verify the field data is present on load. $entity = $this->entitySaveReload($entity); - $this->assertEqual(count($entity->{$this->field_name}), $cardinality, "Data is retrieved for the new bundle"); + $this->assertEqual(count($entity->{$this->fieldTestData->field_name}), $cardinality, "Data is retrieved for the new bundle"); // Rename the bundle. $new_bundle = 'test_bundle_' . drupal_strtolower($this->randomName()); - entity_test_rename_bundle($this->instance_definition['bundle'], $new_bundle, $entity_type); + entity_test_rename_bundle($this->fieldTestData->instance_definition['bundle'], $new_bundle, $entity_type); // Check that the instance definition has been updated. - $this->instance = FieldInstanceConfig::loadByName($entity_type, $new_bundle, $this->field_name); - $this->assertIdentical($this->instance->bundle, $new_bundle, "Bundle name has been updated in the instance."); + $this->fieldTestData->instance = FieldInstanceConfig::loadByName($entity_type, $new_bundle, $this->fieldTestData->field_name); + $this->assertIdentical($this->fieldTestData->instance->bundle, $new_bundle, "Bundle name has been updated in the instance."); // Verify the field data is present on load. $controller = $this->container->get('entity.manager')->getStorage($entity->getEntityTypeId()); $controller->resetCache(); $entity = $controller->load($entity->id()); - $this->assertEqual(count($entity->{$this->field_name}), $cardinality, "Bundle name has been updated in the field storage"); + $this->assertEqual(count($entity->{$this->fieldTestData->field_name}), $cardinality, "Bundle name has been updated in the field storage"); } /** @@ -339,8 +325,8 @@ function testEntityDeleteBundle() { entity_test_create_bundle($new_bundle, NULL, $entity_type); // Add an instance to that bundle. - $this->instance_definition['bundle'] = $new_bundle; - entity_create('field_instance_config', $this->instance_definition)->save(); + $this->fieldTestData->instance_definition['bundle'] = $new_bundle; + entity_create('field_instance_config', $this->fieldTestData->instance_definition)->save(); // Create a second field for the test bundle $field_name = drupal_strtolower($this->randomName() . '_field_name'); @@ -354,7 +340,7 @@ function testEntityDeleteBundle() { $instance = array( 'field_name' => $field_name, 'entity_type' => $entity_type, - 'bundle' => $this->instance->bundle, + 'bundle' => $this->fieldTestData->instance->bundle, 'label' => $this->randomName() . '_label', 'description' => $this->randomName() . '_description', 'weight' => mt_rand(0, 127), @@ -362,29 +348,29 @@ function testEntityDeleteBundle() { entity_create('field_instance_config', $instance)->save(); // Save an entity with data for both fields - $entity = entity_create($entity_type, array('type' => $this->instance->bundle)); - $values = $this->_generateTestFieldValues($this->field_storage->getCardinality()); - $entity->{$this->field_name} = $values; + $entity = entity_create($entity_type, array('type' => $this->fieldTestData->instance->bundle)); + $values = $this->_generateTestFieldValues($this->fieldTestData->field_storage->getCardinality()); + $entity->{$this->fieldTestData->field_name} = $values; $entity->{$field_name} = $this->_generateTestFieldValues(1); $entity = $this->entitySaveReload($entity); // Verify the fields are present on load - $this->assertEqual(count($entity->{$this->field_name}), 4, 'First field got loaded'); + $this->assertEqual(count($entity->{$this->fieldTestData->field_name}), 4, 'First field got loaded'); $this->assertEqual(count($entity->{$field_name}), 1, 'Second field got loaded'); // Delete the bundle. - entity_test_delete_bundle($this->instance->bundle, $entity_type); + entity_test_delete_bundle($this->fieldTestData->instance->bundle, $entity_type); // Verify no data gets loaded $controller = $this->container->get('entity.manager')->getStorage($entity->getEntityTypeId()); $controller->resetCache(); $entity= $controller->load($entity->id()); - $this->assertTrue(empty($entity->{$this->field_name}), 'No data for first field'); + $this->assertTrue(empty($entity->{$this->fieldTestData->field_name}), 'No data for first field'); $this->assertTrue(empty($entity->{$field_name}), 'No data for second field'); // Verify that the instances are gone. - $this->assertFalse(entity_load('field_instance_config', 'entity_test.' . $this->instance->bundle . '.' . $this->field_name), "First field is deleted"); + $this->assertFalse(entity_load('field_instance_config', 'entity_test.' . $this->fieldTestData->instance->bundle . '.' . $this->fieldTestData->field_name), "First field is deleted"); $this->assertFalse(entity_load('field_instance_config', 'entity_test.' . $instance['bundle']. '.' . $field_name), "Second field is deleted"); } diff --git a/core/modules/field/src/Tests/FieldUnitTestBase.php b/core/modules/field/src/Tests/FieldUnitTestBase.php index 9c82d59452b53bd81dfa7d09462b8f75c63aeca1..44b8d218d7b0098b56435d24a6cca2d82b16c562 100644 --- a/core/modules/field/src/Tests/FieldUnitTestBase.php +++ b/core/modules/field/src/Tests/FieldUnitTestBase.php @@ -23,11 +23,30 @@ abstract class FieldUnitTestBase extends DrupalUnitTestBase { */ public static $modules = array('user', 'entity', 'system', 'field', 'text', 'entity_test', 'field_test'); + /** + * Bag of created fields and instances. + * + * Allows easy access to test field/instance names/IDs/objects via: + * - $this->fields->field_name[suffix] + * - $this->fields->field_storage[suffix] + * - $this->fields->field_storage_uuid[suffix] + * - $this->fields->instance[suffix] + * - $this->fields->instance_definition[suffix] + * + * @see \Drupal\field\Tests\FieldUnitTestBase::createFieldWithInstance() + * + * @var \ArrayObject + */ + protected $fieldTestData; + /** * Set the default field storage backend for fields created during tests. */ - function setUp() { + protected function setUp() { parent::setUp(); + + $this->fieldTestData = new \ArrayObject(array(), \ArrayObject::ARRAY_AS_PROPS); + $this->installEntitySchema('entity_test'); $this->installEntitySchema('user'); $this->installSchema('system', array('sequences')); @@ -49,7 +68,7 @@ function setUp() { * (optional) The entity type on which the instance should be created. * Defaults to the default bundle of the entity type. */ - function createFieldWithInstance($suffix = '', $entity_type = 'entity_test', $bundle = NULL) { + protected function createFieldWithInstance($suffix = '', $entity_type = 'entity_test', $bundle = NULL) { if (empty($bundle)) { $bundle = $entity_type; } @@ -59,17 +78,17 @@ function createFieldWithInstance($suffix = '', $entity_type = 'entity_test', $bu $instance = 'instance' . $suffix; $instance_definition = 'instance_definition' . $suffix; - $this->$field_name = drupal_strtolower($this->randomName() . '_field_name' . $suffix); - $this->$field_storage = entity_create('field_storage_config', array( - 'name' => $this->$field_name, + $this->fieldTestData->$field_name = drupal_strtolower($this->randomName() . '_field_name' . $suffix); + $this->fieldTestData->$field_storage = entity_create('field_storage_config', array( + 'name' => $this->fieldTestData->$field_name, 'entity_type' => $entity_type, 'type' => 'test_field', 'cardinality' => 4, )); - $this->$field_storage->save(); - $this->$field_storage_uuid = $this->{$field_storage}->uuid(); - $this->$instance_definition = array( - 'field_storage' => $this->$field_storage, + $this->fieldTestData->$field_storage->save(); + $this->fieldTestData->$field_storage_uuid = $this->fieldTestData->$field_storage->uuid(); + $this->fieldTestData->$instance_definition = array( + 'field_storage' => $this->fieldTestData->$field_storage, 'bundle' => $bundle, 'label' => $this->randomName() . '_label', 'description' => $this->randomName() . '_description', @@ -77,11 +96,11 @@ function createFieldWithInstance($suffix = '', $entity_type = 'entity_test', $bu 'test_instance_setting' => $this->randomName(), ), ); - $this->$instance = entity_create('field_instance_config', $this->$instance_definition); - $this->$instance->save(); + $this->fieldTestData->$instance = entity_create('field_instance_config', $this->fieldTestData->$instance_definition); + $this->fieldTestData->$instance->save(); entity_get_form_display($entity_type, $bundle, 'default') - ->setComponent($this->$field_name, array( + ->setComponent($this->fieldTestData->$field_name, array( 'type' => 'test_field_widget', 'settings' => array( 'test_widget_setting' => $this->randomName(), @@ -114,7 +133,7 @@ protected function entitySaveReload(EntityInterface $entity) { * @return * An array of random values, in the format expected for field values. */ - function _generateTestFieldValues($cardinality) { + protected function _generateTestFieldValues($cardinality) { $values = array(); for ($i = 0; $i < $cardinality; $i++) { // field_test fields treat 0 as 'empty value'. @@ -140,7 +159,7 @@ function _generateTestFieldValues($cardinality) { * @param $column * (Optional) The name of the column to check. Defaults to 'value'. */ - function assertFieldValues(EntityInterface $entity, $field_name, $expected_values, $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED, $column = 'value') { + protected function assertFieldValues(EntityInterface $entity, $field_name, $expected_values, $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED, $column = 'value') { // Re-load the entity to make sure we have the latest changes. entity_get_controller($entity->getEntityTypeId())->resetCache(array($entity->id())); $e = entity_load($entity->getEntityTypeId(), $entity->id()); diff --git a/core/modules/field/src/Tests/FieldValidationTest.php b/core/modules/field/src/Tests/FieldValidationTest.php index 7b9cca21b390500394c7a98970d7431123287e4e..d7bcecb13e73cfa95ad175b519b0b747c3bb56b3 100644 --- a/core/modules/field/src/Tests/FieldValidationTest.php +++ b/core/modules/field/src/Tests/FieldValidationTest.php @@ -16,6 +16,21 @@ */ class FieldValidationTest extends FieldUnitTestBase { + /** + * @var string + */ + private $entityType; + + /** + * @var string + */ + private $bundle; + + /** + * @var \Drupal\Core\Entity\EntityInterface + */ + private $entity; + public function setUp() { parent::setUp(); @@ -35,27 +50,27 @@ public function setUp() { * Tests that the number of values is validated against the field cardinality. */ function testCardinalityConstraint() { - $cardinality = $this->field_storage->cardinality; + $cardinality = $this->fieldTestData->field_storage->cardinality; $entity = $this->entity; for ($delta = 0; $delta < $cardinality + 1; $delta++) { - $entity->{$this->field_name}->get($delta)->set('value', 1); + $entity->{$this->fieldTestData->field_name}->get($delta)->set('value', 1); } // Validate the field. - $violations = $entity->{$this->field_name}->validate(); + $violations = $entity->{$this->fieldTestData->field_name}->validate(); // Check that the expected constraint violations are reported. $this->assertEqual(count($violations), 1); $this->assertEqual($violations[0]->getPropertyPath(), ''); - $this->assertEqual($violations[0]->getMessage(), t('%name: this field cannot hold more than @count values.', array('%name' => $this->instance->getLabel(), '@count' => $cardinality))); + $this->assertEqual($violations[0]->getMessage(), t('%name: this field cannot hold more than @count values.', array('%name' => $this->fieldTestData->instance->getLabel(), '@count' => $cardinality))); } /** * Tests that constraints defined by the field type are validated. */ function testFieldConstraints() { - $cardinality = $this->field_storage->getCardinality(); + $cardinality = $this->fieldTestData->field_storage->getCardinality(); $entity = $this->entity; // The test is only valid if the field cardinality is greater than 2. @@ -70,13 +85,13 @@ function testFieldConstraints() { } else { $value = -1; - $expected_violations[$delta . '.value'][] = t('%name does not accept the value -1.', array('%name' => $this->instance->getLabel())); + $expected_violations[$delta . '.value'][] = t('%name does not accept the value -1.', array('%name' => $this->fieldTestData->instance->getLabel())); } - $entity->{$this->field_name}->get($delta)->set('value', $value); + $entity->{$this->fieldTestData->field_name}->get($delta)->set('value', $value); } // Validate the field. - $violations = $entity->{$this->field_name}->validate(); + $violations = $entity->{$this->fieldTestData->field_name}->validate(); // Check that the expected constraint violations are reported. $violations_by_path = array(); diff --git a/core/modules/quickedit/src/Tests/EditorSelectionTest.php b/core/modules/quickedit/src/Tests/EditorSelectionTest.php index e29cd3fb8f6fecf091f0cfe4a2febd2ea5a73229..10bf38b1fb38f1c72bc7df51c89ff6d040e83575 100644 --- a/core/modules/quickedit/src/Tests/EditorSelectionTest.php +++ b/core/modules/quickedit/src/Tests/EditorSelectionTest.php @@ -68,33 +68,33 @@ public function testText() { ); // Create an entity with values for this text field. - $this->entity = entity_create('entity_test'); - $this->entity->{$field_name}->value = 'Hello, world!'; - $this->entity->{$field_name}->format = 'full_html'; - $this->entity->save(); + $entity = entity_create('entity_test'); + $entity->{$field_name}->value = 'Hello, world!'; + $entity->{$field_name}->format = 'full_html'; + $entity->save(); // Editor selection without text processing, with cardinality 1. - $this->assertEqual('plain_text', $this->getSelectedEditor($this->entity->id(), $field_name), "Without text processing, cardinality 1, the 'plain_text' editor is selected."); + $this->assertEqual('plain_text', $this->getSelectedEditor($entity->id(), $field_name), "Without text processing, cardinality 1, the 'plain_text' editor is selected."); // Editor selection with text processing, cardinality 1. - $this->field_text_instance->settings['text_processing'] = 1; - $this->field_text_instance->save(); - $this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $field_name), "With text processing, cardinality 1, the 'form' editor is selected."); + $this->fields->field_text_instance->settings['text_processing'] = 1; + $this->fields->field_text_instance->save(); + $this->assertEqual('form', $this->getSelectedEditor($entity->id(), $field_name), "With text processing, cardinality 1, the 'form' editor is selected."); // Editor selection without text processing, cardinality 1 (again). - $this->field_text_instance->settings['text_processing'] = 0; - $this->field_text_instance->save(); - $this->assertEqual('plain_text', $this->getSelectedEditor($this->entity->id(), $field_name), "Without text processing again, cardinality 1, the 'plain_text' editor is selected."); + $this->fields->field_text_instance->settings['text_processing'] = 0; + $this->fields->field_text_instance->save(); + $this->assertEqual('plain_text', $this->getSelectedEditor($entity->id(), $field_name), "Without text processing again, cardinality 1, the 'plain_text' editor is selected."); // Editor selection without text processing, cardinality >1 - $this->field_text_field_storage->cardinality = 2; - $this->field_text_field_storage->save(); - $this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $field_name), "Without text processing, cardinality >1, the 'form' editor is selected."); + $this->fields->field_text_field_storage->cardinality = 2; + $this->fields->field_text_field_storage->save(); + $this->assertEqual('form', $this->getSelectedEditor($entity->id(), $field_name), "Without text processing, cardinality >1, the 'form' editor is selected."); // Editor selection with text processing, cardinality >1 - $this->field_text_instance->settings['text_processing'] = 1; - $this->field_text_instance->save(); - $this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $field_name), "With text processing, cardinality >1, the 'form' editor is selected."); + $this->fields->field_text_instance->settings['text_processing'] = 1; + $this->fields->field_text_instance->save(); + $this->assertEqual('form', $this->getSelectedEditor($entity->id(), $field_name), "With text processing, cardinality >1, the 'form' editor is selected."); } /** @@ -122,23 +122,23 @@ public function testTextWysiwyg() { ); // Create an entity with values for this text field. - $this->entity = entity_create('entity_test'); - $this->entity->{$field_name}->value = 'Hello, world!'; - $this->entity->{$field_name}->format = 'filtered_html'; - $this->entity->save(); + $entity = entity_create('entity_test'); + $entity->{$field_name}->value = 'Hello, world!'; + $entity->{$field_name}->format = 'filtered_html'; + $entity->save(); // Editor selection w/ cardinality 1, text format w/o associated text editor. - $this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $field_name), "With cardinality 1, and the filtered_html text format, the 'form' editor is selected."); + $this->assertEqual('form', $this->getSelectedEditor($entity->id(), $field_name), "With cardinality 1, and the filtered_html text format, the 'form' editor is selected."); // Editor selection w/ cardinality 1, text format w/ associated text editor. - $this->entity->{$field_name}->format = 'full_html'; - $this->entity->save(); - $this->assertEqual('wysiwyg', $this->getSelectedEditor($this->entity->id(), $field_name), "With cardinality 1, and the full_html text format, the 'wysiwyg' editor is selected."); + $entity->{$field_name}->format = 'full_html'; + $entity->save(); + $this->assertEqual('wysiwyg', $this->getSelectedEditor($entity->id(), $field_name), "With cardinality 1, and the full_html text format, the 'wysiwyg' editor is selected."); // Editor selection with text processing, cardinality >1 - $this->field_textarea_field_storage->cardinality = 2; - $this->field_textarea_field_storage->save(); - $this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $field_name), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected."); + $this->fields->field_textarea_field_storage->cardinality = 2; + $this->fields->field_textarea_field_storage->save(); + $this->assertEqual('form', $this->getSelectedEditor($entity->id(), $field_name), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected."); } /** @@ -159,17 +159,17 @@ public function testNumber() { ); // Create an entity with values for this text field. - $this->entity = entity_create('entity_test'); - $this->entity->{$field_name}->value = 42; - $this->entity->save(); + $entity = entity_create('entity_test'); + $entity->{$field_name}->value = 42; + $entity->save(); // Editor selection with cardinality 1. - $this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $field_name), "With cardinality 1, the 'form' editor is selected."); + $this->assertEqual('form', $this->getSelectedEditor($entity->id(), $field_name), "With cardinality 1, the 'form' editor is selected."); // Editor selection with cardinality >1. - $this->field_nr_field_storage->cardinality = 2; - $this->field_nr_field_storage->save(); - $this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $field_name), "With cardinality >1, the 'form' editor is selected."); + $this->fields->field_nr_field_storage->cardinality = 2; + $this->fields->field_nr_field_storage->save(); + $this->assertEqual('form', $this->getSelectedEditor($entity->id(), $field_name), "With cardinality >1, the 'form' editor is selected."); } } diff --git a/core/modules/quickedit/src/Tests/MetadataGeneratorTest.php b/core/modules/quickedit/src/Tests/MetadataGeneratorTest.php index 5fd0425ad0339e8880419ce44fdefefd27f3100e..f665071f367960d2159069bb8f4529b46fb957c2 100644 --- a/core/modules/quickedit/src/Tests/MetadataGeneratorTest.php +++ b/core/modules/quickedit/src/Tests/MetadataGeneratorTest.php @@ -89,11 +89,11 @@ public function testSimpleEntityType() { ); // Create an entity with values for this text field. - $this->entity = entity_create('entity_test'); - $this->entity->{$field_1_name}->value = 'Test'; - $this->entity->{$field_2_name}->value = 42; - $this->entity->save(); - $entity = entity_load('entity_test', $this->entity->id()); + $entity = entity_create('entity_test'); + $entity->{$field_1_name}->value = 'Test'; + $entity->{$field_2_name}->value = 42; + $entity->save(); + $entity = entity_load('entity_test', $entity->id()); // Verify metadata for field 1. $items_1 = $entity->getTranslation(LanguageInterface::LANGCODE_NOT_SPECIFIED)->get($field_1_name); @@ -123,7 +123,6 @@ public function testSimpleEntityType() { */ public function testEditorWithCustomMetadata() { $this->installSchema('system', 'url_alias'); - $this->enableModules(array('user', 'filter')); // Enable edit_test module so that the WYSIWYG editor becomes available. $this->enableModules(array('quickedit_test')); @@ -162,11 +161,11 @@ public function testEditorWithCustomMetadata() { $full_html_format->save(); // Create an entity with values for this rich text field. - $this->entity = entity_create('entity_test'); - $this->entity->{$field_name}->value = 'Test'; - $this->entity->{$field_name}->format = 'full_html'; - $this->entity->save(); - $entity = entity_load('entity_test', $this->entity->id()); + $entity = entity_create('entity_test'); + $entity->{$field_name}->value = 'Test'; + $entity->{$field_name}->format = 'full_html'; + $entity->save(); + $entity = entity_load('entity_test', $entity->id()); // Verify metadata. $items = $entity->getTranslation(LanguageInterface::LANGCODE_NOT_SPECIFIED)->get($field_name); diff --git a/core/modules/quickedit/src/Tests/QuickEditTestBase.php b/core/modules/quickedit/src/Tests/QuickEditTestBase.php index ec4d025e9d71ecdc76e7652764b1e96fd71713a7..4f4ece7b50e4ca47f8e64cc1927cf5e172be20bd 100644 --- a/core/modules/quickedit/src/Tests/QuickEditTestBase.php +++ b/core/modules/quickedit/src/Tests/QuickEditTestBase.php @@ -21,12 +21,27 @@ abstract class QuickEditTestBase extends DrupalUnitTestBase { */ public static $modules = array('system', 'entity', 'entity_test', 'field', 'field_test', 'filter', 'user', 'text', 'quickedit'); + /** + * Bag of created fields and instances. + * + * Allows easy access to test field/instance names/IDs/objects via: + * - $this->fields->{$field_name}_field_storage + * - $this->fields->{$field_name}_instance + * + * @see \Drupal\quickedit\Tests\QuickEditTestBase::createFieldWithInstance() + * + * @var \ArrayObject + */ + protected $fields; + /** * Sets the default field storage backend for fields created during tests. */ protected function setUp() { parent::setUp(); + $this->fields = new \ArrayObject(array(), \ArrayObject::ARRAY_AS_PROPS); + $this->installEntitySchema('entity_test'); $this->installConfig(array('field', 'filter')); } @@ -52,26 +67,26 @@ protected function setUp() { * @param array $formatter_settings * The formatter settings. */ - public function createFieldWithInstance($field_name, $type, $cardinality, $label, $instance_settings, $widget_type, $widget_settings, $formatter_type, $formatter_settings) { + protected function createFieldWithInstance($field_name, $type, $cardinality, $label, $instance_settings, $widget_type, $widget_settings, $formatter_type, $formatter_settings) { $field_storage = $field_name . '_field_storage'; - $this->$field_storage = entity_create('field_storage_config', array( + $this->fields->$field_storage = entity_create('field_storage_config', array( 'name' => $field_name, 'entity_type' => 'entity_test', 'type' => $type, 'cardinality' => $cardinality, )); - $this->$field_storage->save(); + $this->fields->$field_storage->save(); $instance = $field_name . '_instance'; - $this->$instance = entity_create('field_instance_config', array( - 'field_storage' => $this->$field_storage, + $this->fields->$instance = entity_create('field_instance_config', array( + 'field_storage' => $this->fields->$field_storage, 'bundle' => 'entity_test', 'label' => $label, 'description' => $label, 'weight' => mt_rand(0, 127), 'settings' => $instance_settings, )); - $this->$instance->save(); + $this->fields->$instance->save(); entity_get_form_display('entity_test', 'entity_test', 'default') ->setComponent($field_name, array(