class DoTrustedCallbackTraitTest in Drupal 8
Same name and namespace in other branches
- 9 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 PhpunitCompatibilityTrait
- class \Drupal\Tests\Core\Security\DoTrustedCallbackTraitTest uses DoTrustedCallbackTrait
Expanded class hierarchy of DoTrustedCallbackTraitTest
File
- core/
tests/ Drupal/ Tests/ Core/ Security/ DoTrustedCallbackTraitTest.php, line 15
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) {
$return = $this
->doTrustedCallback($callback, [], '%s is not trusted', TrustedCallbackInterface::THROW_EXCEPTION, $extra_trusted_interface);
$this
->assertSame('test', $return);
}
/**
* Data provider for ::testTrustedCallbacks().
*/
public function providerTestTrustedCallbacks() {
$closure = function () {
return 'test';
};
$tests['closure'] = [
$closure,
];
$tests['TrustedCallbackInterface_object'] = [
[
new TrustedMethods(),
'callback',
],
TrustedInterface::class,
];
$tests['TrustedCallbackInterface_static_string'] = [
'\\Drupal\\Tests\\Core\\Security\\TrustedMethods::callback',
TrustedInterface::class,
];
$tests['TrustedCallbackInterface_static_array'] = [
[
TrustedMethods::class,
'callback',
],
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) {
$this
->expectException(UntrustedCallbackException::class);
$this
->doTrustedCallback($callback, [], '%s is not trusted', TrustedCallbackInterface::THROW_EXCEPTION, $extra_trusted_interface);
}
/**
* Data provider for ::testUntrustedCallbacks().
*/
public 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) {
$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
* @expectedDeprecation Drupal\Tests\Core\Security\UntrustedObject::callback is not trusted
*/
public function testSilencedDeprecation($callback) {
$this
->doTrustedCallback($callback, [], '%s is not trusted', TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION);
}
/**
* @dataProvider errorTypeProvider
*/
public function testWarning($callback) {
$this
->expectException(Warning::class, 'Drupal\\Tests\\Core\\Security\\UntrustedObject::callback is not trusted');
$this
->doTrustedCallback($callback, [], '%s is not trusted', TrustedCallbackInterface::TRIGGER_WARNING);
}
/**
* Data provider for tests of ::doTrustedCallback $error_type argument.
*/
public 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DoTrustedCallbackTrait:: |
public | function | Performs a callback. | |
DoTrustedCallbackTraitTest:: |
public | function | Data provider for tests of ::doTrustedCallback $error_type argument. | |
DoTrustedCallbackTraitTest:: |
public | function | Data provider for ::testTrustedCallbacks(). | |
DoTrustedCallbackTraitTest:: |
public | function | Data provider for ::testUntrustedCallbacks(). | |
DoTrustedCallbackTraitTest:: |
public | function | @dataProvider errorTypeProvider | |
DoTrustedCallbackTraitTest:: |
public | function | @dataProvider errorTypeProvider @group legacy @expectedDeprecation Drupal\Tests\Core\Security\UntrustedObject::callback is not trusted | |
DoTrustedCallbackTraitTest:: |
public | function | @covers ::doTrustedCallback @dataProvider providerTestTrustedCallbacks | |
DoTrustedCallbackTraitTest:: |
public | function | @covers ::doTrustedCallback @dataProvider providerTestUntrustedCallbacks | |
DoTrustedCallbackTraitTest:: |
public | function | @dataProvider errorTypeProvider | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |