View source
<?php
namespace Drupal\Tests\pbf\Functional;
class PbfAccessByUserDirectTest extends PbfBaseTest {
protected $fieldname;
protected $userMethod;
public function setUp() {
parent::setUp();
$this->fieldname = 'field_pbf_user';
$this->userMethod = 'user';
$this
->attachPbfUserFields($this->fieldname, $this->userMethod);
$this->user = $this
->drupalCreateUser($this->permissions);
$this->article1 = $this
->createSimpleArticle('Article 1', $this->fieldname, $this->user
->id(), 1, 0, 0, 0);
$this->article2 = $this
->createSimpleArticle('Article 2', $this->fieldname, $this->user
->id(), 0, 1, 0, 0);
}
public function testPbfAccessByUser() {
$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->article2->id()}");
$this
->assertResponse(200, 'adminUser is allowed to view the content.');
$this
->drupalGet("node/{$this->article2->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-user"]/td[1]', 'Grant access to user', '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-user"]/td[1]', 'Grant access to user', 'Field appears in the Manage form display page.');
$this
->assertFieldByName('fields[field_pbf_user][type]', 'pbf_widget', 'The expected widget is selected.');
$this
->drupalGet($bundle_path . '/display');
$this
->assertFieldByXPath('//table[@id="field-display-overview"]//tr[@id="field-pbf-user"]/td[1]', 'Grant access to user', 'Field appears in the Manage form display page.');
$this
->assertFieldByName('fields[field_pbf_user][type]', 'pbf_formatter_default', 'The expected formatter is selected.');
if ($this->userMethod == 'ref_user') {
$user_path_config = 'admin/config/people/accounts';
$this
->drupalGet($user_path_config . '/fields');
$this
->assertFieldByXPath('//table[@id="field-overview"]//tr[@id="field-pbf-user"]/td[1]', 'User related to user', 'User Obf field was created and appears in the overview page.');
$this
->drupalGet($user_path_config . '/form-display');
$this
->assertFieldByName('fields[field_pbf_user][type]', 'pbf_widget', 'The expected widget is selected.');
$this
->drupalGet($user_path_config . '/display');
$this
->assertFieldByName('fields[field_pbf_user][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);
$this
->drupalLogin($this->user);
$this
->drupalGet("node/{$this->article2->id()}");
$this
->assertResponse(200);
$this
->checkSearchResults('Article', 2);
$this
->drupalGet("node/{$this->article2->id()}/edit");
$this
->assertResponse(403);
$this
->drupalGet("node/{$this->article2->id()}/delete");
$this
->assertResponse(403);
$value = [
'target_id' => $this->user
->id(),
'grant_public' => 0,
'grant_view' => 1,
'grant_update' => 1,
'grant_delete' => 1,
];
$this->article2
->set($this->fieldname, $value)
->save();
$this
->drupalGet("node/{$this->article2->id()}");
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article2->id()}/edit");
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article2->id()}/delete");
$this
->assertResponse(200);
$this
->drupalLogout();
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article2->id()}");
$this
->assertResponse(403);
$this
->drupalLogin($this->normalUser);
$this
->drupalGet("node/{$this->article1->id()}");
$this
->assertResponse(200);
$this
->drupalGet("node/{$this->article2->id()}");
$this
->assertResponse(403);
}
}