Loading core/modules/views/src/Plugin/views/PluginBase.php +1 −1 Original line number Diff line number Diff line Loading @@ -427,7 +427,7 @@ function ($children, $elements) { public function getAvailableGlobalTokens($prepared = FALSE, array $types = []) { $info = \Drupal::token()->getInfo(); // Site and view tokens should always be available. $types += ['site', 'view']; $types = array_merge($types, ['site', 'view']); $available = array_intersect_key($info['tokens'], array_flip($types)); // Construct the token string for each token. Loading core/modules/views/tests/src/Unit/PluginBaseTest.php +75 −0 Original line number Diff line number Diff line Loading @@ -4,12 +4,14 @@ namespace Drupal\Tests\views\Unit; use Drupal\Core\Utility\Token; use Drupal\Tests\UnitTestCase; use Drupal\views\Plugin\views\PluginBase; use Drupal\views\Tests\TestHelperPlugin; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; use Symfony\Component\DependencyInjection\ContainerBuilder; /** * Tests Drupal\views\Plugin\views\PluginBase. Loading @@ -34,6 +36,79 @@ protected function setUp(): void { $this->testHelperPlugin = new TestHelperPlugin([], 'default', []); } /** * Tests the getAvailableGlobalTokens method. */ #[DataProvider('providerTestGetAvailableGlobalTokens')] public function testGetAvailableGlobalTokens($info, $types, $expected): void { // Get the token service and set the container. $token = $this->createStub(Token::class); $token ->method('getInfo') ->willReturn($info); $container = new ContainerBuilder(); $container->set('token', $token); \Drupal::setContainer($container); $prepared = FALSE; $actual = $this->testHelperPlugin->getAvailableGlobalTokens($prepared, $types); $this->assertEquals($expected, $actual); } /** * Data provider for testGetAvailableGlobalTokens(). * * @return array * - An associative array of token information. * - An array of additional token types. * - The expected tokens. */ public static function providerTestGetAvailableGlobalTokens(): array { return [ '0 global, 0 added' => [ ['tokens' => []], [], [], ], '0 global, 1 added' => [ ['tokens' => []], ['date'], [], ], '1 global, 1 added, same' => [ ['tokens' => ['date' => 'bar']], ['date'], ['date' => 'bar'], ], '1 global, 0 added' => [ ['tokens' => ['site' => 'foo']], [], ['site' => 'foo'], ], '1 global, 1 added' => [ ['tokens' => ['site' => 'foo']], ['date'], ['site' => 'foo'], ], '2 global, 1 added' => [ ['tokens' => ['site' => 'foo', 'date' => 'bar']], ['date'], ['site' => 'foo', 'date' => 'bar'], ], '3 global, 0 added' => [ ['tokens' => ['site' => 'foo', 'view' => 'baz', 'date' => 'bar']], [], ['site' => 'foo', 'view' => 'baz'], ], '3 global, 1 added' => [ ['tokens' => ['site' => 'foo', 'view' => 'baz', 'date' => 'bar']], ['date'], ['site' => 'foo', 'view' => 'baz', 'date' => 'bar'], ], ]; } /** * Tests the unpackOptions method. * Loading Loading
core/modules/views/src/Plugin/views/PluginBase.php +1 −1 Original line number Diff line number Diff line Loading @@ -427,7 +427,7 @@ function ($children, $elements) { public function getAvailableGlobalTokens($prepared = FALSE, array $types = []) { $info = \Drupal::token()->getInfo(); // Site and view tokens should always be available. $types += ['site', 'view']; $types = array_merge($types, ['site', 'view']); $available = array_intersect_key($info['tokens'], array_flip($types)); // Construct the token string for each token. Loading
core/modules/views/tests/src/Unit/PluginBaseTest.php +75 −0 Original line number Diff line number Diff line Loading @@ -4,12 +4,14 @@ namespace Drupal\Tests\views\Unit; use Drupal\Core\Utility\Token; use Drupal\Tests\UnitTestCase; use Drupal\views\Plugin\views\PluginBase; use Drupal\views\Tests\TestHelperPlugin; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; use Symfony\Component\DependencyInjection\ContainerBuilder; /** * Tests Drupal\views\Plugin\views\PluginBase. Loading @@ -34,6 +36,79 @@ protected function setUp(): void { $this->testHelperPlugin = new TestHelperPlugin([], 'default', []); } /** * Tests the getAvailableGlobalTokens method. */ #[DataProvider('providerTestGetAvailableGlobalTokens')] public function testGetAvailableGlobalTokens($info, $types, $expected): void { // Get the token service and set the container. $token = $this->createStub(Token::class); $token ->method('getInfo') ->willReturn($info); $container = new ContainerBuilder(); $container->set('token', $token); \Drupal::setContainer($container); $prepared = FALSE; $actual = $this->testHelperPlugin->getAvailableGlobalTokens($prepared, $types); $this->assertEquals($expected, $actual); } /** * Data provider for testGetAvailableGlobalTokens(). * * @return array * - An associative array of token information. * - An array of additional token types. * - The expected tokens. */ public static function providerTestGetAvailableGlobalTokens(): array { return [ '0 global, 0 added' => [ ['tokens' => []], [], [], ], '0 global, 1 added' => [ ['tokens' => []], ['date'], [], ], '1 global, 1 added, same' => [ ['tokens' => ['date' => 'bar']], ['date'], ['date' => 'bar'], ], '1 global, 0 added' => [ ['tokens' => ['site' => 'foo']], [], ['site' => 'foo'], ], '1 global, 1 added' => [ ['tokens' => ['site' => 'foo']], ['date'], ['site' => 'foo'], ], '2 global, 1 added' => [ ['tokens' => ['site' => 'foo', 'date' => 'bar']], ['date'], ['site' => 'foo', 'date' => 'bar'], ], '3 global, 0 added' => [ ['tokens' => ['site' => 'foo', 'view' => 'baz', 'date' => 'bar']], [], ['site' => 'foo', 'view' => 'baz'], ], '3 global, 1 added' => [ ['tokens' => ['site' => 'foo', 'view' => 'baz', 'date' => 'bar']], ['date'], ['site' => 'foo', 'view' => 'baz', 'date' => 'bar'], ], ]; } /** * Tests the unpackOptions method. * Loading