3 namespace Drupal\Tests\views_ui\Functional;
5 use Drupal\views\Views;
8 * Tests the reordering of fields via AJAX.
11 * @see \Drupal\views_ui\Form\Ajax\Rearrange
13 class RearrangeFieldsTest extends UITestBase {
16 * Views used by this test.
20 public static $testViews = ['test_view'];
23 * Gets the fields from the View.
25 protected function getViewFields($view_name = 'test_view', $display_id = 'default') {
26 $view = Views::getView($view_name);
27 $view->setDisplay($display_id);
29 foreach ($view->displayHandlers->get('default')->getHandlers('field') as $field => $handler) {
36 * Check if the fields are in the correct order.
39 * The name of the view.
41 * Array of field names.
43 protected function assertFieldOrder($view_name, $fields) {
44 $this->drupalGet('admin/structure/views/nojs/rearrange/' . $view_name . '/default/field');
46 foreach ($fields as $idx => $field) {
47 $this->assertFieldById('edit-fields-' . $field . '-weight', $idx + 1);
52 * Tests field sorting.
54 public function testRearrangeFields() {
55 $view_name = 'test_view';
57 // Checks that the order on the rearrange form matches the creation order.
58 $this->assertFieldOrder($view_name, $this->getViewFields($view_name));
60 // Checks that a field is not deleted if a value is not passed back.
62 $this->drupalPostForm('admin/structure/views/nojs/rearrange/' . $view_name . '/default/field', $fields, t('Apply'));
63 $this->assertFieldOrder($view_name, $this->getViewFields($view_name));
65 // Checks that revers the new field order is respected.
66 $reversedFields = array_reverse($this->getViewFields($view_name));
68 foreach ($reversedFields as $delta => $field) {
69 $fields['fields[' . $field . '][weight]'] = $delta;
71 $this->drupalPostForm('admin/structure/views/nojs/rearrange/' . $view_name . '/default/field', $fields, t('Apply'));
72 $this->assertFieldOrder($view_name, $reversedFields);
74 // Checks that there is a remove link for each field.
75 $this->assertEqual(count($this->cssSelect('a.views-remove-link')), count($fields));