View source
<?php
namespace Drupal\Tests\pbf\Functional;
class PbfAccessWithFormWidgetValueTest extends PbfBaseTest {
protected $fieldname;
public function setUp() {
parent::setUp();
$this->fieldname = 'field_pbf_group';
$this
->attachPbfNodeFields($this->fieldname);
$this->article1 = $this
->createSimpleArticle('Article 1', $this->fieldname, $this->group1
->id(), 1, 0, 0, 0);
$this->article2 = $this
->createSimpleArticle('Article 2', $this->fieldname, $this->group1
->id(), 0, 1, 0, 0);
}
public function testPbfAccessWithFormWidget() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertResponse(200, 'adminUser is allowed to view the content.');
$this
->drupalGet("node/{$this->article1->id()}/edit");
$this
->assertResponse(200, 'adminUser is allowed to edit the content.');
$bundle_path = 'admin/structure/types/manage/article';
$this
->drupalGet($bundle_path . '/fields');
$this
->assertFieldByXPath('//table[@id="field-overview"]//tr[@id="field-pbf-group"]/td[1]', 'Content of group', 'Field was created and appears in the overview page.');
$this
->drupalGet($bundle_path . '/form-display');
$this
->assertFieldByXPath('//table[@id="field-display-overview"]//tr[@id="field-pbf-group"]/td[1]', 'Content of group', 'Field appears in the Manage form display page.');
$this
->assertFieldByName('fields[field_pbf_group][type]', 'pbf_widget', 'The expected widget is selected.');
$this
->assertText(t('Grants access set on each node. Default grant access are :'));
$this
->drupalGet($bundle_path . '/display');
$this
->assertFieldByXPath('//table[@id="field-display-overview"]//tr[@id="field-pbf-group"]/td[1]', 'Content of group', 'Field appears in the Manage form display page.');
$this
->assertFieldByName('fields[field_pbf_group][type]', 'pbf_formatter_default', 'The expected formatter is selected.');
$user_path_config = 'admin/config/people/accounts';
$this
->drupalGet($user_path_config . '/fields');
$this
->assertFieldByXPath('//table[@id="field-overview"]//tr[@id="field-pbf-group"]/td[1]', 'Member of group', 'User Obf field was created and appears in the overview page.');
$this
->drupalGet($user_path_config . '/form-display');
$this
->assertFieldByName('fields[field_pbf_group][type]', 'pbf_widget', 'The expected widget is selected.');
$this
->drupalGet($user_path_config . '/display');
$this
->assertFieldByName('fields[field_pbf_group][type]', 'pbf_formatter_default', 'The expected formatter is selected.');
$this
->drupalLogin($this->normalUser);
$this
->drupalGet("node/{$this->article2->id()}");
$this
->assertText(t('Access denied'));
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article1->id()}/edit");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article2->id()}/edit");
$this
->assertResponse(403);
$this->container
->get('cron')
->run();
$this
->checkSearchResults('Article', 1);
$settings = [
'match_operator' => 'CONTAINS',
'size' => 30,
'placeholder' => '',
'grant_global' => 1,
'grant_public' => 1,
'grant_view' => 0,
'grant_update' => 0,
'grant_delete' => 0,
];
$this
->setFormDisplay('node.article.default', 'node', 'article', 'default', $this->fieldname, 'pbf_widget', $settings);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet($bundle_path . '/form-display');
$this
->assertText(t('Grants access set generally. Grant access used are :'));
$edit = [
$this->fieldname . '[0][target_id]' => $this->group1
->getTitle() . ' (' . $this->group1
->id() . ')',
];
$this
->drupalPostForm('/node/' . $this->article1
->id() . '/edit', $edit, t('Save'));
$this
->assertText(t('has been updated'));
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertLink($this->group1
->getTitle());
$edit = [
$this->fieldname . '[0][target_id]' => $this->group1
->getTitle() . ' (' . $this->group1
->id() . ')',
];
$this
->drupalPostForm('/node/' . $this->article2
->id() . '/edit', $edit, t('Save'));
$this
->assertText(t('has been updated'));
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertLink($this->group1
->getTitle());
$this
->drupalLogin($this->normalUser);
$this
->drupalGet("node/{$this->article2->id()}");
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article2->id()}/edit");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article2->id()}/delete");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article1->id()}/edit");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article1->id()}/delete");
$this
->assertResponse(403);
$this
->checkSearchResults('Article', 2);
$settings = [
'match_operator' => 'CONTAINS',
'size' => 30,
'placeholder' => '',
'grant_global' => 1,
'grant_public' => 0,
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
];
$this
->setFormDisplay('node.article.default', 'node', 'article', 'default', $this->fieldname, 'pbf_widget', $settings);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet($bundle_path . '/form-display');
$this
->assertText(t('Grants access set generally. Grant access used are :'));
$this
->assertText(t('Grant view:1'));
$edit = [
$this->fieldname . '[0][target_id]' => $this->group1
->getTitle() . ' (' . $this->group1
->id() . ')',
];
$this
->drupalPostForm('/node/' . $this->article1
->id() . '/edit', $edit, t('Save'));
$this
->assertText(t('has been updated'));
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertLink($this->group1
->getTitle());
$edit = [
$this->fieldname . '[0][target_id]' => $this->group1
->getTitle() . ' (' . $this->group1
->id() . ')',
];
$this
->drupalPostForm('/node/' . $this->article2
->id() . '/edit', $edit, t('Save'));
$this
->assertText(t('has been updated'));
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertLink($this->group1
->getTitle());
$this
->drupalLogin($this->normalUser);
$this
->drupalGet("node/{$this->article2->id()}");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article2->id()}/edit");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article2->id()}/delete");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article1->id()}/edit");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article1->id()}/delete");
$this
->assertResponse(403);
$this
->checkSearchResults('Article', 0);
$this
->setUserField($this->normalUser
->id(), $this->fieldname, [
'target_id' => $this->group1
->id(),
]);
$this
->drupalGet("user/{$this->normalUser->id()}/edit");
$this
->assertResponse(200);
$this
->assertFieldByName('field_pbf_group[0][target_id]', $this->group1
->getTitle() . ' (' . $this->group1
->id() . ')', 'The expected value is found in the Pbf input field');
$this
->drupalGet("user/{$this->normalUser->id()}");
$this
->assertLink($this->group1
->getTitle());
$this
->assertResponse(200);
$this->container
->get('cron')
->run();
$this
->checkSearchResults('Article', 2);
$this
->drupalGet("node/{$this->article2->id()}");
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article2->id()}/edit");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article1->id()}/edit");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article2->id()}/delete");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article1->id()}/delete");
$this
->assertResponse(403);
$settings = [
'match_operator' => 'CONTAINS',
'size' => 30,
'placeholder' => '',
'grant_global' => 1,
'grant_public' => 0,
'grant_view' => 1,
'grant_update' => 1,
'grant_delete' => 1,
];
$this
->setFormDisplay('node.article.default', 'node', 'article', 'default', $this->fieldname, 'pbf_widget', $settings);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet($bundle_path . '/form-display');
$this
->assertText(t('Grants access set generally. Grant access used are :'));
$this
->assertText(t('Grant update:1'));
$this
->assertText(t('Grant delete:1'));
$edit = [
$this->fieldname . '[0][target_id]' => $this->group1
->getTitle() . ' (' . $this->group1
->id() . ')',
];
$this
->drupalPostForm('/node/' . $this->article1
->id() . '/edit', $edit, t('Save'));
$this
->assertText(t('has been updated'));
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertLink($this->group1
->getTitle());
$edit = [
$this->fieldname . '[0][target_id]' => $this->group1
->getTitle() . ' (' . $this->group1
->id() . ')',
];
$this
->drupalPostForm('/node/' . $this->article2
->id() . '/edit', $edit, t('Save'));
$this
->assertText(t('has been updated'));
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article2->id()}");
$this
->assertLink($this->group1
->getTitle());
$this
->drupalLogin($this->normalUser);
$this
->drupalGet("node/{$this->article2->id()}");
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article2->id()}/edit");
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article1->id()}/edit");
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article2->id()}/delete");
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article1->id()}/delete");
$this
->assertResponse(200);
$this
->drupalLogout();
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article2->id()}");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article2->id()}/edit");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article1->id()}/edit");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article2->id()}/delete");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article1->id()}/delete");
$this
->assertResponse(403);
}
}