You are here

public function ProfileRevisionTest::testProfileRevisions in Profile 8

Tests revision handling.

File

tests/src/Functional/ProfileRevisionTest.php, line 83

Class

ProfileRevisionTest
Tests using revisions with profiles.

Namespace

Drupal\Tests\profile\Functional

Code

public function testProfileRevisions() {

  /** @var \Drupal\profile\ProfileStorageInterface $storage */
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('profile');
  $user = $this
    ->createUser([
    "create {$this->type->id()} profile",
    "view own {$this->type->id()} profile",
    "update own {$this->type->id()} profile",
  ]);
  $this
    ->drupalLogin($user);
  $create_url = Url::fromRoute('profile.user_page.single', [
    'user' => $this->loggedInUser
      ->id(),
    'profile_type' => $this->type
      ->id(),
  ]);
  $this
    ->drupalGet($create_url);
  $edit = [
    'profile_fullname[0][value]' => $this
      ->getRandomGenerator()
      ->word(10),
  ];
  $this
    ->submitForm($edit, 'Save');
  $profile = $storage
    ->loadByUser($this->loggedInUser, $this->type
    ->id());
  $existing_profile_id = $profile
    ->id();
  $existing_revision_id = $profile
    ->getRevisionId();
  $create_url = Url::fromRoute('profile.user_page.single', [
    'user' => $this->loggedInUser
      ->id(),
    'profile_type' => $this->type
      ->id(),
  ]);
  $this
    ->drupalGet($create_url);
  $edit = [
    'profile_fullname[0][value]' => $this
      ->getRandomGenerator()
      ->word(10),
  ];
  $this
    ->submitForm($edit, 'Save');
  $storage
    ->resetCache([
    $profile
      ->id(),
  ]);
  $profile = $storage
    ->loadByUser($this->loggedInUser, $this->type
    ->id());
  $this
    ->assertEquals($existing_profile_id, $profile
    ->id());
  $this
    ->assertEquals($existing_revision_id, $profile
    ->getRevisionId());
  $user = $this
    ->createUser([
    "create {$this->useRevisionsType->id()} profile",
    "view own {$this->useRevisionsType->id()} profile",
    "update own {$this->useRevisionsType->id()} profile",
  ]);
  $this
    ->drupalLogin($user);
  $create_url = Url::fromRoute('profile.user_page.single', [
    'user' => $this->loggedInUser
      ->id(),
    'profile_type' => $this->useRevisionsType
      ->id(),
  ]);
  $this
    ->drupalGet($create_url);
  $this
    ->getSession()
    ->getPage()
    ->hasField('revision_log_message');
  $edit = [
    'profile_fullname[0][value]' => $this
      ->getRandomGenerator()
      ->word(10),
  ];
  $this
    ->submitForm($edit, 'Save');
  $profile = $storage
    ->loadByUser($this->loggedInUser, $this->useRevisionsType
    ->id());
  $existing_profile_id = $profile
    ->id();
  $existing_revision_id = $profile
    ->getRevisionId();
  $create_url = Url::fromRoute('profile.user_page.single', [
    'user' => $this->loggedInUser
      ->id(),
    'profile_type' => $this->useRevisionsType
      ->id(),
  ]);
  $this
    ->drupalGet($create_url);
  $edit = [
    'profile_fullname[0][value]' => $this
      ->getRandomGenerator()
      ->word(10),
  ];
  $this
    ->submitForm($edit, 'Save');
  $storage
    ->resetCache([
    $profile
      ->id(),
  ]);
  $profile = $storage
    ->loadByUser($this->loggedInUser, $this->useRevisionsType
    ->id());
  $this
    ->assertEquals($existing_profile_id, $profile
    ->id());
  $this
    ->assertNotEquals($existing_revision_id, $profile
    ->getRevisionId());

  // Assert that unchecking the revision checkbox doesn't create a new
  // revision.
  $existing_revision_id = $profile
    ->getRevisionId();
  $this
    ->drupalGet($create_url);
  $this
    ->getSession()
    ->getPage()
    ->uncheckField('revision');
  $edit = [
    'profile_fullname[0][value]' => $this
      ->getRandomGenerator()
      ->word(10),
  ];
  $this
    ->submitForm($edit, 'Save');
  $storage
    ->resetCache([
    $profile
      ->id(),
  ]);
  $profile = $storage
    ->loadByUser($this->loggedInUser, $this->useRevisionsType
    ->id());
  $this
    ->assertEquals($existing_profile_id, $profile
    ->id());
  $this
    ->assertEquals($existing_revision_id, $profile
    ->getRevisionId());
}