class DisplayVariantTest

Same name in this branch
  1. 11.x core/modules/system/tests/src/Functional/Render/DisplayVariantTest.php \Drupal\Tests\system\Functional\Render\DisplayVariantTest
Same name in other branches
  1. 9 core/modules/system/tests/src/Functional/Render/DisplayVariantTest.php \Drupal\Tests\system\Functional\Render\DisplayVariantTest
  2. 9 core/tests/Drupal/Tests/Core/Display/DisplayVariantTest.php \Drupal\Tests\Core\Display\DisplayVariantTest
  3. 8.9.x core/modules/system/tests/src/Functional/Render/DisplayVariantTest.php \Drupal\Tests\system\Functional\Render\DisplayVariantTest
  4. 8.9.x core/tests/Drupal/Tests/Core/Display/DisplayVariantTest.php \Drupal\Tests\Core\Display\DisplayVariantTest
  5. 10 core/modules/system/tests/src/Functional/Render/DisplayVariantTest.php \Drupal\Tests\system\Functional\Render\DisplayVariantTest
  6. 10 core/tests/Drupal/Tests/Core/Display/DisplayVariantTest.php \Drupal\Tests\Core\Display\DisplayVariantTest

@coversDefaultClass \Drupal\Core\Display\VariantBase @group Display

Hierarchy

Expanded class hierarchy of DisplayVariantTest

File

core/tests/Drupal/Tests/Core/Display/DisplayVariantTest.php, line 14

Namespace

Drupal\Tests\Core\Display
View source
class DisplayVariantTest extends UnitTestCase {
    
    /**
     * Sets up a display variant plugin for testing.
     *
     * @param array $configuration
     *   An array of plugin configuration.
     * @param array $definition
     *   The plugin definition array.
     *
     * @return \Drupal\Core\Display\VariantBase|\PHPUnit\Framework\MockObject\MockObject
     *   A mocked display variant plugin.
     */
    public function setUpDisplayVariant($configuration = [], $definition = []) {
        return $this->getMockBuilder('Drupal\\Core\\Display\\VariantBase')
            ->setConstructorArgs([
            $configuration,
            'test',
            $definition,
        ])
            ->onlyMethods([
            'build',
        ])
            ->getMock();
    }
    
    /**
     * Tests the label() method.
     *
     * @covers ::label
     */
    public function testLabel() : void {
        $display_variant = $this->setUpDisplayVariant([
            'label' => 'foo',
        ]);
        $this->assertSame('foo', $display_variant->label());
    }
    
    /**
     * Tests the label() method using a default value.
     *
     * @covers ::label
     */
    public function testLabelDefault() : void {
        $display_variant = $this->setUpDisplayVariant();
        $this->assertSame('', $display_variant->label());
    }
    
    /**
     * Tests the getWeight() method.
     *
     * @covers ::getWeight
     */
    public function testGetWeight() : void {
        $display_variant = $this->setUpDisplayVariant([
            'weight' => 5,
        ]);
        $this->assertSame(5, $display_variant->getWeight());
    }
    
    /**
     * Tests the getWeight() method using a default value.
     *
     * @covers ::getWeight
     */
    public function testGetWeightDefault() : void {
        $display_variant = $this->setUpDisplayVariant();
        $this->assertSame(0, $display_variant->getWeight());
    }
    
    /**
     * Tests the getConfiguration() method.
     *
     * @covers ::getConfiguration
     *
     * @dataProvider providerTestGetConfiguration
     */
    public function testGetConfiguration($configuration, $expected) : void {
        $display_variant = $this->setUpDisplayVariant($configuration);
        $this->assertSame($expected, $display_variant->getConfiguration());
    }
    
    /**
     * Provides test data for testGetConfiguration().
     */
    public static function providerTestGetConfiguration() {
        $data = [];
        $data[] = [
            [],
            [
                'id' => 'test',
                'label' => '',
                'uuid' => '',
                'weight' => 0,
            ],
        ];
        $data[] = [
            [
                'label' => 'Test',
            ],
            [
                'id' => 'test',
                'label' => 'Test',
                'uuid' => '',
                'weight' => 0,
            ],
        ];
        $data[] = [
            [
                'id' => 'foo',
            ],
            [
                'id' => 'test',
                'label' => '',
                'uuid' => '',
                'weight' => 0,
            ],
        ];
        return $data;
    }
    
    /**
     * Tests the access() method.
     *
     * @covers ::access
     */
    public function testAccess() : void {
        $display_variant = $this->setUpDisplayVariant();
        $this->assertTrue($display_variant->access());
    }
    
    /**
     * Tests the submitConfigurationForm() method.
     *
     * @covers ::submitConfigurationForm
     */
    public function testSubmitConfigurationForm() : void {
        $display_variant = $this->setUpDisplayVariant();
        $this->assertSame('', $display_variant->label());
        $form = [];
        $label = $this->randomMachineName();
        $form_state = new FormState();
        $form_state->setValue('label', $label);
        $display_variant->submitConfigurationForm($form, $form_state);
        $this->assertSame($label, $display_variant->label());
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
DisplayVariantTest::providerTestGetConfiguration public static function Provides test data for testGetConfiguration().
DisplayVariantTest::setUpDisplayVariant public function Sets up a display variant plugin for testing.
DisplayVariantTest::testAccess public function Tests the access() method.
DisplayVariantTest::testGetConfiguration public function Tests the getConfiguration() method.
DisplayVariantTest::testGetWeight public function Tests the getWeight() method.
DisplayVariantTest::testGetWeightDefault public function Tests the getWeight() method using a default value.
DisplayVariantTest::testLabel public function Tests the label() method.
DisplayVariantTest::testLabelDefault public function Tests the label() method using a default value.
DisplayVariantTest::testSubmitConfigurationForm public function Tests the submitConfigurationForm() method.
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 367
UnitTestCase::setUpBeforeClass public static function

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