Issue #3402007 by marvil07, mstrelan, quietone, smustgrave, larowlan: Fix strict type errors in test modules

merge-requests/3548/merge
Dave Long 2024-08-08 12:09:03 +01:00
parent e9574d73b2
commit d43e2ba8f3
No known key found for this signature in database
GPG Key ID: ED52AE211E142771
13 changed files with 21 additions and 18 deletions

View File

@ -20,7 +20,7 @@ class AnnounceTestHttpClientMiddleware {
return function ($handler) {
return function (RequestInterface $request, array $options) use ($handler): PromiseInterface {
$test_end_point = \Drupal::state()->get('announce_test_endpoint');
if ($test_end_point && str_contains($request->getUri(), '://www.drupal.org/announcements.json')) {
if ($test_end_point && str_contains((string) $request->getUri(), '://www.drupal.org/announcements.json')) {
// Only override $uri if it matches the advisories JSON feed to avoid
// changing any other uses of the 'http_client' service during tests with
// this module installed.

View File

@ -22,7 +22,7 @@ class TestMultipleFormController extends ControllerBase {
// see if there's only one in the tests.
$post_render_callable = function ($elements) {
$matches = [];
preg_match_all('<form\s(.*?)action="(.*?)"(.*)>', $elements, $matches);
preg_match_all('<form\s(.*?)action="(.*?)"(.*)>', (string) $elements, $matches);
$action_values = $matches[2];

View File

@ -18,7 +18,7 @@ class AdvisoryTestClientMiddleware {
return function ($handler) {
return function (RequestInterface $request, array $options) use ($handler): PromiseInterface {
$test_end_point = \Drupal::state()->get('advisories_test_endpoint');
if ($test_end_point && str_contains($request->getUri(), '://updates.drupal.org/psa.json')) {
if ($test_end_point && str_contains((string) $request->getUri(), '://updates.drupal.org/psa.json')) {
// Only override $uri if it matches the advisories JSON feed to avoid
// changing any other uses of the 'http_client' service during tests with
// this module installed.

View File

@ -59,7 +59,7 @@ class TestController extends ControllerBase {
* @return \Symfony\Component\HttpFoundation\Response
* CSRF token.
*/
public function getCsrfToken($num) {
public function getCsrfToken(int $num) {
sleep($num);
return new JsonResponse($this->tokenGenerator->get());
}

View File

@ -94,7 +94,7 @@ class EarlyRenderingTestController extends ControllerBase {
public function responseEarly() {
$render_array = $this->earlyRenderContent();
return new Response($this->renderer->render($render_array));
return new Response((string) $this->renderer->render($render_array));
}
public function responseWithAttachments() {
@ -103,7 +103,7 @@ class EarlyRenderingTestController extends ControllerBase {
public function responseWithAttachmentsEarly() {
$render_array = $this->earlyRenderContent();
return new AttachmentsTestResponse($this->renderer->render($render_array));
return new AttachmentsTestResponse((string) $this->renderer->render($render_array));
}
public function cacheableResponse() {
@ -112,7 +112,7 @@ class EarlyRenderingTestController extends ControllerBase {
public function cacheableResponseEarly() {
$render_array = $this->earlyRenderContent();
return new CacheableTestResponse($this->renderer->render($render_array));
return new CacheableTestResponse((string) $this->renderer->render($render_array));
}
public function domainObject() {

View File

@ -61,7 +61,7 @@ class ErrorTestController extends ControllerBase {
public function generateFatalErrors() {
$function = function (array $test) {
};
// Use an incorrect parameter type, string, for testing a fatal error.
$function("test-string");
return [];
}

View File

@ -43,7 +43,10 @@ class FormTestClickedButtonForm extends FormBase {
foreach ($args as $arg) {
$name = 'button' . ++$i;
// 's', 'b', or 'i' in the argument define the button type wanted.
if (str_contains($arg, 's')) {
if (!is_string($arg)) {
$type = NULL;
}
elseif (str_contains($arg, 's')) {
$type = 'submit';
}
elseif (str_contains($arg, 'b')) {

View File

@ -59,7 +59,7 @@ class FormTestInputForgeryForm extends FormBase implements TrustedCallbackInterf
* @see \Drupal\Tests\system\Functional\Form\FormTest::testInputForgery()
*/
public static function postRender($rendered_form) {
return str_replace('value="two"', 'value="FORGERY"', $rendered_form);
return str_replace('value="two"', 'value="FORGERY"', (string) $rendered_form);
}
/**

View File

@ -11,7 +11,7 @@ class RenderArrayNonHtmlSubscriberTestController extends ControllerBase {
* @return string
*/
public function rawString() {
return new Response($this->t('Raw controller response.'));
return new Response((string) $this->t('Raw controller response.'));
}
/**

View File

@ -111,7 +111,7 @@ class SessionTestController extends ControllerBase {
*/
public function setMessage() {
$this->messenger()->addStatus($this->t('This is a dummy message.'));
return new Response($this->t('A message was set.'));
return new Response((string) $this->t('A message was set.'));
// Do not return anything, so the current request does not result in a themed
// page with messages. The message will be displayed in the following request
// instead.
@ -240,8 +240,8 @@ class SessionTestController extends ControllerBase {
/** @var \Drupal\session_test\Session\TestSessionBag */
$bag = $request->getSession()->getBag(TestSessionBag::BAG_NAME);
return new Response(empty($bag->hasFlag())
? $this->t('Flag is absent from session bag')
: $this->t('Flag is present in session bag')
? (string) $this->t('Flag is absent from session bag')
: (string) $this->t('Flag is present in session bag')
);
}

View File

@ -27,7 +27,7 @@ class SessionTestSubscriber implements EventSubscriberInterface {
*/
public function onKernelRequestSessionTest(RequestEvent $event) {
$session = $event->getRequest()->getSession();
$this->emptySession = (int) !($session && $session->start());
$this->emptySession = !($session && $session->start());
}
/**
@ -39,7 +39,7 @@ class SessionTestSubscriber implements EventSubscriberInterface {
public function onKernelResponseSessionTest(ResponseEvent $event) {
// Set header for session testing.
$response = $event->getResponse();
$response->headers->set('X-Session-Empty', $this->emptySession);
$response->headers->set('X-Session-Empty', $this->emptySession ? '1' : '0');
}
/**

View File

@ -303,7 +303,7 @@ class SystemTestController extends ControllerBase implements TrustedCallbackInte
$response = new CacheableResponse();
$response->headers->set($query['name'], $query['value']);
$response->getCacheableMetadata()->addCacheContexts(['url.query_args:name', 'url.query_args:value']);
$response->setContent($this->t('The following header was set: %name: %value', ['%name' => $query['name'], '%value' => $query['value']]));
$response->setContent((string) $this->t('The following header was set: %name: %value', ['%name' => $query['name'], '%value' => $query['value']]));
return $response;
}

View File

@ -85,7 +85,7 @@ class Test {
* @param int $code
* The status code.
*/
public function httpResponseException($code) {
public function httpResponseException(int $code) {
throw new HttpException($code);
}