Newer
Older
Angie Byron
committed
<?php
Alex Pott
committed
namespace Drupal\Tests\field\Kernel;
Angie Byron
committed
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
Alex Pott
committed
use Drupal\KernelTests\KernelTestBase;
Angie Byron
committed
/**
* Parent class for Field API unit tests.
*/
Alex Pott
committed
abstract class FieldKernelTestBase extends KernelTestBase {
Angie Byron
committed
/**
* Modules to enable.
*
* @var array
*/
Alex Pott
committed
public static $modules = ['user', 'system', 'field', 'text', 'entity_test', 'field_test'];
Angie Byron
committed
* Bag of created field storages and fields.
* Allows easy access to test field storage/field names/IDs/objects via:
* - $this->fieldTestData->field_name[suffix]
* - $this->fieldTestData->field_storage[suffix]
* - $this->fieldTestData->field_storage_uuid[suffix]
* - $this->fieldTestData->field[suffix]
* - $this->fieldTestData->field_definition[suffix]
* @see \Drupal\field\Tests\FieldUnitTestBase::createFieldWithStorage()
*
* @var \ArrayObject
*/
protected $fieldTestData;
Angie Byron
committed
/**
* Set the default field storage backend for fields created during tests.
*/
protected function setUp() {
Angie Byron
committed
parent::setUp();
$this->fieldTestData = new \ArrayObject([], \ArrayObject::ARRAY_AS_PROPS);
$this->installEntitySchema('entity_test');
$this->installEntitySchema('user');
Alex Pott
committed
$this->installSchema('system', ['sequences', 'key_value']);
Angie Byron
committed
// Set default storage backend and configure the theme system.
$this->installConfig(['field', 'system']);
Alex Pott
committed
// Create user 1.
$storage = \Drupal::entityManager()->getStorage('user');
$storage
->create([
Alex Pott
committed
'uid' => 1,
'name' => 'entity-test',
'mail' => 'entity@localhost',
'status' => TRUE,
])
Alex Pott
committed
->save();
Angie Byron
committed
}
/**
* Create a field and an associated field storage.
Angie Byron
committed
*
* @param string $suffix
* (optional) A string that should only contain characters that are valid in
* PHP variable names as well.
* @param string $entity_type
* (optional) The entity type on which the field should be created.
* Defaults to "entity_test".
* @param string $bundle
* (optional) The entity type on which the field should be created.
* Defaults to the default bundle of the entity type.
Angie Byron
committed
*/
protected function createFieldWithStorage($suffix = '', $entity_type = 'entity_test', $bundle = NULL) {
if (empty($bundle)) {
$bundle = $entity_type;
}
Angie Byron
committed
$field_name = 'field_name' . $suffix;
$field_storage = 'field_storage' . $suffix;
$field_storage_uuid = 'field_storage_uuid' . $suffix;
$field = 'field' . $suffix;
$field_definition = 'field_definition' . $suffix;
Angie Byron
committed
$this->fieldTestData->$field_name = mb_strtolower($this->randomMachineName() . '_field_name' . $suffix);
$this->fieldTestData->$field_storage = FieldStorageConfig::create([
Alex Pott
committed
'field_name' => $this->fieldTestData->$field_name,
'entity_type' => $entity_type,
'type' => 'test_field',
'cardinality' => 4,
]);
$this->fieldTestData->$field_storage->save();
$this->fieldTestData->$field_storage_uuid = $this->fieldTestData->$field_storage->uuid();
$this->fieldTestData->$field_definition = [
'field_storage' => $this->fieldTestData->$field_storage,
'bundle' => $bundle,
Alex Pott
committed
'label' => $this->randomMachineName() . '_label',
'description' => $this->randomMachineName() . '_description',
'settings' => [
'test_field_setting' => $this->randomMachineName(),
],
];
$this->fieldTestData->$field = FieldConfig::create($this->fieldTestData->$field_definition);
$this->fieldTestData->$field->save();
Alex Pott
committed
entity_get_form_display($entity_type, $bundle, 'default')
->setComponent($this->fieldTestData->$field_name, [
Angie Byron
committed
'type' => 'test_field_widget',
'settings' => [
Alex Pott
committed
'test_widget_setting' => $this->randomMachineName(),
]
])
Alex Pott
committed
->save();
Angie Byron
committed
}
/**
* Saves and reloads an entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to save.
*
* @return \Drupal\Core\Entity\EntityInterface
* The entity, freshly reloaded from storage.
*/
protected function entitySaveReload(EntityInterface $entity) {
$entity->save();
catch
committed
$controller = $this->container->get('entity.manager')->getStorage($entity->getEntityTypeId());
$controller->resetCache();
return $controller->load($entity->id());
}
/**
* Validate and save entity. Fail if violations are found.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to save.
*/
protected function entityValidateAndSave(EntityInterface $entity) {
$violations = $entity->validate();
if ($violations->count()) {
$this->fail($violations);
}
else {
$entity->save();
}
}
Angie Byron
committed
/**
* Generate random values for a field_test field.
*
* @param $cardinality
* Number of values to generate.
* @return
Alex Pott
committed
* An array of random values, in the format expected for field values.
Angie Byron
committed
*/
protected function _generateTestFieldValues($cardinality) {
$values = [];
Angie Byron
committed
for ($i = 0; $i < $cardinality; $i++) {
// field_test fields treat 0 as 'empty value'.
$values[$i]['value'] = mt_rand(1, 127);
}
return $values;
}
/**
* Assert that a field has the expected values in an entity.
*
* This function only checks a single column in the field values.
*
catch
committed
* @param \Drupal\Core\Entity\EntityInterface $entity
Angie Byron
committed
* The entity to test.
* @param $field_name
* The name of the field to test
* @param $expected_values
* The array of expected values.
Angie Byron
committed
* @param $langcode
* (Optional) The language code for the values. Defaults to
* \Drupal\Core\Language\LanguageInterface::LANGCODE_NOT_SPECIFIED.
Angie Byron
committed
* @param $column
Angie Byron
committed
* (Optional) The name of the column to check. Defaults to 'value'.
Angie Byron
committed
*/
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.
catch
committed
$storage = $this->container->get('entity_type.manager')
->getStorage($entity->getEntityTypeId());
$storage->resetCache([$entity->id()]);
$e = $storage->load($this->entityId);
$field = $values = $e->getTranslation($langcode)->$field_name;
// Filter out empty values so that they don't mess with the assertions.
$field->filterEmptyItems();
$values = $field->getValue();
Angie Byron
committed
$this->assertEqual(count($values), count($expected_values), 'Expected number of values were saved.');
foreach ($expected_values as $key => $value) {
$this->assertEqual($values[$key][$column], $value, format_string('Value @value was saved correctly.', ['@value' => $value]));