function NodeStorageLoadFieldDataTest::testLoadFieldData

Same name and namespace in other branches
  1. main core/modules/node/tests/src/Kernel/NodeStorageLoadFieldDataTest.php \Drupal\Tests\node\Kernel\NodeStorageLoadFieldDataTest::testLoadFieldData()

Tests fields load correctly.

File

core/modules/node/tests/src/Kernel/NodeStorageLoadFieldDataTest.php, line 60

Class

NodeStorageLoadFieldDataTest
Tests node storage loads fields of mixed cardinality and translatability.

Namespace

Drupal\Tests\node\Kernel

Code

public function testLoadFieldData() : void {
  $fields = [
    'field_single_translatable',
    'field_single_untranslatable',
    'field_mul_translatable',
    'field_mul_untranslatable',
  ];
  foreach ($fields as $field) {
    $fieldStorage = FieldStorageConfig::create([
      'field_name' => $field,
      'entity_type' => 'node',
      'type' => 'string',
      'cardinality' => str_contains($field, 'single') ? 1 : FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
      'persist_with_no_fields' => TRUE,
    ]);
    $fieldStorage->save();
    FieldConfig::create([
      'field_storage' => $fieldStorage,
      'bundle' => 'page',
      'translatable' => !str_contains($field, 'untranslatable'),
    ])->save();
  }
  $values = [
    'field_single_translatable' => [
      'Single translatable',
    ],
    'field_single_untranslatable' => [
      'Single untranslatable',
    ],
    'field_mul_translatable' => [
      'Multiple translatable 1',
      'Multiple translatable 2',
    ],
    'field_mul_untranslatable' => [
      'Multiple untranslatable 1',
      'Multiple untranslatable 2',
    ],
  ];
  $id = $this->createNode($values + [
    'langcode' => 'en',
  ])
    ->id();
  $storage = \Drupal::entityTypeManager()->getStorage('node');
  $storage->resetCache();
  $node = $storage->load($id);
  $storedValues = $node->toArray();
  foreach ($values as $fieldName => $fieldValue) {
    $this->assertSame($fieldValue, array_column($storedValues[$fieldName], 'value'));
  }
  // Uninstalling the language module will delete the 'en' configurable
  // language. This will cause the langcode for the node in the node_revision
  // table to be changed to 'und'.
  // @see NodeStorage::clearRevisionsLanguage().
  \Drupal::service(ModuleInstallerInterface::class)->uninstall([
    'language',
  ]);
  $storage->resetCache();
  $node = $storage->load($id);
  $storedValues = $node->toArray();
  foreach ($values as $fieldName => $fieldValue) {
    $this->assertSame($fieldValue, array_column($storedValues[$fieldName], 'value'));
  }
}

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