You are here

public function UserTest::testAddPageAccess in Profile 8

Tests access to the profile "add" page.

File

tests/src/Functional/UserTest.php, line 146

Class

UserTest
Tests the user pages.

Namespace

Drupal\Tests\profile\Functional

Code

public function testAddPageAccess() {
  $this->type
    ->setMultiple(TRUE);
  $this->type
    ->save();
  $first_user = $this
    ->createUser([
    'view own test profile',
  ]);
  $second_user = $this
    ->createUser([
    'create test profile',
    'view any profile',
    'access user profiles',
  ]);
  $third_user = $this
    ->createUser([
    'administer profile',
    'access user profiles',
  ]);
  $profile = Profile::create([
    'type' => $this->type
      ->id(),
    'uid' => $first_user
      ->id(),
    'profile_fullname' => 'John Smith',
    'status' => TRUE,
  ]);
  $profile
    ->save();

  // Confirm that the user with only "view" permissions can see
  // the overview page, but not the "add" page.
  $this
    ->drupalLogin($first_user);
  $overview_url = Url::fromRoute('profile.user_page.multiple', [
    'user' => $first_user
      ->id(),
    'profile_type' => $this->type
      ->id(),
  ]);
  $this
    ->drupalGet($overview_url);
  $this
    ->assertSession()
    ->pageTextContains('John Smith');
  $this
    ->assertSession()
    ->linkNotExists('Add profile');
  $add_url = Url::fromRoute('profile.user_page.add_form', [
    'user' => $first_user
      ->id(),
    'profile_type' => $this->type
      ->id(),
  ]);
  $this
    ->drupalGet($add_url);
  $this
    ->assertSession()
    ->pageTextContains('Access denied');

  // Confirm that the second user can't add a profile for the first user.
  $this
    ->drupalLogin($second_user);
  $this
    ->drupalGet($overview_url);
  $this
    ->assertSession()
    ->pageTextContains('John Smith');
  $this
    ->assertSession()
    ->linkNotExists('Add profile');
  $add_url = Url::fromRoute('profile.user_page.add_form', [
    'user' => $first_user
      ->id(),
    'profile_type' => $this->type
      ->id(),
  ]);
  $this
    ->drupalGet($add_url);
  $this
    ->assertSession()
    ->pageTextContains('Access denied');

  // Confirm that the third user can add a profile for the first user.
  $this
    ->drupalLogin($third_user);
  $this
    ->drupalGet($overview_url);
  $this
    ->assertSession()
    ->pageTextContains('John Smith');
  $this
    ->assertSession()
    ->linkExists('Add profile');
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Add profile');
  $this
    ->submitForm([
    'profile_fullname[0][value]' => 'Jane Smith',
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Test profile #2 has been saved.');
  $profile = Profile::load('2');
  $this
    ->assertNotEmpty($profile);
  $this
    ->assertEquals('Jane Smith', $profile
    ->get('profile_fullname')->value);
  $this
    ->assertEquals($first_user
    ->id(), $profile
    ->getOwnerId());
}