You are here

public function UserTest::testSingle in Profile 8

Tests the user pages with a "single" profile type.

File

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

Class

UserTest
Tests the user pages.

Namespace

Drupal\Tests\profile\Functional

Code

public function testSingle() {

  /** @var \Drupal\profile\ProfileStorageInterface $profile_storage */
  $profile_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('profile');
  $first_user = $this
    ->createUser([
    'view own test profile',
  ]);
  $second_user = $this
    ->createUser([
    'create test profile',
    'update own test profile',
    'view own test profile',
  ]);

  // Confirm that the user with only "view" permissions can't see the page.
  $this
    ->drupalLogin($first_user);
  $url = Url::fromRoute('profile.user_page.single', [
    'user' => $first_user
      ->id(),
    'profile_type' => $this->type
      ->id(),
  ]);
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->pageTextContains('Access denied');

  // Confirm that the user with "update" permissions can see the page.
  $this
    ->drupalLogin($second_user);
  $url = Url::fromRoute('profile.user_page.single', [
    'user' => $second_user
      ->id(),
    'profile_type' => $this->type
      ->id(),
  ]);
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->fieldExists('profile_fullname[0][value]');
  $this
    ->assertSession()
    ->titleEquals($this->type
    ->getDisplayLabel() . ' | Drupal');

  // Confirm that a profile can be created.
  $this
    ->submitForm([
    'profile_fullname[0][value]' => 'John Smith',
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('The profile has been saved.');
  $profile = $profile_storage
    ->loadByUser($second_user, 'test');
  $this
    ->assertNotEmpty($profile);
  $this
    ->assertEquals('John Smith', $profile
    ->get('profile_fullname')->value);

  // Confirm that the created profile can be edited.
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->titleEquals($this->type
    ->getDisplayLabel() . ' | Drupal');
  $this
    ->submitForm([
    'profile_fullname[0][value]' => 'John Smith Jr.',
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('The profile has been saved.');
  $profile_storage
    ->resetCache([
    $profile
      ->id(),
  ]);
  $updated_profile = $profile_storage
    ->loadByUser($second_user, 'test');
  $this
    ->assertNotEmpty($updated_profile);
  $this
    ->assertEquals($profile
    ->id(), $updated_profile
    ->id());
  $this
    ->assertEquals('John Smith Jr.', $updated_profile
    ->get('profile_fullname')->value);

  // Confirm that the "multiple" routes are unavailable.
  $routes = [
    'profile.user_page.multiple',
    'profile.user_page.add_form',
  ];
  foreach ($routes as $route_name) {
    $url = Url::fromRoute($route_name, [
      'user' => $second_user
        ->id(),
      'profile_type' => $this->type
        ->id(),
    ]);
    $this
      ->drupalGet($url);
    $this
      ->assertSession()
      ->pageTextContains('Access denied');
  }
}