You are here

public function UserTest::testMultiple in Profile 8

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

File

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

Class

UserTest
Tests the user pages.

Namespace

Drupal\Tests\profile\Functional

Code

public function testMultiple() {
  $this->type
    ->setMultiple(TRUE);
  $this->type
    ->save();

  /** @var \Drupal\profile\ProfileStorageInterface $profile_storage */
  $profile_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('profile');
  $user = $this
    ->createUser([
    'create test profile',
    'update own test profile',
    'view own test profile',
  ]);
  $this
    ->drupalLogin($user);
  $overview_url = Url::fromRoute('profile.user_page.multiple', [
    'user' => $user
      ->id(),
    'profile_type' => $this->type
      ->id(),
  ]);
  $this
    ->drupalGet($overview_url);
  $this
    ->assertSession()
    ->titleEquals($this->type
    ->getDisplayLabel() . ' | Drupal');
  $this
    ->assertSession()
    ->pageTextContains('Add profile');

  // Confirm that a new profile can be added.
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Add profile');
  $this
    ->assertSession()
    ->titleEquals('Add profile | Drupal');
  $this
    ->submitForm([
    'profile_fullname[0][value]' => 'John Smith',
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Test profile #1 has been saved.');
  $profile = $profile_storage
    ->load('1');
  $this
    ->assertNotEmpty($profile);
  $this
    ->assertEquals('John Smith', $profile
    ->get('profile_fullname')->value);

  // Confirm that the new profile is listed.
  $this
    ->assertSession()
    ->pageTextContains('John Smith');

  // Confirm that the profile can be edited.
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Edit');
  $this
    ->assertSession()
    ->titleEquals("Edit {$this->type->label()} #{$profile->id()} | Drupal");
  $this
    ->submitForm([
    'profile_fullname[0][value]' => 'John Smith Jr.',
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Test profile #1 has been saved.');
  $profile_storage
    ->resetCache([
    $profile
      ->id(),
  ]);
  $profile = $profile_storage
    ->load($profile
    ->id());
  $this
    ->assertNotEmpty($profile);
  $this
    ->assertEquals('John Smith Jr.', $profile
    ->get('profile_fullname')->value);

  // Confirm that the "single" route is unavailable.
  $url = Url::fromRoute('profile.user_page.single', [
    'user' => $user
      ->id(),
    'profile_type' => $this->type
      ->id(),
  ]);
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->pageTextContains('Access denied');
}