本文整理汇总了PHP中Drupal\user\Entity\Role::create方法的典型用法代码示例。如果您正苦于以下问题:PHP Role::create方法的具体用法?PHP Role::create怎么用?PHP Role::create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\user\Entity\Role
的用法示例。
在下文中一共展示了Role::create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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();
}
示例2: setUp
/**
* Performs setup tasks before each individual test method is run.
*/
public function setUp()
{
parent::setUp('content_access');
// The parent method already installs most needed node and comment schemas,
// but here we also need the comment statistics.
$this->installSchema('comment', array('comment_entity_statistics'));
// Create a node type for testing.
$type = NodeType::create(array('type' => 'page', 'name' => 'page'));
$type->save();
// Create anonymous user role.
$role = Role::create(array('id' => 'anonymous', 'label' => 'anonymous'));
$role->save();
// Insert the anonymous user into the database, as the user table is inner
// joined by \Drupal\comment\CommentStorage.
User::create(array('uid' => 0, 'name' => ''))->save();
// Create a node with attached comment.
$this->nodes[0] = Node::create(array('status' => NODE_PUBLISHED, 'type' => 'page', 'title' => 'test title'));
$this->nodes[0]->save();
$comment_type = CommentType::create(array('id' => 'comment', 'target_entity_type_id' => 'node'));
$comment_type->save();
$this->installConfig(array('comment'));
$this->addDefaultCommentField('node', 'page');
$comment = Comment::create(array('entity_type' => 'node', 'entity_id' => $this->nodes[0]->id(), 'field_name' => 'comment', 'body' => 'test body', 'comment_type' => $comment_type->id()));
$comment->save();
$this->comments[] = $comment;
$this->nodes[1] = Node::create(array('status' => NODE_PUBLISHED, 'type' => 'page', 'title' => 'some title'));
$this->nodes[1]->save();
$this->nodes[2] = Node::create(array('status' => NODE_NOT_PUBLISHED, 'type' => 'page', 'title' => 'other title'));
$this->nodes[2]->save();
// Also index users, to verify that they are unaffected by the processor.
$this->index->set('datasources', array('entity:comment', 'entity:node', 'entity:user'));
$this->index->save();
$this->index = entity_load('search_api_index', $this->index->id(), TRUE);
}
示例3: 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());
}
示例4: testRevisionView
public function testRevisionView()
{
$entity = EnhancedEntity::create(['name' => 'rev 1', 'type' => 'default']);
$entity->save();
$revision = clone $entity;
$revision->name->value = 'rev 2';
$revision->setNewRevision(TRUE);
$revision->isDefaultRevision(FALSE);
$revision->save();
/** @var \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel */
$http_kernel = \Drupal::service('http_kernel');
$request = Request::create($revision->url('revision'));
$response = $http_kernel->handle($request);
$this->assertEquals(403, $response->getStatusCode());
$role = Role::create(['id' => 'test_role']);
$role->grantPermission('view all entity_test_enhanced revisions');
$role->grantPermission('administer entity_test_enhanced');
$role->save();
$user = User::create(['name' => 'Test user']);
$user->addRole($role->id());
\Drupal::service('account_switcher')->switchTo($user);
$request = Request::create($revision->url('revision'));
$response = $http_kernel->handle($request);
$this->assertEquals(200, $response->getStatusCode());
$this->assertNotContains('rev 1', $response->getContent());
$this->assertContains('rev 2', $response->getContent());
}
示例5: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->role1 = Role::create(array('id' => strtolower($this->randomMachineName(8)), 'label' => $this->randomMachineName(8)));
$this->role1->save();
$this->role2 = Role::create(array('id' => strtolower($this->randomMachineName(8)), 'label' => $this->randomMachineName(8)));
$this->role2->save();
$this->createEntityReferenceField('user', 'user', 'user_reference', 'User reference', 'user');
}
示例6: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->installEntitySchema('user');
$this->installSchema('system', 'sequences');
$devel_role = Role::create(['id' => 'admin', 'permissions' => ['access devel information']]);
$devel_role->save();
$this->develUser = User::create(['name' => $this->randomMachineName(), 'roles' => [$devel_role->id()]]);
$this->develUser->save();
}
示例7: testAllFormatters
/**
* Tests the plain formatter.
*/
public function testAllFormatters()
{
// Tests the plain formatter.
$this->assertFormatterRdfa(array('type' => 'taxonomy_term_reference_plain'), 'http://schema.org/about', array('value' => $this->term->getName(), 'type' => 'literal'));
// Grant the access content permission to the anonymous user.
Role::create(array('id' => DRUPAL_ANONYMOUS_RID))->grantPermission('access content')->save();
// Tests the link formatter.
$term_uri = $this->getAbsoluteUri($this->term);
$this->assertFormatterRdfa(array('type' => 'taxonomy_term_reference_link'), 'http://schema.org/about', array('value' => $term_uri, 'type' => 'uri'));
}
示例8: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->installEntitySchema('user');
$this->installEntitySchema('entity_test_mul');
$this->installEntitySchema('entity_test_admin_routes');
/** @var \Drupal\user\RoleInterface $role */
$role = Role::create(['id' => RoleInterface::ANONYMOUS_ID]);
$role->grantPermission('administer entity_test content')->grantPermission('view test entity');
$role->save();
}
示例9: drupalCreateUser
/**
* @return \Drupal\user\UserInterface
*/
protected function drupalCreateUser(array $permissions = [])
{
$role = Role::create(['id' => 'test_role__' . $this->randomMachineName()]);
foreach ($permissions as $permission) {
$role->grantPermission($permission);
}
$role->save();
$user = User::create(['name' => 'test name ' . $this->randomMachineName()]);
$user->addRole($role->id());
$user->save();
return $user;
}
示例10: testValidUserActionConfigSchema
/**
* Tests whether the user action config schema are valid.
*/
function testValidUserActionConfigSchema()
{
$rid = strtolower($this->randomName(8));
Role::create(array('id' => $rid))->save();
// Test user_add_role_action configuration.
$config = \Drupal::config('system.action.user_add_role_action.' . $rid);
$this->assertEqual($config->get('id'), 'user_add_role_action.' . $rid);
$this->assertConfigSchema(\Drupal::service('config.typed'), $config->getName(), $config->get());
// Test user_remove_role_action configuration.
$config = \Drupal::config('system.action.user_remove_role_action.' . $rid);
$this->assertEqual($config->get('id'), 'user_remove_role_action.' . $rid);
$this->assertConfigSchema(\Drupal::service('config.typed'), $config->getName(), $config->get());
}
示例11: setUp
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE)
{
parent::setUp($import_test_views);
$this->installEntitySchema('user');
$role_with_access = Role::create(['id' => 'with_access', 'permissions' => ['view test entity field']]);
$role_with_access->save();
$role_without_access = Role::create(['id' => 'without_access', 'permissions' => []]);
$role_without_access->save();
$this->userWithAccess = User::create(['name' => $this->randomMachineName(), 'roles' => [$role_with_access->id()]]);
$this->userWithAccess->save();
$this->userWithoutAccess = User::create(['name' => $this->randomMachineName(), 'roles' => [$role_without_access->id()]]);
$this->userWithoutAccess->save();
}
示例12: setUp
/**
* Sets up the test.
*/
protected function setUp()
{
parent::setUp();
$this->installSchema('system', ['sequences', 'key_value_expire']);
$this->installEntitySchema('user');
\Drupal::service('router.builder')->rebuild();
/** @var \Drupal\user\RoleInterface $role */
$role = Role::create(array('id' => 'admin', 'label' => 'admin'));
$role->grantPermission('link to any page');
$role->save();
$this->testUser = User::create(array('name' => 'foobar', 'mail' => 'foobar@example.com'));
$this->testUser->addRole($role->id());
$this->testUser->save();
\Drupal::service('current_user')->setAccount($this->testUser);
}
示例13: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->installEntitySchema('user');
$this->installEntitySchema('entity_test_mul');
$this->installEntitySchema('entity_test_admin_routes');
$this->installSchema('system', 'router');
$router_builder = \Drupal::service('router.builder');
$router_builder->rebuild();
/** @var \Drupal\Core\Routing\RouteBuilderInterface $router_builder */
$router_builder = \Drupal::service('router.builder');
$router_builder->rebuild();
/** @var \Drupal\user\RoleInterface $role */
$role = Role::create(['id' => RoleInterface::ANONYMOUS_ID]);
$role->grantPermission('administer entity_test content')->grantPermission('view test entity');
$role->save();
}
示例14: testDependencies
/**
* Tests that role filter dependencies are calculated correctly.
*/
public function testDependencies()
{
$role = Role::create(['id' => 'test_user_role']);
$role->save();
$view = View::load('test_user_name');
$expected = ['module' => ['user']];
$this->assertEqual($expected, $view->getDependencies());
$display =& $view->getDisplay('default');
$display['display_options']['filters']['roles_target_id'] = ['id' => 'roles_target_id', 'table' => 'user__roles', 'field' => 'roles_target_id', 'value' => ['test_user_role' => 'test_user_role'], 'plugin_id' => 'user_roles'];
$view->save();
$expected['config'][] = 'user.role.test_user_role';
$this->assertEqual($expected, $view->getDependencies());
$view = View::load('test_user_name');
$display =& $view->getDisplay('default');
$display['display_options']['filters']['roles_target_id'] = ['id' => 'roles_target_id', 'table' => 'user__roles', 'field' => 'roles_target_id', 'value' => [], 'plugin_id' => 'user_roles'];
$view->save();
unset($expected['config']);
$this->assertEqual($expected, $view->getDependencies());
}
示例15: setUp
/**
* {@inheritdoc}
*/
public function setUp($processor = NULL)
{
parent::setUp('rendered_item');
// Load configuration and needed schemas. (The necessary schemas for using
// nodes are already installed by the parent method.)
$this->installConfig(array('system', 'filter', 'node', 'comment'));
$this->installSchema('system', array('router'));
\Drupal::service('router.builder')->rebuild();
// Create a node type for testing.
$type = NodeType::create(array('type' => 'page', 'name' => 'page'));
$type->save();
node_add_body_field($type);
// Create anonymous user role.
$role = Role::create(array('id' => 'anonymous', 'label' => 'anonymous'));
$role->save();
// Insert the anonymous user into the database.
$anonymous_user = User::create(array('uid' => 0, 'name' => ''));
$anonymous_user->save();
// Default node values for all nodes we create below.
$node_data = array('status' => NODE_PUBLISHED, 'type' => 'page', 'title' => '', 'body' => array('value' => '', 'summary' => '', 'format' => 'plain_text'), 'uid' => $anonymous_user->id());
// Create some test nodes with valid user on it for rendering a picture.
$node_data['title'] = 'Title for node 1';
$node_data['body']['value'] = 'value for node 1';
$node_data['body']['summary'] = 'summary for node 1';
$this->nodes[1] = Node::create($node_data);
$this->nodes[1]->save();
$node_data['title'] = 'Title for node 2';
$node_data['body']['value'] = 'value for node 2';
$node_data['body']['summary'] = 'summary for node 2';
$this->nodes[2] = Node::create($node_data);
$this->nodes[2]->save();
// Set proper configuration for the tested processor.
$config = $this->processor->getConfiguration();
$config['view_mode'] = array('entity:node' => ['page' => 'full', 'article' => 'teaser'], 'entity:user' => 'compact', 'entity:comment' => 'teaser');
$config['roles'] = array($role->id());
$this->processor->setConfiguration($config);
$this->index->save();
$this->index->getDatasources();
// Enable the classy theme as the tests rely on markup from that.
\Drupal::service('theme_handler')->install(array('classy'));
\Drupal::theme()->setActiveTheme(\Drupal::service('theme.initialization')->initTheme('classy'));
}