mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Compare commits
12 Commits
00fd636f71
...
1353028974
Author | SHA1 | Date | |
---|---|---|---|
|
1353028974 | ||
|
ba97de9458 | ||
|
82b0851a2a | ||
|
4d26063850 | ||
|
8845794012 | ||
|
7401bcf02e | ||
|
969d7b4bd2 | ||
|
acbde0c24d | ||
|
666b4094b4 | ||
|
8ec068f3fd | ||
|
6bb9a0b33d | ||
|
ebbd6427b2 |
@ -39,6 +39,7 @@
|
||||
"sensiolabs/ansi-to-html": "^1.2",
|
||||
"silverstripe/config": "^3",
|
||||
"silverstripe/assets": "^3",
|
||||
"silverstripe/supported-modules": "^1.1",
|
||||
"silverstripe/vendor-plugin": "^2",
|
||||
"sminnee/callbacklist": "^0.1.1",
|
||||
"symfony/cache": "^7.0",
|
||||
|
@ -19,7 +19,6 @@ use SilverStripe\View\TemplateGlobalProvider;
|
||||
*/
|
||||
class Controller extends RequestHandler implements TemplateGlobalProvider
|
||||
{
|
||||
|
||||
/**
|
||||
* An array of arguments extracted from the URL.
|
||||
*
|
||||
@ -627,9 +626,8 @@ class Controller extends RequestHandler implements TemplateGlobalProvider
|
||||
* Caution: All parameters are expected to be URI-encoded already.
|
||||
*
|
||||
* @param string|array $arg One or more link segments, or list of link segments as an array
|
||||
* @return string
|
||||
*/
|
||||
public static function join_links($arg = null)
|
||||
public static function join_links($arg = null): string
|
||||
{
|
||||
if (func_num_args() === 1 && is_array($arg)) {
|
||||
$args = $arg;
|
||||
|
@ -3,11 +3,14 @@
|
||||
namespace SilverStripe\Dev;
|
||||
|
||||
use BadMethodCallException;
|
||||
use RuntimeException;
|
||||
use SilverStripe\Control\Director;
|
||||
use SilverStripe\Core\Environment;
|
||||
use SilverStripe\Core\Injector\InjectionCreator;
|
||||
use SilverStripe\Core\Injector\InjectorLoader;
|
||||
use SilverStripe\Core\Manifest\Module;
|
||||
use SilverStripe\Core\Path;
|
||||
use SilverStripe\SupportedModules\MetaData;
|
||||
|
||||
/**
|
||||
* Handles raising an notice when accessing a deprecated method, class, configuration, or behaviour.
|
||||
@ -77,6 +80,18 @@ class Deprecation
|
||||
*/
|
||||
private static bool $showNoReplacementNotices = false;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
private static bool $showNoticesCalledFromSupportedCode = false;
|
||||
|
||||
/**
|
||||
* Cache of supported module directories, read from silverstripe/supported-modules repositories.json
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
private static array $supportedModuleDirectories = [];
|
||||
|
||||
/**
|
||||
* Enable throwing deprecation warnings. By default, this excludes warnings for
|
||||
* deprecated code which is called by core Silverstripe modules.
|
||||
@ -146,6 +161,12 @@ class Deprecation
|
||||
if (!$level) {
|
||||
$level = 1;
|
||||
}
|
||||
$called = Deprecation::get_called_from_trace($backtrace, $level);
|
||||
return ($called['class'] ?? '') . ($called['type'] ?? '') . ($called['function'] ?? '');
|
||||
}
|
||||
|
||||
private static function get_called_from_trace(array $backtrace, int $level): array
|
||||
{
|
||||
$newLevel = $level;
|
||||
// handle closures inside withSuppressedNotice()
|
||||
if (Deprecation::$insideNoticeSuppression
|
||||
@ -163,8 +184,51 @@ class Deprecation
|
||||
if ($level == 4 && ($backtrace[$newLevel]['class'] ?? '') === InjectionCreator::class) {
|
||||
$newLevel = $newLevel + 4;
|
||||
}
|
||||
// handle noticeWithNoReplacment()
|
||||
foreach ($backtrace as $trace) {
|
||||
if (($trace['class'] ?? '') === Deprecation::class
|
||||
&& ($trace['function'] ?? '') === 'noticeWithNoReplacment'
|
||||
) {
|
||||
$newLevel = $newLevel + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$called = $backtrace[$newLevel] ?? [];
|
||||
return ($called['class'] ?? '') . ($called['type'] ?? '') . ($called['function'] ?? '');
|
||||
return $called;
|
||||
}
|
||||
|
||||
private static function isCalledFromSupportedCode(array $backtrace): bool
|
||||
{
|
||||
$called = Deprecation::get_called_from_trace($backtrace, 1);
|
||||
$file = $called['file'] ?? '';
|
||||
if (!$file) {
|
||||
return false;
|
||||
}
|
||||
return Deprecation::fileIsInSupportedModule($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a file (path to file) is in a supported module
|
||||
*/
|
||||
public static function fileIsInSupportedModule(string $file): bool
|
||||
{
|
||||
// Cache the supported modules list
|
||||
if (count(Deprecation::$supportedModuleDirectories) === 0) {
|
||||
// Do not make a network request when fetching metadata which could slow down a website
|
||||
// While there is a small risk of the list being out of date, there is minimal downside to this
|
||||
$metaData = MetaData::getAllRepositoryMetaData(fromRemote: false);
|
||||
$dirs = array_map(fn($module) => "/vendor/{$module['packagist']}/", $metaData['supportedModules']);
|
||||
// This is a special case for silverstripe-framework when running in CI
|
||||
// Needed because module is run in the root folder rather than in the vendor folder
|
||||
$dirs[] = '/silverstripe-framework/';
|
||||
Deprecation::$supportedModuleDirectories = $dirs;
|
||||
}
|
||||
foreach (Deprecation::$supportedModuleDirectories as $dir) {
|
||||
if (str_contains($file, $dir)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function isEnabled(): bool
|
||||
@ -245,6 +309,22 @@ class Deprecation
|
||||
return Deprecation::$shouldShowForCli;
|
||||
}
|
||||
|
||||
/**
|
||||
* If true, deprecation warnings will be shown for deprecated code which is called by core Silverstripe modules.
|
||||
*/
|
||||
public static function getShowNoticesCalledFromSupportedCode(): bool
|
||||
{
|
||||
return Deprecation::$showNoticesCalledFromSupportedCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether deprecation warnings will be shown for deprecated code which is called by core Silverstripe modules.
|
||||
*/
|
||||
public static function setShowNoticesCalledFromSupportedCode(bool $value): void
|
||||
{
|
||||
Deprecation::$showNoticesCalledFromSupportedCode = $value;
|
||||
}
|
||||
|
||||
public static function outputNotices(): void
|
||||
{
|
||||
if (!Deprecation::isEnabled()) {
|
||||
@ -258,9 +338,13 @@ class Deprecation
|
||||
$arr = array_shift(Deprecation::$userErrorMessageBuffer);
|
||||
$message = $arr['message'];
|
||||
$calledWithNoticeSuppression = $arr['calledWithNoticeSuppression'];
|
||||
$isCalledFromSupportedCode = $arr['isCalledFromSupportedCode'];
|
||||
if ($calledWithNoticeSuppression && !Deprecation::$showNoReplacementNotices) {
|
||||
continue;
|
||||
}
|
||||
if ($isCalledFromSupportedCode && !Deprecation::$showNoticesCalledFromSupportedCode) {
|
||||
continue;
|
||||
}
|
||||
Deprecation::$isTriggeringError = true;
|
||||
user_error($message, E_USER_DEPRECATED);
|
||||
Deprecation::$isTriggeringError = false;
|
||||
@ -294,6 +378,10 @@ class Deprecation
|
||||
$data = [
|
||||
'key' => sha1($string),
|
||||
'message' => $string,
|
||||
// Setting to `false` as here as any SCOPE_CONFIG notices from supported modules have
|
||||
// already been filtered out if needed if they came from a supported module in
|
||||
// SilverStripe\Config\Transformer\YamlTransformer::checkForDeprecatedConfig()
|
||||
'isCalledFromSupportedCode' => false,
|
||||
'calledWithNoticeSuppression' => Deprecation::$insideNoticeSuppression
|
||||
];
|
||||
} else {
|
||||
@ -322,13 +410,13 @@ class Deprecation
|
||||
|
||||
$level = Deprecation::$insideNoticeSuppression ? 4 : 2;
|
||||
$string .= " Called from " . Deprecation::get_called_method_from_trace($backtrace, $level) . '.';
|
||||
|
||||
if ($caller) {
|
||||
$string = $caller . ' is deprecated.' . ($string ? ' ' . $string : '');
|
||||
}
|
||||
$data = [
|
||||
'key' => sha1($string),
|
||||
'message' => $string,
|
||||
'isCalledFromSupportedCode' => Deprecation::isCalledFromSupportedCode($backtrace),
|
||||
'calledWithNoticeSuppression' => Deprecation::$insideNoticeSuppression
|
||||
];
|
||||
}
|
||||
@ -360,6 +448,23 @@ class Deprecation
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shorthand method to create a suppressed notice for something with no immediate replacement.
|
||||
* If $message is empty, then a standardised message will be used
|
||||
*/
|
||||
public static function noticeWithNoReplacment(
|
||||
string $atVersion,
|
||||
string $message = '',
|
||||
int $scope = Deprecation::SCOPE_METHOD
|
||||
): void {
|
||||
if ($message === '') {
|
||||
$message = 'Will be removed without equivalent functionality to replace it.';
|
||||
}
|
||||
Deprecation::withSuppressedNotice(
|
||||
fn() => Deprecation::notice($atVersion, $message, $scope)
|
||||
);
|
||||
}
|
||||
|
||||
private static function varAsBoolean($val): bool
|
||||
{
|
||||
if (is_string($val)) {
|
||||
|
@ -49,6 +49,8 @@ class GridFieldFilterHeader extends AbstractGridFieldComponent implements GridFi
|
||||
*/
|
||||
protected ?string $searchField = null;
|
||||
|
||||
private string $placeHolderText = '';
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
@ -245,6 +247,24 @@ class GridFieldFilterHeader extends AbstractGridFieldComponent implements GridFi
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the text to be used as a placeholder in the search field.
|
||||
* If blank, the placeholder will be generated based on the class held in the GridField.
|
||||
*/
|
||||
public function getPlaceHolderText(): string
|
||||
{
|
||||
return $this->placeHolderText;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the text to be used as a placeholder in the search field.
|
||||
* If blank, this text will be generated based on the class held in the GridField.
|
||||
*/
|
||||
public function setPlaceHolderText(string $placeHolderText): static
|
||||
{
|
||||
$this->placeHolderText = $placeHolderText;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a search context based on the model class of the of the GridField
|
||||
@ -318,7 +338,7 @@ class GridFieldFilterHeader extends AbstractGridFieldComponent implements GridFi
|
||||
$schema = [
|
||||
'formSchemaUrl' => $schemaUrl,
|
||||
'name' => $searchField,
|
||||
'placeholder' => _t(__CLASS__ . '.Search', 'Search "{name}"', ['name' => $this->getTitle($gridField, $inst)]),
|
||||
'placeholder' => $this->getPlaceHolder($inst),
|
||||
'filters' => $filters ?: new \stdClass, // stdClass maps to empty json object '{}'
|
||||
'gridfield' => $gridField->getName(),
|
||||
'searchAction' => $searchAction->getAttribute('name'),
|
||||
@ -330,19 +350,6 @@ class GridFieldFilterHeader extends AbstractGridFieldComponent implements GridFi
|
||||
return json_encode($schema);
|
||||
}
|
||||
|
||||
private function getTitle(GridField $gridField, object $inst): string
|
||||
{
|
||||
if ($gridField->Title) {
|
||||
return $gridField->Title;
|
||||
}
|
||||
|
||||
if (ClassInfo::hasMethod($inst, 'i18n_plural_name')) {
|
||||
return $inst->i18n_plural_name();
|
||||
}
|
||||
|
||||
return ClassInfo::shortName($inst);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the search form for the component
|
||||
*
|
||||
@ -386,7 +393,7 @@ class GridFieldFilterHeader extends AbstractGridFieldComponent implements GridFi
|
||||
$field->addExtraClass('stacked no-change-track');
|
||||
}
|
||||
|
||||
$name = $this->getTitle($gridField, singleton($gridField->getModelClass()));
|
||||
$name = $this->getTitle(singleton($gridField->getModelClass()));
|
||||
|
||||
$this->searchForm = $form = new Form(
|
||||
$gridField,
|
||||
@ -456,4 +463,32 @@ class GridFieldFilterHeader extends AbstractGridFieldComponent implements GridFi
|
||||
)
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the text that will be used as a placeholder in the search field.
|
||||
*
|
||||
* @param object $obj An instance of the class that will be searched against.
|
||||
* If getPlaceHolderText is empty, this object will be used to build the placeholder
|
||||
* e.g. 'Search "My Data Object"'
|
||||
*/
|
||||
private function getPlaceHolder(object $obj): string
|
||||
{
|
||||
$placeholder = $this->getPlaceHolderText();
|
||||
if (!empty($placeholder)) {
|
||||
return $placeholder;
|
||||
}
|
||||
if ($obj) {
|
||||
return _t(__CLASS__ . '.Search', 'Search "{name}"', ['name' => $this->getTitle($obj)]);
|
||||
}
|
||||
return _t(__CLASS__ . '.Search_Default', 'Search');
|
||||
}
|
||||
|
||||
private function getTitle(object $inst): string
|
||||
{
|
||||
if (ClassInfo::hasMethod($inst, 'i18n_plural_name')) {
|
||||
return $inst->i18n_plural_name();
|
||||
}
|
||||
|
||||
return ClassInfo::shortName($inst);
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,6 @@ use SilverStripe\Model\List\PaginatedList;
|
||||
use SilverStripe\ORM\DataList;
|
||||
use SilverStripe\Model\List\ArrayList;
|
||||
use SilverStripe\ORM\DataObject;
|
||||
use SilverStripe\ORM\Queries\SQLSelect;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
@ -31,7 +30,7 @@ class MySQLDatabase extends Database implements TransactionManager
|
||||
* @config
|
||||
* @var String
|
||||
*/
|
||||
private static $connection_charset = 'utf8';
|
||||
private static $connection_charset = 'utf8mb4';
|
||||
|
||||
/**
|
||||
* Default connection collation
|
||||
@ -39,7 +38,7 @@ class MySQLDatabase extends Database implements TransactionManager
|
||||
* @config
|
||||
* @var string
|
||||
*/
|
||||
private static $connection_collation = 'utf8_general_ci';
|
||||
private static $connection_collation = 'utf8mb4_unicode_ci';
|
||||
|
||||
/**
|
||||
* Default charset
|
||||
@ -47,7 +46,7 @@ class MySQLDatabase extends Database implements TransactionManager
|
||||
* @config
|
||||
* @var string
|
||||
*/
|
||||
private static $charset = 'utf8';
|
||||
private static $charset = 'utf8mb4';
|
||||
|
||||
/**
|
||||
* SQL Mode used on connections to MySQL. Defaults to ANSI. For basic ORM
|
||||
@ -73,7 +72,7 @@ class MySQLDatabase extends Database implements TransactionManager
|
||||
* @config
|
||||
* @var string
|
||||
*/
|
||||
private static $collation = 'utf8_general_ci';
|
||||
private static $collation = 'utf8mb4_unicode_ci';
|
||||
|
||||
public function connect($parameters)
|
||||
{
|
||||
|
@ -22,7 +22,6 @@ class DBBoolean extends DBField
|
||||
public function __construct(?string $name = null, bool $defaultVal = false)
|
||||
{
|
||||
$this->setDefaultValue($defaultVal);
|
||||
|
||||
parent::__construct($name);
|
||||
}
|
||||
|
||||
@ -33,7 +32,7 @@ class DBBoolean extends DBField
|
||||
'precision' => 1,
|
||||
'sign' => 'unsigned',
|
||||
'null' => 'not null',
|
||||
'default' => $this->defaultVal,
|
||||
'default' => $this->getDefaultValue(),
|
||||
'arrayValue' => $this->arrayValue
|
||||
];
|
||||
$values = ['type' => 'boolean', 'parts' => $parts];
|
||||
|
@ -108,6 +108,7 @@ abstract class DBField extends ModelData implements DBIndexable, FieldValidation
|
||||
* Default value in the database.
|
||||
* Might be overridden on DataObject-level, but still useful for setting defaults on
|
||||
* already existing records after a db-build.
|
||||
* @deprecated 5.4.0 Use getDefaultValue() and setDefaultValue() instead
|
||||
*/
|
||||
private mixed $defaultValue = null;
|
||||
|
||||
|
@ -13,8 +13,7 @@ class DBFloat extends DBField
|
||||
{
|
||||
public function __construct(?string $name = null, float|int $defaultVal = 0)
|
||||
{
|
||||
$this->setDefaultValue(is_float($defaultVal) ? $defaultVal : (float) 0);
|
||||
|
||||
$this->setDefaultValue((float) $defaultVal);
|
||||
parent::__construct($name);
|
||||
}
|
||||
|
||||
@ -23,7 +22,7 @@ class DBFloat extends DBField
|
||||
$parts = [
|
||||
'datatype' => 'float',
|
||||
'null' => 'not null',
|
||||
'default' => $this->defaultVal,
|
||||
'default' => $this->getDefaultValue(),
|
||||
'arrayValue' => $this->arrayValue
|
||||
];
|
||||
$values = ['type' => 'float', 'parts' => $parts];
|
||||
|
@ -58,7 +58,7 @@ class DBInt extends DBField
|
||||
'datatype' => 'int',
|
||||
'precision' => 11,
|
||||
'null' => 'not null',
|
||||
'default' => $this->defaultVal,
|
||||
'default' => $this->getDefaultValue(),
|
||||
'arrayValue' => $this->arrayValue
|
||||
];
|
||||
$values = ['type' => 'int', 'parts' => $parts];
|
||||
|
@ -26,6 +26,8 @@ class DeprecationTest extends SapphireTest
|
||||
|
||||
private bool $noticesWereEnabled = false;
|
||||
|
||||
private bool $showSupportedNoticesWasEnabled = false;
|
||||
|
||||
protected function setup(): void
|
||||
{
|
||||
// Use custom error handler for two reasons:
|
||||
@ -34,6 +36,7 @@ class DeprecationTest extends SapphireTest
|
||||
// https://github.com/laminas/laminas-di/pull/30#issuecomment-927585210
|
||||
parent::setup();
|
||||
$this->noticesWereEnabled = Deprecation::isEnabled();
|
||||
$this->showSupportedNoticesWasEnabled = Deprecation::getShowNoticesCalledFromSupportedCode();
|
||||
$this->oldHandler = set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) {
|
||||
if ($errno === E_USER_DEPRECATED) {
|
||||
if (str_contains($errstr, 'SilverStripe\\Dev\\Tests\\DeprecationTest')) {
|
||||
@ -49,6 +52,8 @@ class DeprecationTest extends SapphireTest
|
||||
// Fallback to default PHP error handler
|
||||
return false;
|
||||
});
|
||||
// This is required to clear out the notice from instantiating DeprecationTestObject in TableBuilder::buildTables().
|
||||
Deprecation::outputNotices();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
@ -58,6 +63,7 @@ class DeprecationTest extends SapphireTest
|
||||
} else {
|
||||
Deprecation::disable();
|
||||
}
|
||||
Deprecation::setShowNoticesCalledFromSupportedCode($this->showSupportedNoticesWasEnabled);
|
||||
restore_error_handler();
|
||||
$this->oldHandler = null;
|
||||
parent::tearDown();
|
||||
@ -69,6 +75,18 @@ class DeprecationTest extends SapphireTest
|
||||
return 'abc';
|
||||
}
|
||||
|
||||
private function myDeprecatedMethodNoReplacement(): string
|
||||
{
|
||||
Deprecation::noticeWithNoReplacment('1.2.3');
|
||||
return 'abc';
|
||||
}
|
||||
|
||||
private function enableDeprecationNotices(bool $showNoReplacementNotices = false): void
|
||||
{
|
||||
Deprecation::enable($showNoReplacementNotices);
|
||||
Deprecation::setShowNoticesCalledFromSupportedCode(true);
|
||||
}
|
||||
|
||||
public function testNotice()
|
||||
{
|
||||
$message = implode(' ', [
|
||||
@ -78,7 +96,7 @@ class DeprecationTest extends SapphireTest
|
||||
]);
|
||||
$this->expectException(DeprecationTestException::class);
|
||||
$this->expectExceptionMessage($message);
|
||||
Deprecation::enable();
|
||||
$this->enableDeprecationNotices();
|
||||
$ret = $this->myDeprecatedMethod();
|
||||
$this->assertSame('abc', $ret);
|
||||
// call outputNotices() directly because the regular shutdown function that emits
|
||||
@ -86,6 +104,29 @@ class DeprecationTest extends SapphireTest
|
||||
Deprecation::outputNotices();
|
||||
}
|
||||
|
||||
public function testNoticeNoReplacement()
|
||||
{
|
||||
$message = implode(' ', [
|
||||
'SilverStripe\Dev\Tests\DeprecationTest->myDeprecatedMethodNoReplacement is deprecated.',
|
||||
'Will be removed without equivalent functionality to replace it.',
|
||||
'Called from SilverStripe\Dev\Tests\DeprecationTest->testNoticeNoReplacement.'
|
||||
]);
|
||||
$this->expectDeprecation();
|
||||
$this->expectDeprecationMessage($message);
|
||||
$this->enableDeprecationNotices(true);
|
||||
$ret = $this->myDeprecatedMethodNoReplacement();
|
||||
$this->assertSame('abc', $ret);
|
||||
Deprecation::outputNotices();
|
||||
}
|
||||
|
||||
public function testNoticeNoReplacementNoSupressed()
|
||||
{
|
||||
$this->enableDeprecationNotices();
|
||||
$ret = $this->myDeprecatedMethodNoReplacement();
|
||||
$this->assertSame('abc', $ret);
|
||||
Deprecation::outputNotices();
|
||||
}
|
||||
|
||||
public function testCallUserFunc()
|
||||
{
|
||||
$message = implode(' ', [
|
||||
@ -95,7 +136,7 @@ class DeprecationTest extends SapphireTest
|
||||
]);
|
||||
$this->expectException(DeprecationTestException::class);
|
||||
$this->expectExceptionMessage($message);
|
||||
Deprecation::enable();
|
||||
$this->enableDeprecationNotices();
|
||||
$ret = call_user_func([$this, 'myDeprecatedMethod']);
|
||||
$this->assertSame('abc', $ret);
|
||||
Deprecation::outputNotices();
|
||||
@ -110,7 +151,7 @@ class DeprecationTest extends SapphireTest
|
||||
]);
|
||||
$this->expectException(DeprecationTestException::class);
|
||||
$this->expectExceptionMessage($message);
|
||||
Deprecation::enable();
|
||||
$this->enableDeprecationNotices();
|
||||
$ret = call_user_func_array([$this, 'myDeprecatedMethod'], []);
|
||||
$this->assertSame('abc', $ret);
|
||||
Deprecation::outputNotices();
|
||||
@ -118,7 +159,7 @@ class DeprecationTest extends SapphireTest
|
||||
|
||||
public function testwithSuppressedNoticeDefault()
|
||||
{
|
||||
Deprecation::enable();
|
||||
$this->enableDeprecationNotices();
|
||||
$ret = Deprecation::withSuppressedNotice(function () {
|
||||
return $this->myDeprecatedMethod();
|
||||
});
|
||||
@ -135,7 +176,7 @@ class DeprecationTest extends SapphireTest
|
||||
]);
|
||||
$this->expectException(DeprecationTestException::class);
|
||||
$this->expectExceptionMessage($message);
|
||||
Deprecation::enable(true);
|
||||
$this->enableDeprecationNotices(true);
|
||||
$ret = Deprecation::withSuppressedNotice(function () {
|
||||
return $this->myDeprecatedMethod();
|
||||
});
|
||||
@ -152,7 +193,7 @@ class DeprecationTest extends SapphireTest
|
||||
]);
|
||||
$this->expectException(DeprecationTestException::class);
|
||||
$this->expectExceptionMessage($message);
|
||||
Deprecation::enable(true);
|
||||
$this->enableDeprecationNotices(true);
|
||||
$ret = Deprecation::withSuppressedNotice(function () {
|
||||
return call_user_func([$this, 'myDeprecatedMethod']);
|
||||
});
|
||||
@ -169,7 +210,7 @@ class DeprecationTest extends SapphireTest
|
||||
]);
|
||||
$this->expectException(DeprecationTestException::class);
|
||||
$this->expectExceptionMessage($message);
|
||||
Deprecation::enable(true);
|
||||
$this->enableDeprecationNotices(true);
|
||||
Deprecation::withSuppressedNotice(function () {
|
||||
Deprecation::notice('123', 'My message.');
|
||||
});
|
||||
@ -185,7 +226,7 @@ class DeprecationTest extends SapphireTest
|
||||
]);
|
||||
$this->expectException(DeprecationTestException::class);
|
||||
$this->expectExceptionMessage($message);
|
||||
Deprecation::enable(true);
|
||||
$this->enableDeprecationNotices(true);
|
||||
// using this syntax because my IDE was complaining about DeprecationTestObject not existing
|
||||
// when trying to use `new DeprecationTestObject();`
|
||||
$class = DeprecationTestObject::class;
|
||||
@ -202,7 +243,7 @@ class DeprecationTest extends SapphireTest
|
||||
]);
|
||||
$this->expectException(DeprecationTestException::class);
|
||||
$this->expectExceptionMessage($message);
|
||||
Deprecation::enable(true);
|
||||
$this->enableDeprecationNotices(true);
|
||||
Injector::inst()->get(DeprecationTestObject::class);
|
||||
Deprecation::outputNotices();
|
||||
}
|
||||
@ -218,6 +259,50 @@ class DeprecationTest extends SapphireTest
|
||||
Deprecation::outputNotices();
|
||||
}
|
||||
|
||||
public function testshowNoticesCalledFromSupportedCode()
|
||||
{
|
||||
$this->expectNotToPerformAssertions();
|
||||
$this->enableDeprecationNotices(true);
|
||||
// showNoticesCalledFromSupportedCode is set to true by default for these unit tests
|
||||
// as it is testing code within vendor/silverstripe
|
||||
// This test is to ensure that the method works as expected when we disable this
|
||||
// and we should expect no exceptions to be thrown
|
||||
//
|
||||
// Note specifically NOT testing the following because it's counted as being called
|
||||
// from phpunit itself, which is not considered supported code
|
||||
// Deprecation::withSuppressedNotice(function () {
|
||||
// Deprecation::notice('123', 'My message.');
|
||||
// });
|
||||
Deprecation::setShowNoticesCalledFromSupportedCode(false);
|
||||
// notice()
|
||||
$this->myDeprecatedMethod();
|
||||
// noticeNoReplacement()
|
||||
$this->myDeprecatedMethodNoReplacement();
|
||||
// callUserFunc()
|
||||
call_user_func([$this, 'myDeprecatedMethod']);
|
||||
// callUserFuncArray()
|
||||
call_user_func_array([$this, 'myDeprecatedMethod'], []);
|
||||
// withSuppressedNotice()
|
||||
Deprecation::withSuppressedNotice(
|
||||
fn() => $this->myDeprecatedMethod()
|
||||
);
|
||||
// withSuppressedNoticeTrue()
|
||||
Deprecation::withSuppressedNotice(function () {
|
||||
$this->myDeprecatedMethod();
|
||||
});
|
||||
// withSuppressedNoticeTrueCallUserFunc()
|
||||
Deprecation::withSuppressedNotice(function () {
|
||||
call_user_func([$this, 'myDeprecatedMethod']);
|
||||
});
|
||||
// classWithSuppressedNotice()
|
||||
$class = DeprecationTestObject::class;
|
||||
new $class();
|
||||
// classWithInjectorwithSuppressedNotice()
|
||||
Injector::inst()->get(DeprecationTestObject::class);
|
||||
// Output notices - there should be none
|
||||
Deprecation::outputNotices();
|
||||
}
|
||||
|
||||
// The following tests would be better to put in the silverstripe/config module, however this is not
|
||||
// possible to do in a clean way as the config for the DeprecationTestObject will not load if it
|
||||
// is inside the silverstripe/config directory, as there is no _config.php file or _config folder.
|
||||
@ -232,7 +317,7 @@ class DeprecationTest extends SapphireTest
|
||||
]);
|
||||
$this->expectException(DeprecationTestException::class);
|
||||
$this->expectExceptionMessage($message);
|
||||
Deprecation::enable();
|
||||
$this->enableDeprecationNotices();
|
||||
Config::inst()->get(DeprecationTestObject::class, 'first_config');
|
||||
Deprecation::outputNotices();
|
||||
}
|
||||
@ -245,7 +330,7 @@ class DeprecationTest extends SapphireTest
|
||||
]);
|
||||
$this->expectException(DeprecationTestException::class);
|
||||
$this->expectExceptionMessage($message);
|
||||
Deprecation::enable();
|
||||
$this->enableDeprecationNotices();
|
||||
Config::inst()->get(DeprecationTestObject::class, 'second_config');
|
||||
Deprecation::outputNotices();
|
||||
}
|
||||
@ -255,7 +340,7 @@ class DeprecationTest extends SapphireTest
|
||||
$message = 'Config SilverStripe\Dev\Tests\DeprecationTest\DeprecationTestObject.third_config is deprecated.';
|
||||
$this->expectException(DeprecationTestException::class);
|
||||
$this->expectExceptionMessage($message);
|
||||
Deprecation::enable();
|
||||
$this->enableDeprecationNotices();
|
||||
Config::inst()->get(DeprecationTestObject::class, 'third_config');
|
||||
Deprecation::outputNotices();
|
||||
}
|
||||
@ -268,7 +353,7 @@ class DeprecationTest extends SapphireTest
|
||||
]);
|
||||
$this->expectException(DeprecationTestException::class);
|
||||
$this->expectExceptionMessage($message);
|
||||
Deprecation::enable();
|
||||
$this->enableDeprecationNotices();
|
||||
Config::modify()->set(DeprecationTestObject::class, 'first_config', 'abc');
|
||||
Deprecation::outputNotices();
|
||||
}
|
||||
@ -281,7 +366,7 @@ class DeprecationTest extends SapphireTest
|
||||
]);
|
||||
$this->expectException(DeprecationTestException::class);
|
||||
$this->expectExceptionMessage($message);
|
||||
Deprecation::enable();
|
||||
$this->enableDeprecationNotices();
|
||||
Config::modify()->merge(DeprecationTestObject::class, 'array_config', ['abc']);
|
||||
Deprecation::outputNotices();
|
||||
}
|
||||
@ -361,7 +446,7 @@ class DeprecationTest extends SapphireTest
|
||||
switch ($varName) {
|
||||
case 'SS_DEPRECATION_ENABLED':
|
||||
if ($configVal) {
|
||||
Deprecation::enable();
|
||||
$this->enableDeprecationNotices();
|
||||
} else {
|
||||
Deprecation::disable();
|
||||
}
|
||||
@ -533,7 +618,7 @@ class DeprecationTest extends SapphireTest
|
||||
private function setEnabledViaStatic(bool $enabled): void
|
||||
{
|
||||
if ($enabled) {
|
||||
Deprecation::enable();
|
||||
$this->enableDeprecationNotices();
|
||||
} else {
|
||||
Deprecation::disable();
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace SilverStripe\Forms\Tests\GridField;
|
||||
|
||||
use LogicException;
|
||||
use ReflectionMethod;
|
||||
use SilverStripe\Control\HTTPRequest;
|
||||
use SilverStripe\Core\Config\Config;
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
@ -117,6 +118,29 @@ class GridFieldFilterHeaderTest extends SapphireTest
|
||||
$this->assertEquals('testfield', $searchSchema->gridfield);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the private method that returns the placeholder for the search field
|
||||
*/
|
||||
public function testGetPlaceHolder()
|
||||
{
|
||||
$gridField = new GridField('test');
|
||||
$filterHeader = new GridFieldFilterHeader();
|
||||
$reflectionGetPlaceHolder = new ReflectionMethod($filterHeader, 'getPlaceHolder');
|
||||
$reflectionGetPlaceHolder->setAccessible(true);
|
||||
|
||||
// No explicit placeholder or model i18n_plural_name method
|
||||
$this->assertSame('Search "ArrayData"', $reflectionGetPlaceHolder->invoke($filterHeader, new ArrayData()));
|
||||
|
||||
// No explicit placeholder, but model has i18n_plural_name method
|
||||
$model = new DataObject();
|
||||
$this->assertSame('Search "' . $model->i18n_plural_name() . '"', $reflectionGetPlaceHolder->invoke($filterHeader, $model));
|
||||
|
||||
// Explicit placeholder is set, which overrides both of the above cases
|
||||
$filterHeader->setPlaceHolderText('This is the text');
|
||||
$this->assertSame('This is the text', $reflectionGetPlaceHolder->invoke($filterHeader, $model));
|
||||
$this->assertSame('This is the text', $reflectionGetPlaceHolder->invoke($filterHeader, new ArrayData()));
|
||||
}
|
||||
|
||||
public function testHandleActionReset()
|
||||
{
|
||||
// Init Grid state with some pre-existing filters
|
||||
|
Loading…
Reference in New Issue
Block a user