Executes the read write test set for a defined entity type.
string $entity_type: The entity type to run the tests with.
protected function assertReadWrite($entity_type) {
$entity = $this
->createTestEntity($entity_type);
// Access the name field.
$this
->assertTrue($entity->name instanceof FieldInterface, format_string('%entity_type: Field implements interface', array(
'%entity_type' => $entity_type,
)));
$this
->assertTrue($entity->name[0] instanceof FieldItemInterface, format_string('%entity_type: Field item implements interface', array(
'%entity_type' => $entity_type,
)));
$this
->assertEqual($this->entity_name, $entity->name->value, format_string('%entity_type: Name value can be read.', array(
'%entity_type' => $entity_type,
)));
$this
->assertEqual($this->entity_name, $entity->name[0]->value, format_string('%entity_type: Name value can be read through list access.', array(
'%entity_type' => $entity_type,
)));
$this
->assertEqual($entity->name
->getValue(), array(
0 => array(
'value' => $this->entity_name,
),
), format_string('%entity_type: Plain field value returned.', array(
'%entity_type' => $entity_type,
)));
// Change the name.
$new_name = $this
->randomName();
$entity->name->value = $new_name;
$this
->assertEqual($new_name, $entity->name->value, format_string('%entity_type: Name can be updated and read.', array(
'%entity_type' => $entity_type,
)));
$this
->assertEqual($entity->name
->getValue(), array(
0 => array(
'value' => $new_name,
),
), format_string('%entity_type: Plain field value reflects the update.', array(
'%entity_type' => $entity_type,
)));
$new_name = $this
->randomName();
$entity->name[0]->value = $new_name;
$this
->assertEqual($new_name, $entity->name->value, format_string('%entity_type: Name can be updated and read through list access.', array(
'%entity_type' => $entity_type,
)));
// Access the user field.
$this
->assertTrue($entity->user_id instanceof FieldInterface, format_string('%entity_type: Field implements interface', array(
'%entity_type' => $entity_type,
)));
$this
->assertTrue($entity->user_id[0] instanceof FieldItemInterface, format_string('%entity_type: Field item implements interface', array(
'%entity_type' => $entity_type,
)));
$this
->assertEqual($this->entity_user->uid, $entity->user_id->target_id, format_string('%entity_type: User id can be read.', array(
'%entity_type' => $entity_type,
)));
$this
->assertEqual($this->entity_user->name, $entity->user_id->entity->name->value, format_string('%entity_type: User name can be read.', array(
'%entity_type' => $entity_type,
)));
// Change the assigned user by entity.
$new_user = $this
->createUser();
$entity->user_id->entity = $new_user;
$this
->assertEqual($new_user->uid, $entity->user_id->target_id, format_string('%entity_type: Updated user id can be read.', array(
'%entity_type' => $entity_type,
)));
$this
->assertEqual($new_user->name, $entity->user_id->entity->name->value, format_string('%entity_type: Updated user name value can be read.', array(
'%entity_type' => $entity_type,
)));
// Change the assigned user by id.
$new_user = $this
->createUser();
$entity->user_id->target_id = $new_user->uid;
$this
->assertEqual($new_user->uid, $entity->user_id->target_id, format_string('%entity_type: Updated user id can be read.', array(
'%entity_type' => $entity_type,
)));
$this
->assertEqual($new_user->name, $entity->user_id->entity->name->value, format_string('%entity_type: Updated user name value can be read.', array(
'%entity_type' => $entity_type,
)));
// Try unsetting a field.
$entity->name->value = NULL;
$entity->user_id->target_id = NULL;
$this
->assertNull($entity->name->value, format_string('%entity_type: Name field is not set.', array(
'%entity_type' => $entity_type,
)));
$this
->assertNull($entity->user_id->target_id, format_string('%entity_type: User ID field is not set.', array(
'%entity_type' => $entity_type,
)));
$this
->assertNull($entity->user_id->entity, format_string('%entity_type: User entity field is not set.', array(
'%entity_type' => $entity_type,
)));
// Test using isset(), empty() and unset().
$entity->name->value = 'test unset';
unset($entity->name->value);
$this
->assertFalse(isset($entity->name->value), format_string('%entity_type: Name is not set.', array(
'%entity_type' => $entity_type,
)));
$this
->assertFalse(isset($entity->name[0]->value), format_string('%entity_type: Name is not set.', array(
'%entity_type' => $entity_type,
)));
$this
->assertTrue(empty($entity->name->value), format_string('%entity_type: Name is empty.', array(
'%entity_type' => $entity_type,
)));
$this
->assertTrue(empty($entity->name[0]->value), format_string('%entity_type: Name is empty.', array(
'%entity_type' => $entity_type,
)));
$entity->name->value = 'a value';
$this
->assertTrue(isset($entity->name->value), format_string('%entity_type: Name is set.', array(
'%entity_type' => $entity_type,
)));
$this
->assertTrue(isset($entity->name[0]->value), format_string('%entity_type: Name is set.', array(
'%entity_type' => $entity_type,
)));
$this
->assertFalse(empty($entity->name->value), format_string('%entity_type: Name is not empty.', array(
'%entity_type' => $entity_type,
)));
$this
->assertFalse(empty($entity->name[0]->value), format_string('%entity_type: Name is not empty.', array(
'%entity_type' => $entity_type,
)));
$this
->assertTrue(isset($entity->name[0]), format_string('%entity_type: Name string item is set.', array(
'%entity_type' => $entity_type,
)));
$this
->assertFalse(isset($entity->name[1]), format_string('%entity_type: Second name string item is not set as it does not exist', array(
'%entity_type' => $entity_type,
)));
$this
->assertTrue(isset($entity->name), format_string('%entity_type: Name field is set.', array(
'%entity_type' => $entity_type,
)));
$this
->assertFalse(isset($entity->nameInvalid), format_string('%entity_type: Not existing field is not set.', array(
'%entity_type' => $entity_type,
)));
unset($entity->name[0]);
$this
->assertFalse(isset($entity->name[0]), format_string('%entity_type: Name field item is not set.', array(
'%entity_type' => $entity_type,
)));
$this
->assertFalse(isset($entity->name[0]->value), format_string('%entity_type: Name is not set.', array(
'%entity_type' => $entity_type,
)));
$this
->assertFalse(isset($entity->name->value), format_string('%entity_type: Name is not set.', array(
'%entity_type' => $entity_type,
)));
$entity->name = array();
$this
->assertTrue(isset($entity->name), 'Name field is set.');
$this
->assertFalse(isset($entity->name[0]), 'Name field item is not set.');
$this
->assertFalse(isset($entity->name[0]->value), 'First name item value is not set.');
$this
->assertFalse(isset($entity->name->value), 'Name value is not set.');
$entity->name = NULL;
$this
->assertFalse(isset($entity->name), 'Name field is not set.');
$this
->assertFalse(isset($entity->name[0]), 'Name field item is not set.');
$this
->assertFalse(isset($entity->name[0]->value), 'First name item value is not set.');
$this
->assertFalse(isset($entity->name->value), 'Name value is not set.');
$entity->name->value = 'a value';
$this
->assertTrue(isset($entity->name->value), format_string('%entity_type: Name is set.', array(
'%entity_type' => $entity_type,
)));
unset($entity->name);
$this
->assertFalse(isset($entity->name), format_string('%entity_type: Name field is not set.', array(
'%entity_type' => $entity_type,
)));
$this
->assertFalse(isset($entity->name[0]), format_string('%entity_type: Name field item is not set.', array(
'%entity_type' => $entity_type,
)));
$this
->assertFalse(isset($entity->name[0]->value), format_string('%entity_type: Name is not set.', array(
'%entity_type' => $entity_type,
)));
$this
->assertFalse(isset($entity->name->value), format_string('%entity_type: Name is not set.', array(
'%entity_type' => $entity_type,
)));
// Access the language field.
$this
->assertEqual(Language::LANGCODE_NOT_SPECIFIED, $entity->langcode->value, format_string('%entity_type: Language code can be read.', array(
'%entity_type' => $entity_type,
)));
$this
->assertEqual(language_load(Language::LANGCODE_NOT_SPECIFIED), $entity->langcode->language, format_string('%entity_type: Language object can be read.', array(
'%entity_type' => $entity_type,
)));
// Change the language by code.
$entity->langcode->value = language_default()->langcode;
$this
->assertEqual(language_default()->langcode, $entity->langcode->value, format_string('%entity_type: Language code can be read.', array(
'%entity_type' => $entity_type,
)));
$this
->assertEqual(language_default(), $entity->langcode->language, format_string('%entity_type: Language object can be read.', array(
'%entity_type' => $entity_type,
)));
// Revert language by code then try setting it by language object.
$entity->langcode->value = Language::LANGCODE_NOT_SPECIFIED;
$entity->langcode->language = language_default();
$this
->assertEqual(language_default()->langcode, $entity->langcode->value, format_string('%entity_type: Language code can be read.', array(
'%entity_type' => $entity_type,
)));
$this
->assertEqual(language_default(), $entity->langcode->language, format_string('%entity_type: Language object can be read.', array(
'%entity_type' => $entity_type,
)));
// Access the text field and test updating.
$this
->assertEqual($entity->field_test_text->value, $this->entity_field_text, format_string('%entity_type: Text field can be read.', array(
'%entity_type' => $entity_type,
)));
$new_text = $this
->randomName();
$entity->field_test_text->value = $new_text;
$this
->assertEqual($entity->field_test_text->value, $new_text, format_string('%entity_type: Updated text field can be read.', array(
'%entity_type' => $entity_type,
)));
// Test creating the entity by passing in plain values.
$this->entity_name = $this
->randomName();
$name_item[0]['value'] = $this->entity_name;
$this->entity_user = $this
->createUser();
$user_item[0]['target_id'] = $this->entity_user->uid;
$this->entity_field_text = $this
->randomName();
$text_item[0]['value'] = $this->entity_field_text;
$entity = entity_create($entity_type, array(
'name' => $name_item,
'user_id' => $user_item,
'field_test_text' => $text_item,
));
$this
->assertEqual($this->entity_name, $entity->name->value, format_string('%entity_type: Name value can be read.', array(
'%entity_type' => $entity_type,
)));
$this
->assertEqual($this->entity_user->uid, $entity->user_id->target_id, format_string('%entity_type: User id can be read.', array(
'%entity_type' => $entity_type,
)));
$this
->assertEqual($this->entity_user->name, $entity->user_id->entity->name->value, format_string('%entity_type: User name can be read.', array(
'%entity_type' => $entity_type,
)));
$this
->assertEqual($this->entity_field_text, $entity->field_test_text->value, format_string('%entity_type: Text field can be read.', array(
'%entity_type' => $entity_type,
)));
// Test copying field values.
$entity2 = $this
->createTestEntity($entity_type);
$entity2->name = $entity->name;
$entity2->user_id = $entity->user_id;
$entity2->field_test_text = $entity->field_test_text;
$this
->assertTrue($entity->name !== $entity2->name, format_string('%entity_type: Copying properties results in a different field object.', array(
'%entity_type' => $entity_type,
)));
$this
->assertEqual($entity->name->value, $entity2->name->value, format_string('%entity_type: Name field copied.', array(
'%entity_type' => $entity_type,
)));
$this
->assertEqual($entity->user_id->target_id, $entity2->user_id->target_id, format_string('%entity_type: User id field copied.', array(
'%entity_type' => $entity_type,
)));
$this
->assertEqual($entity->field_test_text->value, $entity2->field_test_text->value, format_string('%entity_type: Text field copied.', array(
'%entity_type' => $entity_type,
)));
// Tests adding a value to a field item list.
$entity->name[] = 'Another name';
$this
->assertEqual($entity->name[1]->value == 'Another name', format_string('%entity_type: List item added via [].', array(
'%entity_type' => $entity_type,
)));
$entity->name[2]->value = 'Third name';
$this
->assertEqual($entity->name[2]->value == 'Third name', format_string('%entity_type: List item added by a accessing not yet created item.', array(
'%entity_type' => $entity_type,
)));
// Test removing and empty-ing list items.
$this
->assertEqual(count($entity->name), 3, format_string('%entity_type: List has 3 items.', array(
'%entity_type' => $entity_type,
)));
unset($entity->name[1]);
$this
->assertEqual(count($entity->name), 2, format_string('%entity_type: Second list item has been removed.', array(
'%entity_type' => $entity_type,
)));
$entity->name[2] = NULL;
$this
->assertEqual(count($entity->name), 2, format_string('%entity_type: Assigning NULL does not reduce array count.', array(
'%entity_type' => $entity_type,
)));
$this
->assertTrue($entity->name[2]
->isEmpty(), format_string('%entity_type: Assigning NULL empties the item.', array(
'%entity_type' => $entity_type,
)));
// Test using isEmpty().
unset($entity->name[2]);
$this
->assertFalse($entity->name[0]
->isEmpty(), format_string('%entity_type: Name item is not empty.', array(
'%entity_type' => $entity_type,
)));
$entity->name->value = NULL;
$this
->assertTrue($entity->name[0]
->isEmpty(), format_string('%entity_type: Name item is empty.', array(
'%entity_type' => $entity_type,
)));
$this
->assertTrue($entity->name
->isEmpty(), format_string('%entity_type: Name field is empty.', array(
'%entity_type' => $entity_type,
)));
$this
->assertEqual(count($entity->name), 1, format_string('%entity_type: Empty item is considered when counting.', array(
'%entity_type' => $entity_type,
)));
$this
->assertEqual(count(iterator_to_array($entity->name
->getIterator())), count($entity->name), format_string('%entity_type: Count matches iterator count.', array(
'%entity_type' => $entity_type,
)));
$this
->assertTrue($entity->name
->getValue() === array(
0 => array(
'value' => NULL,
),
), format_string('%entity_type: Name field value contains a NULL value.', array(
'%entity_type' => $entity_type,
)));
// Test removing all list items by assigning an empty array.
$entity->name = array();
$this
->assertIdentical(count($entity->name), 0, format_string('%entity_type: Name field contains no items.', array(
'%entity_type' => $entity_type,
)));
$this
->assertIdentical($entity->name
->getValue(), array(), format_string('%entity_type: Name field value is an empty array.', array(
'%entity_type' => $entity_type,
)));
$entity->name->value = 'foo';
$this
->assertEqual($entity->name->value, 'foo', format_string('%entity_type: Name field set.', array(
'%entity_type' => $entity_type,
)));
// Test removing all list items by setting it to NULL.
$entity->name = NULL;
$this
->assertIdentical(count($entity->name), 0, format_string('%entity_type: Name field contains no items.', array(
'%entity_type' => $entity_type,
)));
$this
->assertNull($entity->name
->getValue(), format_string('%entity_type: Name field value is an empty array.', array(
'%entity_type' => $entity_type,
)));
// Test get and set field values.
$entity->name = 'foo';
$this
->assertEqual($entity->name[0]
->getPropertyValues(), array(
'value' => 'foo',
), format_string('%entity_type: Field value has been retrieved via getPropertyValue()', array(
'%entity_type' => $entity_type,
)));
$entity->name[0]
->setPropertyValues(array(
'value' => 'bar',
));
$this
->assertEqual($entity->name->value, 'bar', format_string('%entity_type: Field value has been set via setPropertyValue()', array(
'%entity_type' => $entity_type,
)));
$values = $entity
->getPropertyValues();
$this
->assertEqual($values['name'], array(
0 => array(
'value' => 'bar',
),
), format_string('%entity_type: Field value has been retrieved via getPropertyValue() from an entity.', array(
'%entity_type' => $entity_type,
)));
$entity
->setPropertyValues(array(
'name' => 'foo',
));
$this
->assertEqual($entity->name->value, 'foo', format_string('%entity_type: Field value has been set via setPropertyValue() on an entity.', array(
'%entity_type' => $entity_type,
)));
// Make sure the user id can be set to zero.
$user_item[0]['target_id'] = 0;
$entity = entity_create($entity_type, array(
'name' => $name_item,
'user_id' => $user_item,
'field_test_text' => $text_item,
));
$this
->assertNotNull($entity->user_id->target_id, format_string('%entity_type: User id is not NULL', array(
'%entity_type' => $entity_type,
)));
$this
->assertIdentical($entity->user_id->target_id, 0, format_string('%entity_type: User id has been set to 0', array(
'%entity_type' => $entity_type,
)));
// Test setting the ID with the value only.
$entity = entity_create($entity_type, array(
'name' => $name_item,
'user_id' => 0,
'field_test_text' => $text_item,
));
$this
->assertNotNull($entity->user_id->target_id, format_string('%entity_type: User id is not NULL', array(
'%entity_type' => $entity_type,
)));
$this
->assertIdentical($entity->user_id->target_id, 0, format_string('%entity_type: User id has been set to 0', array(
'%entity_type' => $entity_type,
)));
}