/** * Tests add profile form access for a profile type that requires users to * have one of multiple roles. */ public function testProfileWithAllRoles() { // Create user with add own profile permissions. $web_user1 = $this->drupalCreateUser(["add own {$this->type3->id()} profile"]); $this->drupalLogin($web_user1); // Test user without role can access add profile form. // Expected: User cannot access form. $this->drupalGet("user/{$web_user1->id()}/{$this->type3->id()}"); $this->assertResponse(403); // Test user with role 1 can access add profile form. // Expected: User can access form. $web_user1->addRole($this->role1); $web_user1->save(); $this->drupalGet("user/{$web_user1->id()}/{$this->type3->id()}"); $this->assertResponse(200); // Test user with both roles can access add profile form. // Expected: User can access form. $web_user1->addRole($this->role2); $web_user1->save(); $this->drupalGet("user/{$web_user1->id()}/{$this->type3->id()}"); $this->assertResponse(200); // Test user with role 2 can access add profile form. // Expected: User can access form. $web_user1->removeRole($this->role1); $web_user1->save(); $this->drupalGet("user/{$web_user1->id()}/{$this->type3->id()}"); $this->assertResponse(200); // Test user without role can access add profile form. // Expected: User cannot access form. $web_user1->removeRole($this->role2); $web_user1->save(); $this->drupalGet("user/{$web_user1->id()}/{$this->type3->id()}"); $this->assertResponse(403); }
/** * Builds a standard list of permissions for a given profile type. * * @param \Drupal\profile\Entity\ProfileType $profile_type * The machine name of the profile type. * * @return array * An array of permission names and descriptions. */ protected function buildPermissions(ProfileType $profile_type) { $type_id = $profile_type->id(); $type_params = ['%type' => $profile_type->label()]; return [ "add own $type_id profile" => [ 'title' => $this->t('%type: Add own profile', $type_params), ], "add any $type_id profile" => [ 'title' => $this->t('%type: Add any profile', $type_params), ], "view own $type_id profile" => [ 'title' => $this->t('%type: View own profile', $type_params), ], "view any $type_id profile" => [ 'title' => $this->t('%type: View any profile', $type_params), ], "edit own $type_id profile" => [ 'title' => $this->t('%type: Edit own profile', $type_params), ], "edit any $type_id profile" => [ 'title' => $this->t('%type: Edit any profile', $type_params), ], "delete own $type_id profile" => [ 'title' => $this->t('%type: Delete own profile', $type_params), ], "delete any $type_id profile" => [ 'title' => $this->t('%type: Delete any profile', $type_params), ], ]; }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->drupalPlaceBlock('local_tasks_block'); $this->drupalPlaceBlock('local_actions_block'); $this->drupalPlaceBlock('page_title_block'); $this->type = $this->createProfileType('test', 'Test profile', TRUE); $id = $this->type->id(); $field_storage = FieldStorageConfig::create(['field_name' => 'profile_fullname', 'entity_type' => 'profile', 'type' => 'text']); $field_storage->save(); $this->field = FieldConfig::create(['field_storage' => $field_storage, 'bundle' => $this->type->id(), 'label' => 'Full name']); $this->field->save(); // Configure the default display. $this->display = EntityViewDisplay::load("profile.{$this->type->id()}.default"); if (!$this->display) { $this->display = EntityViewDisplay::create(['targetEntityType' => 'profile', 'bundle' => $this->type->id(), 'mode' => 'default', 'status' => TRUE]); $this->display->save(); } $this->display->setComponent($this->field->getName(), ['type' => 'string'])->save(); // Configure rhe default form. $this->form = EntityFormDisplay::load("profile.{$this->type->id()}.default"); if (!$this->form) { $this->form = EntityFormDisplay::create(['targetEntityType' => 'profile', 'bundle' => $this->type->id(), 'mode' => 'default', 'status' => TRUE]); $this->form->save(); } $this->form->setComponent($this->field->getName(), ['type' => 'string_textfield'])->save(); $this->checkPermissions(['administer profile types', "view own {$id} profile", "view any {$id} profile", "add own {$id} profile", "add any {$id} profile", "edit own {$id} profile", "edit any {$id} profile", "delete own {$id} profile", "delete any {$id} profile"]); user_role_grant_permissions(AccountInterface::AUTHENTICATED_ROLE, ['access user profiles']); $this->adminUser = $this->drupalCreateUser(['administer profile types', "view any {$id} profile", "add any {$id} profile", "edit any {$id} profile", "delete any {$id} profile"]); }