From acd24cb30d0b79f07d6c3c1645bd11fb5da669f6 Mon Sep 17 00:00:00 2001 From: quietone Date: Wed, 25 Sep 2024 19:57:19 +1200 Subject: [PATCH] Issue #3470203 by mstrelan, smustgrave: Add (more) bool return typehints to protected test helper methods --- .../tests/src/Functional/CommentTestBase.php | 4 ++-- .../Functional/ResourceResponseTestTrait.php | 2 +- .../tests/src/Functional/ResourceTestBase.php | 2 +- .../SettingsTrayTestBase.php | 2 +- .../Plugin/Field/FieldType/FieldTestItem.php | 2 +- .../src/Functional/Cache/CacheTestBase.php | 2 +- .../tests/src/Traits/UserCreationTrait.php | 2 +- .../Plugin/views/Handler/FilterTest.php | 4 ++-- .../BuildTests/Framework/BuildTestBase.php | 2 +- .../FunctionalJavascriptTests/JSWebAssert.php | 2 +- .../Drupal/KernelTests/AssertContentTrait.php | 18 +++++++++--------- .../Commands/TestSiteTearDownCommand.php | 2 +- core/tests/Drupal/Tests/UiHelperTrait.php | 4 ++-- 13 files changed, 24 insertions(+), 24 deletions(-) diff --git a/core/modules/comment/tests/src/Functional/CommentTestBase.php b/core/modules/comment/tests/src/Functional/CommentTestBase.php index 27bd5bf6a87..d7a4f443f55 100644 --- a/core/modules/comment/tests/src/Functional/CommentTestBase.php +++ b/core/modules/comment/tests/src/Functional/CommentTestBase.php @@ -199,7 +199,7 @@ abstract class CommentTestBase extends BrowserTestBase { * @return bool * Boolean indicating whether the comment was found. */ - protected function commentExists(?CommentInterface $comment = NULL, $reply = FALSE) { + protected function commentExists(?CommentInterface $comment = NULL, $reply = FALSE): bool { if ($comment) { $comment_element = $this->cssSelect(($reply ? '.indented ' : '') . 'article#comment-' . $comment->id()); if (empty($comment_element)) { @@ -347,7 +347,7 @@ abstract class CommentTestBase extends BrowserTestBase { * @return bool * Contact info is available. */ - protected function commentContactInfoAvailable() { + protected function commentContactInfoAvailable(): bool { return (bool) preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this->getSession()->getPage()->getContent()); } diff --git a/core/modules/jsonapi/tests/src/Functional/ResourceResponseTestTrait.php b/core/modules/jsonapi/tests/src/Functional/ResourceResponseTestTrait.php index b2954987cec..510035a070a 100644 --- a/core/modules/jsonapi/tests/src/Functional/ResourceResponseTestTrait.php +++ b/core/modules/jsonapi/tests/src/Functional/ResourceResponseTestTrait.php @@ -306,7 +306,7 @@ trait ResourceResponseTestTrait { * @return bool * TRUE if the needle exists is present in the haystack, FALSE otherwise. */ - protected static function collectionHasResourceIdentifier(array $needle, array $haystack) { + protected static function collectionHasResourceIdentifier(array $needle, array $haystack): bool { foreach ($haystack as $resource) { if ($resource['type'] == $needle['type'] && $resource['id'] == $needle['id']) { return TRUE; diff --git a/core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php b/core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php index 6586ac9392b..c37f77ee6b4 100644 --- a/core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php +++ b/core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php @@ -3504,7 +3504,7 @@ abstract class ResourceTestBase extends BrowserTestBase { * TRUE if the field definition is found to be a reference field. FALSE * otherwise. */ - protected static function isReferenceFieldDefinition(FieldDefinitionInterface $field_definition) { + protected static function isReferenceFieldDefinition(FieldDefinitionInterface $field_definition): bool { /** @var \Drupal\Core\Field\TypedData\FieldItemDataDefinition $item_definition */ $item_definition = $field_definition->getItemDefinition(); $main_property = $item_definition->getMainPropertyName(); diff --git a/core/modules/settings_tray/tests/src/FunctionalJavascript/SettingsTrayTestBase.php b/core/modules/settings_tray/tests/src/FunctionalJavascript/SettingsTrayTestBase.php index c03f73b077f..ef3e977a93c 100644 --- a/core/modules/settings_tray/tests/src/FunctionalJavascript/SettingsTrayTestBase.php +++ b/core/modules/settings_tray/tests/src/FunctionalJavascript/SettingsTrayTestBase.php @@ -183,7 +183,7 @@ class SettingsTrayTestBase extends OffCanvasTestBase { * @return bool * TRUE if the label is visible, FALSE if it is not. */ - protected function isLabelInputVisible() { + protected function isLabelInputVisible(): bool { return $this->getSession()->getPage()->find('css', static::LABEL_INPUT_SELECTOR)->isVisible(); } diff --git a/core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/FieldTestItem.php b/core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/FieldTestItem.php index 2a6a468395d..c2dae20e766 100644 --- a/core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/FieldTestItem.php +++ b/core/modules/system/tests/modules/entity_test/src/Plugin/Field/FieldType/FieldTestItem.php @@ -104,7 +104,7 @@ class FieldTestItem extends FieldItemBase { * @return bool * TRUE if the item should be resaved, FALSE otherwise. */ - protected function mustResave() { + protected function mustResave(): bool { return $this->getValue()['value'] == 'resave'; } diff --git a/core/modules/system/tests/src/Functional/Cache/CacheTestBase.php b/core/modules/system/tests/src/Functional/Cache/CacheTestBase.php index 4b8033e9c44..e09f4677196 100644 --- a/core/modules/system/tests/src/Functional/Cache/CacheTestBase.php +++ b/core/modules/system/tests/src/Functional/Cache/CacheTestBase.php @@ -28,7 +28,7 @@ abstract class CacheTestBase extends BrowserTestBase { * @return bool * TRUE on pass, FALSE on fail. */ - protected function checkCacheExists($cid, $var, $bin = NULL) { + protected function checkCacheExists($cid, $var, $bin = NULL): bool { if ($bin == NULL) { $bin = $this->defaultBin; } diff --git a/core/modules/user/tests/src/Traits/UserCreationTrait.php b/core/modules/user/tests/src/Traits/UserCreationTrait.php index 510d722ebd5..09d19c5f339 100644 --- a/core/modules/user/tests/src/Traits/UserCreationTrait.php +++ b/core/modules/user/tests/src/Traits/UserCreationTrait.php @@ -290,7 +290,7 @@ trait UserCreationTrait { * @return bool * TRUE if the permissions are valid, FALSE otherwise. */ - protected function checkPermissions(array $permissions) { + protected function checkPermissions(array $permissions): bool { $available = array_keys(\Drupal::service('user.permissions')->getPermissions()); $valid = TRUE; foreach ($permissions as $permission) { diff --git a/core/modules/views/tests/src/FunctionalJavascript/Plugin/views/Handler/FilterTest.php b/core/modules/views/tests/src/FunctionalJavascript/Plugin/views/Handler/FilterTest.php index 4a574d30853..6ecbeeff788 100644 --- a/core/modules/views/tests/src/FunctionalJavascript/Plugin/views/Handler/FilterTest.php +++ b/core/modules/views/tests/src/FunctionalJavascript/Plugin/views/Handler/FilterTest.php @@ -123,7 +123,7 @@ class FilterTest extends WebDriverTestBase { * @return bool * TRUE if the required number was matched, FALSE otherwise. */ - protected function waitForVisibleElementCount($count, $locator, $timeout = 10000) { + protected function waitForVisibleElementCount($count, $locator, $timeout = 10000): bool { $page = $this->getSession()->getPage(); return $page->waitFor($timeout / 1000, function () use ($count, $page, $locator) { @@ -145,7 +145,7 @@ class FilterTest extends WebDriverTestBase { * @return bool * TRUE if the required number was matched, FALSE otherwise. */ - protected function waitForOnlyContentRows($timeout = 10000) { + protected function waitForOnlyContentRows($timeout = 10000): bool { $page = $this->getSession()->getPage(); return $page->waitFor($timeout / 1000, function () use ($page) { diff --git a/core/tests/Drupal/BuildTests/Framework/BuildTestBase.php b/core/tests/Drupal/BuildTests/Framework/BuildTestBase.php index 14c46215ca3..388680bf0ad 100644 --- a/core/tests/Drupal/BuildTests/Framework/BuildTestBase.php +++ b/core/tests/Drupal/BuildTests/Framework/BuildTestBase.php @@ -503,7 +503,7 @@ abstract class BuildTestBase extends TestCase { * * @return bool */ - protected function checkPortIsAvailable($port) { + protected function checkPortIsAvailable($port): bool { $fp = @fsockopen(self::$hostName, $port, $errno, $errstr, 1); // If fsockopen() fails to connect, probably nothing is listening. // It could be a firewall but that's impossible to detect, so as a diff --git a/core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php b/core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php index 2dc675f8bb1..d3f77f05f8d 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/JSWebAssert.php @@ -398,7 +398,7 @@ JS); * @throws \Behat\Mink\Exception\UnsupportedDriverActionException * When an invalid corner specification is given. */ - private function checkNodeVisibilityInViewport(NodeElement $node, $corner = FALSE) { + private function checkNodeVisibilityInViewport(NodeElement $node, $corner = FALSE): bool { $xpath = $node->getXpath(); // Build the JavaScript to test if the complete element or a specific corner diff --git a/core/tests/Drupal/KernelTests/AssertContentTrait.php b/core/tests/Drupal/KernelTests/AssertContentTrait.php index f5a73f1b648..223d5ea662c 100644 --- a/core/tests/Drupal/KernelTests/AssertContentTrait.php +++ b/core/tests/Drupal/KernelTests/AssertContentTrait.php @@ -557,7 +557,7 @@ trait AssertContentTrait { * @return bool * TRUE on pass, FALSE on fail. */ - protected function assertUniqueText($text, $message = '') { + protected function assertUniqueText($text, $message = ''): bool { return $this->assertUniqueTextHelper($text, $message, NULL, TRUE); } @@ -580,7 +580,7 @@ trait AssertContentTrait { * @return bool * TRUE on pass, FALSE on fail. */ - protected function assertNoUniqueText($text, $message = '') { + protected function assertNoUniqueText($text, $message = ''): bool { return $this->assertUniqueTextHelper($text, $message, NULL, FALSE); } @@ -847,7 +847,7 @@ trait AssertContentTrait { * @return bool * TRUE on pass, FALSE on fail. */ - protected function assertFieldByXPath($xpath, $value = NULL, $message = '') { + protected function assertFieldByXPath($xpath, $value = NULL, $message = ''): bool { $fields = $this->xpath($xpath); return $this->assertFieldsByValue($fields, $value, $message); @@ -933,7 +933,7 @@ trait AssertContentTrait { * @return bool * TRUE on pass, FALSE on fail. */ - protected function assertFieldByName($name, $value = NULL, $message = NULL) { + protected function assertFieldByName($name, $value = NULL, $message = NULL): bool { if (!isset($message)) { if (!isset($value)) { $message = new FormattableMarkup('Found field with name @name', [ @@ -970,7 +970,7 @@ trait AssertContentTrait { * @return bool * TRUE on pass, FALSE on fail. */ - protected function assertNoFieldByName($name, $value = '', $message = '') { + protected function assertNoFieldByName($name, $value = '', $message = ''): bool { return $this->assertNoFieldByXPath($this->constructFieldXpath('name', $name), $value, $message ? $message : new FormattableMarkup('Did not find field by name @name', ['@name' => $name])); } @@ -994,7 +994,7 @@ trait AssertContentTrait { * @return bool * TRUE on pass, FALSE on fail. */ - protected function assertFieldById($id, $value = '', $message = '') { + protected function assertFieldById($id, $value = '', $message = ''): bool { // Cast MarkupInterface objects to string. if (isset($value)) { $value = (string) $value; @@ -1023,7 +1023,7 @@ trait AssertContentTrait { * @return bool * TRUE on pass, FALSE on fail. */ - protected function assertNoFieldById($id, $value = '', $message = '') { + protected function assertNoFieldById($id, $value = '', $message = ''): bool { return $this->assertNoFieldByXPath($this->constructFieldXpath('id', $id), $value, $message ? $message : new FormattableMarkup('Did not find field by id @id', ['@id' => $id])); } @@ -1246,7 +1246,7 @@ trait AssertContentTrait { * @return bool * TRUE on pass, FALSE on fail. */ - protected function assertField($field, $message = '') { + protected function assertField($field, $message = ''): bool { return $this->assertFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field), NULL, $message); } @@ -1265,7 +1265,7 @@ trait AssertContentTrait { * @return bool * TRUE on pass, FALSE on fail. */ - protected function assertNoField($field, $message = '') { + protected function assertNoField($field, $message = ''): bool { return $this->assertNoFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field), NULL, $message); } diff --git a/core/tests/Drupal/TestSite/Commands/TestSiteTearDownCommand.php b/core/tests/Drupal/TestSite/Commands/TestSiteTearDownCommand.php index 9acc0d8d7b9..3d80f3a1c17 100644 --- a/core/tests/Drupal/TestSite/Commands/TestSiteTearDownCommand.php +++ b/core/tests/Drupal/TestSite/Commands/TestSiteTearDownCommand.php @@ -116,7 +116,7 @@ class TestSiteTearDownCommand extends Command { * * @see \Drupal\Core\File\FileSystemInterface::deleteRecursive() */ - protected function fileUnmanagedDeleteRecursive($path, $callback = NULL) { + protected function fileUnmanagedDeleteRecursive($path, $callback = NULL): bool { if (isset($callback)) { call_user_func($callback, $path); } diff --git a/core/tests/Drupal/Tests/UiHelperTrait.php b/core/tests/Drupal/Tests/UiHelperTrait.php index e81a058d61a..047c3da521a 100644 --- a/core/tests/Drupal/Tests/UiHelperTrait.php +++ b/core/tests/Drupal/Tests/UiHelperTrait.php @@ -401,7 +401,7 @@ trait UiHelperTrait { * @return bool * Return TRUE if the user is logged in, FALSE otherwise. */ - protected function drupalUserIsLoggedIn(AccountInterface $account) { + protected function drupalUserIsLoggedIn(AccountInterface $account): bool { $logged_in = FALSE; if (isset($account->sessionId)) { @@ -534,7 +534,7 @@ trait UiHelperTrait { * @return bool * TRUE if test is using DrupalTestBrowser. */ - protected function isTestUsingGuzzleClient() { + protected function isTestUsingGuzzleClient(): bool { $driver = $this->getSession()->getDriver(); if ($driver instanceof BrowserKitDriver) { return $driver->getClient() instanceof DrupalTestBrowser;