Backup of db before drupal security update
[yaffs-website] / web / core / modules / views_ui / src / Tests / RearrangeFieldsTest.php
1 <?php
2
3 namespace Drupal\views_ui\Tests;
4
5 use Drupal\views\Views;
6
7 /**
8  * Tests the reordering of fields via AJAX.
9  *
10  * @group views_ui
11  * @see \Drupal\views_ui\Form\Ajax\Rearrange
12  */
13 class RearrangeFieldsTest extends UITestBase {
14
15   /**
16    * Views used by this test.
17    *
18    * @var array
19    */
20   public static $testViews = ['test_view'];
21
22   /**
23    * Gets the fields from the View.
24    */
25   protected function getViewFields($view_name = 'test_view', $display_id = 'default') {
26     $view = Views::getView($view_name);
27     $view->setDisplay($display_id);
28     $fields = [];
29     foreach ($view->displayHandlers->get('default')->getHandlers('field') as $field => $handler) {
30       $fields[] = $field;
31     }
32     return $fields;
33   }
34
35   /**
36    * Check if the fields are in the correct order.
37    *
38    * @param $view_name
39    *   The name of the view.
40    * @param $fields
41    *   Array of field names.
42    */
43   protected function assertFieldOrder($view_name, $fields) {
44     $this->drupalGet('admin/structure/views/nojs/rearrange/' . $view_name . '/default/field');
45
46     foreach ($fields as $idx => $field) {
47       $this->assertFieldById('edit-fields-' . $field . '-weight', $idx + 1);
48     }
49   }
50
51   /**
52    * Tests field sorting.
53    */
54   public function testRearrangeFields() {
55     $view_name = 'test_view';
56
57     // Checks that the order on the rearrange form matches the creation order.
58     $this->assertFieldOrder($view_name, $this->getViewFields($view_name));
59
60     // Checks that a field is not deleted if a value is not passed back.
61     $fields = [];
62     $this->drupalPostForm('admin/structure/views/nojs/rearrange/' . $view_name . '/default/field', $fields, t('Apply'));
63     $this->assertFieldOrder($view_name, $this->getViewFields($view_name));
64
65     // Checks that revers the new field order is respected.
66     $reversedFields = array_reverse($this->getViewFields($view_name));
67     $fields = [];
68     foreach ($reversedFields as $delta => $field) {
69       $fields['fields[' . $field . '][weight]'] = $delta;
70     }
71     $this->drupalPostForm('admin/structure/views/nojs/rearrange/' . $view_name . '/default/field', $fields, t('Apply'));
72     $this->assertFieldOrder($view_name, $reversedFields);
73
74     // Checks that there is a remove link for each field.
75     $this->assertEqual(count($this->cssSelect('a.views-remove-link')), count($fields));
76   }
77
78 }