Chris@0: drupalCreateUser(['view test_view_field content']); Chris@0: $this->drupalLogin($web_user); Chris@0: Chris@0: // Create content type. Chris@0: $content_type_info = $this->drupalCreateContentType(); Chris@0: $content_type = $content_type_info->id(); Chris@0: Chris@0: $field_storage = [ Chris@0: 'field_name' => 'test_view_field', Chris@0: 'entity_type' => 'node', Chris@0: 'type' => 'text', Chris@0: ]; Chris@0: FieldStorageConfig::create($field_storage)->save(); Chris@0: $field = [ Chris@0: 'field_name' => $field_storage['field_name'], Chris@0: 'entity_type' => 'node', Chris@0: 'bundle' => $content_type, Chris@0: ]; Chris@0: FieldConfig::create($field)->save(); Chris@0: Chris@0: // Assign display properties for the 'default' and 'teaser' view modes. Chris@0: foreach (['default', 'teaser'] as $view_mode) { Chris@0: entity_get_display('node', $content_type, $view_mode) Chris@0: ->setComponent($field_storage['field_name']) Chris@0: ->save(); Chris@0: } Chris@0: Chris@0: // Create test node. Chris@0: $this->testViewFieldValue = 'This is some text'; Chris@0: $settings = []; Chris@0: $settings['type'] = $content_type; Chris@0: $settings['title'] = 'Field view access test'; Chris@0: $settings['test_view_field'] = [['value' => $this->testViewFieldValue]]; Chris@0: $this->node = $this->drupalCreateNode($settings); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test that hook_entity_field_access() is called. Chris@0: */ Chris@0: public function testFieldAccess() { Chris@0: Chris@0: // Assert the text is visible. Chris@0: $this->drupalGet('node/' . $this->node->id()); Chris@0: $this->assertText($this->testViewFieldValue); Chris@0: Chris@0: // Assert the text is not visible for anonymous users. Chris@0: // The field_test module implements hook_entity_field_access() which will Chris@0: // specifically target the 'test_view_field' field. Chris@0: $this->drupalLogout(); Chris@0: $this->drupalGet('node/' . $this->node->id()); Chris@0: $this->assertNoText($this->testViewFieldValue); Chris@0: } Chris@0: Chris@0: }