View source
<?php
namespace Drupal\Tests\webform_node\Functional;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
use Drupal\webform\Entity\Webform;
use Drupal\webform\WebformInterface;
class WebformNodeResultsTest extends WebformNodeBrowserTestBase {
public static $modules = [
'block',
'webform',
'webform_node',
];
protected function setUp() {
parent::setUp();
$this
->placeBlocks();
}
public function testResults() {
global $base_path;
$normal_user = $this
->drupalCreateUser();
$admin_user = $this
->drupalCreateUser([
'administer webform',
]);
$admin_submission_user = $this
->drupalCreateUser([
'administer webform submission',
]);
$submission_storage = \Drupal::entityTypeManager()
->getStorage('webform_submission');
$webform = Webform::load('contact');
$node = $this
->drupalCreateNode([
'type' => 'webform',
]);
$nid = $node
->id();
$this
->drupalLogin($admin_submission_user);
$this
->drupalGet('/node/' . $node
->id() . '/webform/results/submissions');
$this
->assertResponse(403);
$node->webform->target_id = 'contact';
$node->webform->status = WebformInterface::STATUS_OPEN;
$node
->save();
$this
->drupalLogin($normal_user);
$node_sids = [];
$webform_sids = [];
for ($i = 1; $i <= 3; $i++) {
$edit = [
'name' => "node{$i}",
'email' => "node{$i}@example.com",
'subject' => "Node {$i} subject",
'message' => "Node {$i} message",
];
$node_sids[$i] = $this
->postNodeSubmission($node, $edit);
$edit = [
'name' => "webform{$i}",
'email' => "webform{$i}@example.com",
'subject' => "Webform {$i} subject",
'message' => "Webform {$i} message",
];
$webform_sids[$i] = $this
->postSubmission($webform, $edit);
}
$this
->assertEqual($submission_storage
->getTotal($webform, $node), 3);
$this
->assertEqual($submission_storage
->getTotal($webform), 6);
$this
->drupalLogin($admin_submission_user);
$node_route_parameters = [
'node' => $node
->id(),
'webform_submission' => $node_sids[1],
];
$node_submission_url = Url::fromRoute('entity.node.webform_submission.canonical', $node_route_parameters);
$node_submission_title = $node
->label() . ': Submission #' . $node_sids[1];
$webform_submission_route_parameters = [
'webform' => 'contact',
'webform_submission' => $node_sids[1],
];
$webform_submission_url = Url::fromRoute('entity.webform_submission.canonical', $webform_submission_route_parameters);
$this
->drupalGet('/node/' . $node
->id() . '/webform/results/submissions');
$this
->assertResponse(200);
$this
->assertRaw('<h1>' . $node
->label() . '</h1>');
$this
->assertNoRaw('<h1>' . $webform
->label() . '</h1>');
$this
->assertRaw('<a href="' . $node_submission_url
->toString() . '" title="' . Html::escape($node_submission_title) . '" aria-label="' . Html::escape($node_submission_title) . '">' . $node_sids[1] . '</a>');
$this
->assertNoRaw('<a href="' . $webform_submission_url
->toString() . '">' . $webform_sids[1] . '</a>');
$this
->drupalGet('/node/' . $node
->id() . '/webform/submission/' . $node_sids[1]);
$this
->assertRaw($node
->label() . ': Submission #' . $node_sids[1]);
$this
->drupalGet('/node/' . $node
->id() . '/webform/submission/' . $node_sids[2]);
$this
->assertRaw($node
->label() . ': Submission #' . $node_sids[2]);
$this
->drupalGet('/node/' . $node
->id() . '/webform/submission/' . $node_sids[1]);
$node_route_parameters = [
'node' => $node
->id(),
'webform_submission' => $node_sids[2],
];
$node_submission_url = Url::fromRoute('entity.node.webform_submission.canonical', $node_route_parameters);
$this
->assertRaw('<a href="' . $node_submission_url
->toString() . '" rel="next" title="Go to next page">Next submission <b>›</b></a>');
$webform
->setSetting('draft', WebformInterface::DRAFT_AUTHENTICATED);
$webform
->save();
$this
->drupalLogin($normal_user);
$edit = [
'name' => "nodeDraft",
'email' => "nodeDraft@example.com",
'subject' => "Node draft subject",
'message' => "Node draft message",
];
$this
->drupalPostForm('/node/' . $node
->id(), $edit, 'Save Draft');
$this
->drupalGet('/node/' . $node
->id());
$this
->assertRaw('A partially-completed form was found. Please complete the remaining portions.');
$this
->drupalGet('/webform/contact');
$this
->assertNoRaw('A partially-completed form was found. Please complete the remaining portions.');
$this
->drupalLogin($admin_submission_user);
$this
->drupalGet('/admin/structure/webform/manage/' . $webform
->id() . '/results/submissions/custom');
$this
->assertResponse(403);
$this
->drupalLogin($admin_user);
$this
->drupalGet('/admin/structure/webform/manage/' . $webform
->id() . '/results/submissions/custom');
$this
->assertResponse(200);
$this
->drupalGet('/node/' . $node
->id() . '/webform/results/submissions');
$this
->assertRaw('<th specifier="created" class="priority-medium is-active" aria-sort="descending">');
$this
->assertRaw('sort by Created');
$this
->assertNoRaw('sort by Changed');
$edit = [
'columns[created][checkbox]' => FALSE,
'columns[changed][checkbox]' => TRUE,
'sort' => 'serial',
'direction' => 'asc',
'limit' => 20,
'default' => TRUE,
];
$this
->drupalPostForm('/admin/structure/webform/manage/' . $webform
->id() . '/results/submissions/custom', $edit, 'Save');
$this
->assertRaw('The customized table has been saved.');
$this
->drupalGet('/node/' . $node
->id() . '/webform/results/submissions');
$this
->assertRaw('<th specifier="serial" aria-sort="ascending" class="is-active">');
$this
->assertNoRaw('sort by Created');
$this
->assertRaw('sort by Changed');
$this
->drupalLogout();
$any_user = $this
->drupalCreateUser([
'access content',
'view webform submissions any node',
'edit webform submissions any node',
'delete webform submissions any node',
]);
$own_user = $this
->drupalCreateUser([
'access content',
'view webform submissions own node',
'edit webform submissions own node',
'delete webform submissions own node',
]);
$this
->drupalLogin($any_user);
$this
->drupalGet('/node/' . $node
->id() . '/webform/results/submissions');
$this
->assertResponse(200);
foreach ($node_sids as $node_sid) {
$this
->assertLinkByHref("{$base_path}node/{$nid}/webform/submission/{$node_sid}");
}
$this
->drupalLogin($own_user);
$this
->drupalGet('/node/' . $node
->id() . '/webform/results/submissions');
$this
->assertResponse(403);
$node
->setOwnerId($own_user
->id())
->save();
$this
->drupalGet('/node/' . $node
->id() . '/webform/results/submissions');
$this
->assertResponse(200);
foreach ($node_sids as $node_sid) {
$this
->assertLinkByHref("{$base_path}node/{$nid}/webform/submission/{$node_sid}");
}
$this
->drupalPostForm('/node/' . $node
->id() . '/webform/results/clear', [
'confirm' => TRUE,
], 'Clear');
$this
->assertEqual($submission_storage
->getTotal($webform, $node), 0);
$this
->assertEqual($submission_storage
->getTotal($webform), 3);
}
}