View source
<?php
namespace Drupal\Tests\ctools_entity_mask\Functional;
use Drupal\entity_mask_test\Entity\BlockContent;
use Drupal\file\Entity\File;
use Drupal\Tests\BrowserTestBase;
class DisplayTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected static $modules = [
'block',
'block_content',
'ctools_entity_mask',
'entity_mask_test',
'field',
'field_ui',
'file',
'image',
'link',
'system',
'text',
'user',
];
protected function setUp() {
parent::setUp();
$account = $this
->drupalCreateUser([
'administer blocks',
]);
$this
->drupalLogin($account);
}
public function testFormDisplay() {
$assert = $this
->assertSession();
$this
->drupalGet('/fake-block/add/basic');
$assert
->statusCodeEquals(200);
$assert
->fieldExists('Body');
$assert
->fieldExists('Link');
$assert
->fieldExists('Image');
}
public function testViewDisplay() {
$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();
$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);
$this
->assertArrayHasKey('body', $build);
$this
->assertArrayHasKey('field_link', $build);
$this
->assertArrayHasKey('field_image', $build);
$rendered = $this->container
->get('renderer')
->renderRoot($build);
$rendered = (string) $rendered;
$this
->assertNotFalse(strpos($rendered, $block->body->value));
$this
->assertNotFalse(strpos($rendered, $block->field_link->uri));
$image_url = $block->field_image->entity
->getFileUri();
$image_url = file_create_url($image_url);
$image_url = file_url_transform_relative($image_url);
$this
->assertNotFalse(strpos($rendered, $image_url));
}
}