Problem/Motivation

The test class in core\modules\user\tests\src\Kernel\UserInstallTest.php uses a regular database query instead of using an entity query.

Proposed resolution

Replace the regular database query with an entity query and then use user storage to load the user entities. Use the loaded user entities in the assertions.

Info for novices:
The entity query for loading the user ids: $user_ids = \Drupal::entityQuery('user')->sort('uid')->execute();
The user entities are loaded with: $users = \Drupal::entityTypeManager()->getStorage('user')->loadMultiple($user_ids);

Remaining tasks

TBD

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

TBD

Comments

daffie created an issue. See original summary.

pavnish’s picture

Assigned: Unassigned » pavnish

Working on it

pavnish’s picture

Assigned: pavnish » Unassigned
Status: Active » Needs review
StatusFileSize
new2.02 KB

@daffie Hi
Database query with an entity query in UserInstallTest for user has been changes as suggested.
I am also ran the testcase in my local it's working fine.
Please review this patch.

Thanks
Pavnish

daffie’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/modules/user/tests/src/Kernel/UserInstallTest.php
    @@ -33,22 +33,22 @@ protected function setUp(): void {
    +    $this->assertFalse(empty($anon->get('uuid')->value), 'Anon user has a UUID');
    +    $this->assertFalse(empty($admin->get('uuid')->value), 'Admin user has a UUID');
    

    assertNotEmpty() also let's use the uuid() method since we have a User object...

        $this->assertNotEmpty($anon->uuid(), 'Anon user has a UUID');
        $this->assertNotEmpty($admin->uuid(), 'Admin user has a UUID');
    
  2. +++ b/core/modules/user/tests/src/Kernel/UserInstallTest.php
    @@ -33,22 +33,22 @@ protected function setUp(): void {
    -    $this->assertEqual($anon->langcode, \Drupal::languageManager()->getDefaultLanguage()->getId());
    -    $this->assertEqual($admin->langcode, \Drupal::languageManager()->getDefaultLanguage()->getId());
    +    $this->assertEqual($anon->get('langcode')->value, \Drupal::languageManager()->getDefaultLanguage()->getId());
    +    $this->assertEqual($admin->get('langcode')->value, \Drupal::languageManager()->getDefaultLanguage()->getId());
    

    Let's take the opportunity to use assertEquals as assertEqual() is deprecated and hardcode test assumptions because it make things easier to read.

        // Test that the anonymous and administrators languages are equal to the
        // site's default language.
        $this->assertEquals('en', $anon->language()->getId());
        $this->assertEquals('en', $admin->language()->getId());
    
  3. +++ b/core/modules/user/tests/src/Kernel/UserInstallTest.php
    @@ -33,22 +33,22 @@ protected function setUp(): void {
         // Test that the administrator is active.
    -    $this->assertEqual($admin->status, 1);
    +    $this->assertEqual($admin->get('status')->value, 1);
         // Test that the anonymous user is blocked.
    -    $this->assertEqual($anon->status, 0);
    +    $this->assertEqual($anon->get('status')->value, 0);
    

    As above...

        // Test that the administrator is active.
        $this->assertTrue($admin->isActive());
        // Test that the anonymous user is blocked.
        $this->assertTrue($anon->isBlocked());
    
adityasingh’s picture

Assigned: Unassigned » adityasingh

I'm working on it.

adityasingh’s picture

Assigned: adityasingh » Unassigned
Status: Needs work » Needs review
StatusFileSize
new1.85 KB
new1.45 KB

Hi @alexpott
Made changes as you suggested please review.

daffie’s picture

Status: Needs review » Needs work

All changes look good. Just one nitpick:

+++ b/core/modules/user/tests/src/Kernel/UserInstallTest.php
@@ -33,22 +33,22 @@ protected function setUp(): void {
+    $this->assertNotEmpty($admin->uuid(), 'Admin user has a UUID'); ¶

Nitpick: Remove the extra space on the end of the line.

adityasingh’s picture

Assigned: Unassigned » adityasingh
adityasingh’s picture

Status: Needs work » Needs review
StatusFileSize
new1.85 KB

Hi @daffie
Space issue is fixed please review.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

All points of @alexpott are addressed.
Back to RTBC.

alexpott’s picture

Version: 9.1.x-dev » 8.9.x-dev
Status: Reviewed & tested by the community » Fixed

As a test-only change backported to 8.9.x to keep tests aligned - I ran on 8.9.x locally before committing.

Committed and pushed 5ded531763 to 9.1.x and da1bddfb40 to 9.0.x and 243e291028 to 8.9.x. Thanks!

diff --git a/core/modules/user/tests/src/Kernel/UserInstallTest.php b/core/modules/user/tests/src/Kernel/UserInstallTest.php
index e4f804e9c9..1ab0df44ac 100644
--- a/core/modules/user/tests/src/Kernel/UserInstallTest.php
+++ b/core/modules/user/tests/src/Kernel/UserInstallTest.php
@@ -2,7 +2,6 @@
 
 namespace Drupal\Tests\user\Kernel;
 
-use Drupal\Core\Database\Database;
 use Drupal\KernelTests\KernelTestBase;
 
 /**

Fixed unused use on commit.

  • alexpott committed 5ded531 on 9.1.x
    Issue #3151520 by adityasingh, pavnish, daffie, alexpott: Replace the...

  • alexpott committed da1bddf on 9.0.x
    Issue #3151520 by adityasingh, pavnish, daffie, alexpott: Replace the...

  • alexpott committed 243e291 on 8.9.x
    Issue #3151520 by adityasingh, pavnish, daffie, alexpott: Replace the...
adityasingh’s picture

Assigned: adityasingh » Unassigned

Thanks

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.