DoTrustedCallbackTraitTest.php

Same filename and directory in other branches
  1. 10 core/tests/Drupal/Tests/Core/Security/DoTrustedCallbackTraitTest.php
  2. 9 core/tests/Drupal/Tests/Core/Security/DoTrustedCallbackTraitTest.php
  3. 8.9.x core/tests/Drupal/Tests/Core/Security/DoTrustedCallbackTraitTest.php
  4. main core/tests/Drupal/Tests/Core/Security/DoTrustedCallbackTraitTest.php

Namespace

Drupal\Tests\Core\Security

File

core/tests/Drupal/Tests/Core/Security/DoTrustedCallbackTraitTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\Core\Security;

use Drupal\Core\Security\Attribute\TrustedCallback;
use Drupal\Core\Security\DoTrustedCallbackTrait;
use Drupal\Core\Security\StaticTrustedCallbackHelper;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\Security\UntrustedCallbackException;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;

/**
 * Tests Drupal\Core\Security\DoTrustedCallbackTrait.
 */
class DoTrustedCallbackTraitTest extends UnitTestCase {
  use DoTrustedCallbackTrait;
  
  /**
   * Tests trusted callbacks.
   *
   * @legacy-covers ::doTrustedCallback
   */
  public function testTrustedCallbacks(callable $callback, $extra_trusted_interface = NULL) : void {
    $return = $this->doTrustedCallback($callback, [], '%s is not trusted', $extra_trusted_interface);
    $this->assertSame('test', $return);
  }
  
  /**
   * Tests trusted callback argument deprecation.
   *
   * @legacy-covers ::doTrustedCallback
   */
  public function testTrustedCallbacksWithDeprecatedArg(callable $callback, $extra_trusted_interface = NULL) : void {
    $this->expectUserDeprecationMessage('Calling Drupal\\Core\\Security\\StaticTrustedCallbackHelper::callback() with 5 arguments is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. See https://www.drupal.org/node/3627046');
    $return = $this->doTrustedCallback($callback, [], '%s is not trusted', 'exception', $extra_trusted_interface);
    $this->assertSame('test', $return);
  }
  
  /**
   * Tests StaticTrustedCallbackHelper::callback() argument deprecation.
   */
  public function testStaticTrustedCallbackHelperWithDeprecatedArg(callable $callback, $extra_trusted_interface = NULL) : void {
    $this->expectUserDeprecationMessage('Calling Drupal\\Core\\Security\\StaticTrustedCallbackHelper::callback() with 5 arguments is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. See https://www.drupal.org/node/3627046');
    $return = StaticTrustedCallbackHelper::callback($callback, [], '%s is not trusted', 'exception', $extra_trusted_interface);
    $this->assertSame('test', $return);
  }
  
  /**
   * Data provider for ::testTrustedCallbacks().
   */
  public static function providerTestTrustedCallbacks() : array {
    $closure = function () {
      return 'test';
    };
    $tests['closure'] = [
      $closure,
    ];
    $tests['TrustedCallbackInterface_object'] = [
      [
        new TrustedMethods(),
        'callback',
      ],
      TrustedInterface::class,
    ];
    $tests['TrustedCallbackInterface_object_attribute'] = [
      [
        new TrustedMethods(),
        'attributeCallback',
      ],
      TrustedInterface::class,
    ];
    $tests['TrustedCallbackInterface_static_string'] = [
      '\\Drupal\\Tests\\Core\\Security\\TrustedMethods::callback',
      TrustedInterface::class,
    ];
    $tests['TrustedCallbackInterface_static_array'] = [
      [
        TrustedMethods::class,
        'callback',
      ],
      TrustedInterface::class,
    ];
    $tests['TrustedCallbackInterface_static_array_attribute'] = [
      [
        TrustedMethods::class,
        'attributeCallback',
      ],
      TrustedInterface::class,
    ];
    $tests['extra_trusted_interface_object'] = [
      [
        new TrustedObject(),
        'callback',
      ],
      TrustedInterface::class,
    ];
    $tests['extra_trusted_interface_static_string'] = [
      '\\Drupal\\Tests\\Core\\Security\\TrustedObject::callback',
      TrustedInterface::class,
    ];
    $tests['extra_trusted_interface_static_array'] = [
      [
        TrustedObject::class,
        'callback',
      ],
      TrustedInterface::class,
    ];
    return $tests;
  }
  
