View source
<?php
namespace Drupal\Tests\views\Functional\Plugin;
use Drupal\Core\Database\Database;
use Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Entity\View;
class StyleTableTest extends ViewTestBase {
public static $testViews = [
'test_table',
];
protected $defaultTheme = 'stark';
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this
->enableViewsTestModule();
}
public function testAccessibilitySettings() {
$this
->drupalGet('test-table');
$result = $this
->xpath('//caption/child::text()');
$this
->assertNotEmpty($result, 'The caption appears on the table.');
$this
->assertEqual(trim($result[0]
->getText()), 'caption-text');
$result = $this
->xpath('//summary/child::text()');
$this
->assertNotEmpty($result, 'The summary appears on the table.');
$this
->assertEqual(trim($result[0]
->getText()), 'summary-text');
$summary = $this
->xpath('//summary')[0];
$this
->assertTrue($summary
->hasAttribute('role'));
$this
->assertTrue($summary
->hasAttribute('aria-expanded'));
$result = $this
->xpath('//caption/details/child::text()[normalize-space()]');
$this
->assertNotEmpty($result, 'The table description appears on the table.');
$this
->assertEqual(trim($result[0]
->getText()), 'description-text');
$view = View::load('test_table');
$display =& $view
->getDisplay('default');
$display['display_options']['style']['options']['caption'] = '';
$view
->save();
$this
->drupalGet('test-table');
$result = $this
->xpath('//caption/child::text()');
$this
->assertEmpty(trim($result[0]
->getText()), 'Ensure that the caption disappears.');
$display =& $view
->getDisplay('default');
$display['display_options']['style']['options']['summary'] = '';
$view
->save();
$this
->drupalGet('test-table');
$result = $this
->xpath('//summary/child::text()');
$this
->assertEmpty($result, 'Ensure that the summary disappears.');
$display =& $view
->getDisplay('default');
$display['display_options']['style']['options']['description'] = '';
$view
->save();
$this
->drupalGet('test-table');
$result = $this
->xpath('//caption/details/child::text()[normalize-space()]');
$this
->assertEmpty($result, 'Ensure that the description disappears.');
}
public function testFieldInColumns() {
$this
->drupalGet('test-table');
$result = $this
->xpath('//tbody/tr/td[contains(concat(" ", @class, " "), " views-field-job ")]');
$this
->assertGreaterThan(0, count($result), '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
->assertGreaterThan(0, count($result), 'Ensure there is a td with the class views-field-job-1');
$view = View::load('test_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('test-table');
$result = $this
->xpath('//tbody/tr/td[contains(concat(" ", @class, " "), " views-field-job views-field-job-1 ")]');
$this
->assertGreaterThan(0, count($result), 'Ensure that the job column class names are joined into a single column');
$result = $this
->xpath('//tbody/tr/td[contains(., "Drummer, Drummer")]');
$this
->assertGreaterThan(0, count($result), 'Ensure the job column values are joined into a single column');
}
public function testNumericFieldVisible() {
$data_set = $this
->dataSet();
$query = Database::getConnection()
->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('test-table');
$result = $this
->xpath('//tbody/tr/td[contains(., "Baby")]');
$this
->assertGreaterThan(0, count($result), 'Ensure that the baby is found.');
$result = $this
->xpath('//tbody/tr/td[text()=0]');
$this
->assertGreaterThan(0, count($result), 'Ensure that the baby\'s age is shown');
}
public function testEmptyColumn() {
\Drupal::database()
->update('views_test_data')
->fields([
'job' => '',
])
->execute();
$this
->drupalGet('test-table');
$result = $this
->xpath('//thead/tr/th/a[text()="Job"]');
$this
->assertCount(1, $result, 'Ensure that empty column header is hidden.');
$result = $this
->xpath('//tbody/tr/td[contains(concat(" ", @class, " "), " views-field-job-1 ")]');
$this
->assertCount(0, $result, 'Ensure the empty table cells are hidden.');
}
public function testGrouping() {
$view = \Drupal::entityTypeManager()
->getStorage('view')
->load('test_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('test-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.");
$this
->assertEmpty($this
->xpath('//caption/details'));
foreach ($expected_captions as $raw_caption) {
$this
->assertEscaped($raw_caption);
}
$display =& $view
->getDisplay('default');
$display['display_options']['fields']['job']['label'] = '';
$view
->save();
$this
->drupalGet('test-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 testTableCacheability() {
\Drupal::service('module_installer')
->uninstall([
'page_cache',
]);
$url = 'test-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));
}
}