View source
<?php
namespace Drupal\Tests\views\Kernel\Plugin;
use Drupal\views\Views;
use Drupal\views\ViewExecutable;
use Symfony\Component\HttpFoundation\Request;
class StyleTableUnitTest extends PluginKernelTestBase {
public static $testViews = [
'test_table',
];
public function testTable() {
$view = Views::getView('test_table');
$view
->setDisplay('default');
$view
->initStyle();
$view
->initHandlers();
$view
->initQuery();
$style_plugin = $view->style_plugin;
$request = new Request();
$view
->setRequest($request);
$style_plugin->options['override'] = FALSE;
$style_plugin->options['default'] = '';
$this
->assertTrue($style_plugin
->buildSort(), 'If no order and no default order is specified, the normal sort should be used.');
$style_plugin->options['default'] = 'id';
$this
->assertTrue($style_plugin
->buildSort(), 'If no order but a default order is specified, the normal sort should be used.');
$request->attributes
->set('order', $this
->randomMachineName());
$this
->assertTrue($style_plugin
->buildSort(), 'If no valid field is chosen for order, the normal sort should be used.');
$request->attributes
->set('order', 'id');
$this
->assertTrue($style_plugin
->buildSort(), 'If a valid order is specified but the table is configured to not override, the normal sort should be used.');
$style_plugin->options['override'] = TRUE;
$this
->assertFalse($style_plugin
->buildSort(), 'If a valid order is specified and the table is configured to override, the normal sort should not be used.');
$request = new Request();
$view
->setRequest($request);
$this
->prepareView($view);
$style_plugin = $view->style_plugin;
$style_plugin->options['default'] = '';
$style_plugin
->buildSortPost();
$this
->assertNull($style_plugin->order, 'No sort order was set, when no order was specified and no default column was selected.');
$this
->assertNull($style_plugin->active, 'No sort field was set, when no order was specified and no default column was selected.');
$view
->destroy();
$this
->prepareView($view);
$style_plugin = $view->style_plugin;
$style_plugin->options['default'] = 'id';
$style_plugin->options['info']['id']['default_sort_order'] = 'desc';
$style_plugin
->buildSortPost();
$this
->assertSame('desc', $style_plugin->order, 'Fallback to the right default sort order.');
$this
->assertSame('id', $style_plugin->active, 'Fallback to the right default sort field.');
$view
->destroy();
$this
->prepareView($view);
$style_plugin = $view->style_plugin;
$style_plugin->options['default'] = 'id';
$style_plugin->options['info']['id']['default_sort_order'] = NULL;
$style_plugin->options['order'] = 'asc';
$style_plugin
->buildSortPost();
$this
->assertSame('asc', $style_plugin->order, 'Fallback to the right default sort order.');
$this
->assertSame('id', $style_plugin->active, 'Fallback to the right default sort field.');
$view
->destroy();
$this
->prepareView($view);
$style_plugin = $view->style_plugin;
$request->query
->set('sort', 'asc');
$random_name = $this
->randomMachineName();
$request->query
->set('order', $random_name);
$style_plugin
->buildSortPost();
$this
->assertSame('asc', $style_plugin->order, 'No sort order was set, when invalid sort order was specified.');
$this
->assertNull($style_plugin->active, 'No sort field was set, when invalid sort order was specified.');
$view
->destroy();
foreach ([
'asc',
'desc',
] as $order) {
$this
->prepareView($view);
$style_plugin = $view->style_plugin;
$request->query
->set('sort', $order);
$request->query
->set('order', 'id');
$style_plugin
->buildSortPost();
$this
->assertSame($order, $style_plugin->order, 'Ensure the right sort order was set.');
$this
->assertSame('id', $style_plugin->active, 'Ensure the right order was set.');
$view
->destroy();
}
$view
->destroy();
$this
->prepareView($view);
$view->field['name']->options['exclude'] = TRUE;
$output = $view
->preview();
$output = \Drupal::service('renderer')
->renderRoot($output);
$this
->assertStringNotContainsString('views-field-name', $output, "Excluded field's wrapper was not rendered.");
$view
->destroy();
$this
->executeView($view);
$output = $view
->preview();
$output = \Drupal::service('renderer')
->renderRoot($output);
$this
->assertStringNotContainsString('custom text', $output, 'Empty handler was not rendered on a non empty table.');
$view
->setDisplay('default');
$view->executed = TRUE;
$view->result = [];
$output = $view
->preview();
$output = \Drupal::service('renderer')
->renderRoot($output);
$this
->assertStringContainsString('custom text', $output, 'Empty handler got rendered on an empty table.');
}
protected function prepareView(ViewExecutable $view) {
$view
->setDisplay();
$view
->initStyle();
$view
->initHandlers();
$view
->initQuery();
}
}