function ViewsDataHelperTest::testFetchFields

Same name in other branches
  1. 8.9.x core/modules/views/tests/src/Unit/ViewsDataHelperTest.php \Drupal\Tests\views\Unit\ViewsDataHelperTest::testFetchFields()
  2. 10 core/modules/views/tests/src/Unit/ViewsDataHelperTest.php \Drupal\Tests\views\Unit\ViewsDataHelperTest::testFetchFields()
  3. 11.x core/modules/views/tests/src/Unit/ViewsDataHelperTest.php \Drupal\Tests\views\Unit\ViewsDataHelperTest::testFetchFields()

Tests fetchFields.

File

core/modules/views/tests/src/Unit/ViewsDataHelperTest.php, line 41

Class

ViewsDataHelperTest
@coversDefaultClass \Drupal\views\ViewsDataHelper @group views

Namespace

Drupal\Tests\views\Unit

Code

public function testFetchFields() {
    $views_data = $this->getMockBuilder('Drupal\\views\\ViewsData')
        ->disableOriginalConstructor()
        ->getMock();
    $views_data->expects($this->once())
        ->method('getAll')
        ->willReturn($this->viewsData());
    $data_helper = new ViewsDataHelper($views_data);
    $expected = [
        'field' => [
            'age',
            'created',
            'job',
            'name',
            'status',
        ],
        'argument' => [
            'age',
            'created',
            'id',
            'job',
        ],
        'filter' => [
            'created',
            'id',
            'job',
            'name',
            'status',
        ],
        'sort' => [
            'age',
            'created',
            'id',
            'name',
            'status',
        ],
        'area' => [
            'age',
            'created',
            'job',
        ],
        'header' => [
            'age',
            'created',
            'job',
        ],
        'footer' => [
            'age',
            'created',
            'job',
        ],
    ];
    $handler_types = [
        'field',
        'argument',
        'filter',
        'sort',
        'area',
    ];
    foreach ($handler_types as $handler_type) {
        $fields = $data_helper->fetchFields('views_test_data', $handler_type);
        $expected_keys = $expected[$handler_type];
        array_walk($expected_keys, function (&$item) {
            $item = "views_test_data.{$item}";
        });
        $this->assertEquals($expected_keys, array_keys($fields), "Handlers of type {$handler_type} are not listed as expected");
    }
    // Check for subtype filtering, so header and footer.
    foreach ([
        'header',
        'footer',
    ] as $sub_type) {
        $fields = $data_helper->fetchFields('views_test_data', 'area', FALSE, $sub_type);
        $expected_keys = $expected[$sub_type];
        array_walk($expected_keys, function (&$item) {
            $item = "views_test_data.{$item}";
        });
        $this->assertEquals($expected_keys, array_keys($fields), "Sub_type {$sub_type} is not filtered as expected.");
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.