Version 1
[yaffs-website] / web / core / modules / views / tests / src / Kernel / Plugin / PluginBaseTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\views\Tests\Plugin\PluginBaseTest.
6  */
7
8 namespace Drupal\Tests\views\Kernel\Plugin;
9
10 use Drupal\Core\Render\RenderContext;
11 use Drupal\Core\Render\Markup;
12 use Drupal\KernelTests\KernelTestBase;
13 use Drupal\views\Plugin\views\PluginBase;
14
15 /**
16  * Tests the PluginBase class.
17  *
18  * @group views
19  */
20 class PluginBaseTest extends KernelTestBase {
21
22   /**
23    * @var TestPluginBase
24    */
25   protected $testPluginBase;
26
27   protected function setUp() {
28     parent::setUp();
29     $this->testPluginBase = new TestPluginBase();
30   }
31
32   /**
33    * Test that the token replacement in views works correctly.
34    */
35   public function testViewsTokenReplace() {
36     $text = '{{ langcode__value }} means {{ langcode }}';
37     $tokens = ['{{ langcode }}' => Markup::create('English'), '{{ langcode__value }}' => 'en'];
38
39     $result = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($text, $tokens) {
40       return $this->testPluginBase->viewsTokenReplace($text, $tokens);
41     });
42
43     $this->assertIdentical($result, 'en means English');
44   }
45
46   /**
47    * Test that the token replacement in views works correctly with dots.
48    */
49   public function testViewsTokenReplaceWithDots() {
50     $text = '{{ argument.first }} comes before {{ argument.second }}';
51     $tokens = ['{{ argument.first }}' => 'first', '{{ argument.second }}' => 'second'];
52
53     $result = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($text, $tokens) {
54       return $this->testPluginBase->viewsTokenReplace($text, $tokens);
55     });
56
57     $this->assertIdentical($result, 'first comes before second');
58
59     // Test tokens with numeric indexes.
60     $text = '{{ argument.0.first }} comes before {{ argument.1.second }}';
61     $tokens = ['{{ argument.0.first }}' => 'first', '{{ argument.1.second }}' => 'second'];
62
63     $result = \Drupal::service('renderer')->executeInRenderContext(new RenderContext(), function () use ($text, $tokens) {
64       return $this->testPluginBase->viewsTokenReplace($text, $tokens);
65     });
66
67     $this->assertIdentical($result, 'first comes before second');
68   }
69
70   /**
71    * Tests viewsTokenReplace without any twig tokens.
72    */
73   public function testViewsTokenReplaceWithTwigTokens() {
74     $text = 'Just some text';
75     $tokens = [];
76     $result = $this->testPluginBase->viewsTokenReplace($text, $tokens);
77     $this->assertIdentical($result, 'Just some text');
78   }
79
80 }
81
82 /**
83  * Helper class for using the PluginBase abstract class.
84  */
85 class TestPluginBase extends PluginBase {
86
87   public function __construct() {
88     parent::__construct([], '', []);
89   }
90
91   public function viewsTokenReplace($text, $tokens) {
92     return parent::viewsTokenReplace($text, $tokens);
93   }
94
95 }