  /**
   * Tests untrusted callbacks.
   *
   * @legacy-covers ::doTrustedCallback
   */
  public function testUntrustedCallbacks(callable $callback, $extra_trusted_interface = NULL) : void {
    $this->expectException(UntrustedCallbackException::class);
    $this->doTrustedCallback($callback, [], '%s is not trusted', $extra_trusted_interface);
  }
  
  /**
   * Data provider for ::testUntrustedCallbacks().
   */
  public static function providerTestUntrustedCallbacks() : array {
    $tests['TrustedCallbackInterface_object'] = [
      [
        new TrustedMethods(),
        'unTrustedCallback',
      ],
      TrustedInterface::class,
    ];
    $tests['TrustedCallbackInterface_static_string'] = [
      '\\Drupal\\Tests\\Core\\Security\\TrustedMethods::unTrustedCallback',
      TrustedInterface::class,
    ];
    $tests['TrustedCallbackInterface_static_array'] = [
      [
        TrustedMethods::class,
        'unTrustedCallback',
      ],
      TrustedInterface::class,
    ];
    $tests['untrusted_object'] = [
      [
        new UntrustedObject(),
        'callback',
      ],
      TrustedInterface::class,
    ];
    $tests['untrusted_object_static_string'] = [
      '\\Drupal\\Tests\\Core\\Security\\UntrustedObject::callback',
      TrustedInterface::class,
    ];
    $tests['untrusted_object_static_array'] = [
      [
        UntrustedObject::class,
        'callback',
      ],
      TrustedInterface::class,
    ];
    $tests['invokable_untrusted_object_static_array'] = [
      new InvokableUntrustedObject(),
      TrustedInterface::class,
    ];
    return $tests;
  }
  
  /**
   * Tests exceptions are thrown for untrusted callbacks.
   */
  public function testException(callable $callback) : void {
    $this->expectException(UntrustedCallbackException::class);
    $this->expectExceptionMessageIs('Drupal\\Tests\\Core\\Security\\UntrustedObject::callback is not trusted');
    $this->doTrustedCallback($callback, [], '%s is not trusted');
  }
  
  /**
   * Data provider for testException().
   */
  public static function providerTestException() : array {
    $tests['untrusted_object'] = [
      [
        new UntrustedObject(),
        'callback',
      ],
    ];
    $tests['untrusted_object_static_string'] = [
      'Drupal\\Tests\\Core\\Security\\UntrustedObject::callback',
    ];
    $tests['untrusted_object_static_array'] = [
      [
        UntrustedObject::class,
        'callback',
      ],
    ];
    return $tests;
  }

}

/**
 * Interface representing classes with trusted callbacks.
 */
interface TrustedInterface {

}

/**
 * Class with a trusted interface implementation with callback.
 */
class TrustedObject implements TrustedInterface {
  public static function callback() : string {
    return 'test';
  }

}

/**
 * Class representing untrusted callback.
 */
class UntrustedObject {
  public static function callback() : string {
    return 'test';
  }

}

/**
 * Invokable untrusted test class.
 */
class InvokableUntrustedObject {
  public function __invoke() : string {
    return 'test';
  }

}

/**
 * Test class with implemented trusted callbacks.
 */
class TrustedMethods implements TrustedCallbackInterface {
  public static function trustedCallbacks() : array {
    return [
      'callback',
    ];
  }
  public static function callback() : string {
    return 'test';
  }
  public static function attributeCallback() : string {
    return 'test';
  }
  public static function unTrustedCallback() : string {
    return 'test';
  }

}

Classes

Title Deprecated Summary
DoTrustedCallbackTraitTest Tests Drupal\Core\Security\DoTrustedCallbackTrait.
InvokableUntrustedObject Invokable untrusted test class.
TrustedMethods Test class with implemented trusted callbacks.
TrustedObject Class with a trusted interface implementation with callback.
UntrustedObject Class representing untrusted callback.

Interfaces

Title Deprecated Summary
TrustedInterface Interface representing classes with trusted callbacks.

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