FivestarTestTrait.php in Fivestar 8
File
tests/src/Traits/FivestarTestTrait.php
View source
<?php
namespace Drupal\Tests\fivestar\Traits;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
trait FivestarTestTrait {
protected function createFivestarField(array $options = []) {
$options = $options + [
'content_type' => 'test_node_type',
'widget_type' => 'stars',
'display' => [],
];
$field_storage = FieldStorageConfig::create([
'field_name' => 'fivestar_test',
'entity_type' => 'node',
'type' => 'fivestar',
'cardinality' => 1,
'settings' => [
'axis' => 'vote',
],
]);
$field_storage
->save();
$field = FieldConfig::create([
'entity_type' => 'node',
'field_name' => $field_storage
->getName(),
'label' => 'Fivestar test field',
'bundle' => $options['content_type'],
'widget' => [
'type' => $options['widget_type'],
'settings' => [
'widget' => [
'fivestar_widget' => 'default',
],
],
],
'settings' => [
'axis' => 'vote',
'stars' => '5',
],
'display' => $options['display'],
]);
$field
->save();
}
}