public function ReadonlyFieldWidgetTest::testFieldAccess in Read-only Field Widget 8
Test field access on readonly fields.
File
- tests/
src/ Functional/ ReadonlyFieldWidgetTest.php, line 149
Class
- ReadonlyFieldWidgetTest
- Tests Readonly Field Widget basic behaviour.
Namespace
Drupal\Tests\readonly_field_widget\FunctionalCode
public function testFieldAccess() {
$assert = $this
->assertSession();
$test_string = $this
->randomMachineName();
$restricted_test_string = $this
->randomMachineName();
$article = $this
->createNode([
'type' => 'article',
'title' => 'test-article',
]);
$tag_term = Term::create([
'vid' => 'tags',
'name' => 'test-tag',
]);
$tag_term
->save();
$page = $this
->createNode([
'type' => 'page',
'field_some_plain_text' => [
[
'value' => $test_string,
],
],
'field_restricted_text' => [
[
'value' => $restricted_test_string,
],
],
'field_article_reference' => $article,
'field_term_reference' => $tag_term,
]);
// As an admin, verify the widgets are readonly.
$this
->drupalLogin($this->admin);
$this
->drupalGet('node/' . $page
->id() . '/edit');
// Test the title field shows with a label.
$field_wrapper = $assert
->elementExists('css', '#edit-title-wrapper');
$assert
->elementNotExists('css', 'input', $field_wrapper);
$assert
->elementNotExists('css', 'a', $field_wrapper);
$this
->assertFieldWrapperContainsString('Title', $field_wrapper);
$this
->assertFieldWrapperContainsString($page
->label(), $field_wrapper);
$field_wrapper = $assert
->elementExists('css', '#edit-field-some-plain-text-wrapper');
$this
->assertFieldWrapperContainsString($test_string, $field_wrapper);
$assert
->elementNotExists('css', 'input', $field_wrapper);
// This shouldn't be editable by admin, but they can view it.
$field_wrapper = $assert
->elementExists('css', '#edit-field-restricted-text-wrapper');
$this
->assertFieldWrapperContainsString($restricted_test_string, $field_wrapper);
$assert
->elementNotExists('css', 'input', $field_wrapper);
$field_wrapper = $assert
->elementExists('css', '#edit-field-article-reference-wrapper');
$this
->assertFieldWrapperContainsString('test-article', $field_wrapper);
$title_element = $assert
->elementExists('css', 'h2 a span', $field_wrapper);
$this
->assertEquals($title_element
->getText(), 'test-article');
$assert
->elementNotExists('css', 'input', $field_wrapper);
$assert
->elementNotExists('css', 'select', $field_wrapper);
$field_wrapper = $assert
->elementExists('css', '#edit-field-term-reference-wrapper');
$this
->assertFieldWrapperContainsString('test-tag', $field_wrapper);
$title_element = $assert
->elementExists('css', '.field__item a', $field_wrapper);
$this
->assertEquals($title_element
->getText(), 'test-tag');
$assert
->elementNotExists('css', 'input', $field_wrapper);
$assert
->elementNotExists('css', 'select', $field_wrapper);
// Create a regular who can update page nodes.
$user = $this
->createUser([
'edit any page content',
]);
$this
->drupalLogin($user);
$this
->drupalGet('node/' . $page
->id() . '/edit');
$field_wrapper = $assert
->elementExists('css', '#edit-field-some-plain-text-wrapper');
$this
->assertFieldWrapperContainsString($test_string, $field_wrapper);
$assert
->elementNotExists('css', 'input', $field_wrapper);
// This field is restricted via hooks in readonly_field_widget_test.module.
$assert
->elementNotExists('css', '#edit-field-restricted-text-wrapper');
$this
->assertSession()
->responseNotContains($restricted_test_string);
$field_wrapper = $assert
->elementExists('css', '#edit-field-article-reference-wrapper');
$this
->assertFieldWrapperContainsString('test-article', $field_wrapper);
$title_element = $assert
->elementExists('css', 'h2 a span', $field_wrapper);
$this
->assertEquals($title_element
->getText(), 'test-article');
$assert
->elementNotExists('css', 'input', $field_wrapper);
$assert
->elementNotExists('css', 'select', $field_wrapper);
$field_wrapper = $assert
->elementExists('css', '#edit-field-term-reference-wrapper');
$this
->assertFieldWrapperContainsString('test-tag', $field_wrapper);
$title_element = $assert
->elementExists('css', '.field__item a', $field_wrapper);
$this
->assertEquals($title_element
->getText(), 'test-tag');
$assert
->elementNotExists('css', 'input', $field_wrapper);
$assert
->elementNotExists('css', 'select', $field_wrapper);
}