4fa77c5ec24e9f6a075475f1748749603bed055c
[yaffs-website] / web / modules / contrib / ctools / modules / ctools_block / tests / src / Functional / EntityFieldBlockTest.php
1 <?php
2
3 namespace Drupal\Tests\ctools_block\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests the entity field block.
9  *
10  * @group ctools_block
11  */
12 class EntityFieldBlockTest extends BrowserTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public static $modules = ['block', 'ctools_block', 'ctools_block_field_test'];
18
19   /**
20    * Tests using the node body field in a block.
21    */
22   public function testBodyField() {
23     $block = $this->drupalPlaceBlock('entity_field:node:body', [
24       'formatter' => [
25         'type' => 'text_default',
26       ],
27       'context_mapping' => [
28         'entity' => '@node.node_route_context:node',
29       ],
30     ]);
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);
36
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());
41   }
42
43   /**
44    * Tests that empty image fields will still render their default value.
45    */
46   public function testEmptyImageField() {
47     $source = \Drupal::moduleHandler()->getModule('image')->getPath() . '/sample.png';
48     file_unmanaged_copy($source, 'public://sample.png');
49
50     /** @var \Drupal\file\FileInterface $file */
51     $file = \Drupal::entityTypeManager()
52       ->getStorage('file')
53       ->create([
54         'uri' => 'public://sample.png',
55       ]);
56     $file->save();
57
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();
65
66     $this->drupalPlaceBlock('entity_field:node:field_image', [
67       'formatter' => [
68         'type' => 'image_image',
69       ],
70       'context_mapping' => [
71         'entity' => '@node.node_route_context:node',
72       ],
73     ]);
74
75     $node = $this->drupalCreateNode(['type' => 'ctools_block_field_test']);
76     $this->drupalGet('node/' . $node->id());
77
78     $url = $file->getFileUri();
79     $url = file_create_url($url);
80     $url = file_url_transform_relative($url);
81     $this->assertSession()->responseContains('src="' . $url . '"');
82   }
83
84   /**
85    * Tests using the node uid base field in a block.
86    */
87   public function testNodeBaseFields() {
88     $block = $this->drupalPlaceBlock('entity_field:node:title', [
89       'formatter' => [
90         'type' => 'string',
91       ],
92       'context_mapping' => [
93         'entity' => '@node.node_route_context:node',
94       ],
95     ]);
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());
101   }
102
103   /**
104    * Tests that we are setting the render cache metadata correctly.
105    */
106   public function testRenderCache() {
107     $this->drupalPlaceBlock('entity_field:node:body', [
108       'formatter' => [
109         'type' => 'text_default',
110       ],
111       'context_mapping' => [
112         'entity' => '@node.node_route_context:node',
113       ],
114     ]);
115     $a = $this->drupalCreateNode(['type' => 'ctools_block_field_test']);
116     $b = $this->drupalCreateNode(['type' => 'ctools_block_field_test']);
117
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);
124
125     $text = 'This is my text. Are you not entertained?';
126     $a->body->value = $text;
127     $a->save();
128     $this->drupalGet('node/' . $a->id());
129     $assert->pageTextContains($text);
130   }
131
132 }