You are here

protected function EntityReferenceFieldNormalizerTest::setUp in JSON:API 8.2

Overrides KernelTestBase::setUp

File

tests/src/Kernel/Normalizer/EntityReferenceFieldNormalizerTest.php, line 71

Class

EntityReferenceFieldNormalizerTest
@coversDefaultClass \Drupal\jsonapi\Normalizer\EntityReferenceFieldNormalizer @group jsonapi

Namespace

Drupal\Tests\jsonapi\Kernel\Normalizer

Code

protected function setUp() {
  parent::setUp();

  // Set up the data model.
  // Add the entity schemas.
  $this
    ->installEntitySchema('node');
  $this
    ->installEntitySchema('user');
  $this
    ->installEntitySchema('file');

  // Add the additional table schemas.
  $this
    ->installSchema('system', [
    'sequences',
  ]);
  $this
    ->installSchema('node', [
    'node_access',
  ]);
  $this
    ->installSchema('file', [
    'file_usage',
  ]);
  NodeType::create([
    'type' => 'referencer',
  ])
    ->save();
  $this
    ->createEntityReferenceField('node', 'referencer', 'field_user', 'User', 'user', 'default', [
    'target_bundles' => NULL,
  ], 1);
  $this
    ->createEntityReferenceField('node', 'referencer', 'field_users', 'Users', 'user', 'default', [
    'target_bundles' => NULL,
  ], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
  $field_storage_config = [
    'type' => 'image',
    'entity_type' => 'node',
  ];
  FieldStorageConfig::create([
    'field_name' => 'field_image',
    'cardinality' => 1,
  ] + $field_storage_config)
    ->save();
  FieldStorageConfig::create([
    'field_name' => 'field_images',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ] + $field_storage_config)
    ->save();
  $field_config = [
    'entity_type' => 'node',
    'bundle' => 'referencer',
  ];
  FieldConfig::create([
    'field_name' => 'field_image',
    'label' => 'Image',
  ] + $field_config)
    ->save();
  FieldConfig::create([
    'field_name' => 'field_images',
    'label' => 'Images',
  ] + $field_config)
    ->save();

  // Set up the test data.
  $this->account = $this
    ->prophesize(AccountInterface::class)
    ->reveal();
  $this->user1 = User::create([
    'name' => $this
      ->randomMachineName(),
    'mail' => $this
      ->randomMachineName() . '@example.com',
    'uuid' => static::$userIds[0],
  ]);
  $this->user1
    ->save();
  $this->user2 = User::create([
    'name' => $this
      ->randomMachineName(),
    'mail' => $this
      ->randomMachineName() . '@example.com',
    'uuid' => static::$userIds[1],
  ]);
  $this->user2
    ->save();
  $this->image1 = File::create([
    'uri' => 'public:/image1.png',
    'uuid' => static::$imageIds[0],
  ]);
  $this->image1
    ->save();
  $this->image2 = File::create([
    'uri' => 'public:/image2.png',
    'uuid' => static::$imageIds[1],
  ]);
  $this->image2
    ->save();

  // Create the node from which all the previously created entities will be
  // referenced.
  $this->referencer = Node::create([
    'title' => 'Referencing node',
    'type' => 'referencer',
    'status' => 1,
    'uuid' => static::$referencerId,
  ]);
  $this->referencer
    ->save();

  // Set up the test dependencies.
  $this->referencingResourceType = $this->container
    ->get('jsonapi.resource_type.repository')
    ->get('node', 'referencer');
  $this->normalizer = new EntityReferenceFieldNormalizer();
  $this->normalizer
    ->setSerializer($this->container
    ->get('jsonapi.serializer'));
}