View source
<?php
namespace Drupal\yamlform_node\Tests;
use Drupal\yamlform\Entity\YamlForm;
use Drupal\yamlform\Tests\YamlFormTestBase;
class YamlFormNodeAccessTest extends YamlFormTestBase {
public static $modules = [
'system',
'block',
'node',
'user',
'yamlform',
'yamlform_test',
'yamlform_node',
];
public function testAccessRules() {
$yamlform = YamlForm::load('contact');
$node = $this
->drupalCreateNode([
'type' => 'yamlform',
]);
$node->yamlform->target_id = 'contact';
$node->yamlform->status = 1;
$node
->save();
$nid = $node
->id();
$this
->drupalLogin($this->normalUser);
$roles = $this->normalUser
->getRoles(TRUE);
$rid = reset($roles);
$uid = $this->normalUser
->id();
$edit = [
'name' => '{name}',
'email' => 'example@example.com',
'subject' => '{subject}',
'message' => '{message',
];
$this
->drupalPostForm('node/' . $node
->id(), $edit, t('Send message'));
$sid = $this
->getLastSubmissionId($yamlform);
$yamlform
->setAccessRules(YamlForm::getDefaultAccessRules())
->save();
$this
->drupalGet('node/' . $node
->id());
$this
->assertFieldByName('name', $this->normalUser
->getAccountName());
$this
->assertFieldByName('email', $this->normalUser
->getEmail());
$access_rules = [
'create' => [
'roles' => [],
'users' => [],
],
] + YamlForm::getDefaultAccessRules();
$yamlform
->setAccessRules($access_rules)
->save();
$this
->drupalGet('node/' . $node
->id());
$this
->assertNoFieldByName('name', $this->normalUser
->getAccountName());
$this
->assertNoFieldByName('email', $this->normalUser
->getEmail());
$any_tests = [
'node/{node}/yamlform/results/submissions' => 'view_any',
'node/{node}/yamlform/results/table' => 'view_any',
'node/{node}/yamlform/results/download' => 'view_any',
'node/{node}/yamlform/results/clear' => 'purge_any',
'node/{node}/yamlform/submission/{yamlform_submission}' => 'view_any',
'node/{node}/yamlform/submission/{yamlform_submission}/text' => 'view_any',
'node/{node}/yamlform/submission/{yamlform_submission}/yaml' => 'view_any',
'node/{node}/yamlform/submission/{yamlform_submission}/edit' => 'update_any',
'node/{node}/yamlform/submission/{yamlform_submission}/delete' => 'delete_any',
];
foreach ($any_tests as $path => $permission) {
$path = str_replace('{node}', $nid, $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('{node}', $nid, $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');
}
}
}