Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\field\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
6 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Tests Field access.
|
Chris@0
|
10 *
|
Chris@0
|
11 * @group field
|
Chris@0
|
12 */
|
Chris@0
|
13 class FieldAccessTest extends FieldTestBase {
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Modules to enable.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @var array
|
Chris@0
|
19 */
|
Chris@0
|
20 public static $modules = ['node', 'field_test'];
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Node entity to use in this test.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @var \Drupal\node\Entity\Node
|
Chris@0
|
26 */
|
Chris@0
|
27 protected $node;
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * Field value to test display on nodes.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @var string
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $testViewFieldValue;
|
Chris@0
|
35
|
Chris@0
|
36 protected function setUp() {
|
Chris@0
|
37 parent::setUp();
|
Chris@0
|
38
|
Chris@0
|
39 $web_user = $this->drupalCreateUser(['view test_view_field content']);
|
Chris@0
|
40 $this->drupalLogin($web_user);
|
Chris@0
|
41
|
Chris@0
|
42 // Create content type.
|
Chris@0
|
43 $content_type_info = $this->drupalCreateContentType();
|
Chris@0
|
44 $content_type = $content_type_info->id();
|
Chris@0
|
45
|
Chris@0
|
46 $field_storage = [
|
Chris@0
|
47 'field_name' => 'test_view_field',
|
Chris@0
|
48 'entity_type' => 'node',
|
Chris@0
|
49 'type' => 'text',
|
Chris@0
|
50 ];
|
Chris@0
|
51 FieldStorageConfig::create($field_storage)->save();
|
Chris@0
|
52 $field = [
|
Chris@0
|
53 'field_name' => $field_storage['field_name'],
|
Chris@0
|
54 'entity_type' => 'node',
|
Chris@0
|
55 'bundle' => $content_type,
|
Chris@0
|
56 ];
|
Chris@0
|
57 FieldConfig::create($field)->save();
|
Chris@0
|
58
|
Chris@0
|
59 // Assign display properties for the 'default' and 'teaser' view modes.
|
Chris@0
|
60 foreach (['default', 'teaser'] as $view_mode) {
|
Chris@0
|
61 entity_get_display('node', $content_type, $view_mode)
|
Chris@0
|
62 ->setComponent($field_storage['field_name'])
|
Chris@0
|
63 ->save();
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 // Create test node.
|
Chris@0
|
67 $this->testViewFieldValue = 'This is some text';
|
Chris@0
|
68 $settings = [];
|
Chris@0
|
69 $settings['type'] = $content_type;
|
Chris@0
|
70 $settings['title'] = 'Field view access test';
|
Chris@0
|
71 $settings['test_view_field'] = [['value' => $this->testViewFieldValue]];
|
Chris@0
|
72 $this->node = $this->drupalCreateNode($settings);
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 /**
|
Chris@0
|
76 * Test that hook_entity_field_access() is called.
|
Chris@0
|
77 */
|
Chris@0
|
78 public function testFieldAccess() {
|
Chris@0
|
79
|
Chris@0
|
80 // Assert the text is visible.
|
Chris@0
|
81 $this->drupalGet('node/' . $this->node->id());
|
Chris@0
|
82 $this->assertText($this->testViewFieldValue);
|
Chris@0
|
83
|
Chris@0
|
84 // Assert the text is not visible for anonymous users.
|
Chris@0
|
85 // The field_test module implements hook_entity_field_access() which will
|
Chris@0
|
86 // specifically target the 'test_view_field' field.
|
Chris@0
|
87 $this->drupalLogout();
|
Chris@0
|
88 $this->drupalGet('node/' . $this->node->id());
|
Chris@0
|
89 $this->assertNoText($this->testViewFieldValue);
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 }
|