View source
<?php
namespace Drupal\Tests\charts\FunctionalJavascript;
use Drupal\charts_test\Form\DataCollectorTableTestForm;
use Drupal\Core\Url;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class DataCollectorTableTest extends WebDriverTestBase {
protected $defaultTheme = 'stable';
public static $modules = [
'charts',
'charts_test',
];
const TABLE_SELECTOR = 'table[data-drupal-selector="edit-series-data-collector-table"]';
const TABLE_ROW_SELECTOR = 'table[data-drupal-selector="edit-series-data-collector-table"] tr.data-collector-table--row';
const TABLE_COLUMN_SELECTOR = 'table[data-drupal-selector="edit-series-data-collector-table"] tbody tr:nth-child(1) td:not(.data-collector-table--row--delete)';
public function testDataCollectorTable() {
$this
->drupalGet('/charts_test/data_collector_table_test_form');
$table_id_selector = static::TABLE_SELECTOR;
$rows = $this
->cssSelect(static::TABLE_ROW_SELECTOR);
$this
->assertTrue(count($rows) === DataCollectorTableTestForm::INITIAL_ROWS, 'Expected rows were found.');
$columns = $this
->cssSelect(static::TABLE_COLUMN_SELECTOR);
$this
->assertTrue(count($columns) === DataCollectorTableTestForm::INITIAL_COLUMNS, 'Expected columns were found.');
$cell_input_selector = $table_id_selector . ' tr.data-collector-table--row td > .data-collector-table--row--cell .form-text';
$cell_inputs = $this
->cssSelect($cell_input_selector);
$this
->assertNotEmpty($cell_inputs, t('@count inputs were found', [
'@count' => count($cell_inputs),
]));
$this
->fillInputs($cell_inputs);
$this
->doTableOperation('add', 'row');
$this
->assertNewCellIsEmpty('row');
$this
->doTableOperation('add', 'column');
$this
->assertNewCellIsEmpty('column');
$this
->doTableOperation('delete', 'row', [
2,
3,
]);
$this
->doTableOperation('delete', 'column', [
2,
]);
}
protected function doTableOperation($operation, $on, array $positions = []) {
$value = ucfirst($operation) . ' ' . $on;
if ($operation === 'add') {
$selector = static::TABLE_SELECTOR . ' input[value="' . $value . '"]';
$this
->pressAjaxButton($selector);
if ($on === 'row') {
$this
->assertRowsIncreased();
}
else {
$this
->assertColumnsIncreased();
}
}
else {
$on_row = $on === 'row';
if ($on_row) {
$counter = DataCollectorTableTestForm::INITIAL_ROWS + 1;
$locator = static::TABLE_ROW_SELECTOR;
}
else {
$counter = DataCollectorTableTestForm::INITIAL_COLUMNS + 1;
$locator = static::TABLE_COLUMN_SELECTOR;
}
foreach ($positions as $position) {
if ($on_row) {
$button_selector = static::TABLE_ROW_SELECTOR . ':nth-child(' . $position . ') .data-collector-table--row--delete input[value="Delete row"]';
}
else {
$button_selector = static::TABLE_SELECTOR . ' .data-collector-table--column-deletes-row';
$button_selector .= ' .data-collector-table--column--delete:nth-child(' . $position . ') input[value="Delete column"]';
}
$this
->pressAjaxButton($button_selector);
$this
->assertDeletionOperation($counter, $locator);
}
}
}
protected function assertRowsIncreased() {
$page = $this
->getSession()
->getPage();
$this
->assertTrue($page
->waitFor(10, function ($page) {
$expected_rows = DataCollectorTableTestForm::INITIAL_ROWS + 1;
$rows = $page
->findAll('css', static::TABLE_ROW_SELECTOR);
return count($rows) === $expected_rows;
}), 'Expected rows were increased by one after add row click.');
}
protected function assertColumnsIncreased() {
$page = $this
->getSession()
->getPage();
$this
->assertTrue($page
->waitFor(10, function ($page) {
$expected_rows = DataCollectorTableTestForm::INITIAL_COLUMNS + 1;
$columns = $page
->findAll('css', static::TABLE_COLUMN_SELECTOR);
return count($columns) === $expected_rows;
}), 'Expected columns were increased by one after add column click.');
}
protected function assertNewCellIsEmpty($on) {
$page = $this
->getSession()
->getPage();
if ($on === 'row') {
$counter = DataCollectorTableTestForm::INITIAL_ROWS + 1;
$selector = static::TABLE_ROW_SELECTOR . ':nth-child(' . $counter . ') td:first-child input';
}
else {
$counter = DataCollectorTableTestForm::INITIAL_COLUMNS + 1;
$selector = static::TABLE_COLUMN_SELECTOR . ':nth-child(' . $counter . ') input';
}
$cell_input = $page
->find('css', $selector);
$this
->assertEmpty($cell_input
->getValue(), 'Added row cells are empty.');
}
protected function assertDeletionOperation(&$current_count, $locator) {
$web_assert = $this
->assertSession();
$web_assert
->assertWaitOnAjaxRequest();
$current_count--;
$page = $this
->getSession()
->getPage();
$targets = $page
->findAll('css', $locator);
$this
->assertTrue(count($targets) === $current_count);
}
protected function pressAjaxButton($selector) {
$button = $this
->getSession()
->getPage()
->find('css', $selector);
$this
->getSession()
->executeScript("jQuery('" . $selector . "').trigger('mouseleave')");
$button
->mouseOver();
$button
->press();
}
protected function fillInputs(array $inputs) {
foreach ($inputs as $input) {
$value = rand(0, count($inputs));
$input
->setValue($value);
}
return $inputs;
}
protected function getResourcesUrl() {
$resources_path = drupal_get_path('module', 'charts') . '/tests/resources';
return Url::fromUri('internal:/' . $resources_path)
->toString();
}
}