3 namespace Drupal\Tests\ctools_block\Functional;
5 use Drupal\Tests\BrowserTestBase;
8 * Tests the entity field block.
12 class EntityFieldBlockTest extends BrowserTestBase {
17 public static $modules = ['block', 'ctools_block', 'ctools_block_field_test'];
20 * Tests using the node body field in a block.
22 public function testBodyField() {
23 $block = $this->drupalPlaceBlock('entity_field:node:body', [
25 'type' => 'text_default',
27 'context_mapping' => [
28 'entity' => '@node.node_route_context:node',
31 $node = $this->drupalCreateNode(['type' => 'ctools_block_field_test']);
32 $this->drupalGet('node/' . $node->id());
33 $assert = $this->assertSession();
34 $assert->pageTextContains($block->label());
35 $assert->pageTextContains($node->body->value);
37 $node->set('body', NULL)->save();
38 $this->getSession()->reload();
39 // The block should not appear if there is no value in the field.
40 $assert->pageTextNotContains($block->label());
44 * Tests that empty image fields will still render their default value.
46 public function testEmptyImageField() {
47 $source = \Drupal::moduleHandler()->getModule('image')->getPath() . '/sample.png';
48 file_unmanaged_copy($source, 'public://sample.png');
50 /** @var \Drupal\file\FileInterface $file */
51 $file = \Drupal::entityTypeManager()
54 'uri' => 'public://sample.png',
58 /** @var \Drupal\field\FieldConfigInterface $field */
59 $field = \Drupal::entityTypeManager()
60 ->getStorage('field_config')
61 ->load('node.ctools_block_field_test.field_image');
62 $settings = $field->getSettings();
63 $settings['default_image']['uuid'] = $file->uuid();
64 $field->set('settings', $settings)->save();
66 $this->drupalPlaceBlock('entity_field:node:field_image', [
68 'type' => 'image_image',
70 'context_mapping' => [
71 'entity' => '@node.node_route_context:node',
75 $node = $this->drupalCreateNode(['type' => 'ctools_block_field_test']);
76 $this->drupalGet('node/' . $node->id());
78 $url = $file->getFileUri();
79 $url = file_create_url($url);
80 $url = file_url_transform_relative($url);
81 $this->assertSession()->responseContains('src="' . $url . '"');
85 * Tests using the node uid base field in a block.
87 public function testNodeBaseFields() {
88 $block = $this->drupalPlaceBlock('entity_field:node:title', [
92 'context_mapping' => [
93 'entity' => '@node.node_route_context:node',
96 $node = $this->drupalCreateNode(['type' => 'ctools_block_field_test', 'uid' => 1]);
97 $this->drupalGet('node/' . $node->id());
98 $assert = $this->assertSession();
99 $assert->pageTextContains($block->label());
100 $assert->pageTextContains($node->getTitle());
104 * Tests that we are setting the render cache metadata correctly.
106 public function testRenderCache() {
107 $this->drupalPlaceBlock('entity_field:node:body', [
109 'type' => 'text_default',
111 'context_mapping' => [
112 'entity' => '@node.node_route_context:node',
115 $a = $this->drupalCreateNode(['type' => 'ctools_block_field_test']);
116 $b = $this->drupalCreateNode(['type' => 'ctools_block_field_test']);
118 $assert = $this->assertSession();
119 $this->drupalGet('node/' . $a->id());
120 $assert->pageTextContains($a->body->value);
121 $this->drupalGet('node/' . $b->id());
122 $assert->pageTextNotContains($a->body->value);
123 $assert->pageTextContains($b->body->value);
125 $text = 'This is my text. Are you not entertained?';
126 $a->body->value = $text;
128 $this->drupalGet('node/' . $a->id());
129 $assert->pageTextContains($text);