Commit f26b5328 authored by webchick's avatar webchick

Issue #2385443 by larowlan, dawehner: Test that base entity fields on views...

Issue #2385443 by larowlan, dawehner: Test that base entity fields on views respect field level access
parent 261e709a
<?php
/**
* @file
* Contains \Drupal\views\Tests\Entity\BaseFieldAccessTest.
*/
namespace Drupal\views\Tests\Entity;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\views\Tests\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
/**
* Tests views base field access.
*
* @group views
*/
class BaseFieldAccessTest extends ViewTestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = array('test_entity_test_protected_access');
/**
* Modules to enable
*
* @var array
*/
public static $modules = [
'views', 'views_test_config', 'entity_test', 'node', 'views_entity_test',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
/** @var \Drupal\Core\Entity\EntityDefinitionUpdateManager $update_manager */
$update_manager = $this->container->get('entity.definition_update_manager');
\Drupal::entityManager()->clearCachedDefinitions();
$update_manager->applyUpdates();
ViewTestData::createTestViews(get_class($this), array('comment_test_views'));
\Drupal::state()->set('entity_test.views_data', [
'entity_test' => [
'test_text_access' => [
'field' => [
'id' => 'standard',
],
],
],
]);
$entity_1 = EntityTest::create([
'test_text_access' => 'no access value',
]);
$entity_1->save();
$entity_2 = EntityTest::create([
'test_text_access' => 'ok to see this one',
]);
$entity_2->save();
$this->drupalLogin($this->drupalCreateUser(['access content']));
}
/**
* Test access to protected base fields.
*/
public function testProtectedField() {
$this->drupalGet('test-entity-protected-access');
$this->assertText('ok to see this one');
$this->assertNoText('no access value');
}
}
name: 'Views Entity Test'
type: module
description: 'Provides base fields for views tests of entity_test entity type.'
package: Testing
version: VERSION
core: 8.x
dependencies:
- views
- entity_test
<?php
/**
* @file
* Contains main module functionality.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Session\AccountInterface;
/**
* Implements hook_entity_bundle_field_info().
*/
function views_entity_test_entity_base_field_info(EntityTypeInterface $entity_type) {
if ($entity_type->id() == 'entity_test') {
$definitions['test_text_access'] = BaseFieldDefinition::create('string')
->setLabel(t('Test access'))
->setTranslatable(FALSE)
->setSetting('max_length', 64)
->setDisplayOptions('form', array(
'type' => 'string_textfield',
'weight' => 10,
));
return $definitions;
}
}
/**
* Implements hook_entity_field_access().
*
* @see \Drupal\system\Tests\Entity\FieldAccessTest::testFieldAccess()
*/
function views_entity_test_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
if ($field_definition->getName() == 'test_text_access') {
if ($items) {
if ($items->value == 'no access value') {
return AccessResult::forbidden()->cacheUntilEntityChanges($items->getEntity());
}
}
}
// No opinion.
return AccessResult::neutral();
}
langcode: en
status: true
dependencies:
module:
- entity_test
- text
- user
id: test_entity_test_protected_access
label: test_entity_test_protected_access
module: views
description: ''
tag: ''
base_table: entity_test
base_field: id
core: 8.x
display:
default:
display_plugin: default
id: default
display_title: Master
position: 0
display_options:
access:
type: perm
options:
perm: 'access content'
cache:
type: none
options: { }
query:
type: views_query
options:
disable_sql_rewrite: false
distinct: false
replica: false
query_comment: ''
query_tags: { }
exposed_form:
type: basic
options:
submit_button: Apply
reset_button: false
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
pager:
type: full
options:
items_per_page: 10
offset: 0
id: 0
total_pages: null
expose:
items_per_page: false
items_per_page_label: 'Items per page'
items_per_page_options: '5, 10, 25, 50'
items_per_page_options_all: false
items_per_page_options_all_label: '- All -'
offset: false
offset_label: Offset
tags:
previous: ' previous'
next: 'next ›'
first: '« first'
last: 'last »'
quantity: 9
style:
type: default
row:
type: fields
options:
default_field_elements: true
inline: { }
separator: ''
hide_empty: false
fields:
test_text_access:
id: test_text_access
table: entity_test
field: test_text_access
relationship: none
group_type: group
admin_label: ''
label: ''
exclude: false
entity_type: entity_test
entity_field: test_text_access
plugin_id: standard
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: false
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
filters: { }
sorts: { }
title: ''
header: { }
footer: { }
empty: { }
arguments: { }
display_extenders: { }
page_1:
display_plugin: page
id: page_1
display_title: Page
position: 1
display_options:
path: test-entity-protected-access
display_extenders: { }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment