View source
<?php
namespace Drupal\Tests\pbf\Functional;
use Drupal\node\Entity\Node;
use Drupal\field\Entity\FieldConfig;
class PbfAccessWithSynchronizationTest extends PbfBaseTest {
protected $fieldname;
protected $group_fieldname;
protected $group3;
public function setUp() {
parent::setUp();
$this->fieldname = 'field_pbf_group';
$this->group_fieldname = 'field_group_user';
$this
->attachPbfSynchronizedFields($this->fieldname, $this->group_fieldname);
$this->article1 = $this
->createSimpleArticle('Article 1', $this->fieldname, $this->group1
->id(), 0, 1, 0, 0);
$this->article2 = $this
->createSimpleArticle('Article 2', $this->fieldname, $this->group1
->id(), 0, 1, 0, 0);
\Drupal::service('entity_field.manager')
->clearCachedFieldDefinitions();
drupal_flush_all_caches();
$node_search_plugin = $this->container
->get('plugin.manager.search')
->createInstance('node_search');
$node_search_plugin
->updateIndex();
}
public function testPbfAccessWithSynchronization() {
$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.');
$this
->drupalGet("node/{$this->group1->id()}/edit");
$this
->assertResponse(200, 'adminUser is allowed to edit the group 1.');
$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
->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.');
$bundle_path = 'admin/structure/types/manage/group';
$this
->drupalGet($bundle_path . '/fields');
$this
->assertFieldByXPath('//table[@id="field-overview"]//tr[@id="field-group-user"]/td[1]', 'Group members', '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-group-user"]/td[1]', 'Group members', 'Field appears in the Manage form display page.');
$this
->assertFieldByName('fields[field_group_user][type]', 'pbf_widget', 'The expected widget is selected.');
$this
->drupalGet($bundle_path . '/display');
$this
->assertFieldByXPath('//table[@id="field-display-overview"]//tr[@id="field-group-user"]/td[1]', 'Group members', 'Field appears in the Manage form display page.');
$this
->assertFieldByName('fields[field_group_user][type]', 'pbf_formatter_default', 'The expected formatter is selected.');
$field_group_path_storage_settings = 'admin/structure/types/manage/group/fields/node.group.' . $this->group_fieldname . '/storage';
$edit = [
'cardinality' => 'number',
'cardinality_number' => 1,
];
$this
->drupalPostForm($field_group_path_storage_settings, $edit, t('Save field settings'));
$this
->drupalGet($field_group_path_storage_settings);
$this
->assertFieldByName('cardinality', 'number', 'The storage settings field is set to limited.');
$field_group_path_settings = 'admin/structure/types/manage/group/fields/node.group.' . $this->group_fieldname;
$this
->drupalGet($field_group_path_settings);
$this
->assertText(t('Only field with an unlimited cardinality can be synchronized'));
$edit = [
'cardinality' => -1,
];
$this
->drupalPostForm($field_group_path_storage_settings, $edit, t('Save field settings'));
$this
->drupalGet($field_group_path_storage_settings);
$this
->assertFieldByName('cardinality', -1, 'The storage settings field is set to unlimited.');
$this
->drupalGet($field_group_path_settings);
$this
->assertOption('edit-settings-synchronized-with', 'user.user.' . $this->fieldname);
$user_path_storage_settings = 'admin/config/people/accounts/fields/user.user.' . $this->fieldname . '/storage';
$edit = [
'cardinality' => 'number',
'cardinality_number' => 1,
];
$this
->drupalPostForm($user_path_storage_settings, $edit, t('Save field settings'));
$this
->drupalGet($user_path_storage_settings);
$this
->assertFieldByName('cardinality', 'number', 'The storage settings field for user is set to limited.');
$this
->drupalGet($field_group_path_settings);
$this
->assertNoOption('edit-settings-synchronized-with', 'user.user.' . $this->fieldname);
$edit = [
'cardinality' => -1,
];
$this
->drupalPostForm($user_path_storage_settings, $edit, t('Save field settings'));
$this
->drupalGet($user_path_storage_settings);
$this
->assertFieldByName('cardinality', -1, 'The storage settings field for user is set to unlimited.');
$edit = [
'settings[synchronized_with]' => 'user.user.' . $this->fieldname,
];
$this
->drupalPostForm($field_group_path_settings, $edit, t('Save settings'));
$this
->drupalGet($field_group_path_settings);
$this
->assertOptionSelected('edit-settings-synchronized-with', 'user.user.' . $this->fieldname);
$this
->assertNoFieldChecked('edit-settings-synchronized-from-target');
$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(403);
$this
->drupalGet("node/{$this->article1->id()}/edit");
$this
->assertResponse(403);
$this->container
->get('cron')
->run();
$this
->checkSearchResults('Article', 0);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet("node/{$this->group1->id()}/edit");
$this
->assertResponse(200);
$edit = [
$this->group_fieldname . '[0][target_id]' => $this->normalUser
->getDisplayName() . ' (' . $this->normalUser
->id() . ')',
];
$this
->drupalPostForm('/node/' . $this->group1
->id() . '/edit', $edit, t('Save'));
$this
->drupalGet("user/{$this->normalUser->id()}");
$this
->assertLink($this->group1
->getTitle());
$this
->drupalLogin($this->normalUser);
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article2->id()}");
$this
->assertResponse(200);
$this->container
->get('cron')
->run();
$this
->checkSearchResults('Article', 2);
$this
->drupalLogin($this->otherUser);
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article2->id()}");
$this
->assertResponse(403);
$this
->checkSearchResults('Article', 0);
$this
->drupalLogin($this->adminUser);
$edit = [
$this->group_fieldname . '[0][target_id]' => $this->normalUser
->getDisplayName() . ' (' . $this->normalUser
->id() . ')',
$this->group_fieldname . '[1][target_id]' => $this->otherUser
->getDisplayName() . ' (' . $this->otherUser
->id() . ')',
];
$this
->drupalPostForm('/node/' . $this->group1
->id() . '/edit', $edit, t('Save'));
$this
->drupalGet("user/{$this->otherUser->id()}");
$this
->assertLink($this->group1
->getTitle());
$this
->drupalLogin($this->otherUser);
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article2->id()}");
$this
->assertResponse(200);
$this
->checkSearchResults('Article', 2);
$this
->drupalGet("node/{$this->article2->id()}/edit");
$this
->assertResponse(403);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet("node/{$this->group1->id()}");
$this
->assertLink($this->normalUser
->getDisplayName());
$this
->assertLink($this->otherUser
->getDisplayName());
$edit = [
$this->fieldname . '[0][target_id]' => '',
];
$this
->drupalPostForm('/user/' . $this->otherUser
->id() . '/edit', $edit, t('Save'));
$this
->assertNoLink($this->group1
->getTitle());
$this
->drupalGet("node/{$this->group1->id()}");
$this
->assertLink($this->otherUser
->getDisplayName());
$this
->drupalLogin($this->otherUser);
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article2->id()}");
$this
->assertResponse(403);
$this
->checkSearchResults('Article', 0);
$this
->drupalLogin($this->adminUser);
$edit = [
'settings[synchronized_with]' => 'user.user.' . $this->fieldname,
'settings[synchronized_from_target]' => '1',
];
$this
->drupalPostForm($field_group_path_settings, $edit, t('Save settings'));
$this
->drupalGet($field_group_path_settings);
$this
->assertOptionSelected('edit-settings-synchronized-with', 'user.user.' . $this->fieldname);
$this
->assertFieldChecked('edit-settings-synchronized-from-target');
$edit = [
$this->group_fieldname . '[0][target_id]' => $this->normalUser
->getDisplayName() . ' (' . $this->normalUser
->id() . ')',
$this->group_fieldname . '[1][target_id]' => $this->otherUser
->getDisplayName() . ' (' . $this->otherUser
->id() . ')',
];
$this
->drupalPostForm('/node/' . $this->group1
->id() . '/edit', $edit, t('Save'));
$this
->drupalGet("user/{$this->otherUser->id()}");
$this
->assertLink($this->group1
->getTitle());
$this
->drupalLogin($this->otherUser);
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article2->id()}");
$this
->assertResponse(200);
$this
->checkSearchResults('Article', 2);
$edit = [
$this->fieldname . '[0][target_id]' => '',
];
$this
->drupalPostForm('/user/' . $this->otherUser
->id() . '/edit', $edit, t('Save'));
$this
->drupalGet("user/{$this->otherUser->id()}");
$this
->assertNoLink($this->group1
->getTitle());
$this
->drupalGet("node/{$this->group1->id()}");
$this
->assertNoLink($this->otherUser
->getDisplayName());
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article2->id()}");
$this
->assertResponse(403);
}
}