2010-11-24 07:23:50 +01:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Dev\Tests;
|
|
|
|
|
2022-06-23 05:54:36 +02:00
|
|
|
use ReflectionMethod;
|
|
|
|
use SilverStripe\Core\BaseKernel;
|
|
|
|
use SilverStripe\Core\CoreKernel;
|
|
|
|
use SilverStripe\Core\Kernel;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Dev\Backtrace;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2022-06-23 05:54:36 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class BacktraceTest extends SapphireTest
|
|
|
|
{
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testFullFuncNameWithArgsAndCustomCharLimit()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$func = [
|
2016-12-16 05:34:21 +01:00
|
|
|
'class' => 'MyClass',
|
|
|
|
'type' => '->',
|
|
|
|
'file' => 'MyFile.php',
|
|
|
|
'line' => 99,
|
|
|
|
'function' => 'myFunction',
|
2020-04-20 19:58:09 +02:00
|
|
|
'args' => [
|
2016-12-16 05:34:21 +01:00
|
|
|
'number' => 1,
|
|
|
|
'mylongstring' => 'more than 20 characters 1234567890',
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2017-05-16 18:04:10 +02:00
|
|
|
'MyClass->myFunction(1, more than 20 charact...)',
|
2016-12-16 05:34:21 +01:00
|
|
|
Backtrace::full_func_name($func, true, 20)
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testIgnoredFunctionArgs()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$bt = [
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'type' => '->',
|
|
|
|
'file' => 'MyFile.php',
|
|
|
|
'line' => 99,
|
|
|
|
'function' => 'myIgnoredGlobalFunction',
|
2020-04-20 19:58:09 +02:00
|
|
|
'args' => ['password' => 'secred',]
|
|
|
|
],
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'class' => 'MyClass',
|
|
|
|
'type' => '->',
|
|
|
|
'file' => 'MyFile.php',
|
|
|
|
'line' => 99,
|
|
|
|
'function' => 'myIgnoredClassFunction',
|
2020-04-20 19:58:09 +02:00
|
|
|
'args' => ['password' => 'secred',]
|
|
|
|
],
|
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'class' => 'MyClass',
|
|
|
|
'type' => '->',
|
|
|
|
'file' => 'MyFile.php',
|
|
|
|
'line' => 99,
|
|
|
|
'function' => 'myFunction',
|
2020-04-20 19:58:09 +02:00
|
|
|
'args' => ['myarg' => 'myval']
|
|
|
|
]
|
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
Backtrace::config()->update(
|
|
|
|
'ignore_function_args',
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['MyClass', 'myIgnoredClassFunction'],
|
2016-12-16 05:34:21 +01:00
|
|
|
'myIgnoredGlobalFunction'
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2011-05-26 12:04:01 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$filtered = Backtrace::filter_backtrace($bt);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals('<filtered>', $filtered[0]['args']['password'], 'Filters global functions');
|
|
|
|
$this->assertEquals('<filtered>', $filtered[1]['args']['password'], 'Filters class functions');
|
|
|
|
$this->assertEquals('myval', $filtered[2]['args']['myarg'], 'Doesnt filter other functions');
|
|
|
|
}
|
2018-07-14 20:30:29 +02:00
|
|
|
|
|
|
|
public function testFilteredWildCard()
|
|
|
|
{
|
2020-04-20 19:58:09 +02:00
|
|
|
$bt = [
|
|
|
|
[
|
2018-07-14 20:30:29 +02:00
|
|
|
'type' => '->',
|
|
|
|
'file' => 'MyFile.php',
|
|
|
|
'line' => 99,
|
|
|
|
'function' => 'myIgnoredGlobalFunction',
|
2020-04-20 19:58:09 +02:00
|
|
|
'args' => ['password' => 'secred',]
|
|
|
|
],
|
|
|
|
[
|
2018-07-14 20:30:29 +02:00
|
|
|
'class' => 'MyClass',
|
|
|
|
'type' => '->',
|
|
|
|
'file' => 'MyFile.php',
|
|
|
|
'line' => 99,
|
|
|
|
'function' => 'myIgnoredClassFunction',
|
2020-04-20 19:58:09 +02:00
|
|
|
'args' => ['password' => 'secred',]
|
|
|
|
],
|
|
|
|
[
|
2018-07-14 20:30:29 +02:00
|
|
|
'class' => 'MyClass',
|
|
|
|
'type' => '->',
|
|
|
|
'file' => 'MyFile.php',
|
|
|
|
'line' => 99,
|
|
|
|
'function' => 'myFunction',
|
2020-04-20 19:58:09 +02:00
|
|
|
'args' => ['myarg' => 'myval']
|
|
|
|
]
|
|
|
|
];
|
2018-07-14 20:30:29 +02:00
|
|
|
Backtrace::config()->update(
|
|
|
|
'ignore_function_args',
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
|
|
|
['*', 'myIgnoredClassFunction'],
|
|
|
|
]
|
2018-07-14 20:30:29 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$filtered = Backtrace::filter_backtrace($bt);
|
|
|
|
|
|
|
|
$this->assertEquals('secred', $filtered[0]['args']['password']);
|
|
|
|
$this->assertEquals('<filtered>', $filtered[1]['args']['password']);
|
|
|
|
$this->assertEquals('myval', $filtered[2]['args']['myarg']);
|
|
|
|
}
|
2022-06-23 05:54:36 +02:00
|
|
|
|
|
|
|
public function matchesFilterableClassProvider(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'anything',
|
|
|
|
'*',
|
|
|
|
true,
|
|
|
|
'Wildcard counts as a match',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
DataObject::class,
|
|
|
|
BaseKernel::class,
|
|
|
|
false,
|
|
|
|
'No match',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
DataObject::class,
|
|
|
|
DataObject::class,
|
|
|
|
true,
|
|
|
|
'Exact match',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
CoreKernel::class,
|
|
|
|
BaseKernel::class,
|
|
|
|
true,
|
|
|
|
'Subclass counts as a match',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
BaseKernel::class,
|
|
|
|
CoreKernel::class,
|
|
|
|
false,
|
|
|
|
'Superclass does not count as a match',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
CoreKernel::class,
|
|
|
|
Kernel::class,
|
|
|
|
true,
|
|
|
|
'Implements interface counts as a match',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider matchesFilterableClassProvider
|
|
|
|
*/
|
|
|
|
public function testMatchesFilterableClass(string $className, string $filterableClass, bool $expected, string $message): void
|
|
|
|
{
|
|
|
|
$reflectionMethod = new ReflectionMethod(Backtrace::class . '::matchesFilterableClass');
|
|
|
|
$reflectionMethod->setAccessible(true);
|
|
|
|
$this->assertSame($expected, $reflectionMethod->invoke(null, $className, $filterableClass), $message);
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|