本文整理汇总了PHP中Drupal\user\UserInterface::id方法的典型用法代码示例。如果您正苦于以下问题:PHP UserInterface::id方法的具体用法?PHP UserInterface::id怎么用?PHP UserInterface::id使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\user\UserInterface
的用法示例。
在下文中一共展示了UserInterface::id方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testSubscriberFormFieldSync
/**
* Tests that fields are synchronized using the Subscriber form.
*/
public function testSubscriberFormFieldSync()
{
// Create a subscriber for the user.
$subscriber = Subscriber::create(array('uid' => $this->user->id(), 'mail' => 'anything@example.com'));
$subscriber->save();
// Edit subscriber field and assert user field is changed accordingly.
$this->drupalLogin($this->user);
$this->drupalGet('admin/people/simplenews/edit/' . $subscriber->id());
$this->assertField('field_shared[0][value]');
$this->assertRaw($this->user->field_shared->value);
$new_value = $this->randomMachineName();
$this->drupalPostForm(NULL, array('field_shared[0][value]' => $new_value), t('Save'));
$this->drupalGet('admin/people/simplenews/edit/' . $subscriber->id());
$this->assertRaw($new_value);
$this->user = User::load($this->user->id());
$this->assertEqual($this->user->field_shared->value, $new_value);
// Unset the sync setting and assert field is not synced.
$this->drupalPostForm('admin/config/people/simplenews/settings/subscriber', array('simplenews_sync_fields' => FALSE), t('Save configuration'));
$unsynced_value = $this->randomMachineName();
$this->drupalPostForm('admin/people/simplenews/edit/' . $subscriber->id(), array('field_shared[0][value]' => $unsynced_value), t('Save'));
$this->drupalGet('admin/people/simplenews/edit/' . $subscriber->id());
$this->assertRaw($unsynced_value);
$this->user = User::load($this->user->id());
$this->assertEqual($this->user->field_shared->value, $new_value);
$this->assertNotEqual($this->user->field_shared->value, $unsynced_value);
}
示例2: testCommentNodeCommentStatistics
/**
* Tests the node comment statistics.
*/
function testCommentNodeCommentStatistics()
{
$node_storage = $this->container->get('entity.manager')->getStorage('node');
// Set comments to have subject and preview disabled.
$this->drupalLogin($this->adminUser);
$this->setCommentPreview(DRUPAL_DISABLED);
$this->setCommentForm(TRUE);
$this->setCommentSubject(FALSE);
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
$this->drupalLogout();
// Checks the initial values of node comment statistics with no comment.
$node = $node_storage->load($this->node->id());
$this->assertEqual($node->get('comment')->last_comment_timestamp, $this->node->getCreatedTime(), 'The initial value of node last_comment_timestamp is the node created date.');
$this->assertEqual($node->get('comment')->last_comment_name, NULL, 'The initial value of node last_comment_name is NULL.');
$this->assertEqual($node->get('comment')->last_comment_uid, $this->webUser->id(), 'The initial value of node last_comment_uid is the node uid.');
$this->assertEqual($node->get('comment')->comment_count, 0, 'The initial value of node comment_count is zero.');
// Post comment #1 as web_user2.
$this->drupalLogin($this->webUser2);
$comment_text = $this->randomMachineName();
$this->postComment($this->node, $comment_text);
// Checks the new values of node comment statistics with comment #1.
// The node cache needs to be reset before reload.
$node_storage->resetCache(array($this->node->id()));
$node = $node_storage->load($this->node->id());
$this->assertEqual($node->get('comment')->last_comment_name, NULL, 'The value of node last_comment_name is NULL.');
$this->assertEqual($node->get('comment')->last_comment_uid, $this->webUser2->id(), 'The value of node last_comment_uid is the comment #1 uid.');
$this->assertEqual($node->get('comment')->comment_count, 1, 'The value of node comment_count is 1.');
// Prepare for anonymous comment submission (comment approval enabled).
$this->drupalLogin($this->adminUser);
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array('access comments' => TRUE, 'post comments' => TRUE, 'skip comment approval' => FALSE));
// Ensure that the poster can leave some contact info.
$this->setCommentAnonymous('1');
$this->drupalLogout();
// Post comment #2 as anonymous (comment approval enabled).
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
$anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), '', TRUE);
// Checks the new values of node comment statistics with comment #2 and
// ensure they haven't changed since the comment has not been moderated.
// The node needs to be reloaded with the cache reset.
$node_storage->resetCache(array($this->node->id()));
$node = $node_storage->load($this->node->id());
$this->assertEqual($node->get('comment')->last_comment_name, NULL, 'The value of node last_comment_name is still NULL.');
$this->assertEqual($node->get('comment')->last_comment_uid, $this->webUser2->id(), 'The value of node last_comment_uid is still the comment #1 uid.');
$this->assertEqual($node->get('comment')->comment_count, 1, 'The value of node comment_count is still 1.');
// Prepare for anonymous comment submission (no approval required).
$this->drupalLogin($this->adminUser);
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array('access comments' => TRUE, 'post comments' => TRUE, 'skip comment approval' => TRUE));
$this->drupalLogout();
// Post comment #3 as anonymous.
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
$anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), '', array('name' => $this->randomMachineName()));
$comment_loaded = Comment::load($anonymous_comment->id());
// Checks the new values of node comment statistics with comment #3.
// The node needs to be reloaded with the cache reset.
$node_storage->resetCache(array($this->node->id()));
$node = $node_storage->load($this->node->id());
$this->assertEqual($node->get('comment')->last_comment_name, $comment_loaded->getAuthorName(), 'The value of node last_comment_name is the name of the anonymous user.');
$this->assertEqual($node->get('comment')->last_comment_uid, 0, 'The value of node last_comment_uid is zero.');
$this->assertEqual($node->get('comment')->comment_count, 2, 'The value of node comment_count is 2.');
}
示例3: testProfileTabs
/**
* Tests tabs in profile UI.
*/
public function testProfileTabs()
{
$types_data = ['profile_type_0' => ['label' => $this->randomMachineName()], 'profile_type_1' => ['label' => $this->randomMachineName()]];
/** @var ProfileType[] $types */
$types = [];
foreach ($types_data as $id => $values) {
$types[$id] = ProfileType::create(['id' => $id] + $values);
$types[$id]->save();
}
$this->container->get('router.builder')->rebuild();
$this->user1 = User::create(['name' => $this->randomMachineName(), 'mail' => $this->randomMachineName() . '@example.com']);
$this->user1->save();
$this->user2 = User::create(['name' => $this->randomMachineName(), 'mail' => $this->randomMachineName() . '@example.com']);
$this->user2->save();
// Create new profiles.
$profile1 = Profile::create($expected = ['type' => $types['profile_type_0']->id(), 'uid' => $this->user1->id()]);
$profile1->save();
$profile2 = Profile::create($expected = ['type' => $types['profile_type_1']->id(), 'uid' => $this->user2->id()]);
$profile2->save();
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/config');
$this->clickLink('User profiles');
$this->assertResponse(200);
$this->assertUrl('admin/config/people/profiles');
$this->assertLink($profile1->label());
$this->assertLinkByHref($profile2->toUrl('canonical')->toString());
$tasks = [['entity.profile.collection', []], ['entity.profile_type.collection', []]];
$this->assertLocalTasks($tasks, 0);
}
示例4: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->installSchema('system', 'sequences');
$this->installEntitySchema('user');
$this->manager = $this->container->get('plugin.manager.condition');
// Set up the authenticated and anonymous roles.
Role::create(array('id' => RoleInterface::ANONYMOUS_ID, 'label' => 'Anonymous user'))->save();
Role::create(array('id' => RoleInterface::AUTHENTICATED_ID, 'label' => 'Authenticated user'))->save();
// Create new role.
$rid = strtolower($this->randomMachineName(8));
$label = $this->randomString(8);
$role = Role::create(array('id' => $rid, 'label' => $label));
$role->save();
$this->role = $role;
// Setup an anonymous user for our tests.
$this->anonymous = User::create(array('name' => '', 'uid' => 0));
$this->anonymous->save();
// Loading the anonymous user adds the correct role.
$this->anonymous = User::load($this->anonymous->id());
// Setup an authenticated user for our tests.
$this->authenticated = User::create(array('name' => $this->randomMachineName()));
$this->authenticated->save();
// Add the custom role.
$this->authenticated->addRole($this->role->id());
}
示例5: setUp
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE)
{
parent::setUp($import_test_views);
$this->installEntitySchema('user');
$this->installEntitySchema('comment');
// Create the anonymous role.
$this->installConfig(['user']);
// Create an anonymous user.
$storage = \Drupal::entityManager()->getStorage('user');
// Insert a row for the anonymous user.
$storage->create(array('uid' => 0, 'name' => '', 'status' => 0))->save();
$admin_role = Role::create(['id' => 'admin', 'permissions' => ['administer comments', 'access user profiles']]);
$admin_role->save();
/* @var \Drupal\user\RoleInterface $anonymous_role */
$anonymous_role = Role::load(Role::ANONYMOUS_ID);
$anonymous_role->grantPermission('access comments');
$anonymous_role->save();
$this->adminUser = User::create(['name' => $this->randomMachineName(), 'roles' => [$admin_role->id()]]);
$this->adminUser->save();
// Create some comments.
$comment = Comment::create(['subject' => 'My comment title', 'uid' => $this->adminUser->id(), 'name' => $this->adminUser->label(), 'entity_type' => 'entity_test', 'comment_type' => 'entity_test', 'status' => 1]);
$comment->save();
$comment_anonymous = Comment::create(['subject' => 'Anonymous comment title', 'uid' => 0, 'name' => 'barry', 'mail' => 'test@example.com', 'homepage' => 'https://example.com', 'entity_type' => 'entity_test', 'comment_type' => 'entity_test', 'created' => 123456, 'status' => 1]);
$comment_anonymous->save();
}
示例6: testWhosOnlineBlock
/**
* Test the Who's Online block.
*/
function testWhosOnlineBlock()
{
$block = $this->drupalPlaceBlock('views_block:who_s_online-who_s_online_block');
// Generate users.
$user1 = $this->drupalCreateUser(array('access user profiles'));
$user2 = $this->drupalCreateUser(array());
$user3 = $this->drupalCreateUser(array());
// Update access of two users to be within the active timespan.
$this->updateAccess($user1->id());
$this->updateAccess($user2->id(), REQUEST_TIME + 1);
// Insert an inactive user who should not be seen in the block, and ensure
// that the admin user used in setUp() does not appear.
$inactive_time = REQUEST_TIME - 15 * 60 - 1;
$this->updateAccess($user3->id(), $inactive_time);
$this->updateAccess($this->adminUser->id(), $inactive_time);
// Test block output.
\Drupal::currentUser()->setAccount($user1);
$content = entity_view($block, 'block');
$this->drupalSetContent(render($content));
$this->assertRaw(t('2 users'), 'Correct number of online users (2 users).');
$this->assertText($user1->getUsername(), 'Active user 1 found in online list.');
$this->assertText($user2->getUsername(), 'Active user 2 found in online list.');
$this->assertNoText($user3->getUsername(), 'Inactive user not found in online list.');
$this->assertTrue(strpos($this->drupalGetContent(), $user1->getUsername()) > strpos($this->drupalGetContent(), $user2->getUsername()), 'Online users are ordered correctly.');
}
示例7: userProfileForm
public function userProfileForm(RouteMatchInterface $route_match, UserInterface $user, ProfileTypeInterface $profile_type) {
$profile = $this->entityManager()->getStorage('profile')->create([
'uid' => $user->id(),
'type' => $profile_type->id(),
]);
return $this->entityFormBuilder()->getForm($profile, 'add', ['uid' => $user->id(), 'created' => REQUEST_TIME]);
}
示例8: testUserPasswordReset
/**
* Tests password reset functionality.
*/
function testUserPasswordReset()
{
// Try to reset the password for an invalid account.
$this->drupalGet('user/password');
$edit = array('name' => $this->randomName(32));
$this->drupalPostForm(NULL, $edit, t('Email new password'));
$this->assertText(t('Sorry, @name is not recognized as a username or an email address.', array('@name' => $edit['name'])), 'Validation error message shown when trying to request password for invalid account.');
$this->assertEqual(count($this->drupalGetMails(array('id' => 'user_password_reset'))), 0, 'No email was sent when requesting a password for an invalid account.');
// Reset the password by username via the password reset page.
$edit['name'] = $this->account->getUsername();
$this->drupalPostForm(NULL, $edit, t('Email new password'));
// Verify that the user was sent an email.
$this->assertMail('to', $this->account->getEmail(), 'Password email sent to user.');
$subject = t('Replacement login information for @username at @site', array('@username' => $this->account->getUsername(), '@site' => \Drupal::config('system.site')->get('name')));
$this->assertMail('subject', $subject, 'Password reset email subject is correct.');
$resetURL = $this->getResetURL();
$this->drupalGet($resetURL);
// Check the one-time login page.
$this->assertText($this->account->getUsername(), 'One-time login page contains the correct username.');
$this->assertText(t('This login can be used only once.'), 'Found warning about one-time login.');
// Check successful login.
$this->drupalPostForm(NULL, NULL, t('Log in'));
$this->assertLink(t('Log out'));
$this->assertTitle(t('@name | @site', array('@name' => $this->account->getUsername(), '@site' => \Drupal::config('system.site')->get('name'))), 'Logged in using password reset link.');
// Change the forgotten password.
$password = user_password();
$edit = array('pass[pass1]' => $password, 'pass[pass2]' => $password);
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertText(t('The changes have been saved.'), 'Forgotten password changed.');
// Verify that the password reset session has been destroyed.
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertText(t('Your current password is missing or incorrect; it\'s required to change the Password.'), 'Password needed to make profile changes.');
// Log out, and try to log in again using the same one-time link.
$this->drupalLogout();
$this->drupalGet($resetURL);
$this->assertText(t('You have tried to use a one-time login link that has either been used or is no longer valid. Please request a new one using the form below.'), 'One-time link is no longer valid.');
// Request a new password again, this time using the email address.
$this->drupalGet('user/password');
// Count email messages before to compare with after.
$before = count($this->drupalGetMails(array('id' => 'user_password_reset')));
$edit = array('name' => $this->account->getEmail());
$this->drupalPostForm(NULL, $edit, t('Email new password'));
$this->assertTrue(count($this->drupalGetMails(array('id' => 'user_password_reset'))) === $before + 1, 'Email sent when requesting password reset using email address.');
// Create a password reset link as if the request time was 60 seconds older than the allowed limit.
$timeout = \Drupal::config('user.settings')->get('password_reset_timeout');
$bogus_timestamp = REQUEST_TIME - $timeout - 60;
$_uid = $this->account->id();
$this->drupalGet("user/reset/{$_uid}/{$bogus_timestamp}/" . user_pass_rehash($this->account->getPassword(), $bogus_timestamp, $this->account->getLastLoginTime()));
$this->assertText(t('You have tried to use a one-time login link that has expired. Please request a new one using the form below.'), 'Expired password reset request rejected.');
// Create a user, block the account, and verify that a login link is denied.
$timestamp = REQUEST_TIME - 1;
$blocked_account = $this->drupalCreateUser()->block();
$blocked_account->save();
$this->drupalGet("user/reset/" . $blocked_account->id() . "/{$timestamp}/" . user_pass_rehash($blocked_account->getPassword(), $timestamp, $blocked_account->getLastLoginTime()));
$this->assertResponse(403);
}
示例9: flickr_photos_access
public function flickr_photos_access(\Drupal\user\UserInterface $user, Drupal\Core\Session\AccountInterface $account)
{
$view_access = FALSE;
if (!empty($user) && $user->id()) {
if (isset($user->flickr['nsid'])) {
$view_access = \Drupal::currentUser()->hasPermission('administer flickr') || $user->status && (\Drupal::currentUser()->hasPermission('view all flickr photos') || \Drupal::currentUser()->hasPermission('view own flickr photos') && \Drupal::currentUser()->uid == $user->id());
}
}
return $view_access;
}
示例10: testUserEntityClone
public function testUserEntityClone() {
$this->drupalPostForm('entity_clone/user/' . $this->adminUser->id(), [], t('Clone'));
$users = \Drupal::entityTypeManager()
->getStorage('user')
->loadByProperties([
'name' => 'test_user_cloned',
]);
$user = reset($users);
$this->assertTrue($user, 'Test user cloned found in database.');
}
示例11: testCustomerAdminPages
/**
* Tests customer overview.
*/
public function testCustomerAdminPages()
{
$this->drupalLogin($this->adminUser);
$country = Country::load('US');
Order::create(array('uid' => $this->customer->id(), 'billing_country' => $country->id(), 'billing_zone' => 'AK'))->save();
$this->drupalGet('admin/store/customers/view');
$this->assertResponse(200);
$this->assertLinkByHref('user/' . $this->customer->id());
$this->assertText($country->getZones()['AK']);
$this->assertText($country->label());
}
示例12: setUp
protected function setUp()
{
parent::setUp();
// Create Basic page node type.
if ($this->profile != 'standard') {
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
}
$this->privilegedUser = $this->drupalCreateUser(array('administer statistics', 'view post access counter', 'create page content'));
$this->drupalLogin($this->privilegedUser);
$this->testNode = $this->drupalCreateNode(array('type' => 'page', 'uid' => $this->privilegedUser->id()));
$this->client = \Drupal::service('http_client_factory')->fromOptions(['config/curl' => [CURLOPT_TIMEOUT => 10]]);
}
示例13: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->installEntitySchema('file');
$this->installEntitySchema('user');
$this->installSchema('file', array('file_usage'));
$this->installSchema('system', 'sequences');
$this->user1 = User::create(['name' => 'user1', 'status' => 1]);
$this->user1->save();
$this->user2 = User::create(['name' => 'user2', 'status' => 1]);
$this->user2->save();
$this->file = File::create(array('uid' => $this->user1->id(), 'filename' => 'druplicon.txt', 'filemime' => 'text/plain'));
}
示例14: testLabelCallback
/**
* Test label callback.
*/
function testLabelCallback()
{
$this->assertEqual($this->account->label(), $this->account->getUsername(), 'The username should be used as label');
// Setup a random anonymous name to be sure the name is used.
$name = $this->randomMachineName();
$this->config('user.settings')->set('anonymous', $name)->save();
$this->assertEqual($this->anonymous->label(), $name, 'The variable anonymous should be used for name of uid 0');
$this->assertEqual($this->anonymous->getDisplayName(), $name, 'The variable anonymous should be used for display name of uid 0');
$this->assertEqual($this->anonymous->getUserName(), '', 'The raw anonymous user name should be empty string');
// Set to test the altered username.
\Drupal::state()->set('user_hooks_test_user_format_name_alter', TRUE);
$this->assertEqual($this->account->getDisplayName(), '<em>' . $this->account->id() . '</em>', 'The user display name should be altered.');
$this->assertEqual($this->account->getUsername(), $this->account->name->value, 'The user name should not be altered.');
}
示例15: testTrackerCronIndexing
/**
* Tests that existing nodes are indexed by cron.
*/
function testTrackerCronIndexing()
{
$this->drupalLogin($this->user);
// Create 3 nodes.
$edits = array();
$nodes = array();
for ($i = 1; $i <= 3; $i++) {
$edits[$i] = array('title' => $this->randomMachineName());
$nodes[$i] = $this->drupalCreateNode($edits[$i]);
}
// Add a comment to the last node as other user.
$this->drupalLogin($this->otherUser);
$comment = array('subject[0][value]' => $this->randomMachineName(), 'comment_body[0][value]' => $this->randomMachineName(20));
$this->drupalPostForm('comment/reply/node/' . $nodes[3]->id() . '/comment', $comment, t('Save'));
// Start indexing backwards from node 3.
\Drupal::state()->set('tracker.index_nid', 3);
// Clear the current tracker tables and rebuild them.
db_delete('tracker_node')->execute();
db_delete('tracker_user')->execute();
tracker_cron();
$this->drupalLogin($this->user);
// Fetch the user's tracker.
$this->drupalGet('activity/' . $this->user->id());
// Assert that all node titles are displayed.
foreach ($nodes as $i => $node) {
$this->assertText($node->label(), format_string('Node @i is displayed on the tracker listing pages.', array('@i' => $i)));
}
// Fetch the site-wide tracker.
$this->drupalGet('activity');
// Assert that all node titles are displayed.
foreach ($nodes as $i => $node) {
$this->assertText($node->label(), format_string('Node @i is displayed on the tracker listing pages.', array('@i' => $i)));
}
}