diff --git a/core/modules/system/tests/modules/tabledrag_test/js/catchErrors.js b/core/modules/system/tests/modules/tabledrag_test/js/catchErrors.js new file mode 100644 index 0000000..aa33001 --- /dev/null +++ b/core/modules/system/tests/modules/tabledrag_test/js/catchErrors.js @@ -0,0 +1,22 @@ +(function () { + 'use strict'; + + window.errors = []; + + function catchError(error) { + if (typeof(error) !== 'object') { + return; + } + + window.errors.push(error); + } + + window.onerror = function (message, url, line, column) { + catchError({ + url: url, + line: line, + column: column, + message: message + }); + }; +})(); diff --git a/core/modules/system/tests/modules/tabledrag_test/src/Form/TableDragTestForm.php b/core/modules/system/tests/modules/tabledrag_test/src/Form/TableDragTestForm.php new file mode 100644 index 0000000..2d75d07 --- /dev/null +++ b/core/modules/system/tests/modules/tabledrag_test/src/Form/TableDragTestForm.php @@ -0,0 +1,100 @@ + 'fieldset', + '#title' => $this->t('Content of "@tab" item', ['@tab' => $tab]), + ]; + + $form[$tab]['table'] = [ + '#type' => 'table', + '#sticky' => FALSE, + '#header' => [ + $this->t('Column 1'), + $this->t('Column 2'), + $this->t('Weight'), + ], + '#attributes' => [ + 'id' => "table_tab$tab", + ], + ]; + + for ($i = 0; $i < 3; $i++) { + $row = []; + + $row['column1'] = [ + '#type' => 'textfield', + ]; + + $row['column2'] = [ + '#type' => 'textfield', + ]; + + $row['weight'] = [ + '#type' => 'weight', + '#attributes' => ['class' => [static::GROUP]], + ]; + + $form[$tab]['table'][] = $row; + } + + $form[$tab]['table']['custom-row'] = []; + + $form[$tab]['table']['custom-row']['button'] = [ + '#type' => 'button', + '#value' => t('Add new item'), + ]; + + if ($add_colspan) { + $form[$tab]['table']['custom-row']['button']['#wrapper_attributes'] = [ + 'colspan' => count($form[$tab]['table']['#header']), + ]; + } + + drupal_attach_tabledrag($form[$tab]['table'], [ + 'group' => static::GROUP, + 'action' => 'order', + 'table_id' => $form[$tab]['table']['#attributes']['id'], + 'relationship' => 'sibling', + ]); + } + + // Add the library for collecting JS errors in "window.errors" variable. + $form['#attached']['library'][] = 'tabledrag_test/base'; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + + } + +} diff --git a/core/modules/system/tests/modules/tabledrag_test/tabledrag_test.info.yml b/core/modules/system/tests/modules/tabledrag_test/tabledrag_test.info.yml new file mode 100644 index 0000000..e975206 --- /dev/null +++ b/core/modules/system/tests/modules/tabledrag_test/tabledrag_test.info.yml @@ -0,0 +1,5 @@ +name: TableDrag test +description: Testing tabledrag +package: Testing +type: module +core: 8.x diff --git a/core/modules/system/tests/modules/tabledrag_test/tabledrag_test.libraries.yml b/core/modules/system/tests/modules/tabledrag_test/tabledrag_test.libraries.yml new file mode 100644 index 0000000..f123da7 --- /dev/null +++ b/core/modules/system/tests/modules/tabledrag_test/tabledrag_test.libraries.yml @@ -0,0 +1,6 @@ +base: + js: + js/catchErrors.js: + # Set the weight more than in "JS_SETTING" constant, which is "200". + # This script should be as high as possible to catch all JS errors. + weight: -999 diff --git a/core/modules/system/tests/modules/tabledrag_test/tabledrag_test.routing.yml b/core/modules/system/tests/modules/tabledrag_test/tabledrag_test.routing.yml new file mode 100644 index 0000000..0195201 --- /dev/null +++ b/core/modules/system/tests/modules/tabledrag_test/tabledrag_test.routing.yml @@ -0,0 +1,9 @@ +tabledrag_test.tabledrag_test_form: + path: /test/tabledrag/{type} + defaults: + _title: TableDrag test + _form: \Drupal\tabledrag_test\Form\TableDragTestForm + # "with-colspan" is also available. + type: without-colspan + requirements: + _access: 'TRUE' diff --git a/core/modules/system/tests/modules/tabledrag_test/tests/src/FunctionalJavascript/TableDragTest.php b/core/modules/system/tests/modules/tabledrag_test/tests/src/FunctionalJavascript/TableDragTest.php new file mode 100644 index 0000000..da43eb1 --- /dev/null +++ b/core/modules/system/tests/modules/tabledrag_test/tests/src/FunctionalJavascript/TableDragTest.php @@ -0,0 +1,54 @@ + NULL, 'with-colspan' => 2]) { + foreach ($data as $type => $expected_colspan) { + $this->drupalGet("test/tabledrag/$type"); + $this->assertEmpty($this->getJsErrors(), 'No JS errors on the page.'); + + for ($tab = 1; $tab <= 3; $tab++) { + $selector = "#table_tab$tab > tbody > tr:nth-child(4) > td"; + $elements = $this->cssSelect($selector); + + if (empty($elements)) { + $this->fail(sprintf('Element not found using "%s" CSS selector.', $selector)); + } + + $this->assertEquals($expected_colspan, $elements[0]->getAttribute('colspan')); + } + } + } + + /** + * Check saving a state in localStorage and "colspan" calculation. + */ + public function testTableDragCustomRowWithShowingWeights() { + // Visit the page. + $this->drupalGet('test/tabledrag/with-colspan'); + // Click on "Show row weights". + $this->pressTableDragSwitch(t('Show row weights')); + // Reload the page. + $this->drupalGet('test/tabledrag/with-colspan'); + // Ensure that state has been saved in localStorage. + $this->assertSession()->buttonExists(t('Hide row weights')); + // Check correctness of the "colspan" attribute. + $this->testTableDragCustomRow(['with-colspan' => 3]); + } + +} diff --git a/core/modules/system/tests/modules/tabledrag_test/tests/src/FunctionalJavascript/TableDragTestBase.php b/core/modules/system/tests/modules/tabledrag_test/tests/src/FunctionalJavascript/TableDragTestBase.php new file mode 100644 index 0000000..fc38299 --- /dev/null +++ b/core/modules/system/tests/modules/tabledrag_test/tests/src/FunctionalJavascript/TableDragTestBase.php @@ -0,0 +1,63 @@ +getSession() + ->getDriver() + ->find("//button[contains(@class, 'tabledrag-toggle-weight')][text() = '$text']"); + + if (empty($buttons[$index])) { + $this->fail(sprintf('TableDrag switch #%s is not exists on the page.', $index)); + } + + $buttons[$index]->press(); + } + + /** + * Returns listing of JS errors on the page. + * + * @return array[] + * An array of associative arrays with the following list of items: + * - url + * - line + * - column + * - message + */ + protected function getJsErrors() { + // The "window.errors" is of array type in JS and will be converted + // to array in PHP as well. + return $this + ->getSession() + ->evaluateScript('return window.errors'); + } + +}