View source
<?php
namespace Drupal\yamlform\Tests;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\yamlform\Entity\YamlForm;
class YamlFormAccessTest extends YamlFormTestBase {
public function testAccessControlHandler() {
$this
->drupalLogin($this->ownFormUser);
$this
->drupalPostForm('admin/structure/yamlform/add', [
'id' => 'test_own',
'title' => 'test_own',
'elements' => "test:\n '#markup': 'test'",
], t('Save'));
$this
->drupalGet('admin/structure/yamlform/manage/test_own/duplicate');
$this
->assertResponse(200);
$this
->drupalGet('admin/structure/yamlform/manage/test_own/delete');
$this
->assertResponse(200);
$this
->drupalGet('admin/structure/yamlform/manage/test_own/results/submissions');
$this
->assertResponse(200);
$this
->drupalLogin($this->anyFormUser);
$this
->drupalGet('admin/structure/yamlform/manage/test_own/duplicate');
$this
->assertResponse(200);
$this
->drupalGet('admin/structure/yamlform/manage/test_own/delete');
$this
->assertResponse(200);
$this
->drupalGet('admin/structure/yamlform/manage/test_own/results/submissions');
$this
->assertResponse(200);
$own_yamlform = YamlForm::load('test_own');
$own_yamlform
->setOwner($this->anyFormUser)
->save();
$this
->drupalLogin($this->ownFormUser);
$this
->drupalGet('admin/structure/yamlform/manage/test_own/duplicate');
$this
->assertResponse(403);
$this
->drupalGet('admin/structure/yamlform/manage/test_own/delete');
$this
->assertResponse(403);
$this
->drupalGet('admin/structure/yamlform/manage/test_own/results/submissions');
$this
->assertResponse(403);
}
public function testAccessRules() {
global $base_path;
list($yamlform, $submissions) = $this
->createYamlFormWithSubmissions();
$account = $this
->drupalCreateUser([
'access content',
]);
$yamlform_id = $yamlform
->id();
$sid = $submissions[0]
->id();
$uid = $account
->id();
$rid = $account
->getRoles()[1];
$yamlform
->setAccessRules(YamlForm::getDefaultAccessRules())
->save();
$this
->drupalGet('yamlform/' . $yamlform
->id());
$this
->assertResponse(200, 'Form create submission access for anonymous/authenticated user.');
$access_rules = [
'create' => [
'roles' => [],
'users' => [],
],
] + YamlForm::getDefaultAccessRules();
$yamlform
->setAccessRules($access_rules)
->save();
$this
->drupalGet('yamlform/' . $yamlform
->id());
$this
->assertResponse(403, 'Form returns access denied');
$any_tests = [
'yamlform/{yamlform}' => 'create',
'admin/structure/yamlform/manage/{yamlform}/results/submissions' => 'view_any',
'admin/structure/yamlform/manage/{yamlform}/results/table' => 'view_any',
'admin/structure/yamlform/manage/{yamlform}/results/download' => 'view_any',
'admin/structure/yamlform/manage/{yamlform}/results/clear' => 'purge_any',
'admin/structure/yamlform/manage/{yamlform}/submission/{yamlform_submission}' => 'view_any',
'admin/structure/yamlform/manage/{yamlform}/submission/{yamlform_submission}/text' => 'view_any',
'admin/structure/yamlform/manage/{yamlform}/submission/{yamlform_submission}/yaml' => 'view_any',
'admin/structure/yamlform/manage/{yamlform}/submission/{yamlform_submission}/edit' => 'update_any',
'admin/structure/yamlform/manage/{yamlform}/submission/{yamlform_submission}/delete' => 'delete_any',
];
foreach ($any_tests as $path => $permission) {
$path = str_replace('{yamlform}', $yamlform_id, $path);
$path = str_replace('{yamlform_submission}', $sid, $path);
$this
->drupalGet($path);
$this
->assertResponse(403, 'Form returns access denied');
}
$this
->drupalLogin($account);
foreach ($any_tests as $path => $permission) {
$path = str_replace('{yamlform}', $yamlform_id, $path);
$path = str_replace('{yamlform_submission}', $sid, $path);
$this
->drupalGet($path);
$this
->assertResponse(403, 'Form returns access denied');
}
foreach ($any_tests as $path => $permission) {
$path = str_replace('{yamlform}', $yamlform_id, $path);
$path = str_replace('{yamlform_submission}', $sid, $path);
$access_rules = [
$permission => [
'roles' => [
$rid,
],
'users' => [],
],
] + YamlForm::getDefaultAccessRules();
$yamlform
->setAccessRules($access_rules)
->save();
$this
->drupalGet($path);
$this
->assertResponse(200, 'Form allows access via role access rules');
$access_rules = [
$permission => [
'roles' => [],
'users' => [
$uid,
],
],
] + YamlForm::getDefaultAccessRules();
$yamlform
->setAccessRules($access_rules)
->save();
$this
->drupalGet($path);
$this
->assertResponse(200, 'Form allows access via user access rules');
}
$access_rules = [
'view_own' => [
'roles' => [
$rid,
],
'users' => [],
],
'update_own' => [
'roles' => [
$rid,
],
'users' => [],
],
'delete_own' => [
'roles' => [
$rid,
],
'users' => [],
],
] + YamlForm::getDefaultAccessRules();
$yamlform
->setAccessRules($access_rules)
->save();
$this
->drupalLogin($account);
$this
->drupalGet('yamlform/' . $yamlform
->id());
$this
->assertNoRaw('You have already submitted this form.');
$this
->assertNoRaw('View your previous submission');
$sid = $this
->postSubmission($yamlform);
$this
->drupalGet('yamlform/' . $yamlform
->id());
$this
->assertRaw('You have already submitted this form.');
$this
->assertRaw("<a href=\"{$base_path}yamlform/{$yamlform_id}/submissions/{$sid}\">View your previous submission</a>.");
$sid = $this
->postSubmission($yamlform);
$this
->drupalGet('yamlform/' . $yamlform
->id());
$this
->assertRaw('You have already submitted this form.');
$this
->assertRaw("<a href=\"{$base_path}yamlform/{$yamlform_id}/submissions\">View your previous submissions</a>");
$test_own = [
'admin/structure/yamlform/manage/{yamlform}/results/submissions' => 403,
'admin/structure/yamlform/manage/{yamlform}/results/table' => 403,
'admin/structure/yamlform/manage/{yamlform}/results/download' => 403,
'admin/structure/yamlform/manage/{yamlform}/results/clear' => 403,
'admin/structure/yamlform/manage/{yamlform}/submission/{yamlform_submission}' => 200,
'admin/structure/yamlform/manage/{yamlform}/submission/{yamlform_submission}/text' => 403,
'admin/structure/yamlform/manage/{yamlform}/submission/{yamlform_submission}/yaml' => 403,
'admin/structure/yamlform/manage/{yamlform}/submission/{yamlform_submission}/edit' => 200,
'admin/structure/yamlform/manage/{yamlform}/submission/{yamlform_submission}/delete' => 200,
];
foreach ($test_own as $path => $status_code) {
$path = str_replace('{yamlform}', $yamlform_id, $path);
$path = str_replace('{yamlform_submission}', $sid, $path);
$this
->drupalGet($path);
$this
->assertResponse($status_code, new FormattableMarkup('Form @status_code access via own access rules.', [
'@status_code' => $status_code == 403 ? 'denies' : 'allows',
]));
}
}
}