PluginBaseTest.php

Same filename in this branch
  1. 11.x core/modules/views/tests/src/Unit/PluginBaseTest.php
  2. 11.x core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php
Same filename in other branches
  1. 9 core/modules/views/tests/src/Unit/PluginBaseTest.php
  2. 9 core/modules/views/tests/src/Kernel/Plugin/PluginBaseTest.php
  3. 9 core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php
  4. 8.9.x core/modules/views/tests/src/Unit/PluginBaseTest.php
  5. 8.9.x core/modules/views/tests/src/Kernel/Plugin/PluginBaseTest.php
  6. 8.9.x core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php
  7. 10 core/modules/views/tests/src/Unit/PluginBaseTest.php
  8. 10 core/modules/views/tests/src/Kernel/Plugin/PluginBaseTest.php
  9. 10 core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php

Namespace

Drupal\Tests\views\Kernel\Plugin

File

core/modules/views/tests/src/Kernel/Plugin/PluginBaseTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\views\Kernel\Plugin;

use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\Markup;
use Drupal\KernelTests\KernelTestBase;
use Drupal\views\Plugin\views\PluginBase;

/**
 * Tests the PluginBase class.
 *
 * @group views
 */
class PluginBaseTest extends KernelTestBase {
    
    /**
     * @var TestPluginBase
     */
    protected $testPluginBase;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->testPluginBase = new TestPluginBase();
    }
    
    /**
     * Tests that the token replacement in views works correctly.
     */
    public function testViewsTokenReplace() : void {
        $text = '{{ langcode__value }} means {{ langcode }}';
        $tokens = [
            '{{ langcode }}' => Markup::create('English'),
            '{{ langcode__value }}' => 'en',
        ];
        $result = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($text, $tokens) {
            return $this->testPluginBase
                ->viewsTokenReplace($text, $tokens);
        });
        $this->assertSame('en means English', $result);
    }
    
    /**
     * Tests that the token replacement in views works correctly with dots.
     */
    public function testViewsTokenReplaceWithDots() : void {
        $text = '{{ argument.first }} comes before {{ argument.second }}';
        $tokens = [
            '{{ argument.first }}' => 'first',
            '{{ argument.second }}' => 'second',
        ];
        $result = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($text, $tokens) {
            return $this->testPluginBase
                ->viewsTokenReplace($text, $tokens);
        });
        $this->assertSame('first comes before second', $result);
        // Test tokens with numeric indexes.
        $text = '{{ argument.0.first }} comes before {{ argument.1.second }}';
        $tokens = [
            '{{ argument.0.first }}' => 'first',
            '{{ argument.1.second }}' => 'second',
        ];
        $result = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($text, $tokens) {
            return $this->testPluginBase
                ->viewsTokenReplace($text, $tokens);
        });
        $this->assertSame('first comes before second', $result);
    }
    
    /**
     * Tests viewsTokenReplace without any twig tokens.
     */
    public function testViewsTokenReplaceWithTwigTokens() : void {
        $text = 'Just some text';
        $tokens = [];
        $result = $this->testPluginBase
            ->viewsTokenReplace($text, $tokens);
        $this->assertSame('Just some text', $result);
    }

}

/**
 * Helper class for using the PluginBase abstract class.
 */
class TestPluginBase extends PluginBase {
    public function __construct() {
        parent::__construct([], '', []);
    }
    public function viewsTokenReplace($text, $tokens) {
        return parent::viewsTokenReplace($text, $tokens);
    }

}

Classes

Title Deprecated Summary
PluginBaseTest Tests the PluginBase class.
TestPluginBase Helper class for using the PluginBase abstract class.

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