function CToolsViewsBasicViewBlockTest::testOrderFields

Same name and namespace in other branches
  1. 4.0.x modules/ctools_views/tests/src/Functional/CToolsViewsBasicViewBlockTest.php \Drupal\Tests\ctools_views\Functional\CToolsViewsBasicViewBlockTest::testOrderFields()

Test ctools_views 'sort_fields' configuration.

File

modules/ctools_views/tests/src/Functional/CToolsViewsBasicViewBlockTest.php, line 272

Class

CToolsViewsBasicViewBlockTest
Tests ctools_views block display plugin overrides settings from a basic View.

Namespace

Drupal\Tests\ctools_views\Functional

Code

public function testOrderFields() {
    $default_theme = $this->config('system.theme')
        ->get('default');
    // Get the "Configure block" form for our Views block.
    $this->drupalGet('admin/structure/block/add/views_block:ctools_views_test_view-block_fields/' . $default_theme);
    $this->assertSession()
        ->fieldValueEquals('edit-settings-override-order-fields-id-weight', 0);
    // Add block to sidebar_first region with default settings.
    $edit = [];
    $edit['region'] = 'sidebar_first';
    $edit['id'] = 'views_block__ctools_views_test_view_block_fields';
    $this->drupalGet('admin/structure/block/add/views_block:ctools_views_test_view-block_fields/' . $default_theme);
    $this->submitForm($edit, 'Save block');
    // Assert sort_fields default settings.
    $this->drupalGet('<front>');
    // Check that the td with class "views-field-id" is the first td in the
    // first tr element.
    $this->assertEquals(0, count($this->xpath('//div[contains(@class, "view-display-id-block_fields")]//table//tr[1]//td[contains(@class, "views-field-id")]/preceding-sibling::td')));
    // Override sort_fields settings.
    $edit = [];
    $edit['region'] = 'sidebar_first';
    $edit['settings[override][order_fields][name][weight]'] = -50;
    $edit['settings[override][order_fields][age][weight]'] = -49;
    $edit['settings[override][order_fields][job][weight]'] = -48;
    $edit['settings[override][order_fields][created][weight]'] = -47;
    $edit['settings[override][order_fields][id][weight]'] = -46;
    $edit['settings[override][order_fields][name_1][weight]'] = -45;
    $this->drupalGet('admin/structure/block/manage/views_block__ctools_views_test_view_block_fields');
    $this->submitForm($edit, 'Save block');
    $block = $this->storage
        ->load('views_block__ctools_views_test_view_block_fields');
    $config = $block->getPlugin()
        ->getConfiguration();
    $this->assertEquals(-46, $config['fields']['id']['weight'], "'sort_fields' setting is properly saved.");
    $this->assertEquals(-50, $config['fields']['name']['weight'], "'sort_fields' setting is properly saved.");
    // Assert sort_fields overridden settings.
    $this->drupalGet('<front>');
    // Check that the td with class "views-field-id" is the 5th td in the first
    // tr element.
    $this->assertEquals(4, count($this->xpath('//div[contains(@class, "view-display-id-block_fields")]//table//tr[1]//td[contains(@class, "views-field-id")]/preceding-sibling::td')));
    // Check that duplicate fields in the View produce expected output.
    $name1_element = $this->xpath('//div[contains(@class, "view-display-id-block_fields")]//table//tr[1]/td[contains(@class, "views-field-name")]');
    $name1 = $name1_element[0]->getText();
    $this->assertEquals('John', trim($name1));
    $name2_element = $this->xpath('//div[contains(@class, "view-display-id-block_fields")]//table//tr[1]/td[contains(@class, "views-field-name-1")]');
    $name2 = $name2_element[0]->getText();
    $this->assertEquals('John', trim($name2));
}