FieldAccessTest.php in Drupal 10
File
core/modules/field/tests/src/Functional/FieldAccessTest.php
View source
<?php
namespace Drupal\Tests\field\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
class FieldAccessTest extends FieldTestBase {
protected static $modules = [
'node',
'field_test',
];
protected $defaultTheme = 'stark';
protected $node;
protected $testViewFieldValue;
protected function setUp() : void {
parent::setUp();
$web_user = $this
->drupalCreateUser([
'view test_view_field content',
]);
$this
->drupalLogin($web_user);
$content_type_info = $this
->drupalCreateContentType();
$content_type = $content_type_info
->id();
$field_storage = [
'field_name' => 'test_view_field',
'entity_type' => 'node',
'type' => 'text',
];
FieldStorageConfig::create($field_storage)
->save();
$field = [
'field_name' => $field_storage['field_name'],
'entity_type' => 'node',
'bundle' => $content_type,
];
FieldConfig::create($field)
->save();
foreach ([
'default',
'teaser',
] as $view_mode) {
\Drupal::service('entity_display.repository')
->getViewDisplay('node', $content_type, $view_mode)
->setComponent($field_storage['field_name'])
->save();
}
$this->testViewFieldValue = 'This is some text';
$settings = [];
$settings['type'] = $content_type;
$settings['title'] = 'Field view access test';
$settings['test_view_field'] = [
[
'value' => $this->testViewFieldValue,
],
];
$this->node = $this
->drupalCreateNode($settings);
}
public function testFieldAccess() {
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertSession()
->pageTextContains($this->testViewFieldValue);
$this
->drupalLogout();
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertSession()
->pageTextNotContains($this->testViewFieldValue);
}
}