class ViewsDataHelperTest

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

@coversDefaultClass \Drupal\views\ViewsDataHelper @group views

Hierarchy

Expanded class hierarchy of ViewsDataHelperTest

File

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

Namespace

Drupal\Tests\views\Unit
View source
class ViewsDataHelperTest extends UnitTestCase {
    
    /**
     * Returns the views data definition.
     *
     * @return array
     */
    protected function viewsData() {
        $data = ViewTestData::viewsData();
        // Tweak the views data to have a base for testing
        // \Drupal\views\ViewsDataHelper::fetchFields().
        unset($data['views_test_data']['id']['field']);
        unset($data['views_test_data']['name']['argument']);
        unset($data['views_test_data']['age']['filter']);
        unset($data['views_test_data']['job']['sort']);
        $data['views_test_data']['created']['area']['id'] = 'text';
        $data['views_test_data']['age']['area']['id'] = 'text';
        $data['views_test_data']['age']['area']['sub_type'] = 'header';
        $data['views_test_data']['job']['area']['id'] = 'text';
        $data['views_test_data']['job']['area']['sub_type'] = [
            'header',
            'footer',
        ];
        return $data;
    }
    
    /**
     * Tests fetchFields.
     */
    public function testFetchFields() : void {
        $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.");
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 355
UnitTestCase::setUpBeforeClass public static function
ViewsDataHelperTest::testFetchFields public function Tests fetchFields.
ViewsDataHelperTest::viewsData protected function Returns the views data definition.

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