function MultiCardinalitySpineTest::testSpineLoad

Same name and namespace in other branches
  1. main core/tests/Drupal/KernelTests/Core/Entity/MultiCardinalitySpineTest.php \Drupal\KernelTests\Core\Entity\MultiCardinalitySpineTest::testSpineLoad()

Loads several multiple cardinality fields together.

File

core/tests/Drupal/KernelTests/Core/Entity/MultiCardinalitySpineTest.php, line 53

Class

MultiCardinalitySpineTest
Checks loading several multilingual multiple cardinality fields at once.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testSpineLoad() : void {
  $entity = EntityTestMulRev::create([
    'langcode' => 'en',
    // Different counts per field would previously multiply into a cartesian
    // product of rows.
'field_a' => [
      10,
      11,
      12,
      13,
    ],
    'field_b' => [
      20,
    ],
    'field_c' => [
      30,
      31,
      32,
    ],
  ]);
  $entity->addTranslation('de', [
    'field_a' => [
      110,
      111,
    ],
    'field_b' => [
      120,
      121,
      122,
    ],
  ]);
  $entity->save();
  $id = $entity->id();
  // Introduce a sparse delta: delete the middle value of field_a directly in
  // the dedicated table, leaving deltas 0, 2, 3. The highest cardinality
  // field would not contain delta 1 as a spine.
  \Drupal::database()->delete('entity_test_mulrev__field_a')
    ->condition('entity_id', $id)
    ->condition('langcode', 'en')
    ->condition('delta', 1)
    ->execute();
  $this->container
    ->get('entity_type.manager')
    ->getStorage('entity_test_mulrev')
    ->resetCache();
  $loaded = EntityTestMulRev::load($id);
  $this->assertSame([
    '10',
    '12',
    '13',
  ], array_column($loaded->get('field_a')
    ->getValue(), 'value'));
  $this->assertSame([
    '20',
  ], array_column($loaded->get('field_b')
    ->getValue(), 'value'));
  $this->assertSame([
    '30',
    '31',
    '32',
  ], array_column($loaded->get('field_c')
    ->getValue(), 'value'));
  $de = $loaded->getTranslation('de');
  $this->assertSame([
    '110',
    '111',
  ], array_column($de->get('field_a')
    ->getValue(), 'value'));
  $this->assertSame([
    '120',
    '121',
    '122',
  ], array_column($de->get('field_b')
    ->getValue(), 'value'));
  // Non-translatable field shows the default translation values.
  $this->assertSame([
    '30',
    '31',
    '32',
  ], array_column($de->get('field_c')
    ->getValue(), 'value'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.