function DisplayTest::testViewDisplay

Same name and namespace in other branches
  1. 4.0.x modules/ctools_entity_mask/tests/src/Functional/DisplayTest.php \Drupal\Tests\ctools_entity_mask\Functional\DisplayTest::testViewDisplay()

Tests that the view display for a masked entity replicates its source.

File

modules/ctools_entity_mask/tests/src/Functional/DisplayTest.php, line 65

Class

DisplayTest
Class DisplayTest.

Namespace

Drupal\Tests\ctools_entity_mask\Functional

Code

public function testViewDisplay() {
    // Generate a random image for the image field, since that can potentially
    // be tricky.
    $image_uri = uniqid('public://') . '.png';
    $image_uri = $this->getRandomGenerator()
        ->image($image_uri, '100x100', '200x200');
    $image = File::create([
        'uri' => $image_uri,
    ]);
    $image->save();
    $body = 'Qui animated corpse, cricket bat max brucks terribilem incessu zomby.';
    $link = 'https://www.drupal.org/project/ctools';
    $block = BlockContent::create([
        'type' => 'basic',
        'body' => $body,
        'field_link' => $link,
        'field_image' => $image,
    ]);
    $block->save();
    // Ensure that the entity is intact after serialization and deserialization,
    // since that may prove to be a common storage mechanism for mask entities.
    $block = serialize($block);
    $block = unserialize($block);
    $this->assertSame($body, $block->body->value);
    $this->assertSame($link, $block->field_link->uri);
    $this->assertSame($image_uri, $block->field_image->entity
        ->getFileUri());
    $build = $this->container
        ->get('entity_type.manager')
        ->getViewBuilder('fake_block_content')
        ->view($block);
    // If the fields are not in the renderable array, something has gone awry.
    $this->assertArrayHasKey('body', $build);
    $this->assertArrayHasKey('field_link', $build);
    $this->assertArrayHasKey('field_image', $build);
    // Render the block and check the output too, just to be sure.
    $rendered = $this->container
        ->get('renderer')
        ->renderRoot($build);
    $rendered = (string) $rendered;
    // @todo Use assertStringContainsString() when we rely exclusively on
    // PHPUnit 8.
    $this->assertNotFalse(strpos($rendered, $block->body->value));
    $this->assertNotFalse(strpos($rendered, $block->field_link->uri));
    $image_url = $block->field_image->entity
        ->getFileUri();
    $image_url = $this->container
        ->get('file_url_generator')
        ->generateAbsoluteString($image_url);
    $image_url = $this->container
        ->get('file_url_generator')
        ->transformRelative($image_url);
    // @todo Use assertStringContainsString() when we rely exclusively on
    // PHPUnit 8.
    $this->assertNotFalse(strpos($rendered, $image_url));
}