Verified Commit 9b26fc7c authored by Michael Strelan's avatar Michael Strelan
Browse files

task: #3629689 Fix usage of with*() without expects() in mocks - round 3

By: mondrake
By: dcam
By: mstrelan
parent 8d0055d8
Loading
Loading
Loading
Loading
Loading
+0 −6
Changes for core/.phpstan-baseline.php: 0 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -46752,12 +46752,6 @@
	'count' => 1,
	'path' => __DIR__ . '/modules/user/tests/src/Unit/UserAccessControlHandlerTest.php',
];
$ignoreErrors[] = [
	'message' => '#^Call to an undefined method Drupal\\\\Core\\\\Password\\\\PasswordInterface\\:\\:method\\(\\)\\.$#',
	'identifier' => 'method.notFound',
	'count' => 5,
	'path' => __DIR__ . '/modules/user/tests/src/Unit/UserAuthenticationTest.php',
];
$ignoreErrors[] = [
	'message' => '#^Storing entity storage as a class property is not recommended\\. Call Drupal\\\\Core\\\\Entity\\\\EntityTypeManagerInterface\\:\\:getStorage\\(\\) at the call\\-site instead\\.$#',
	'identifier' => 'drupal.entityStoragePropertyAssignment',
+1 −14
Changes for core/modules/config_translation/tests/src/Unit/ConfigEntityMapperTest.php: 1 added line, 14 removed lines.
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Routing\Route;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
@@ -38,11 +37,6 @@ class ConfigEntityMapperTest extends UnitTestCase {
   */
  protected EntityTypeManagerInterface&MockObject $entityTypeManager;

  /**
   * The route provider used for testing.
   */
  protected RouteProviderInterface&MockObject $routeProvider;

  /**
   * {@inheritdoc}
   */
@@ -51,13 +45,6 @@ protected function setUp(): void {

    $this->entityTypeManager = $this->createMock('Drupal\Core\Entity\EntityTypeManagerInterface');

    $this->routeProvider = $this->createMock(RouteProviderInterface::class);

    $this->routeProvider
      ->method('getRouteByName')
      ->with('entity.configurable_language.edit_form')
      ->willReturn(new Route('/admin/config/regional/language/edit/{configurable_language}'));

    $definition = [
      'class' => '\Drupal\config_translation\ConfigEntityMapper',
      'base_route_name' => 'entity.configurable_language.edit_form',
@@ -74,7 +61,7 @@ protected function setUp(): void {
      $this->createStub(TypedConfigManagerInterface::class),
      $this->createStub(LocaleConfigManager::class),
      $this->createStub(ConfigMapperManagerInterface::class),
      $this->routeProvider,
      $this->createStub(RouteProviderInterface::class),
      $this->getStringTranslationStub(),
      $this->entityTypeManager,
      $this->createStub(LanguageManagerInterface::class),
+6 −5
Changes for core/modules/path_alias/tests/src/Unit/AliasManagerTest.php: 6 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -154,10 +154,11 @@ public function testGetAliasByPathPrefixList(): void {
    $path_part2 = $this->randomMachineName();
    $path = '/' . $path_part1 . '/' . $path_part2;

    $this->setUpCurrentLanguage();

    $language = new Language(['id' => 'en']);
    $this->languageManager->expects($this->atLeastOnce())
      ->method('getCurrentLanguage');
      ->method('getCurrentLanguage')
      ->with(LanguageInterface::TYPE_URL)
      ->willReturn($language);

    $this->aliasPrefixList->expects($this->once())
      ->method('get')
@@ -287,8 +288,8 @@ public function testGetAliasByPathCachedMissLanguage(): void {
  public function testCacheClear(): void {
    $path = '/path';
    $alias = '/alias';
    $language = $this->setUpCurrentLanguage();

    $language = new Language(['id' => 'en']);
    $this->languageManager->expects($this->never())
      ->method('getCurrentLanguage');

@@ -328,7 +329,7 @@ public function testCacheClear(): void {
  protected function setUpCurrentLanguage(): Language {
    $language = new Language(['id' => 'en']);

    $this->languageManager
    $this->languageManager->expects($this->exactly(2))
      ->method('getCurrentLanguage')
      ->with(LanguageInterface::TYPE_URL)
      ->willReturn($language);
+10 −16
Changes for core/modules/user/tests/src/Unit/UserAuthenticationTest.php: 10 added lines, 16 removed lines.
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
@@ -33,16 +34,14 @@
class UserAuthenticationTest extends UnitTestCase {

  /**
   * The mock user storage.
   * The user storage.
   */
  protected EntityStorageInterface&MockObject $userStorage;

  /**
   * The mocked password service.
   *
   * @var \Drupal\Core\Password\PasswordInterface
   * The password service.
   */
  protected $passwordService;
  protected PasswordInterface&Stub $passwordService;

  /**
   * The user auth object under test.
@@ -78,15 +77,6 @@ protected function setUp(): void {
    $this->userAuth = new UserAuthentication($entity_type_manager, $this->passwordService);
  }

  /**
   * Reinitializes the password service as a mock object.
   */
  protected function setUpMockPasswordService(): void {
    $this->passwordService = $this->createMock(PasswordInterface::class);
    $reflection = new \ReflectionProperty($this->userAuth, 'passwordChecker');
    $reflection->setValue($this->userAuth, $this->passwordService);
  }

  /**
   * Tests lookupAccount() with a valid username.
   */
@@ -148,16 +138,20 @@ public function testAuthenticateWithCorrectPassword(): void {
   * We discovered in https://www.drupal.org/node/2563751 that logging in with a
   * password that is literally "0" was not possible. This test ensures that
   * this regression can't happen again.
   * Differently from other tests, we get a mock of the password service
   * instead of a stub so we can control the return on the "0" input.
   */
  public function testAuthenticateWithZeroPassword(): void {
    $this->setUpMockPasswordService();
    $passwordService = $this->createMock(PasswordInterface::class);
    $reflection = new \ReflectionProperty($this->userAuth, 'passwordChecker');
    $reflection->setValue($this->userAuth, $passwordService);

    $this->userStorage->expects($this->once())
      ->method('loadByProperties')
      ->with(['name' => $this->username])
      ->willReturn([$this->createStub(User::class)]);

    $this->passwordService
    $passwordService->expects($this->once())
      ->method('check')
      ->with('0', 0)
      ->willReturn(TRUE);
+3 −4
Changes for core/tests/Drupal/Tests/Core/Asset/CssCollectionOptimizerLazyUnitTest.php: 3 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub;
use Symfony\Component\HttpFoundation\RequestStack;

/**
@@ -34,17 +34,16 @@ class CssCollectionOptimizerLazyUnitTest extends UnitTestCase {
  /**
   * The file URL generator mock.
   */
  protected FileUrlGeneratorInterface&MockObject $fileUrlGenerator;
  protected FileUrlGeneratorInterface&Stub $fileUrlGenerator;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    $this->fileUrlGenerator = $this->createMock(FileUrlGeneratorInterface::class);
    $this->fileUrlGenerator = $this->createStub(FileUrlGeneratorInterface::class);
    $this->fileUrlGenerator
      ->method('generateString')
      ->with($this->isString())
      ->willReturnCallback(function (string $uri): string {
        return 'generated-relative-url:' . $uri;
      });
Loading