View source
<?php
namespace Drupal\Tests\webform\Functional;
use Drupal\webform\Entity\Webform;
class WebformSubmissionListBuilderCustomizeTest extends WebformBrowserTestBase {
public static $modules = [
'node',
'webform',
'webform_test_submissions',
];
protected static $testWebforms = [
'test_submissions',
];
public function testCustomize() {
global $base_path;
$admin_user = $this
->drupalCreateUser([
'administer webform',
]);
$own_submission_user = $this
->drupalCreateUser([
'view own webform submission',
'edit own webform submission',
'delete own webform submission',
'access webform submission user',
]);
$admin_submission_user = $this
->drupalCreateUser([
'administer webform submission',
]);
$webform = Webform::load('test_submissions');
$submissions = array_values(\Drupal::entityTypeManager()
->getStorage('webform_submission')
->loadByProperties([
'webform_id' => 'test_submissions',
]));
$user_data = \Drupal::service('user.data');
$this
->drupalLogin($admin_submission_user);
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions/custom');
$this
->assertResponse(403);
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions/custom/user');
$this
->assertResponse(403);
$this
->drupalLogin($admin_user);
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions/custom');
$this
->assertResponse(200);
$this
->drupalLogin($admin_user);
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions/custom/user');
$this
->assertResponse(403);
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions');
$this
->assertRaw('sort by Created');
$this
->assertNoRaw('sort by Changed');
$this
->assertPattern('#First name.+Last name#s');
$this
->assertNoRaw('<nav class="pager" role="navigation" aria-labelledby="pagination-heading">');
$this
->assertRaw('<th specifier="created" class="priority-medium is-active" aria-sort="descending">');
$this
->assertPattern('#Hillary.+Abraham.+George#ms');
$this
->assertRaw('data-webform-href="' . $submissions[0]
->toUrl()
->toString() . '"');
$this
->assertRaw('data-webform-href="' . $submissions[1]
->toUrl()
->toString() . '"');
$this
->assertRaw('data-webform-href="' . $submissions[2]
->toUrl()
->toString() . '"');
$actual_state = \Drupal::state()
->get('webform.webform.test_submissions');
$this
->assertNull($actual_state);
$edit = [
'columns[created][checkbox]' => FALSE,
'columns[changed][checkbox]' => TRUE,
'columns[element__first_name][weight]' => '8',
'columns[element__last_name][weight]' => '7',
'sort' => 'element__first_name',
'direction' => 'desc',
'limit' => 20,
'link_type' => 'table',
];
$this
->drupalPostForm('/admin/structure/webform/manage/test_submissions/results/submissions/custom', $edit, 'Save');
$this
->assertRaw('The customized table has been saved.');
$actual_state = \Drupal::state()
->get('webform.webform.test_submissions');
$expected_state = [
'results.custom.columns' => [
0 => 'serial',
1 => 'sid',
2 => 'label',
3 => 'uuid',
4 => 'in_draft',
5 => 'sticky',
6 => 'locked',
7 => 'notes',
8 => 'element__last_name',
9 => 'element__first_name',
10 => 'completed',
11 => 'changed',
12 => 'entity',
13 => 'uid',
14 => 'remote_addr',
15 => 'element__sex',
16 => 'element__dob',
17 => 'element__node',
18 => 'element__colors',
19 => 'element__likert',
20 => 'element__likert__q1',
21 => 'element__likert__q2',
22 => 'element__likert__q3',
23 => 'element__address',
24 => 'element__address__address',
25 => 'element__address__address_2',
26 => 'element__address__city',
27 => 'element__address__state_province',
28 => 'element__address__postal_code',
29 => 'element__address__country',
30 => 'operations',
],
'results.custom.sort' => 'element__first_name',
'results.custom.direction' => 'desc',
'results.custom.limit' => 20,
'results.custom.link_type' => 'table',
'results.custom.format' => [
'header_format' => 'label',
'element_format' => 'value',
],
'results.custom.default' => TRUE,
];
$this
->assertEquals($expected_state, $actual_state);
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions');
$this
->assertRaw('data-webform-href="' . $submissions[0]
->toUrl('table')
->toString() . '"');
$this
->assertRaw('data-webform-href="' . $submissions[1]
->toUrl('table')
->toString() . '"');
$this
->assertRaw('data-webform-href="' . $submissions[2]
->toUrl('table')
->toString() . '"');
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions');
$this
->assertNoRaw('sort by Created');
$this
->assertRaw('sort by Changed');
$this
->assertPattern('#Last name.+First name#ms');
$this
->assertPattern('#Hillary.+George.+Abraham#ms');
$webform
->setState('results.custom.limit', 1);
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions');
$this
->assertNoRaw('George');
$this
->assertNoRaw('Abraham');
$this
->assertNoRaw('Hillary');
$this
->assertRaw('quotes' "');
$this
->assertRaw('<nav class="pager" role="navigation" aria-labelledby="pagination-heading">');
$webform
->setState('results.custom.limit', 20);
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions');
$this
->assertTableHeaderSort('User');
$this
->assertRaw('<td class="priority-medium">Anonymous</td>');
$this
->assertTableHeaderSort('Date of birth');
$this
->assertRaw('<td>Sunday, October 26, 1947</td>');
$webform
->setState('results.custom.format', [
'header_format' => 'key',
'element_format' => 'raw',
]);
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions');
$this
->assertTableHeaderSort('uid');
$this
->assertRaw('<td class="priority-medium">0</td>');
$this
->assertTableHeaderSort('dob');
$this
->assertRaw('<td>1947-10-26</td>');
$this
->drupalLogin($admin_user);
$this
->drupalPostForm('/admin/structure/webform/manage/test_submissions/results/submissions/custom', $edit, 'Reset');
$this
->assertRaw('The customized table has been reset.');
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions');
$this
->assertRaw('>Customize<');
$this
->assertLinkByHref("{$base_path}admin/structure/webform/manage/test_submissions/results/submissions/custom");
$webform
->setSetting('results_customize', TRUE)
->save();
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions');
$this
->assertNoRaw('>Customize<');
$this
->assertLinkByHref("{$base_path}admin/structure/webform/manage/test_submissions/results/submissions/custom");
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions');
$this
->assertRaw('>Customize my table<');
$this
->assertLinkByHref("{$base_path}admin/structure/webform/manage/test_submissions/results/submissions/custom/user");
$this
->assertPattern('#First name.+Last name#s');
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions/custom/user');
$this
->assertRaw('>Customize default table<');
$this
->assertLinkByHref("{$base_path}admin/structure/webform/manage/test_submissions/results/submissions/custom");
$this
->drupalLogin($admin_submission_user);
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions/custom');
$this
->assertResponse(403);
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions/custom/user');
$this
->assertResponse(200);
$edit = [
'columns[element__first_name][weight]' => '8',
'columns[element__last_name][weight]' => '7',
];
$this
->drupalPostForm('/admin/structure/webform/manage/test_submissions/results/submissions/custom/user', $edit, 'Save');
$this
->assertRaw('Your customized table has been saved.');
$this
->assertPattern('#Last name.+First name#ms');
$this
->drupalLogin($admin_user);
$edit = [
'columns[element__first_name][checkbox]' => FALSE,
'columns[element__last_name][checkbox]' => FALSE,
];
$this
->drupalPostForm('/admin/structure/webform/manage/test_submissions/results/submissions/custom', $edit, 'Save');
$this
->assertRaw('The default customized table has been saved.');
$this
->assertNoRaw('First name');
$this
->assertNoRaw('Last name');
$this
->drupalLogin($admin_submission_user);
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions');
$this
->assertPattern('#Last name.+First name#ms');
$webform
->setSetting('results_customize', FALSE)
->save();
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions');
$this
->assertNoRaw('First name');
$this
->assertNoRaw('Last name');
$webform
->setSetting('results_customize', TRUE)
->save();
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions');
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions');
$this
->assertPattern('#Last name.+First name#ms');
$this
->drupalPostForm('/admin/structure/webform/manage/test_submissions/results/submissions/custom/user', $edit, 'Reset');
$this
->assertRaw('Your customized table has been reset.');
$this
->drupalGet('/admin/structure/webform/manage/test_submissions/results/submissions');
$this
->assertNoRaw('First name');
$this
->assertNoRaw('Last name');
$this
->drupalLogin($own_submission_user);
$this
->drupalget('/webform/test_submissions/submissions');
$this
->assertRaw('<th specifier="serial">');
$this
->assertRaw('<th specifier="created" class="priority-medium is-active" aria-sort="descending">');
$this
->assertRaw('<th specifier="remote_addr" class="priority-low">');
$webform
->setSetting('submission_user_columns', [
'element__first_name',
'element__last_name',
])
->save();
$this
->drupalget('/webform/test_submissions/submissions');
$this
->assertNoRaw('<th specifier="serial">');
$this
->assertNoRaw('<th specifier="created" class="priority-medium is-active" aria-sort="descending">');
$this
->assertNoRaw('<th specifier="remote_addr" class="priority-low">');
$this
->assertRaw('<th specifier="element__first_name" aria-sort="ascending" class="is-active">');
$this
->assertRaw('<th specifier="element__last_name">');
$this
->drupalLogin($admin_user);
$edit = [
'columns[element__first_name][weight]' => '8',
'columns[element__last_name][weight]' => '7',
];
$this
->drupalPostForm('/admin/structure/webform/manage/test_submissions/results/submissions/custom', $edit, 'Save');
$edit = [
'columns[element__first_name][weight]' => '8',
'columns[element__last_name][weight]' => '7',
];
$this
->drupalPostForm('/admin/structure/webform/manage/test_submissions/results/submissions/custom/user', $edit, 'Save');
$this
->assertNotEmpty(\Drupal::state()
->get('webform.webform.test_submissions'));
$this
->assertNotEmpty($user_data
->get('webform', NULL, 'test_submissions'));
$webform
->delete();
$this
->assertEmpty(\Drupal::state()
->get('webform.webform.test_submissions'));
$this
->assertEmpty($user_data
->get('webform', NULL, 'test_submissions'));
}
protected function assertTableHeaderSort($order, $sort = 'asc', $label = NULL) {
global $base_path;
$label = $label ?: $order;
if (floatval(\Drupal::VERSION) >= 8.9) {
$this
->assertRaw('<a href="' . $base_path . 'admin/structure/webform/manage/test_submissions/results/submissions?sort=' . $sort . '&order=' . str_replace(' ', '%20', $order) . '" title="sort by ' . $label . '" rel="nofollow">' . $label . '</a>');
}
else {
$this
->assertRaw('<a href="' . $base_path . 'admin/structure/webform/manage/test_submissions/results/submissions?sort=' . $sort . '&order=' . str_replace(' ', '%20', $order) . '" title="sort by ' . $label . '">' . $label . '</a>');
}
}
}