Verified Commit bb1b9c25 authored by godotislate's avatar godotislate
Browse files

fix: #3152267 Invalid use of array union operator

By: s_bhandari
By: jayelless
By: quietone
By: lendude
parent d290c9a7
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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.
+75 −0
Original line number Diff line number Diff line
@@ -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.
@@ -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.
   *