function EntityReferenceSelectionAccessTest::testUserHandler

Same name in other branches
  1. 8.9.x core/modules/system/tests/src/Functional/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php \Drupal\Tests\system\Functional\Entity\EntityReferenceSelection\EntityReferenceSelectionAccessTest::testUserHandler()
  2. 10 core/modules/system/tests/src/Kernel/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php \Drupal\Tests\system\Kernel\Entity\EntityReferenceSelection\EntityReferenceSelectionAccessTest::testUserHandler()
  3. 11.x core/modules/system/tests/src/Kernel/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php \Drupal\Tests\system\Kernel\Entity\EntityReferenceSelection\EntityReferenceSelectionAccessTest::testUserHandler()

Tests the user-specific overrides of the entity handler.

File

core/modules/system/tests/src/Functional/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php, line 246

Class

EntityReferenceSelectionAccessTest
Tests for the base handlers provided by Entity Reference.

Namespace

Drupal\Tests\system\Functional\Entity\EntityReferenceSelection

Code

public function testUserHandler() {
    $selection_options = [
        'target_type' => 'user',
        'handler' => 'default',
        'target_bundles' => NULL,
        'include_anonymous' => TRUE,
    ];
    // Build a set of test data.
    $user_values = [
        'anonymous' => User::load(0),
        'admin' => User::load(1),
        'non_admin' => [
            'name' => 'non_admin <&>',
            'mail' => 'non_admin@example.com',
            'roles' => [],
            'pass' => \Drupal::service('password_generator')->generate(),
            'status' => 1,
        ],
        'blocked' => [
            'name' => 'blocked <&>',
            'mail' => 'blocked@example.com',
            'roles' => [],
            'pass' => \Drupal::service('password_generator')->generate(),
            'status' => 0,
        ],
    ];
    $user_values['anonymous']->name = $this->config('user.settings')
        ->get('anonymous');
    $users = [];
    $user_labels = [];
    foreach ($user_values as $key => $values) {
        if (is_array($values)) {
            $account = User::create($values);
            $account->save();
        }
        else {
            $account = $values;
        }
        $users[$key] = $account;
        $user_labels[$key] = Html::escape($account->getAccountName());
    }
    // Test as a non-admin.
    $this->setCurrentUser($users['non_admin']);
    $referenceable_tests = [
        [
            'arguments' => [
                [
                    NULL,
                    'CONTAINS',
                ],
            ],
            'result' => [
                'user' => [
                    $users['admin']->id() => $user_labels['admin'],
                    $users['non_admin']->id() => $user_labels['non_admin'],
                ],
            ],
        ],
        [
            'arguments' => [
                [
                    'non_admin',
                    'CONTAINS',
                ],
                [
                    'NON_ADMIN',
                    'CONTAINS',
                ],
            ],
            'result' => [
                'user' => [
                    $users['non_admin']->id() => $user_labels['non_admin'],
                ],
            ],
        ],
        [
            'arguments' => [
                [
                    'invalid user',
                    'CONTAINS',
                ],
            ],
            'result' => [],
        ],
        [
            'arguments' => [
                [
                    'blocked',
                    'CONTAINS',
                ],
            ],
            'result' => [],
        ],
    ];
    $this->assertReferenceable($selection_options, $referenceable_tests, 'User handler');
    $this->setCurrentUser($users['admin']);
    $referenceable_tests = [
        [
            'arguments' => [
                [
                    NULL,
                    'CONTAINS',
                ],
            ],
            'result' => [
                'user' => [
                    $users['anonymous']->id() => $user_labels['anonymous'],
                    $users['admin']->id() => $user_labels['admin'],
                    $users['non_admin']->id() => $user_labels['non_admin'],
                    $users['blocked']->id() => $user_labels['blocked'],
                ],
            ],
        ],
        [
            'arguments' => [
                [
                    'blocked',
                    'CONTAINS',
                ],
            ],
            'result' => [
                'user' => [
                    $users['blocked']->id() => $user_labels['blocked'],
                ],
            ],
        ],
        [
            'arguments' => [
                [
                    'Anonymous',
                    'CONTAINS',
                ],
                [
                    'anonymous',
                    'CONTAINS',
                ],
            ],
            'result' => [
                'user' => [
                    $users['anonymous']->id() => $user_labels['anonymous'],
                ],
            ],
        ],
    ];
    $this->assertReferenceable($selection_options, $referenceable_tests, 'User handler (admin)');
    // Test the 'include_anonymous' option.
    $selection_options['include_anonymous'] = FALSE;
    $referenceable_tests = [
        [
            'arguments' => [
                [
                    'Anonymous',
                    'CONTAINS',
                ],
                [
                    'anonymous',
                    'CONTAINS',
                ],
            ],
            'result' => [],
        ],
    ];
    $this->assertReferenceable($selection_options, $referenceable_tests, 'User handler (does not include anonymous)');
    // Check that the Anonymous user is not included in the results when no
    // label matching is done, for example when using the 'options_select'
    // widget.
    $referenceable_tests = [
        [
            'arguments' => [
                [
                    NULL,
                ],
            ],
            'result' => [
                'user' => [
                    $users['admin']->id() => $user_labels['admin'],
                    $users['non_admin']->id() => $user_labels['non_admin'],
                    $users['blocked']->id() => $user_labels['blocked'],
                ],
            ],
        ],
    ];
    $this->assertReferenceable($selection_options, $referenceable_tests, 'User handler (does not include anonymous)');
}

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