comparison core/modules/field/tests/src/Functional/FieldAccessTest.php @ 0:4c8ae668cc8c

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