You are here

public function FieldFieldTest::testGetValueMethod in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Kernel/Handler/FieldFieldTest.php \Drupal\Tests\views\Kernel\Handler\FieldFieldTest::testGetValueMethod()

Tests \Drupal\views\Plugin\views\field\EntityField::getValue

File

core/modules/views/tests/src/Kernel/Handler/FieldFieldTest.php, line 562

Class

FieldFieldTest
Provides some integration tests for the Field handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testGetValueMethod() {
  $bundle = 'test_bundle';
  entity_test_create_bundle($bundle);
  $field_multiple = FieldConfig::create([
    'field_name' => 'field_test_multiple',
    'entity_type' => 'entity_test',
    'bundle' => 'test_bundle',
  ]);
  $field_multiple
    ->save();
  foreach ($this->entities as $entity) {
    $entity
      ->delete();
  }
  $this->entities = [];
  $this->entities[] = $entity = EntityTest::create([
    'type' => 'entity_test',
    'name' => 'test name',
    'user_id' => $this->testUsers[0]
      ->id(),
  ]);
  $entity
    ->save();
  $this->entities[] = $entity = EntityTest::create([
    'type' => 'entity_test',
    'name' => 'test name 2',
    'user_id' => $this->testUsers[0]
      ->id(),
  ]);
  $entity
    ->save();
  $this->entities[] = $entity = EntityTest::create([
    'type' => $bundle,
    'name' => 'test name 3',
    'user_id' => $this->testUsers[0]
      ->id(),
    'field_test_multiple' => [
      1,
      2,
      3,
    ],
  ]);
  $entity
    ->save();
  $executable = Views::getView('test_field_field_test');
  $executable
    ->execute();
  $field_normal = $executable->field['field_test'];
  $field_entity_reference = $executable->field['user_id'];
  $field_multi_cardinality = $executable->field['field_test_multiple'];
  $this
    ->assertEquals($this->entities[0]->field_test->value, $field_normal
    ->getValue($executable->result[0]));
  $this
    ->assertEquals($this->entities[0]->user_id->target_id, $field_entity_reference
    ->getValue($executable->result[0]));
  $this
    ->assertEquals($this->entities[1]->field_test->value, $field_normal
    ->getValue($executable->result[1]));
  $this
    ->assertEquals($this->entities[1]->user_id->target_id, $field_entity_reference
    ->getValue($executable->result[1]));
  $this
    ->assertEquals([], $field_multi_cardinality
    ->getValue($executable->result[0]));
  $this
    ->assertEquals([], $field_multi_cardinality
    ->getValue($executable->result[1]));
  $this
    ->assertEquals([
    1,
    2,
    3,
  ], $field_multi_cardinality
    ->getValue($executable->result[2]));
}