UserTest.php

Same filename in this branch
  1. 9 core/modules/jsonapi/tests/src/Functional/UserTest.php
  2. 9 core/modules/user/tests/src/Kernel/Plugin/migrate/source/d6/UserTest.php
  3. 9 core/modules/user/tests/src/Kernel/Plugin/migrate/source/d7/UserTest.php
Same filename in other branches
  1. 8.9.x core/modules/jsonapi/tests/src/Functional/UserTest.php
  2. 8.9.x core/modules/user/tests/src/Unit/Plugin/Core/Entity/UserTest.php
  3. 8.9.x core/modules/user/tests/src/Kernel/Plugin/migrate/source/d6/UserTest.php
  4. 8.9.x core/modules/user/tests/src/Kernel/Plugin/migrate/source/d7/UserTest.php
  5. 10 core/modules/jsonapi/tests/src/Functional/UserTest.php
  6. 10 core/modules/user/tests/src/Unit/Plugin/Core/Entity/UserTest.php
  7. 10 core/modules/user/tests/src/Kernel/Plugin/migrate/source/d6/UserTest.php
  8. 10 core/modules/user/tests/src/Kernel/Plugin/migrate/source/d7/UserTest.php
  9. 11.x core/modules/jsonapi/tests/src/Functional/UserTest.php
  10. 11.x core/modules/user/tests/src/Unit/Plugin/Core/Entity/UserTest.php
  11. 11.x core/modules/user/tests/src/Kernel/Plugin/migrate/source/d6/UserTest.php
  12. 11.x core/modules/user/tests/src/Kernel/Plugin/migrate/source/d7/UserTest.php

Namespace

Drupal\Tests\user\Unit\Plugin\Core\Entity

File

core/modules/user/tests/src/Unit/Plugin/Core/Entity/UserTest.php

View source
<?php

namespace Drupal\Tests\user\Unit\Plugin\Core\Entity;

use Drupal\Tests\Core\Session\UserSessionTest;
use Drupal\user\RoleInterface;

/**
 * @coversDefaultClass \Drupal\user\Entity\User
 * @group user
 */
class UserTest extends UserSessionTest {
    
    /**
     * {@inheritdoc}
     */
    protected function createUserSession(array $rids = [], $authenticated = FALSE) {
        $user = $this->getMockBuilder('Drupal\\user\\Entity\\User')
            ->disableOriginalConstructor()
            ->onlyMethods([
            'get',
            'id',
        ])
            ->getMock();
        $user->expects($this->any())
            ->method('id')
            ->willReturn($authenticated ? 2 : 0);
        $roles = [];
        foreach ($rids as $rid) {
            $roles[] = (object) [
                'target_id' => $rid,
            ];
        }
        $user->expects($this->any())
            ->method('get')
            ->with('roles')
            ->willReturn($roles);
        return $user;
    }
    
    /**
     * Tests the method getRoles exclude or include locked roles based in param.
     *
     * @see \Drupal\user\Entity\User::getRoles()
     * @covers ::getRoles
     */
    public function testUserGetRoles() {
        // Anonymous user.
        $user = $this->createUserSession([]);
        $this->assertEquals([
            RoleInterface::ANONYMOUS_ID,
        ], $user->getRoles());
        $this->assertEquals([], $user->getRoles(TRUE));
        // Authenticated user.
        $user = $this->createUserSession([], TRUE);
        $this->assertEquals([
            RoleInterface::AUTHENTICATED_ID,
        ], $user->getRoles());
        $this->assertEquals([], $user->getRoles(TRUE));
    }

}

Classes

Title Deprecated Summary
UserTest @coversDefaultClass \Drupal\user\Entity\User @group user

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.