WebformViewsFieldTestBase.php in Webform Views Integration 8.5
File
tests/src/Kernel/field/WebformViewsFieldTestBase.php
View source
<?php
namespace Drupal\Tests\webform_views\Kernel\field;
use Drupal\Tests\webform_views\Kernel\WebformViewsTestBase;
abstract class WebformViewsFieldTestBase extends WebformViewsTestBase {
public function testField() {
$this->webform = $this
->createWebform($this->webform_elements);
$this
->createWebformSubmissions($this->webform_submissions_data, $this->webform);
$this->view = $this
->initView($this->webform, $this->view_handlers);
$rendered_cells = $this
->renderView($this->view);
$this
->assertSame($this->webform_submissions_data, $rendered_cells, 'Views field on a webform element produces correct output.');
}
public function testMultiValueAllInOne() {
$webform_elements = $this->webform_elements;
foreach ($webform_elements as $k => $v) {
$webform_elements[$k]['#multiple'] = 10;
}
$this->webform = $this
->createWebform($webform_elements);
$this
->createWebformSubmissions($this->webform_submission_multivalue_data, $this->webform);
$view_handlers = $this->view_handlers;
foreach ($view_handlers['field'] as $k => $v) {
$view_handlers['field'][$k]['options']['webform_multiple_value'] = TRUE;
}
$this->view = $this
->initView($this->webform, $view_handlers);
$rendered_cells = $this
->renderView($this->view);
$expected = [];
foreach ($this->webform_submission_multivalue_data as $i => $submission) {
foreach ($submission as $element => $data) {
$render = [
'#theme' => 'item_list',
'#items' => $data,
];
$expected[$i][$element] = (string) \Drupal::service('renderer')
->renderRoot($render);
}
}
$this
->assertSame($expected, $rendered_cells);
}
public function testMultiValueDeltaOffset() {
$offset = 0;
$webform_elements = $this->webform_elements;
foreach ($webform_elements as $k => $v) {
$webform_elements[$k]['#multiple'] = 10;
}
$this->webform = $this
->createWebform($webform_elements);
$this
->createWebformSubmissions($this->webform_submission_multivalue_data, $this->webform);
$view_handlers = $this->view_handlers;
foreach ($view_handlers['field'] as $k => $v) {
$view_handlers['field'][$k]['options']['webform_multiple_value'] = FALSE;
$view_handlers['field'][$k]['options']['webform_multiple_delta'] = $offset;
}
$this->view = $this
->initView($this->webform, $view_handlers);
$rendered_cells = $this
->renderView($this->view);
$expected = [];
foreach ($this->webform_submission_multivalue_data as $i => $submission) {
foreach ($submission as $element => $data) {
$expected[$i][$element] = $data[$offset];
}
}
$this
->assertSame($expected, $rendered_cells);
}
public function testClickSort($field_handler_id, $order, $expected) {
$this->webform = $this
->createWebform($this->webform_elements);
$this
->createWebformSubmissions($this->webform_submissions_data, $this->webform);
$this->view = $this
->initView($this->webform, $this->view_handlers);
$this->view
->getExecutable()
->build();
$this->view
->getExecutable()->field[$field_handler_id]
->clickSort($order);
$this->view
->getExecutable()->built = FALSE;
$rendered_cells = $this
->renderView($this->view);
$this
->assertSame($expected, $rendered_cells, 'Click sorting works for ' . $order . ' order');
}
public function providerClickSort() {
$tests = [];
$tests[] = [
$this->view_handlers['field'][0]['id'],
'asc',
$this->webform_submissions_data,
];
$tests[] = [
$this->view_handlers['field'][0]['id'],
'desc',
array_reverse($this->webform_submissions_data),
];
return $tests;
}
}