View source
<?php
namespace Drupal\Tests\views_aggregator\Functional\Plugin;
use Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Entity\View;
class ViewsAggregatorStyleTableTest extends ViewTestBase {
public static $testViews = [
'va_test_style_table',
];
public static $modules = [
'views',
'views_aggregator',
'views_aggregator_test_config',
];
protected $defaultTheme = 'stark';
protected $strictConfigSchema = FALSE;
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this
->enableViewsTestModule();
ViewTestData::createTestViews(get_class($this), [
'views_aggregator_test_config',
]);
}
public function testAccessibilitySettings() {
$this
->drupalGet('va-test-style-table');
$result = $this
->xpath('//caption/child::text()');
$this
->assertTrue(count($result) > 0, 'The caption appears on the table.');
$this
->assertEquals('caption-text', trim($result[0]
->getText()));
$result = $this
->xpath('//summary/child::text()');
$this
->assertTrue(count($result) > 0, 'The summary appears on the table.');
$this
->assertEquals('summary-text', trim($result[0]
->getText()));
$view = View::load('va_test_style_table');
$display =& $view
->getDisplay('default');
$display['display_options']['style']['options']['caption'] = '';
$view
->save();
$this
->drupalGet('va-test-style-table');
$result = $this
->xpath('//caption/child::text()');
$this
->assertEquals('', trim($result[0]
->getText()), 'Ensure that the caption disappears.');
$display =& $view
->getDisplay('default');
$display['display_options']['style']['options']['summary'] = '';
$view
->save();
$this
->drupalGet('va-test-style-table');
$result = $this
->xpath('//summary/child::text()');
$this
->assertEquals(0, count($result), 'Ensure that the summary disappears.');
}
public function testFieldInColumns() {
$this
->drupalGet('va-test-style-table');
$result = $this
->xpath('//tbody/tr/td[contains(concat(" ", @class, " "), " views-field-job ")]');
$this
->assertTrue(count($result) > 0, 'Ensure there is a td with the class views-field-job');
$result = $this
->xpath('//tbody/tr/td[contains(concat(" ", @class, " "), " views-field-job-1 ")]');
$this
->assertTrue(count($result) > 0, 'Ensure there is a td with the class views-field-job-1');
$view = View::load('va_test_style_table');
$display =& $view
->getDisplay('default');
$display['display_options']['style']['options']['columns']['job_1'] = 'job';
$display['display_options']['style']['options']['info']['job']['separator'] = ', ';
$view
->save();
$this
->drupalGet('va-test-style-table');
$result = $this
->xpath('//tbody/tr/td[contains(concat(" ", @class, " "), " views-field-job views-field-job-1 ")]');
$this
->assertTrue(count($result) > 0, 'Ensure that the job column class names are joined into a single column');
$result = $this
->xpath('//tbody/tr/td[contains(., "Drummer, Drummer")]');
$this
->assertTrue(count($result) > 0, 'Ensure the job column values are joined into a single column');
}
public function testNumericFieldVisible() {
$data_set = $this
->dataSet();
$connection = \Drupal::database();
$query = $connection
->insert('views_test_data')
->fields(array_keys($data_set[0]));
$query
->values([
'name' => 'James McCartney',
'age' => 0,
'job' => 'Baby',
'created' => gmmktime(6, 30, 10, 1, 1, 2000),
'status' => 1,
]);
$query
->execute();
$this
->drupalGet('va-test-style-table');
$result = $this
->xpath('//tbody/tr/td[contains(., "Baby")]');
$this
->assertTrue(count($result) > 0, 'Ensure that the baby is found.');
$result = $this
->xpath('//tbody/tr/td[text()=0]');
$this
->assertTrue(count($result) > 0, 'Ensure that the baby\'s age is shown');
}
public function testEmptyColumn() {
\Drupal::database()
->update('views_test_data')
->fields([
'job' => '',
])
->execute();
$this
->drupalGet('va-test-style-table');
$result = $this
->xpath('//thead/tr/th/a[text()="Job"]');
$this
->assertEquals(1, count($result), 'Ensure that empty column header is hidden.');
$result = $this
->xpath('//tbody/tr/td[contains(concat(" ", @class, " "), " views-field-job-1 ")]');
$this
->assertEquals(0, count($result), 'Ensure the empty table cells are hidden.');
}
public function testGrouping() {
$view = \Drupal::entityTypeManager()
->getStorage('view')
->load('va_test_style_table');
$display =& $view
->getDisplay('default');
$display['display_options']['style']['options']['grouping'][0] = [
'field' => 'job',
'rendered' => TRUE,
'rendered_strip' => FALSE,
];
$display['display_options']['style']['options']['caption'] = '';
$display['display_options']['style']['options']['summary'] = '';
$display['display_options']['style']['options']['description'] = '';
$view
->save();
$unsafe_markup = '<script>alert("Rapper");</script>';
$unsafe_markup_data = [
'name' => 'Marshall',
'age' => 42,
'job' => $unsafe_markup,
'created' => gmmktime(0, 0, 0, 2, 15, 2001),
'status' => 1,
];
$database = $this->container
->get('database');
$database
->insert('views_test_data')
->fields(array_keys($unsafe_markup_data))
->values($unsafe_markup_data)
->execute();
$this
->drupalGet('va-test-style-table');
$expected_captions = [
'Job: Speaker',
'Job: Songwriter',
'Job: Drummer',
'Job: Singer',
'Job: ' . $unsafe_markup,
];
$this
->assertNoRaw($unsafe_markup, "Didn't find caption containing unsafe markup.");
foreach ($expected_captions as $raw_caption) {
$this
->assertEscaped($raw_caption);
}
$display =& $view
->getDisplay('default');
$display['display_options']['fields']['job']['label'] = '';
$view
->save();
$this
->drupalGet('va-test-style-table');
$expected_captions = [
'Speaker',
'Songwriter',
'Drummer',
'Singer',
$unsafe_markup,
];
$this
->assertNoRaw($unsafe_markup, "Didn't find caption containing unsafe markup.");
foreach ($expected_captions as $raw_caption) {
$this
->assertEscaped($raw_caption);
}
}
public function testViewsAggregatorTableCacheability() {
\Drupal::service('module_installer')
->uninstall([
'page_cache',
]);
$url = 'va-test-style-table';
$this
->drupalGet($url);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertEquals('MISS', $this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER));
$this
->drupalGet($url);
$this
->assertEquals('HIT', $this
->drupalGetHeader(DynamicPageCacheSubscriber::HEADER));
}
}