function EntityFieldBlockTest::testEmptyImageField

Same name in other branches
  1. 8.x-3.x modules/ctools_block/tests/src/Functional/EntityFieldBlockTest.php \Drupal\Tests\ctools_block\Functional\EntityFieldBlockTest::testEmptyImageField()

Tests that empty image fields will still render their default value.

File

modules/ctools_block/tests/src/Functional/EntityFieldBlockTest.php, line 51

Class

EntityFieldBlockTest
Tests the entity field block.

Namespace

Drupal\Tests\ctools_block\Functional

Code

public function testEmptyImageField() {
    $entityTypeManager = $this->container
        ->get('entity_type.manager');
    $source = $this->container
        ->get('module_handler')
        ->getModule('image')
        ->getPath() . '/sample.png';
    
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = $this->container
        ->get('file_system');
    $file_system->copy($source, 'public://sample.png');
    
    /** @var \Drupal\file\FileInterface $file */
    $file = $entityTypeManager->getStorage('file')
        ->create([
        'uri' => 'public://sample.png',
    ]);
    $file->save();
    
    /** @var \Drupal\field\FieldConfigInterface $field */
    $field = $entityTypeManager->getStorage('field_config')
        ->load('node.ctools_block_field_test.field_image');
    $settings = $field->getSettings();
    $settings['default_image']['uuid'] = $file->uuid();
    $field->set('settings', $settings)
        ->save();
    $this->drupalPlaceBlock('entity_field:node:field_image', [
        'formatter' => [
            'type' => 'image_image',
        ],
        'context_mapping' => [
            'entity' => '@node.node_route_context:node',
        ],
    ]);
    $node = $this->drupalCreateNode([
        'type' => 'ctools_block_field_test',
    ]);
    $this->drupalGet('node/' . $node->id());
    $url = $file->getFileUri();
    $url = $this->container
        ->get('file_url_generator')
        ->generateAbsoluteString($url);
    $url = $this->container
        ->get('file_url_generator')
        ->transformRelative($url);
    $this->assertSession()
        ->responseContains('src="' . $url . '"');
}