function FieldAttachStorageTest::testFieldAttachLoadMultiple in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/field/src/Tests/FieldAttachStorageTest.php \Drupal\field\Tests\FieldAttachStorageTest::testFieldAttachLoadMultiple()
Test the 'multiple' load feature.
File
- core/
modules/ field/ src/ Tests/ FieldAttachStorageTest.php, line 77 - Contains \Drupal\field\Tests\FieldAttachStorageTest.
Class
- FieldAttachStorageTest
- Tests storage-related Field Attach API functions.
Namespace
Drupal\field\TestsCode
function testFieldAttachLoadMultiple() {
$entity_type = 'entity_test_rev';
// Define 2 bundles.
$bundles = array(
1 => 'test_bundle_1',
2 => 'test_bundle_2',
);
entity_test_create_bundle($bundles[1]);
entity_test_create_bundle($bundles[2]);
// Define 3 fields:
// - field_1 is in bundle_1 and bundle_2,
// - field_2 is in bundle_1,
// - field_3 is in bundle_2.
$field_bundles_map = array(
1 => array(
1,
2,
),
2 => array(
1,
),
3 => array(
2,
),
);
for ($i = 1; $i <= 3; $i++) {
$field_names[$i] = 'field_' . $i;
$field_storage = entity_create('field_storage_config', array(
'field_name' => $field_names[$i],
'entity_type' => $entity_type,
'type' => 'test_field',
));
$field_storage
->save();
$field_ids[$i] = $field_storage
->uuid();
foreach ($field_bundles_map[$i] as $bundle) {
entity_create('field_config', array(
'field_name' => $field_names[$i],
'entity_type' => $entity_type,
'bundle' => $bundles[$bundle],
))
->save();
}
}
// Create one test entity per bundle, with random values.
foreach ($bundles as $index => $bundle) {
$entities[$index] = entity_create($entity_type, array(
'id' => $index,
'revision_id' => $index,
'type' => $bundle,
));
$entity = clone $entities[$index];
foreach ($field_names as $field_name) {
if (!$entity
->hasField($field_name)) {
continue;
}
$values[$index][$field_name] = mt_rand(1, 127);
$entity->{$field_name}
->setValue(array(
'value' => $values[$index][$field_name],
));
}
$entity
->enforceIsnew();
$entity
->save();
}
// Check that a single load correctly loads field values for both entities.
$controller = \Drupal::entityManager()
->getStorage($entity
->getEntityTypeId());
$controller
->resetCache();
$entities = $controller
->loadMultiple();
foreach ($entities as $index => $entity) {
foreach ($field_names as $field_name) {
if (!$entity
->hasField($field_name)) {
continue;
}
// The field value loaded matches the one inserted.
$this
->assertEqual($entity->{$field_name}->value, $values[$index][$field_name], format_string('Entity %index: expected value was found.', array(
'%index' => $index,
)));
}
}
}