class DoTrustedCallbackTraitTest
Same name in other branches
- 9 core/tests/Drupal/Tests/Core/Security/DoTrustedCallbackTraitTest.php \Drupal\Tests\Core\Security\DoTrustedCallbackTraitTest
- 8.9.x core/tests/Drupal/Tests/Core/Security/DoTrustedCallbackTraitTest.php \Drupal\Tests\Core\Security\DoTrustedCallbackTraitTest
- 10 core/tests/Drupal/Tests/Core/Security/DoTrustedCallbackTraitTest.php \Drupal\Tests\Core\Security\DoTrustedCallbackTraitTest
@coversDefaultClass \Drupal\Core\Security\DoTrustedCallbackTrait @group Security
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\Core\Security\DoTrustedCallbackTraitTest extends \Drupal\Tests\UnitTestCase uses \Drupal\Core\Security\DoTrustedCallbackTrait
Expanded class hierarchy of DoTrustedCallbackTraitTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Security/ DoTrustedCallbackTraitTest.php, line 17
Namespace
Drupal\Tests\Core\SecurityView source
class DoTrustedCallbackTraitTest extends UnitTestCase {
use DoTrustedCallbackTrait;
/**
* @covers ::doTrustedCallback
* @dataProvider providerTestTrustedCallbacks
*/
public function testTrustedCallbacks(callable $callback, $extra_trusted_interface = NULL) : void {
$return = $this->doTrustedCallback($callback, [], '%s is not trusted', TrustedCallbackInterface::THROW_EXCEPTION, $extra_trusted_interface);
$this->assertSame('test', $return);
}
/**
* Data provider for ::testTrustedCallbacks().
*/
public static function providerTestTrustedCallbacks() {
$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;
}
/**
* @covers ::doTrustedCallback
* @dataProvider providerTestUntrustedCallbacks
*/
public function testUntrustedCallbacks(callable $callback, $extra_trusted_interface = NULL) : void {
$this->expectException(UntrustedCallbackException::class);
$this->doTrustedCallback($callback, [], '%s is not trusted', TrustedCallbackInterface::THROW_EXCEPTION, $extra_trusted_interface);
}
/**
* Data provider for ::testUntrustedCallbacks().
*/
public static function providerTestUntrustedCallbacks() {
$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;
}
/**
* @dataProvider errorTypeProvider
*/
public function testException($callback) : void {
$this->expectException(UntrustedCallbackException::class);
$this->expectExceptionMessage('Drupal\\Tests\\Core\\Security\\UntrustedObject::callback is not trusted');
$this->doTrustedCallback($callback, [], '%s is not trusted');
}
/**
* @dataProvider errorTypeProvider
* @group legacy
*/
public function testSilencedDeprecation($callback) : void {
$this->expectDeprecation('Drupal\\Tests\\Core\\Security\\UntrustedObject::callback is not trusted');
$this->doTrustedCallback($callback, [], '%s is not trusted', TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION);
}
/**
* Data provider for tests of ::doTrustedCallback $error_type argument.
*/
public static function errorTypeProvider() {
$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;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overrides |
---|---|---|---|---|
DoTrustedCallbackTrait::doTrustedCallback | public | function | Performs a callback. | |
DoTrustedCallbackTraitTest::errorTypeProvider | public static | function | Data provider for tests of ::doTrustedCallback $error_type argument. | |
DoTrustedCallbackTraitTest::providerTestTrustedCallbacks | public static | function | Data provider for ::testTrustedCallbacks(). | |
DoTrustedCallbackTraitTest::providerTestUntrustedCallbacks | public static | function | Data provider for ::testUntrustedCallbacks(). | |
DoTrustedCallbackTraitTest::testException | public | function | @dataProvider errorTypeProvider | |
DoTrustedCallbackTraitTest::testSilencedDeprecation | public | function | @dataProvider errorTypeProvider @group legacy |
|
DoTrustedCallbackTraitTest::testTrustedCallbacks | public | function | @covers ::doTrustedCallback @dataProvider providerTestTrustedCallbacks |
|
DoTrustedCallbackTraitTest::testUntrustedCallbacks | public | function | @covers ::doTrustedCallback @dataProvider providerTestUntrustedCallbacks |
|
ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
ExpectDeprecationTrait::getCallableName | private static | function | Returns a callable as a string suitable for inclusion in a message. | |
ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | |
ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | |
RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |
RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |
RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |
RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
UnitTestCase::$root | protected | property | The app root. | |
UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |
UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase::setUp | protected | function | 367 | |
UnitTestCase::setUpBeforeClass | public static | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.