protected function ResourceTestBase::setUpFields in JSON:API 8
Same name and namespace in other branches
- 8.2 tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::setUpFields()
Sets up additional fields for testing.
Parameters
\Drupal\Core\Entity\EntityInterface $entity: The primary test entity.
\Drupal\user\UserInterface $account: The primary test user account.
Return value
\Drupal\Core\Entity\EntityInterface The reloaded entity with the new fields attached.
Throws
\Drupal\Core\Entity\EntityStorageException
1 call to ResourceTestBase::setUpFields()
- ResourceTestBase::setUp in tests/
src/ Functional/ ResourceTestBase.php
File
- tests/
src/ Functional/ ResourceTestBase.php, line 208
Class
- ResourceTestBase
- Subclass this for every JSON API resource type.
Namespace
Drupal\Tests\jsonapi\FunctionalCode
protected function setUpFields(EntityInterface $entity, UserInterface $account) {
if (!$entity instanceof FieldableEntityInterface) {
return $entity;
}
$entity_bundle = $entity
->bundle();
$account_bundle = $account
->bundle();
// Add access-protected field.
FieldStorageConfig::create([
'entity_type' => static::$entityTypeId,
'field_name' => 'field_rest_test',
'type' => 'text',
])
->setCardinality(1)
->save();
FieldConfig::create([
'entity_type' => static::$entityTypeId,
'field_name' => 'field_rest_test',
'bundle' => $entity_bundle,
])
->setLabel('Test field')
->setTranslatable(FALSE)
->save();
FieldStorageConfig::create([
'entity_type' => static::$entityTypeId,
'field_name' => 'field_jsonapi_test_entity_ref',
'type' => 'entity_reference',
])
->setSetting('target_type', 'user')
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->save();
FieldConfig::create([
'entity_type' => static::$entityTypeId,
'field_name' => 'field_jsonapi_test_entity_ref',
'bundle' => $entity_bundle,
])
->setTranslatable(FALSE)
->setSetting('handler', 'default')
->setSetting('handler_settings', [
'target_bundles' => NULL,
])
->save();
// @todo Do this unconditionally when JSON API requires Drupal 8.5 or newer.
if (floatval(\Drupal::VERSION) >= 8.5) {
// Add multi-value field.
FieldStorageConfig::create([
'entity_type' => static::$entityTypeId,
'field_name' => 'field_rest_test_multivalue',
'type' => 'string',
])
->setCardinality(3)
->save();
FieldConfig::create([
'entity_type' => static::$entityTypeId,
'field_name' => 'field_rest_test_multivalue',
'bundle' => $entity_bundle,
])
->setLabel('Test field: multi-value')
->setTranslatable(FALSE)
->save();
}
\Drupal::service('jsonapi.resource_type.repository')
->clearCachedDefinitions();
\Drupal::service('router.builder')
->rebuild();
// Reload entity so that it has the new field.
$reloaded_entity = $this->entityStorage
->loadUnchanged($entity
->id());
// Some entity types are not stored, hence they cannot be reloaded.
if ($reloaded_entity !== NULL) {
$entity = $reloaded_entity;
// Set a default value on the fields.
$entity
->set('field_rest_test', [
'value' => 'All the faith he had had had had no effect on the outcome of his life.',
]);
$entity
->set('field_jsonapi_test_entity_ref', [
'user' => $account
->id(),
]);
// @todo Do this unconditionally when JSON API requires Drupal 8.5 or newer.
if (floatval(\Drupal::VERSION) >= 8.5) {
$entity
->set('field_rest_test_multivalue', [
[
'value' => 'One',
],
[
'value' => 'Two',
],
]);
}
$entity
->save();
}
return $entity;
}