diff --git a/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php b/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php index 3daf090..d26c079 100644 --- a/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php @@ -16,7 +16,47 @@ */ class BulkDeleteTest extends FieldUnitTestBase { - protected $field; + /** + * The fields to use in this test. + * + * @var array + */ + protected $fields; + + /** + * The instance to use in this test. + * + * @var array + */ + protected $instances; + + /** + * The entities to use in this test. + * + * @var array + */ + protected $entities; + + /** + * The entities to use in this test, keyed by bundle. + * + * @var array + */ + protected $entities_by_bundles; + + /** + * The bundles for the entities used in this test. + * + * @var array + */ + protected $bundles; + + /** + * The entity type to be used in the test classes. + * + * @var array + */ + protected $entity_type = 'test_entity'; public static function getInfo() { return array( @@ -37,8 +77,7 @@ public static function getInfo() { * @param $field_name * A field name whose data should be copied from $entities into the returned * partial entities. - * @return - * An array of partial entities corresponding to $entities. + * @return array An array of partial entities corresponding to $entities. */ protected function convertToPartialEntities($entities, $field_name) { $partial_entities = array(); @@ -122,7 +161,6 @@ function setUp() { // For each bundle, create an instance of each field, and 10 // entities with values for each field. $id = 1; - $this->entity_type = 'test_entity'; foreach ($this->bundles as $bundle) { foreach ($this->fields as $field) { $instance = entity_create('field_instance', array( @@ -136,7 +174,7 @@ function setUp() { for ($i = 0; $i < 10; $i++) { $entity = field_test_create_entity($id, $id, $bundle); foreach ($this->fields as $field) { - $entity->{$field->id()}[Language::LANGCODE_NOT_SPECIFIED] = $this->_generateTestFieldValues($field->cardinality]); + $entity->{$field->id()}[Language::LANGCODE_NOT_SPECIFIED] = $this->_generateTestFieldValues($field->cardinality); } $entity->save(); $id++; @@ -174,7 +212,7 @@ function testDeleteFieldInstance() { $instance->delete(); // The instance still exists, deleted. - $instances = field_read_instances(array('field_id' => $field->id(), 'deleted' => TRUE), array('include_deleted' => TRUE, 'include_inactive' => TRUE)); + $instances = field_read_instances(array('field_id' => $field->uuid, 'deleted' => TRUE), array('include_deleted' => TRUE, 'include_inactive' => TRUE)); $this->assertEqual(count($instances), 1, 'There is one deleted instance'); $this->assertEqual($instances[0]['bundle'], $bundle, 'The deleted instance is for the correct bundle'); @@ -344,4 +382,5 @@ function testPurgeField() { $fields = field_read_fields(array('uuid' => $field->uuid), array('include_deleted' => TRUE, 'include_inactive' => TRUE)); $this->assertEqual(count($fields), 0, 'The field is purged.'); } + } diff --git a/core/modules/field/lib/Drupal/field/Tests/CrudTest.php b/core/modules/field/lib/Drupal/field/Tests/CrudTest.php index bdd71ca..3f9846f 100644 --- a/core/modules/field/lib/Drupal/field/Tests/CrudTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/CrudTest.php @@ -416,7 +416,7 @@ function testUpdateFieldForbid() { 'settings' => array( 'changeable' => 0, 'unchangeable' => 0 - )); + ))); $field->save(); $field->settings['changeable']++; try { diff --git a/core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php b/core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php index aaec563..57b917d 100644 --- a/core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php @@ -11,6 +11,48 @@ class DisplayApiTest extends FieldUnitTestBase { + /** + * The field name to use in this test. + * + * @var string + */ + protected $field_name; + + /** + * The field label to use in this test. + * + * @var string + */ + protected $label; + + /** + * The field cardinality to use in this test. + * + * @var number + */ + protected $cardinality; + + /** + * The field display options to use in this test. + * + * @var array + */ + protected $display_options; + + /** + * The field display options to use in this test. + * + * @var \Drupal\Core\Entity\EntityInterface + */ + protected $entity; + + /** + * An array of random values, in the format expected for field values. + * + * @var array + */ + protected $values; + public static function getInfo() { return array( 'name' => 'Field Display API tests', @@ -27,12 +69,12 @@ function setUp() { $this->label = $this->randomName(); $this->cardinality = 4; - $this->field = array( + $field = array( 'field_name' => $this->field_name, 'type' => 'test_field', 'cardinality' => $this->cardinality, ); - $this->instance = array( + $instance = array( 'field_name' => $this->field_name, 'entity_type' => 'test_entity', 'bundle' => 'test_bundle', @@ -54,14 +96,14 @@ function setUp() { ), ); - entity_create('field_entity', $this->field)->save(); - entity_create('field_instance', $this->instance)->save(); + entity_create('field_entity', $field)->save(); + entity_create('field_instance', $instance)->save(); // Create a display for the default view mode. - entity_get_display($this->instance['entity_type'], $this->instance['bundle'], 'default') + entity_get_display($instance['entity_type'], $instance['bundle'], 'default') ->setComponent($this->field_name, $this->display_options['default']) ->save(); // Create a display for the teaser view mode. - entity_get_display($this->instance['entity_type'], $this->instance['bundle'], 'teaser') + entity_get_display($instance['entity_type'], $instance['bundle'], 'teaser') ->setComponent($this->field_name, $this->display_options['teaser']) ->save(); diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldAccessTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldAccessTest.php index 3416f31..58fb767 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldAccessTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldAccessTest.php @@ -19,6 +19,20 @@ class FieldAccessTest extends FieldTestBase { */ public static $modules = array('node', 'field_test'); + /** + * Node entity to use in this test. + * + * @var \Drupal\node\Plugin\Core\Entity\Node + */ + protected $node; + + /** + * Field value to test display on nodes. + * + * @var string + */ + protected $test_view_field_value; + public static function getInfo() { return array( 'name' => 'Field access tests', @@ -34,32 +48,32 @@ function setUp() { $this->drupalLogin($web_user); // Create content type. - $this->content_type_info = $this->drupalCreateContentType(); - $this->content_type = $this->content_type_info->type; + $content_type_info = $this->drupalCreateContentType(); + $content_type = $content_type_info->type; - $this->field = array( + $field = array( 'field_name' => 'test_view_field', 'type' => 'text', ); - entity_create('field_entity', $this->field)->save(); - $this->instance = array( - 'field_name' => $this->field['field_name'], + entity_create('field_entity', $field)->save(); + $instance = array( + 'field_name' => $field['field_name'], 'entity_type' => 'node', - 'bundle' => $this->content_type, + 'bundle' => $content_type, ); - entity_create('field_instance', $this->instance)->save(); + entity_create('field_instance', $instance)->save(); // Assign display properties for the 'default' and 'teaser' view modes. foreach (array('default', 'teaser') as $view_mode) { - entity_get_display('node', $this->content_type, $view_mode) - ->setComponent($this->field['field_name']) + entity_get_display('node', $content_type, $view_mode) + ->setComponent($field['field_name']) ->save(); } // Create test node. $this->test_view_field_value = 'This is some text'; $settings = array(); - $settings['type'] = $this->content_type; + $settings['type'] = $content_type; $settings['title'] = 'Field view access test'; $settings['test_view_field'] = array(array('value' => $this->test_view_field_value)); $this->node = $this->drupalCreateNode($settings); diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php index e795a5a..b44d1cb 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php @@ -14,6 +14,21 @@ * Unit test class for non-storage related field_attach_* functions. */ 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 static function getInfo() { return array( 'name' => 'Field attach tests (other)', @@ -554,4 +569,5 @@ function testFieldAttachExtractFormValues() { $this->assertFalse(isset($entity->{$this->field_name}), 'The first field does not exist in the entity object'); $this->assertIdentical($entity->{$this->field_name_2}[$langcode], $expected_values_2, 'Submit filters empty values'); } + } diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php index 0e46e15..d2b29e7 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php @@ -24,6 +24,13 @@ class FieldAttachStorageTest extends FieldUnitTestBase { */ protected $instance; + /** + * Field name to use in the test. + * + * @var string + */ + protected $field_name; + public static function getInfo() { return array( 'name' => 'Field attach tests (storage-related)', @@ -531,4 +538,5 @@ function testEntityDeleteBundle() { $this->assertFalse(field_read_instance('test_entity', $this->field_name, $this->instance['bundle']), "First field is deleted"); $this->assertFalse(field_read_instance('test_entity', $field_name, $instance['bundle']), "Second field is deleted"); } + } diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php index dde8d57..1388168 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php @@ -333,11 +333,10 @@ function testFieldInfoCache() { * Test that the widget definition functions work. */ function testWidgetDefinition() { - $widget_definition = field_info_widget_types('test_field_widget_multiple'); // Test if hook_field_widget_info_alter is beïng called. $this->assertTrue(in_array('test_field', $widget_definition['field_types']), "The 'test_field_widget_multiple' widget is enabled for the 'test_field' field type in field_test_field_widget_info_alter()."); - } + } diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php index 4e8fff1..9caf203 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldInstanceCrudTest.php @@ -215,4 +215,5 @@ function testDeleteFieldInstance() { $field = field_read_field($another_instance['field_name']); $this->assertFalse($field, 'The field marked to be deleted is not found anymore in the configuration.'); } + } diff --git a/core/modules/field/lib/Drupal/field/Tests/FormTest.php b/core/modules/field/lib/Drupal/field/Tests/FormTest.php index 1947c16..955b4c0 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FormTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FormTest.php @@ -19,6 +19,34 @@ class FormTest extends FieldTestBase { */ public static $modules = array('node', 'field_test', 'options'); + /** + * An array of values defining a field single. + * + * @var array + */ + protected $field_single; + + /** + * An array of values defining a field multiple. + * + * @var array + */ + protected $field_multiple; + + /** + * An array of values defining a field with unlimited cardinality. + * + * @var array + */ + protected $field_unlimited; + + /** + * An array of values defining a field instance. + * + * @var array + */ + protected $instance; + public static function getInfo() { return array( 'name' => 'Field form tests', @@ -50,13 +78,13 @@ function setUp() { } function testFieldFormSingle() { - $this->field = $this->field_single; - $this->field_name = $this->field['field_name']; - $this->instance['field_name'] = $this->field_name; - entity_create('field_entity', $this->field)->save(); + $field = $this->field_single; + $field_name = $field['field_name']; + $this->instance['field_name'] = $field_name; + entity_create('field_entity', $field)->save(); entity_create('field_instance', $this->instance)->save(); entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') - ->setComponent($this->field_name) + ->setComponent($field_name) ->save(); $langcode = Language::LANGCODE_NOT_SPECIFIED; @@ -66,93 +94,93 @@ function testFieldFormSingle() { // Create token value expected for description. $token_description = check_plain(config('system.site')->get('name')) . '_description'; $this->assertText($token_description, 'Token replacement for description is displayed'); - $this->assertFieldByName("{$this->field_name}[$langcode][0][value]", '', 'Widget is displayed'); - $this->assertNoField("{$this->field_name}[$langcode][1][value]", 'No extraneous widget is displayed'); + $this->assertFieldByName("{$field_name}[$langcode][0][value]", '', 'Widget is displayed'); + $this->assertNoField("{$field_name}[$langcode][1][value]", 'No extraneous widget is displayed'); // Check that hook_field_widget_form_alter() does not believe this is the // default value form. $this->assertNoText('From hook_field_widget_form_alter(): Default form is true.', 'Not default value form in hook_field_widget_form_alter().'); // Submit with invalid value (field-level validation). - $edit = array("{$this->field_name}[$langcode][0][value]" => -1); + $edit = array("{$field_name}[$langcode][0][value]" => -1); $this->drupalPost(NULL, $edit, t('Save')); $this->assertRaw(t('%name does not accept the value -1.', array('%name' => $this->instance['label'])), 'Field validation fails with invalid input.'); // TODO : check that the correct field is flagged for error. // Create an entity $value = mt_rand(1, 127); - $edit = array("{$this->field_name}[$langcode][0][value]" => $value); + $edit = array("{$field_name}[$langcode][0][value]" => $value); $this->drupalPost(NULL, $edit, t('Save')); preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match); $id = $match[1]; $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created'); $entity = field_test_entity_test_load($id); - $this->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'Field value was saved'); + $this->assertEqual($entity->{$field_name}[$langcode][0]['value'], $value, 'Field value was saved'); // Display edit form. $this->drupalGet('test-entity/manage/' . $id . '/edit'); - $this->assertFieldByName("{$this->field_name}[$langcode][0][value]", $value, 'Widget is displayed with the correct default value'); - $this->assertNoField("{$this->field_name}[$langcode][1][value]", 'No extraneous widget is displayed'); + $this->assertFieldByName("{$field_name}[$langcode][0][value]", $value, 'Widget is displayed with the correct default value'); + $this->assertNoField("{$field_name}[$langcode][1][value]", 'No extraneous widget is displayed'); // Update the entity. $value = mt_rand(1, 127); - $edit = array("{$this->field_name}[$langcode][0][value]" => $value); + $edit = array("{$field_name}[$langcode][0][value]" => $value); $this->drupalPost(NULL, $edit, t('Save')); $this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id)), 'Entity was updated'); $this->container->get('plugin.manager.entity')->getStorageController('test_entity')->resetCache(array($id)); $entity = field_test_entity_test_load($id); - $this->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'Field value was updated'); + $this->assertEqual($entity->{$field_name}[$langcode][0]['value'], $value, 'Field value was updated'); // Empty the field. $value = ''; - $edit = array("{$this->field_name}[$langcode][0][value]" => $value); + $edit = array("{$field_name}[$langcode][0][value]" => $value); $this->drupalPost('test-entity/manage/' . $id . '/edit', $edit, t('Save')); $this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id)), 'Entity was updated'); $this->container->get('plugin.manager.entity')->getStorageController('test_entity')->resetCache(array($id)); $entity = field_test_entity_test_load($id); - $this->assertIdentical($entity->{$this->field_name}, array(), 'Field was emptied'); + $this->assertIdentical($entity->{$field_name}, array(), 'Field was emptied'); } /** * Tests field widget default values on entity forms. */ function testFieldFormDefaultValue() { - $this->field = $this->field_single; - $this->field_name = $this->field['field_name']; - $this->instance['field_name'] = $this->field_name; + $field = $this->field_single; + $field_name = $field['field_name']; + $this->instance['field_name'] = $field_name; $default = rand(1, 127); $this->instance['default_value'] = array(array('value' => $default)); - entity_create('field_entity', $this->field)->save(); + entity_create('field_entity', $field)->save(); entity_create('field_instance', $this->instance)->save(); entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') - ->setComponent($this->field_name) + ->setComponent($field_name) ->save(); $langcode = Language::LANGCODE_NOT_SPECIFIED; // Display creation form. $this->drupalGet('test-entity/add/test_bundle'); // Test that the default value is displayed correctly. - $this->assertFieldByXpath("//input[@name='{$this->field_name}[$langcode][0][value]' and @value='$default']"); + $this->assertFieldByXpath("//input[@name='{$field_name}[$langcode][0][value]' and @value='$default']"); // Try to submit an empty value. - $edit = array("{$this->field_name}[$langcode][0][value]" => ''); + $edit = array("{$field_name}[$langcode][0][value]" => ''); $this->drupalPost(NULL, $edit, t('Save')); preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match); $id = $match[1]; $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created.'); $entity = field_test_entity_test_load($id); - $this->assertTrue(empty($entity->{$this->field_name}), 'Field is now empty.'); + $this->assertTrue(empty($entity->{$field_name}), 'Field is now empty.'); } function testFieldFormSingleRequired() { - $this->field = $this->field_single; - $this->field_name = $this->field['field_name']; - $this->instance['field_name'] = $this->field_name; + $field = $this->field_single; + $field_name = $field['field_name']; + $this->instance['field_name'] = $field_name; $this->instance['required'] = TRUE; - entity_create('field_entity', $this->field)->save(); + entity_create('field_entity', $field)->save(); entity_create('field_instance', $this->instance)->save(); entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') - ->setComponent($this->field_name) + ->setComponent($field_name) ->save(); $langcode = Language::LANGCODE_NOT_SPECIFIED; @@ -163,50 +191,50 @@ function testFieldFormSingleRequired() { // Create an entity $value = mt_rand(1, 127); - $edit = array("{$this->field_name}[$langcode][0][value]" => $value); + $edit = array("{$field_name}[$langcode][0][value]" => $value); $this->drupalPost(NULL, $edit, t('Save')); preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match); $id = $match[1]; $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created'); $entity = field_test_entity_test_load($id); - $this->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'Field value was saved'); + $this->assertEqual($entity->{$field_name}[$langcode][0]['value'], $value, 'Field value was saved'); // Edit with missing required value. $value = ''; - $edit = array("{$this->field_name}[$langcode][0][value]" => $value); + $edit = array("{$field_name}[$langcode][0][value]" => $value); $this->drupalPost('test-entity/manage/' . $id . '/edit', $edit, t('Save')); $this->assertRaw(t('!name field is required.', array('!name' => $this->instance['label'])), 'Required field with no value fails validation'); } // function testFieldFormMultiple() { // $this->field = $this->field_multiple; -// $this->field_name = $this->field['field_name']; -// $this->instance['field_name'] = $this->field_name; +// $field_name = $this->field['field_name']; +// $this->instance['field_name'] = $field_name; // entity_create('field_entity', $this->field)->save(); // entity_create('field_instance', $this->instance)->save(); // } function testFieldFormUnlimited() { - $this->field = $this->field_unlimited; - $this->field_name = $this->field['field_name']; - $this->instance['field_name'] = $this->field_name; - entity_create('field_entity', $this->field)->save(); + $field = $this->field_unlimited; + $field_name = $field['field_name']; + $this->instance['field_name'] = $field_name; + entity_create('field_entity', $field)->save(); entity_create('field_instance', $this->instance)->save(); entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') - ->setComponent($this->field_name) + ->setComponent($field_name) ->save(); $langcode = Language::LANGCODE_NOT_SPECIFIED; // Display creation form -> 1 widget. $this->drupalGet('test-entity/add/test_bundle'); - $this->assertFieldByName("{$this->field_name}[$langcode][0][value]", '', 'Widget 1 is displayed'); - $this->assertNoField("{$this->field_name}[$langcode][1][value]", 'No extraneous widget is displayed'); + $this->assertFieldByName("{$field_name}[$langcode][0][value]", '', 'Widget 1 is displayed'); + $this->assertNoField("{$field_name}[$langcode][1][value]", 'No extraneous widget is displayed'); // Press 'add more' button -> 2 widgets. $this->drupalPost(NULL, array(), t('Add another item')); - $this->assertFieldByName("{$this->field_name}[$langcode][0][value]", '', 'Widget 1 is displayed'); - $this->assertFieldByName("{$this->field_name}[$langcode][1][value]", '', 'New widget is displayed'); - $this->assertNoField("{$this->field_name}[$langcode][2][value]", 'No extraneous widget is displayed'); + $this->assertFieldByName("{$field_name}[$langcode][0][value]", '', 'Widget 1 is displayed'); + $this->assertFieldByName("{$field_name}[$langcode][1][value]", '', 'New widget is displayed'); + $this->assertNoField("{$field_name}[$langcode][2][value]", 'No extraneous widget is displayed'); // TODO : check that non-field inpurs are preserved ('title')... // Yet another time so that we can play with more values -> 3 widgets. @@ -224,8 +252,8 @@ function testFieldFormUnlimited() { do { $weight = mt_rand(-$delta_range, $delta_range); } while (in_array($weight, $weights)); - $edit["$this->field_name[$langcode][$delta][value]"] = $value; - $edit["$this->field_name[$langcode][$delta][_weight]"] = $weight; + $edit["{$field_name}[$langcode][$delta][value]"] = $value; + $edit["{$field_name}[$langcode][$delta][_weight]"] = $weight; // We'll need three slightly different formats to check the values. $values[$delta] = $value; $weights[$delta] = $weight; @@ -236,15 +264,15 @@ function testFieldFormUnlimited() { // Press 'add more' button -> 4 widgets $this->drupalPost(NULL, $edit, t('Add another item')); for ($delta = 0; $delta <= $delta_range; $delta++) { - $this->assertFieldByName("$this->field_name[$langcode][$delta][value]", $values[$delta], "Widget $delta is displayed and has the right value"); - $this->assertFieldByName("$this->field_name[$langcode][$delta][_weight]", $weights[$delta], "Widget $delta has the right weight"); + $this->assertFieldByName("{$field_name}[$langcode][$delta][value]", $values[$delta], "Widget $delta is displayed and has the right value"); + $this->assertFieldByName("{$field_name}[$langcode][$delta][_weight]", $weights[$delta], "Widget $delta has the right weight"); } ksort($pattern); $pattern = implode('.*', array_values($pattern)); $this->assertPattern("|$pattern|s", 'Widgets are displayed in the correct order'); - $this->assertFieldByName("$this->field_name[$langcode][$delta][value]", '', "New widget is displayed"); - $this->assertFieldByName("$this->field_name[$langcode][$delta][_weight]", $delta, "New widget has the right weight"); - $this->assertNoField("$this->field_name[$langcode][" . ($delta + 1) . '][value]', 'No extraneous widget is displayed'); + $this->assertFieldByName("{$field_name}[$langcode][$delta][value]", '', "New widget is displayed"); + $this->assertFieldByName("{$field_name}[$langcode][$delta][_weight]", $delta, "New widget has the right weight"); + $this->assertNoField("{$field_name}[$langcode][" . ($delta + 1) . '][value]', 'No extraneous widget is displayed'); // Submit the form and create the entity. $this->drupalPost(NULL, $edit, t('Save')); @@ -254,7 +282,7 @@ function testFieldFormUnlimited() { $entity = field_test_entity_test_load($id); ksort($field_values); $field_values = array_values($field_values); - $this->assertIdentical($entity->{$this->field_name}[$langcode], $field_values, 'Field values were saved in the correct order'); + $this->assertIdentical($entity->{$field_name}[$langcode], $field_values, 'Field values were saved in the correct order'); // Display edit form: check that the expected number of widgets is // displayed, with correct values change values, reorder, leave an empty @@ -270,13 +298,13 @@ function testFieldFormUnlimited() { */ function testFieldFormMultivalueWithRequiredRadio() { // Create a multivalue test field. - $this->field = $this->field_unlimited; - $this->field_name = $this->field['field_name']; - $this->instance['field_name'] = $this->field_name; - entity_create('field_entity', $this->field)->save(); + $field = $this->field_unlimited; + $field_name = $field['field_name']; + $this->instance['field_name'] = $field_name; + entity_create('field_entity', $field)->save(); entity_create('field_instance', $this->instance)->save(); entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') - ->setComponent($this->field_name) + ->setComponent($field_name) ->save(); $langcode = Language::LANGCODE_NOT_SPECIFIED; @@ -311,19 +339,19 @@ function testFieldFormMultivalueWithRequiredRadio() { $this->assertNoFieldByXpath('//div[contains(@class, "error")]', FALSE, 'No error message is displayed.'); // Verify that the widget is added. - $this->assertFieldByName("{$this->field_name}[$langcode][0][value]", '', 'Widget 1 is displayed'); - $this->assertFieldByName("{$this->field_name}[$langcode][1][value]", '', 'New widget is displayed'); - $this->assertNoField("{$this->field_name}[$langcode][2][value]", 'No extraneous widget is displayed'); + $this->assertFieldByName("{$field_name}[$langcode][0][value]", '', 'Widget 1 is displayed'); + $this->assertFieldByName("{$field_name}[$langcode][1][value]", '', 'New widget is displayed'); + $this->assertNoField("{$field_name}[$langcode][2][value]", 'No extraneous widget is displayed'); } function testFieldFormJSAddMore() { - $this->field = $this->field_unlimited; - $this->field_name = $this->field['field_name']; - $this->instance['field_name'] = $this->field_name; - entity_create('field_entity', $this->field)->save(); + $field = $this->field_unlimited; + $field_name = $field['field_name']; + $this->instance['field_name'] = $field_name; + entity_create('field_entity', $field)->save(); entity_create('field_instance', $this->instance)->save(); entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') - ->setComponent($this->field_name) + ->setComponent($field_name) ->save(); $langcode = Language::LANGCODE_NOT_SPECIFIED; @@ -348,8 +376,8 @@ function testFieldFormJSAddMore() { do { $weight = mt_rand(-$delta_range, $delta_range); } while (in_array($weight, $weights)); - $edit["$this->field_name[$langcode][$delta][value]"] = $value; - $edit["$this->field_name[$langcode][$delta][_weight]"] = $weight; + $edit["{$field_name}[$langcode][$delta][value]"] = $value; + $edit["{$field_name}[$langcode][$delta][_weight]"] = $weight; // We'll need three slightly different formats to check the values. $values[$delta] = $value; $weights[$delta] = $weight; @@ -358,19 +386,19 @@ function testFieldFormJSAddMore() { } // Press 'add more' button through Ajax, and place the expected HTML result // as the tested content. - $commands = $this->drupalPostAJAX(NULL, $edit, $this->field_name . '_add_more'); + $commands = $this->drupalPostAJAX(NULL, $edit, $field_name . '_add_more'); $this->content = $commands[1]['data']; for ($delta = 0; $delta <= $delta_range; $delta++) { - $this->assertFieldByName("$this->field_name[$langcode][$delta][value]", $values[$delta], "Widget $delta is displayed and has the right value"); - $this->assertFieldByName("$this->field_name[$langcode][$delta][_weight]", $weights[$delta], "Widget $delta has the right weight"); + $this->assertFieldByName("{$field_name}[$langcode][$delta][value]", $values[$delta], "Widget $delta is displayed and has the right value"); + $this->assertFieldByName("{$field_name}[$langcode][$delta][_weight]", $weights[$delta], "Widget $delta has the right weight"); } ksort($pattern); $pattern = implode('.*', array_values($pattern)); $this->assertPattern("|$pattern|s", 'Widgets are displayed in the correct order'); - $this->assertFieldByName("$this->field_name[$langcode][$delta][value]", '', "New widget is displayed"); - $this->assertFieldByName("$this->field_name[$langcode][$delta][_weight]", $delta, "New widget has the right weight"); - $this->assertNoField("$this->field_name[$langcode][" . ($delta + 1) . '][value]', 'No extraneous widget is displayed'); + $this->assertFieldByName("{$field_name}[$langcode][$delta][value]", '', "New widget is displayed"); + $this->assertFieldByName("{$field_name}[$langcode][$delta][_weight]", $delta, "New widget has the right weight"); + $this->assertNoField("{$field_name}[$langcode][" . ($delta + 1) . '][value]', 'No extraneous widget is displayed'); } /** @@ -379,13 +407,13 @@ function testFieldFormJSAddMore() { function testFieldFormMultipleWidget() { // Create a field with fixed cardinality and an instance using a multiple // widget. - $this->field = $this->field_multiple; - $this->field_name = $this->field['field_name']; - $this->instance['field_name'] = $this->field_name; - entity_create('field_entity', $this->field)->save(); + $field = $this->field_multiple; + $field_name = $field['field_name']; + $this->instance['field_name'] = $field_name; + entity_create('field_entity', $field)->save(); entity_create('field_instance', $this->instance)->save(); entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') - ->setComponent($this->field_name, array( + ->setComponent($field_name, array( 'type' => 'test_field_widget_multiple', )) ->save(); @@ -393,28 +421,28 @@ function testFieldFormMultipleWidget() { // Display creation form. $this->drupalGet('test-entity/add/test_bundle'); - $this->assertFieldByName("{$this->field_name}[$langcode]", '', 'Widget is displayed.'); + $this->assertFieldByName("{$field_name}[$langcode]", '', 'Widget is displayed.'); // Create entity with three values. - $edit = array("{$this->field_name}[$langcode]" => '1, 2, 3'); + $edit = array("{$field_name}[$langcode]" => '1, 2, 3'); $this->drupalPost(NULL, $edit, t('Save')); preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match); $id = $match[1]; // Check that the values were saved. $entity_init = field_test_create_entity($id); - $this->assertFieldValues($entity_init, $this->field_name, $langcode, array(1, 2, 3)); + $this->assertFieldValues($entity_init, $field_name, $langcode, array(1, 2, 3)); // Display the form, check that the values are correctly filled in. $this->drupalGet('test-entity/manage/' . $id . '/edit'); - $this->assertFieldByName("{$this->field_name}[$langcode]", '1, 2, 3', 'Widget is displayed.'); + $this->assertFieldByName("{$field_name}[$langcode]", '1, 2, 3', 'Widget is displayed.'); // Submit the form with more values than the field accepts. - $edit = array("{$this->field_name}[$langcode]" => '1, 2, 3, 4, 5'); + $edit = array("{$field_name}[$langcode]" => '1, 2, 3, 4, 5'); $this->drupalPost(NULL, $edit, t('Save')); $this->assertRaw('this field cannot hold more than 4 values', 'Form validation failed.'); // Check that the field values were not submitted. - $this->assertFieldValues($entity_init, $this->field_name, $langcode, array(1, 2, 3)); + $this->assertFieldValues($entity_init, $field_name, $langcode, array(1, 2, 3)); } /** @@ -615,11 +643,11 @@ function testNestedFieldForm() { * Tests the Hidden widget. */ function testFieldFormHiddenWidget() { - $this->field = $this->field_single; - $this->field_name = $this->field['field_name']; - $this->instance['field_name'] = $this->field_name; + $field = $this->field_single; + $field_name = $field['field_name']; + $this->instance['field_name'] = $field_name; $this->instance['default_value'] = array(0 => array('value' => 99)); - entity_create('field_entity', $this->field)->save(); + entity_create('field_entity', $field)->save(); $this->instance = entity_create('field_instance', $this->instance); $this->instance->save(); entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') @@ -634,13 +662,13 @@ function testFieldFormHiddenWidget() { // Create an entity and test that the default value is assigned correctly to // the field that uses the hidden widget. - $this->assertNoField("{$this->field_name}[$langcode][0][value]", 'The hidden widget is not displayed'); + $this->assertNoField("{$field_name}[$langcode][0][value]", 'The hidden widget is not displayed'); $this->drupalPost(NULL, array(), t('Save')); preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match); $id = $match[1]; $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created'); $entity = field_test_entity_test_load($id); - $this->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], 99, 'Default value was saved'); + $this->assertEqual($entity->{$field_name}[$langcode][0]['value'], 99, 'Default value was saved'); // Update the instance to remove the default value and switch to the // default widget. @@ -654,16 +682,16 @@ function testFieldFormHiddenWidget() { // Display edit form. $this->drupalGet('test-entity/manage/' . $id . '/edit'); - $this->assertFieldByName("{$this->field_name}[$langcode][0][value]", 99, 'Widget is displayed with the correct default value'); + $this->assertFieldByName("{$field_name}[$langcode][0][value]", 99, 'Widget is displayed with the correct default value'); // Update the entity. $value = mt_rand(1, 127); - $edit = array("{$this->field_name}[$langcode][0][value]" => $value); + $edit = array("{$field_name}[$langcode][0][value]" => $value); $this->drupalPost(NULL, $edit, t('Save')); $this->assertRaw(t('test_entity @id has been updated.', array('@id' => $id)), 'Entity was updated'); entity_get_controller('test_entity')->resetCache(array($id)); $entity = field_test_entity_test_load($id); - $this->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'Field value was updated'); + $this->assertEqual($entity->{$field_name}[$langcode][0]['value'], $value, 'Field value was updated'); // Update the form display and switch to the Hidden widget again. entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default') @@ -679,7 +707,7 @@ function testFieldFormHiddenWidget() { // Check that the expected value has been carried over to the new revision. entity_get_controller('test_entity')->resetCache(array($id)); $entity = field_test_entity_test_load($id); - $this->assertEqual($entity->{$this->field_name}[$langcode][0]['value'], $value, 'New revision has the expected value for the field with the Hidden widget'); + $this->assertEqual($entity->{$field_name}[$langcode][0]['value'], $value, 'New revision has the expected value for the field with the Hidden widget'); } /** @@ -698,4 +726,5 @@ function assertFieldValues(EntityInterface $entity, $field_name, $langcode, $exp $this->assertEqual($values[$key][$column], $value, format_string('Value @value was saved correctly.', array('@value' => $value))); } } + } diff --git a/core/modules/field/lib/Drupal/field/Tests/ShapeItemTest.php b/core/modules/field/lib/Drupal/field/Tests/ShapeItemTest.php index 3437924..6f7f3a0 100644 --- a/core/modules/field/lib/Drupal/field/Tests/ShapeItemTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/ShapeItemTest.php @@ -22,6 +22,13 @@ class ShapeItemTest extends FieldUnitTestBase { */ public static $modules = array('field_test'); + /** + * The name of the field to use in this test. + * + * @var string + */ + protected $field_name = 'field_shape'; + public static function getInfo() { return array( 'name' => 'Shape field item', @@ -34,17 +41,17 @@ public function setUp() { parent::setUp(); // Create an field field and instance for validation. - $this->field = array( - 'field_name' => 'field_shape', + $field = array( + 'field_name' => $this->field_name, 'type' => 'shape', ); - entity_create('field_entity', $this->field)->save(); - $this->instance = array( + entity_create('field_entity', $field)->save(); + $instance = array( 'entity_type' => 'entity_test', - 'field_name' => 'field_shape', + 'field_name' => $this->field_name, 'bundle' => 'entity_test', ); - entity_create('field_instance', $this->instance)->save(); + entity_create('field_instance', $instance)->save(); } /** @@ -55,34 +62,34 @@ public function testShapeItem() { $entity = entity_create('entity_test', array()); $shape = 'cube'; $color = 'blue'; - $entity->field_shape->shape = $shape; - $entity->field_shape->color = $color; + $entity->{$this->field_name}->shape = $shape; + $entity->{$this->field_name}->color = $color; $entity->name->value = $this->randomName(); $entity->save(); // Verify entity has been created properly. $id = $entity->id(); $entity = entity_load('entity_test', $id); - $this->assertTrue($entity->field_shape instanceof FieldInterface, 'Field implements interface.'); - $this->assertTrue($entity->field_shape[0] instanceof FieldItemInterface, 'Field item implements interface.'); - $this->assertEqual($entity->field_shape->shape, $shape); - $this->assertEqual($entity->field_shape->color, $color); - $this->assertEqual($entity->field_shape[0]->shape, $shape); - $this->assertEqual($entity->field_shape[0]->color, $color); + $this->assertTrue($entity->{$this->field_name} instanceof FieldInterface, 'Field implements interface.'); + $this->assertTrue($entity->{$this->field_name}[0] instanceof FieldItemInterface, 'Field item implements interface.'); + $this->assertEqual($entity->{$this->field_name}->shape, $shape); + $this->assertEqual($entity->{$this->field_name}->color, $color); + $this->assertEqual($entity->{$this->field_name}[0]->shape, $shape); + $this->assertEqual($entity->{$this->field_name}[0]->color, $color); // Verify changing the field value. $new_shape = 'circle'; $new_color = 'red'; - $entity->field_shape->shape = $new_shape; - $entity->field_shape->color = $new_color; - $this->assertEqual($entity->field_shape->shape, $new_shape); - $this->assertEqual($entity->field_shape->color, $new_color); + $entity->{$this->field_name}->shape = $new_shape; + $entity->{$this->field_name}->color = $new_color; + $this->assertEqual($entity->{$this->field_name}->shape, $new_shape); + $this->assertEqual($entity->{$this->field_name}->color, $new_color); // Read changed entity and assert changed values. $entity->save(); $entity = entity_load('entity_test', $id); - $this->assertEqual($entity->field_shape->shape, $new_shape); - $this->assertEqual($entity->field_shape->color, $new_color); + $this->assertEqual($entity->{$this->field_name}->shape, $new_shape); + $this->assertEqual($entity->{$this->field_name}->color, $new_color); } } diff --git a/core/modules/field/lib/Drupal/field/Tests/TestItemTest.php b/core/modules/field/lib/Drupal/field/Tests/TestItemTest.php index 063dca4..e8b85d1 100644 --- a/core/modules/field/lib/Drupal/field/Tests/TestItemTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/TestItemTest.php @@ -22,6 +22,13 @@ class TestItemTest extends FieldUnitTestBase { */ public static $modules = array('field_test'); + /** + * The name of the field to use in this test. + * + * @var string + */ + protected $field_name = 'field_test'; + public static function getInfo() { return array( 'name' => 'Test field item', @@ -34,17 +41,17 @@ public function setUp() { parent::setUp(); // Create an field field and instance for validation. - $this->field = array( - 'field_name' => 'field_test', + $field = array( + 'field_name' => $this->field_name, 'type' => 'test_field', ); - entity_create('field_entity', $this->field)->save(); - $this->instance = array( + entity_create('field_entity', $field)->save(); + $instance = array( 'entity_type' => 'entity_test', - 'field_name' => 'field_test', + 'field_name' => $this->field_name, 'bundle' => 'entity_test', ); - entity_create('field_instance', $this->instance)->save(); + entity_create('field_instance', $instance)->save(); } /** @@ -61,20 +68,20 @@ public function testTestItem() { // Verify entity has been created properly. $id = $entity->id(); $entity = entity_load('entity_test', $id); - $this->assertTrue($entity->field_test instanceof FieldInterface, 'Field implements interface.'); - $this->assertTrue($entity->field_test[0] instanceof FieldItemInterface, 'Field item implements interface.'); - $this->assertEqual($entity->field_test->value, $value); - $this->assertEqual($entity->field_test[0]->value, $value); + $this->assertTrue($entity->{$this->field_name} instanceof FieldInterface, 'Field implements interface.'); + $this->assertTrue($entity->{$this->field_name}[0] instanceof FieldItemInterface, 'Field item implements interface.'); + $this->assertEqual($entity->{$this->field_name}->value, $value); + $this->assertEqual($entity->{$this->field_name}[0]->value, $value); // Verify changing the field value. $new_value = rand(1, 10); $entity->field_test->value = $new_value; - $this->assertEqual($entity->field_test->value, $new_value); + $this->assertEqual($entity->{$this->field_name}->value, $new_value); // Read changed entity and assert changed values. $entity->save(); $entity = entity_load('entity_test', $id); - $this->assertEqual($entity->field_test->value, $new_value); + $this->assertEqual($entity->{$this->field_name}->value, $new_value); } } diff --git a/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php b/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php index 37787a8..f755e51 100644 --- a/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php @@ -26,6 +26,49 @@ class TranslationTest extends FieldUnitTestBase { */ public static $modules = array('language', 'node'); + /** + * The name of the field to use in this test. + * + * @var string + */ + protected $field_name; + + /** + * The name of the entity type to use in this test. + * + * @var string + */ + protected $entity_type = 'test_entity'; + + + /** + * An array defining the field to use in this test. + * + * @var array + */ + protected $field_definition; + + /** + * An array defining the field instance to use in this test. + * + * @var array + */ + protected $instance_definition; + + /** + * The field to use in this test. + * + * @var \Drupal\field\Plugin\Core\Entity\Field + */ + protected $field; + + /** + * The field instance to use in this test. + * + * @var \Drupal\field\Plugin\Core\Entity\FieldInstance + */ + protected $instance; + public static function getInfo() { return array( 'name' => 'Field translations tests', @@ -41,8 +84,6 @@ function setUp() { $this->field_name = drupal_strtolower($this->randomName() . '_field_name'); - $this->entity_type = 'test_entity'; - $this->field_definition = array( 'field_name' => $this->field_name, 'type' => 'test_field', @@ -107,7 +148,6 @@ function testFieldInvoke() { // Enable field translations for the entity. field_test_entity_info_translatable('test_entity', TRUE); - $entity_type = 'test_entity'; $entity = field_test_create_entity(0, 0, $this->instance['bundle']); // Populate some extra languages to check if _field_invoke() correctly uses @@ -388,4 +428,5 @@ function testFieldDisplayLanguage() { $display_langcode = field_language($entity, $this->field_name, $requested_langcode); $this->assertEqual($display_langcode, $requested_langcode, 'Display language behave correctly when language fallback is disabled'); } + } diff --git a/core/modules/field/lib/Drupal/field/Tests/TranslationWebTest.php b/core/modules/field/lib/Drupal/field/Tests/TranslationWebTest.php index 102c69c..7001ad0 100644 --- a/core/modules/field/lib/Drupal/field/Tests/TranslationWebTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/TranslationWebTest.php @@ -21,6 +21,34 @@ class TranslationWebTest extends FieldTestBase { */ public static $modules = array('language', 'field_test'); + /** + * The name of the field to use in this test. + * + * @var string + */ + protected $field_name; + + /** + * The name of the entity type to use in this test. + * + * @var string + */ + protected $entity_type = 'test_entity'; + + /** + * The field to use in this test. + * + * @var \Drupal\field\Plugin\Core\Entity\Field + */ + protected $field; + + /** + * The field instance to use in this test. + * + * @var \Drupal\field\Plugin\Core\Entity\FieldInstance + */ + protected $instance; + public static function getInfo() { return array( 'name' => 'Field translations web tests', @@ -34,8 +62,6 @@ function setUp() { $this->field_name = drupal_strtolower($this->randomName() . '_field_name'); - $this->entity_type = 'test_entity'; - $field = array( 'field_name' => $this->field_name, 'type' => 'test_field', diff --git a/core/modules/field/lib/Drupal/field/Tests/Views/ApiDataTest.php b/core/modules/field/lib/Drupal/field/Tests/Views/ApiDataTest.php index 386e35d..4bf2625 100644 --- a/core/modules/field/lib/Drupal/field/Tests/Views/ApiDataTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/Views/ApiDataTest.php @@ -66,7 +66,7 @@ function setUp() { 'field_name_0' => array((array('value' => $this->randomName()))), 'field_name_2' => array((array('value' => $this->randomName()))), ); - $this->nodes[] = $this->drupalCreateNode($edit); + $nodes[] = $this->drupalCreateNode($edit); } $this->container->get('views.views_data')->clear(); diff --git a/core/modules/field/lib/Drupal/field/Tests/Views/FieldTestBase.php b/core/modules/field/lib/Drupal/field/Tests/Views/FieldTestBase.php index dcc43ae..3cba44c 100644 --- a/core/modules/field/lib/Drupal/field/Tests/Views/FieldTestBase.php +++ b/core/modules/field/lib/Drupal/field/Tests/Views/FieldTestBase.php @@ -35,12 +35,15 @@ /** * Stores the field definitions used by the test. + * * @var array */ public $fields; + /** * Stores the instances of the fields. They have * the same keys as the fields. + * * @var array */ public $instances;