You are here

function ProfileFieldAccessTest::testPrivateField in Profile 2 8

Tests private profile field access.

File

src/Tests/ProfileFieldAccessTest.php, line 58
Contains \Drupal\profile\Tests\ProfileFieldAccessTest.

Class

ProfileFieldAccessTest
Tests profile field access functionality.

Namespace

Drupal\profile\Tests

Code

function testPrivateField() {
  $id = $this->type
    ->id();
  $this
    ->drupalLogin($this->admin_user);

  // Create a private profile field.
  $edit = array(
    'new_storage_type' => 'string',
    'label' => 'Secret',
    'field_name' => 'secret',
  );
  $this
    ->drupalPostForm("admin/config/people/profiles/types/manage/{$id}/fields/add-field", $edit, t('Save and continue'));
  $edit = array(
    'field[settings][profile_private]' => 1,
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save field settings'));
  $this
    ->drupalPostForm(NULL, array(), t('Save settings'));

  // Fill in a field value.
  $this
    ->drupalLogin($this->web_user);
  $uid = $this->web_user
    ->id();
  $secret = $this
    ->randomMachineName();
  $edit = array(
    'field_secret[0][value]' => $secret,
  );
  $this
    ->drupalPostForm("user/{$uid}/edit/profile/{$id}", $edit, t('Save'));

  // User cache page need to be cleared to see new profile.
  // TODO: We shouldn't have to clear all cache to display this.
  drupal_flush_all_caches();

  // Verify that the private field value appears for the profile owner.
  $this
    ->drupalGet("user/{$uid}");
  $this
    ->assertText($secret);

  // Verify that the private field value appears for the administrator.
  $this
    ->drupalLogin($this->admin_user);
  $this
    ->drupalGet("user/{$uid}");
  $this
    ->assertText($secret);

  // Verify that the private field value does not appear for other users.
  $this
    ->drupalLogin($this->other_user);
  $this
    ->drupalGet("user/{$uid}");
  $this
    ->assertNoText($secret);
}